How to Make an Optical Encoder with an Opto Switch and Arduino


In this tutorial, we will learn How to Make an Optical Encoder with an Opto Switch and Arduino. An optical encoder is a device that converts mechanical motion into electrical signals. It is commonly used in robotics, automation, and other applications where precise position sensing is required. By interfacing the sensor module with the Arduino, we can capture changes in light intensity and translate them into actionable digital signals, opening up a multitude of possibilities for applications such as object detection, position sensing, and speed measurement.

Hardware Required

You will require the following Hardware Components for Interfacing the Opto Switch or OptoCoupler Sensor Module with Arduino.

Components#Buy From Amazon
Arduino UNO1Buy Link
OptoCoupler Sensor1Buy Link
9v DC Adapter (Optional)1Buy Link
Jumper Wires3Buy Link
Breadboard1Buy Link

What is the Module?

An opto-switch is a sensor shaped like the letter “U” that detects objects passing through its interior slot.


Opto switches are straightforward sensors comprising an infrared emitting diode at one end and a phototransistor at the other. As an object moves through the slot, it disrupts the infrared light beam, which the phototransistor detects.

These switches find extensive application as encoders to monitor the rotation speed and position of motor shafts. Typically, a disc with slots attached to the shaft is employed for this purpose. Alternatively, a transparent sheet featuring printed black stripes, a configuration commonly found in printers, can also be used.


Indeed, beyond its role as an encoder, an opto-switch proves versatile in detecting various objects that obstruct the beam. For instance, it can be employed to sense the closure of a door or serve as an optical limit switch during machine operations. Its ability to swiftly detect interruptions in the beam makes it a valuable component in diverse applications beyond rotational monitoring.



Pin Configuration

Pin NamePin Type
+5V or VCCPositive supply Pin
GNGGround Pin
A0Analog Pin
D0Digital Pin


Here are some specifications of the optocoupler sensor module:

  • Operating Voltage: Typically operates within a range of 3.3V to 5V, compatible with most microcontroller platforms.
  • Output Type: Digital output signal, usually in the form of a logic high or low.
  • Detection Range: Varies depending on the specific sensor module, typically ranging from a few millimeters to several centimeters.
  • Response Time: Fast response time, enabling real-time detection and monitoring.
  • Dimensions: Compact and lightweight design, suitable for integration into various projects and systems.


The optocoupler sensor module offers the following features:

  • Non-Contact Sensing: Utilizes light for detection, eliminating the need for physical contact with the object being sense.
  • High Reliability: OptoCoupler sensors offer high reliability and durability, making them suitable for continuous operation in various environments.
  • Easy Integration: Compatible with microcontroller platforms such as Arduino, Raspberry Pi, and others, making them easy to integrate into projects.
  • Adjustable Sensitivity: Some sensor modules may feature adjustable sensitivity settings, allowing users to fine-tune detection parameters according to their requirements.
  • Cost-Effective: OptoCoupler sensor modules are cost-effective solutions for object detection and sensing applications, offering a balance between performance and affordability.

Circuit Diagram

The following circuit shows you the connection of the How to Make an Optical Encoder with an Opto Switch and Arduino Please make the connection carefully


Circuit Connections

ArduinoOptoCoupler Sensor
+5V+5V or VCC Pin
D0D9 Pin

Installing Arduino IDE Software

First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow

Step-by-Step Instructions

Follow these steps to How to Make an Optical Encoder with an Opto Switch and Arduino:

  1. Connect the VCC pin of the opto switch module to the 5V pin of the Arduino.
  2. Connect the GND pin of the opto switch module to the GND pin of the Arduino.
  3. Connect the D0 pin of the opto switch module to the digital pin D9 of the Arduino.
  4. Upload the following Arduino code:


//For more Projects:

const int sensorPin = 9;

void setup() {
  Serial.begin(9600); //start serial port
  pinMode(sensorPin , INPUT); //define pin as input
void loop(){
  int value = 0;
  value = digitalRead(sensorPin ); //digital pin reading
  if (value == LOW) {
      Serial.println("Opto switch on");


The optocoupler sensor module is using in various applications, including:

  • Position Sensing: Used in rotary encoders and linear position sensors to determine the position of moving parts or objects.
  • Object Detection: Employed in automated systems for detecting the presence or absence of objects on conveyor belts, production lines, or assembly processes.
  • Speed Measurement: Utilized in conjunction with rotating components to measure rotational speed or angular velocity.
  • Counting and Monitoring: Integrated into systems for counting objects passing through a specific point or monitoring the movement of components.
  • Security Systems: Incorporated into security systems for detecting unauthorized entry or movement in restricted areas.

Leave a Comment