ESP32 C3 Super Mini - IR Slotted Optical Speed Sensor
Learn how to use the IR slotted optical speed sensor with the ESP32 C3 Super Mini to count objects and measure motor speed in RPM.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of IR Slotted Optical Speed Sensor
This module is a U-shaped photo interrupter. It tells the ESP32 C3 Super Mini when something passes through its slot.
Quick facts:
- Sensor Type: Slot-type photoelectric (IR LED + NPN phototransistor)
- Slot Width: 5.9 mm
- Operating Voltage: 3.3V to 5V DC
- Output: Digital (TTL) - LOW when blocked, HIGH when clear
- Signal Conditioning: Built-in Schmitt trigger
- Mounting: M3 screw holes at both ends
- Pins: 3 (VCC, GND, OUT)
Good for:
- Motor speed (RPM) with an encoder disc
- A simple tachometer
- Counting objects or pulses
- Distance from wheel turns
- Robot wheel speed feedback
- Limit or position detection
Pinout

- VCC — Power in (3.3V to 5V)
- GND — Ground (0V)
- OUT — Digital signal to an ESP32 C3 input pin
※ NOTE THAT:
The pin order can change from batch to batch. Always follow the labels printed on your module.
How It Works

- Two sides: An IR LED sits on one side of the slot. A phototransistor faces it on the other side.
- Slot empty: IR light reaches the phototransistor → OUT is HIGH
- Slot blocked: An opaque object (a card, a disc tooth) stops the light → OUT is LOW
- Clean signal: The Schmitt trigger gives a stable 0 or 1. No jitter, so no software debounce is needed.
- Clear plastic: Transparent objects may not block IR well. Use opaque objects.
- Opposite logic: Some modules from other makers work the other way round. If yours says "blocked" with an empty slot, swap LOW and HIGH in the code.
Wiring Diagram
Here's how to connect the IR slotted optical speed sensor to the ESP32 C3 Super Mini:

This image is created using Fritzing. Click to enlarge image
| Speed Sensor Pin | ESP32 C3 Super Mini Pin |
|---|---|
| VCC | 3.3V |
| GND | GND |
| OUT | GPIO4 |
- Use 3.3V, not 5V: The OUT pin goes up to the VCC voltage. The ESP32 C3 is a 3.3V board, so power the sensor from 3.3V.
- No extra parts: No resistor is needed. Three wires are enough.
How To Program For IR Slotted Optical Speed Sensor
Here's the code to read the IR slot sensor. No library is needed.
- Set the pin as input:
- Read the state (LOW = blocked, HIGH = clear):
- Count fast pulses with an interrupt (one count per tooth, on the FALLING edge):
- Turn pulses into RPM (pulses counted in 1 second):
ESP32 C3 Super Mini Code - Detect Object in the Slot
What this code does:
- Reads the OUT pin again and again with digitalRead()
- Prints a message only when the state changes
- Adds 1 to a counter each time the slot is blocked
Detailed Instructions
- First time with this board? Read Getting Started with ESP32 C3 Super Mini to set up Arduino IDE.
- Wire the sensor as shown in the table above.
- Plug the ESP32 C3 Super Mini into your PC with the USB Type-C cable.
- In Arduino IDE, select the ESP32 C3 Super Mini board and the right COM port.
- Copy the code and paste it into Arduino IDE.
- Click Upload. If the upload fails, hold the BOOT button, press RESET, then try again.
- Open the Serial Monitor and set it to 9600 baud.
- Move a piece of card or your finger through the slot a few times.
- Check the result on the Serial Monitor.
ESP32 C3 Super Mini Code - Measure Motor Speed (RPM)
What this code does:
- Uses an interrupt on the FALLING edge. Each disc tooth that blocks the slot adds 1 pulse.
- Every 1 second, it reads the pulse count and resets it to 0
- Works out the RPM and prints pulses and speed
Why an interrupt?
- The pulses come very fast. Checking the pin in loop() could miss some.
- FALLING means one count per tooth.
The RPM formula:
- 1 turn = 20 pulses (the common disc on TT gear motors has 20 slots)
- Pulses in 1 second × 60 = pulses per minute
- Pulses per minute ÷ 20 = turns per minute (RPM)
- Your disc has a different number of slots? Change SLOTS_PER_TURN in the code.
Detailed Instructions
- Keep the same wiring as the first example.
- Fix the encoder disc on the motor shaft or wheel. Its teeth must pass through the slot without touching the sensor.
- Upload the code the same way as before.
- Open the Serial Monitor at 9600 baud.
- Spin the disc by hand, or power the motor.
- Watch the pulses and RPM change every second.
Example output with the motor at about 300 RPM:
- The RPM code counts one edge per slot. So it works even if your module has the opposite logic.
Additional Knowledge
Slot sensor vs other sensors:
- IR slotted optical speed sensor: Senses an object inside its 5.9 mm slot. Best for speed, RPM, and counting.
- IR obstacle avoidance sensor: Senses an object at a distance in front of it, not inside a slot.
- Rotary encoder: A knob for hand input. It also tells the turn direction.
Picking slots per turn:
- More slots = more pulses per turn = finer speed reading
Distance from wheel turns:
- Turns = pulses ÷ slots per turn
- Distance = turns × wheel circumference (π × diameter)