To have a normal erection you should be sexually aroused. Erection happens with sexual stimulation. Sexual stimulation can be tactile or mental. Sexual stimulation is necessary that is why sexual foreplay may be helpful for those men who have problems with erections.
When you are sexually aroused, electrical impulses are produced by brain and along the nerves go to the penis and this makes the nerves to release nitric oxide. Nitric oxide enlarges the production of cyclic GMP in the smooth muscle cells of the corpora cavernosa. The cGMP makes the smooth muscles of the corpora cavernosa to relax and allow rapid blood flow into the penis. The incoming blood fills the penis and it expands.