There is two things you should check before trying the solved solution:
1. Make sure you have all the updates done on the phone. By going to the settings menu and updating the phone to the latest version.
2. If you have just had the phone worked on ( like replaceing the battery ) , the sensor could be pinched or pushed into the wrong position. This sometimes happens also dropping the phone.
3. If none of the above work, you most probably will have to take your phone in for repair.
Wishing you luck on making it work.