Hardware electronic compass built in your device needs sometimes calibration. Either it does not show the bearing correctly or some application prompts you about the necessity of the compass calibration.
Google as the manufacturer of Android OS has this official instruction:
It does not have to be this complicated though. It is enough when you move your phone in the shape of 8: