Well, you should start by making a PRAM reset and a SMC reset.
PRAM reset:
- With the iMac turn off: Press Cmnd, Option, P and R Keys while starting the Mac, release the power button but continue pressing the other keys. (you have to make sure you get those keys pressed before the gray screen comes up or it won’t work.)
- It will start and chime, then it will reset, keep pressing for 2 chimes and release all.
If the Problem continues do the SMC reset:
- Shut down your Mac
- Disconnect the power cord
- Press and hold the Mac’s power button for 5 seconds
- Release the button
- Reattach the power cables and boot the Mac as usual
If the problem persist you might need to run the apple diagnostic online:
- Power on the iMac and immediately press and hold D.
- Follow the on-screen instructiosn