A displacement sensor configured for repeatedly executing, in a normal operation mode, a detection process of projecting light to an object and receiving the light reflected from the object, a measurement process of measuring an amount of displacement of the object based on the result of reception of said reflected light, and a moving average calculation of data of said measurement, for outputting detection data based on an average value obtained by the moving average calculation, comprising: input means configured for inputting either one of a numerical value representing a target value of a response time of said displacement sensor, or a numerical value representing an error tolerance value of detection data, or both; and teaching mode executing means for executing a teaching mode in which the number of pieces of data of said moving average calculation is determined and registered, said teaching mode executing means including: measurement data variation calculating means for executing said detection process and said measurement process, each for a plurality of times, on an actual model of the object to be detected, and executing a process of calculating variation of measurement data obtained by said measurement process, data number derivation means for calculating the number of pieces of data of moving average calculation at which the target value of the response time and/or the error tolerance value of the detection data input into the input means are met, by an operation using results of processing by said measurement data variation calculating means, and registration processing means for registering the calculated number of pieces of data of said moving average calculation; and a memory in which the number of pieces of data of said moving average calculation can be registered by the registration processing means for use in the normal operation mode. input means configured for inputting either one of a numerical value representing a target value of a response time of said displacement sensor, or a numerical value representing an error tolerance value of detection data, or both; and teaching mode executing means for executing a teaching mode in which the number of pieces of data of said moving average calculation is determined and registered, said teaching mode executing means including: measurement data variation calculating means for executing said detection process and said measurement process, each for a plurality of times, on an actual model of the object to be detected, and executing a process of calculating variation of measurement data obtained by said measurement process, data number derivation means for calculating the number of pieces of data of moving average calculation at which the target value of the response time and/or the error tolerance value of the detection data input into the input means are met, by an operation using results of processing by said measurement data variation calculating means, and registration processing means for registering the calculated number of pieces of data of said moving average calculation; and measurement data variation calculating means for executing said detection process and said measurement process, each for a plurality of times, on an actual model of the object to be detected, and executing a process of calculating variation of measurement data obtained by said measurement process, data number derivation means for calculating the number of pieces of data of moving average calculation at which the target value of the response time and/or the error tolerance value of the detection data input into the input means are met, by an operation using results of processing by said measurement data variation calculating means, and registration processing means for registering the calculated number of pieces of data of said moving average calculation; and a memory in which the number of pieces of data of said moving average calculation can be registered by the registration processing means for use in the normal operation mode.