Build it step by step
0 of 8 doneWire the sensor
VCC to 3V3, GND to GND, DO to GPIO4.
Wire the OLED
3V3, GND, SDA to GPIO8, SCL to GPIO9.
Install the libraries
Adafruit SSD1306 and Adafruit GFX.
Upload the sketch
The display shows 0.
Fix the tape
A small piece on the shaft or a light disc on it.
Aim the sensor
5 to 10 mm from the tape.
Adjust the dial
The sensor's light flickers once for each pass.
Run the motor
The RPM appears on the display.
How do you build an ESP32-C3 RPM meter?
An ESP32-C3 counts passes of a piece of reflective tape on a small spinning shaft with a TCRT5000 sensor and shows revolutions per minute on an OLED. You need ESP32-C3 SuperMini, TCRT5000 IR reflective sensor module, SSD1306 OLED Display and Breadboard 830 Tie Points. It takes 8 steps and about 150 minutes.
- Level
- Intermediate
- Time
- About 150 min
- Parts
- 4
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| TCRT5000 VCC and OLED VCC | ESP32-C3 SuperMini 3V3 | Power at 3.3 V |
| TCRT5000 GND and OLED GND | ESP32-C3 SuperMini GND | Common ground |
| TCRT5000 DO | ESP32-C3 SuperMini GPIO4 | One pulse for each pass of the tape |
| OLED SDA and SCL | ESP32-C3 SuperMini GPIO8 and GPIO9 | I2C data and clock |
Code
The TCRT5000 sends out infrared light and measures how much comes back. A shiny piece of tape on a dark shaft sends back more light than the shaft does, so each time the tape passes the sensor the output changes. The module has a comparator and a small dial that sets the switching level, so the output is a clean on and off. The program measures the time between two passes with a microsecond timer inside an interrupt, which catches every pulse, and works out revolutions per minute as sixty million divided by that time in microseconds. A little smoothing steadies the display. If the shaft stops, no pulse arrives for a second and the reading drops to zero. Measuring the time between pulses gives a much better result at low speeds than counting pulses over a second. Use only a small low-voltage toy motor with a guard around it, and keep hands, hair and leads away from anything that spins.
Runs on a virtual ESP32-C3 in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int SENSOR = 4;
const int MARKS = 1; // pieces of tape on the shaft
volatile unsigned long lastEdge = 0; // time of the last pulse in microseconds
volatile unsigned long period = 0; // time between the last two pulses
void IRAM_ATTR onPulse() {
unsigned long now = micros();
unsigned long dt = now - lastEdge;
if (dt > 2000) { // ignore anything faster than 2 ms, it is noise
period = dt;
lastEdge = now;
}
}
float smoothed = 0;
void setup() {
pinMode(SENSOR, INPUT);
Wire.begin(8, 9);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
attachInterrupt(digitalPinToInterrupt(SENSOR), onPulse, RISING);
}
void loop() {
noInterrupts();
unsigned long p = period;
unsigned long last = lastEdge;
interrupts();
float rpm = 0;
if (p > 0 && micros() - last < 1000000UL) { // no pulse for a second means stopped
rpm = 60000000.0 / (p * (float)MARKS);
}
smoothed = 0.7 * smoothed + 0.3 * rpm;
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print("RPM meter");
oled.setTextSize(3);
oled.setCursor(0, 24);
oled.print((int)smoothed);
oled.display();
delay(100);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Motor stopped | 0 |
| Motor running slowly | A steady low number |
| Motor at full speed | A higher steady number |
| Two pieces of tape | Set MARKS to 2 for the right value |
| Hand over the sensor | The reading jumps or goes to zero |
If it does not work
Always zero
Check DO is on GPIO4, the sensor distance and the dial setting.
Reading is double
Reflections from the shaft end, add a black cover or set MARKS to match.
Reading jumps
Shield the sensor from bright light, and keep it steady.
Display blank
Check the OLED address 0x3C and the I2C wires.
Motor makes noise on the reading
Keep the sensor leads away from the motor leads.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the ESP32-C3 RPM meter?
ESP32-C3 SuperMini, TCRT5000 IR reflective sensor module, SSD1306 OLED Display and Breadboard 830 Tie Points.
How long does the ESP32-C3 RPM meter take to build?
About 150 minutes for a intermediate maker, in 8 steps.
How do I know the ESP32-C3 RPM meter works?
Motor stopped: 0. Motor running slowly: A steady low number. Motor at full speed: A higher steady number.
Always zero: what should I check?
Check DO is on GPIO4, the sensor distance and the dial setting.
Reading is double: what should I check?
Reflections from the shaft end, add a black cover or set MARKS to match.
Reading jumps: what should I check?
Shield the sensor from bright light, and keep it steady.
Display blank: what should I check?
Check the OLED address 0x3C and the I2C wires.
