In an optical encoder, a plurality of light sources is controlled on and off so as to use light rays to irradiate an optical grating of a scale from a plurality of different directions. The light rays are received by a plurality of photoreceptor elements. Operations are performed using signals output from the photoreceptor elements. Accordingly, the relative position between the photoreceptor elements and the scale is detected precisely.