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.

ESP32 C3 Super Mini IR slotted optical speed sensor

Hardware Preparation

1×ESP32 C3 Super Mini
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×IR Slotted Optical Speed Sensor
1×Jumper Wires
1×Optionally, Breadboard
1×Optionally, DC Power Jack

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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

IR slotted optical speed sensor 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

How IR slotted optical speed sensor 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:

The wiring diagram between ESP32 C3 Super Mini IR slotted optical speed sensor

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:
pinMode(4, INPUT);
  • Read the state (LOW = blocked, HIGH = clear):
int state = digitalRead(4);
  • Count fast pulses with an interrupt (one count per tooth, on the FALLING edge):
attachInterrupt(digitalPinToInterrupt(4), countPulse, FALLING);
  • Turn pulses into RPM (pulses counted in 1 second):
float rpm = pulses * 60.0 / SLOTS_PER_TURN;

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
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-ir-slotted-optical-speed-sensor */ const int SENSOR_PIN = 4; // ESP32 C3 Super Mini GPIO4 connected to the OUT pin of the speed sensor int lastState; // the previous state of the sensor int objectCount = 0; // how many times the slot was blocked void setup() { Serial.begin(9600); pinMode(SENSOR_PIN, INPUT); lastState = digitalRead(SENSOR_PIN); } void loop() { int state = digitalRead(SENSOR_PIN); if (state != lastState) { // the state has changed if (state == LOW) { // LOW = something is in the slot objectCount++; Serial.print("The slot is blocked - count: "); Serial.println(objectCount); } else { // HIGH = the slot is clear Serial.println("The slot is clear"); } lastState = state; } }

Detailed Instructions

  1. First time with this board? Read Getting Started with ESP32 C3 Super Mini to set up Arduino IDE.
  2. Wire the sensor as shown in the table above.
  3. Plug the ESP32 C3 Super Mini into your PC with the USB Type-C cable.
  4. In Arduino IDE, select the ESP32 C3 Super Mini board and the right COM port.
  5. Copy the code and paste it into Arduino IDE.
  6. Click Upload. If the upload fails, hold the BOOT button, press RESET, then try again.
  7. Open the Serial Monitor and set it to 9600 baud.
  8. Move a piece of card or your finger through the slot a few times.
  9. Check the result on the Serial Monitor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
The slot is blocked - count: 1 The slot is clear The slot is blocked - count: 2 The slot is clear The slot is blocked - count: 3 The slot is clear
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2

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.
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-ir-slotted-optical-speed-sensor */ const int SENSOR_PIN = 4; // ESP32 C3 Super Mini GPIO4 connected to the OUT pin of the speed sensor const int SLOTS_PER_TURN = 20; // number of slots (holes) in the encoder disc volatile unsigned long pulseCount = 0; // changed inside the interrupt unsigned long lastTime = 0; // runs every time a tooth of the disc blocks the slot void IRAM_ATTR countPulse() { pulseCount++; } void setup() { Serial.begin(9600); pinMode(SENSOR_PIN, INPUT); attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), countPulse, FALLING); } void loop() { if (millis() - lastTime >= 1000) { // every 1 second lastTime = millis(); unsigned long pulses = pulseCount; // read and reset the counter pulseCount = 0; float rpm = pulses * 60.0 / SLOTS_PER_TURN; // pulses per second -> turns per minute Serial.print("Pulses: "); Serial.print(pulses); Serial.print(" | Speed: "); Serial.print(rpm); Serial.println(" RPM"); } }

Detailed Instructions

  1. Keep the same wiring as the first example.
  2. Fix the encoder disc on the motor shaft or wheel. Its teeth must pass through the slot without touching the sensor.
  3. Upload the code the same way as before.
  4. Open the Serial Monitor at 9600 baud.
  5. Spin the disc by hand, or power the motor.
  6. Watch the pulses and RPM change every second.

Example output with the motor at about 300 RPM:

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
Pulses: 0 | Speed: 0.00 RPM Pulses: 98 | Speed: 294.00 RPM Pulses: 101 | Speed: 303.00 RPM Pulses: 100 | Speed: 300.00 RPM Pulses: 99 | Speed: 297.00 RPM Pulses: 0 | Speed: 0.00 RPM
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2
  • 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)

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!