How to Read Feedback from a Optical Sensor

Optical Sensor

Optical Sensors, when used with linear actuators, function very similar to hall effect sensors, except they detect light instead of magnetic fields [1]. Optical Sensors operate by using light from a LED or some other light source that is passed through an encoder disk. This encoder disk is slotted to allow the light to pass through it periodically. On the other side of the disk is a photodetector, which detects the light when it passes through the slots in the disk and creates an output signal [1]. As the actuator moves, the encoder disk rotates and light is detected by the photodetector which produces a square wave of voltage pulses. These pulses can be used similarly to the pulses of a hall effect sensor to determine how far the actuator has moved.

Optical Sensor

Positional Feedback from an Optical Sensor

As optical sensors function quite similar to hall effect sensors in terms of their output, this blog post will briefly cover how to read their output for positional feedback. If you are looking for more details check out our post on how to read positional feedback from a hall effect sensor as much will be similar.

Like hall effect sensors, optical sensors will have 3 pins to connect to; one is the input voltage, another is the ground, and lastly, one is the output signal. To utilize the pulses on the output signal for positional feedback, you’ll need to use a microcontroller to count the produced pulses. You’ll want to make use of the external interrupt pins of your microcontroller to accurately count these pulses. As external interrupts are triggered by a change in voltage, they can be used to detect each pulse as they occur. Once you have setup your interrupt in your microcontroller’s code, you’ll need to set up an interrupt service routine that counts the pulses as they occur. The function countSteps() in the code example below is used to count the number of pulses from the optical sensor.

To utilize these pulses to determine a positional value, you’ll need to know the previous position of the linear actuator and the direction that linear actuator is travelling in. As you control which way your linear actuator moves, you can simply set up a variable to track the direction of the actuator in your code. This variable can be used to determine whether you need to add or subtract the pulses from your previous position. Once you have updated your position, you’ll need to reset the counted pulses to zero. The code example below shows you a function that updates the position based on the number of pulses counted. Once you have a position in terms of pulses you can convert into inches using the pulse per inch specification of your linear actuator.

Homing your Linear Actuator

To accurately utilize positional feedback from an optical sensor, you need to always know the starting position of your linear actuator. Although when you first turn on your system, your microcontroller will not be able to tell whether the actuator is extended or not. This will require you to home your linear actuator to a known position. To home your linear actuator, you will need to drive it to a known position, like fully retracted. Using the Arduino code below as an example, we have set up a WHILE loop that will drive our linear actuator towards our known position. In this example, we will know that we are at our known position by checking whether or not the interrupt has been triggered by checking if the steps variable has changed. We also need to make sure that enough time has passed to expect the interrupt to be triggered, for this we make use of the millis() function and compare it to the previous time stamp. Once we have determined that the linear actuator is at our home position, we stop driving the actuator, reset the steps variable, and exit the WHILE loop.

Dealing With False Triggers

While optical sensors are not as sensitive to electrical noise as potentiometers, electrical noise and switch bouncing can still impact the output signal and may trigger false pulses to be counted. A few extra pulses won’t affect the positioning very much, but over time it may be a bigger issue. You can combat these issues by making use of an internal timer to filter out false triggers. As you can determine how frequent you would expect new pulses to be detected, you can filter out when the interrupt was triggered by noise. In the code sample below, trigDelay is the time delay between each pulse. If the interrupt was triggered before this delay, then the pulse won’t be counted. The length of time of this delay will vary based on your application, but if it is too short it will not filter out the noise properly and if it is too long it will miss actual pulses from the optical sensor.

Another way to combat false triggers is to correct the positional value every time the actuator reaches a known position. Like homing the linear actuator, if you have driven the linear actuator to a fully retracted or extended position or if you make use of external limit switches, you’ll know how many pulses it takes to reach that position. You can then simply correct the positional value using that value when you reach that known position. In the code sample below, this is done for the fully extended and fully retracted positions. This method provides a practical solution to ensure your positional value stays accurate.


Optical sensors utilized for positional feedback provide greater accuracy and resolution compared to hall effect sensors and potentiometers, while having similar strengths and drawbacks to hall effect sensors. Although they do not measure absolute position and require a known starting position to provide a positional value, the large number of pulses per inch allow for reliability in positioning and ensuring multiple linear actuators move simultaneously. Utilizing our FA-SYNC-X actuator controller or through additional code, you can even ensure the actuators move in unison irrespective of load.

For a complete code example check out our blog on how to read positional feedback from a hall effect sensor as much will be similar. Values like pulse per inch and time delays between pulses are some of the changes you’ll need to make to use that code to accurately utilize your optical sensor.


[1] Paschotta, R. Article on Optical Sensors. Retrieved From:


Share this article

Featured Collections

Need Help Finding the Right Actuator?

We precision engineer and manufacture our products so you get direct manufacturers pricing. We offer same day shipping and knowledgeable customer support.

Give us a call or email us and we'll be happy to help you find the best actuator for your project.