An optical encoder (100, 100A, 100B), comprising: a main scale (10, 110) on which at least two displacement-measuring optical gratings (12, 13, 14, 112, 113) are arranged in a predetermined direction; a detector head (20, 120) movable in the predetermined direction relative to the main scale (10, 110); at least two light-emitting elements (30, 31, 32, 130, 131, 132) that emits light respectively to the optical gratings provided on the main scale (10, 110); and at least two photo detectors (41, 42, 140, 141, 142) respectively provided for the optical gratings (12, 13, 14, 112, 113) on the detector head (20, 120), the photo detectors receiving the light transmitted through or reflected by the optical gratings (12, 13, 14, 112, 113), wherein each of the optical gratings comprises a main-signal optical grating (13, 112) and an origin optical grating (14, 113), the light-emitting elements (30, 31, 32, 130, 131, 132) comprise an origin light-emitting element (32, 132) that emits infrared light to the origin optical grating (14, 113) provided on the main scale (10, 110) and a main-signal light-emitting element (31, 131) that emits a red light to the main-signal optical grating (13, 112), the photo detectors (41, 42, 140, 141, 142) comprise an origin photo detector (42, 142) that receives the infrared light reflected by the origin optical grating (14, 113) and outputs signals corresponding only to the infrared light and a main-signal photo detector (41, 141) that receives the red light reflected by the main-signal optical grating (13, 112) and outputs signals corresponding only to the red light, a filter (35A, 35B) is provided to at least one of a light incident surface and light emitting surface of the optical gratings (12, 13, 14, 112, 113), the filter transmitting light that is emitted from the light-emitting elements (30, 31, 32, 130, 131, 132) and is provided with a predetermined wavelength while shielding light having a wavelength other than the predetermined wavelength, the filter (35A, 35B) comprises: an origin optical filter that transmits only the infrared light on an optical path of the infrared light emitted by the origin light-emitting element (32, 132), and a main-signal optical filter that transmits only the red light on an optical path of the red light-emitted by the main-signal light-emitting element (31, 131), the main-signal optical grating (13, 112) and the origin optical grating (14, 113) are provided in parallel on the main scale (10, 110) adjacent to each other, the origin light-emitting element (32, 132), the main-signal light-emitting element (31, 131), the main-signal photo detector (41, 141) and the origin photo detector (42, 142) are each provided on the single detector head (20, 120) adjacently with each other corresponding to the main-signal optical grating (13, 112) and the origin optical grating (14, 113). a main scale (10, 110) on which at least two displacement-measuring optical gratings (12, 13, 14, 112, 113) are arranged in a predetermined direction; a detector head (20, 120) movable in the predetermined direction relative to the main scale (10, 110); at least two light-emitting elements (30, 31, 32, 130, 131, 132) that emits light respectively to the optical gratings provided on the main scale (10, 110); and at least two photo detectors (41, 42, 140, 141, 142) respectively provided for the optical gratings (12, 13, 14, 112, 113) on the detector head (20, 120), the photo detectors receiving the light transmitted through or reflected by the optical gratings (12, 13, 14, 112, 113), wherein each of the optical gratings comprises a main-signal optical grating (13, 112) and an origin optical grating (14, 113), the light-emitting elements (30, 31, 32, 130, 131, 132) comprise an origin light-emitting element (32, 132) that emits infrared light to the origin optical grating (14, 113) provided on the main scale (10, 110) and a main-signal light-emitting element (31, 131) that emits a red light to the main-signal optical grating (13, 112), the photo detectors (41, 42, 140, 141, 142) comprise an origin photo detector (42, 142) that receives the infrared light reflected by the origin optical grating (14, 113) and outputs signals corresponding only to the infrared light and a main-signal photo detector (41, 141) that receives the red light reflected by the main-signal optical grating (13, 112) and outputs signals corresponding only to the red light, a filter (35A, 35B) is provided to at least one of a light incident surface and light emitting surface of the optical gratings (12, 13, 14, 112, 113), the filter transmitting light that is emitted from the light-emitting elements (30, 31, 32, 130, 131, 132) and is provided with a predetermined wavelength while shielding light having a wavelength other than the predetermined wavelength, the filter (35A, 35B) comprises: an origin optical filter that transmits only the infrared light on an optical path of the infrared light emitted by the origin light-emitting element (32, 132), and a main-signal optical filter that transmits only the red light on an optical path of the red light-emitted by the main-signal light-emitting element (31, 131), the main-signal optical grating (13, 112) and the origin optical grating (14, 113) are provided in parallel on the main scale (10, 110) adjacent to each other, the origin light-emitting element (32, 132), the main-signal light-emitting element (31, 131), the main-signal photo detector (41, 141) and the origin photo detector (42, 142) are each provided on the single detector head (20, 120) adjacently with each other corresponding to the main-signal optical grating (13, 112) and the origin optical grating (14, 113).