Why does your penis get hard even when you are not horny?

An erection does not necessarily originate from sexual excitement. It can happen during sleep or any time spontaneously.