Projects / ESP32-C3 RPM meter

ESP32-C3 RPM meter

Intermediate2.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. ESP32

    Wire the sensor

    VCC to 3V3, GND to GND, DO to GPIO4.

  2. ESP3213.4 mASSD1306

    Wire the OLED

    3V3, GND, SDA to GPIO8, SCL to GPIO9.

  3. ESP3213.4 mASSD1306

    Install the libraries

    Adafruit SSD1306 and Adafruit GFX.

  4. ESP32

    Upload the sketch

    The display shows 0.

  5. ESP32

    Fix the tape

    A small piece on the shaft or a light disc on it.

  6. ESP32

    Aim the sensor

    5 to 10 mm from the tape.

  7. ESP32

    Adjust the dial

    The sensor's light flickers once for each pass.

  8. ESP32

    Run the motor

    The RPM appears on the display.

In short

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

FromToWhy
TCRT5000 VCC and OLED VCCESP32-C3 SuperMini 3V3Power at 3.3 V
TCRT5000 GND and OLED GNDESP32-C3 SuperMini GNDCommon ground
TCRT5000 DOESP32-C3 SuperMini GPIO4One pulse for each pass of the tape
OLED SDA and SCLESP32-C3 SuperMini GPIO8 and GPIO9I2C 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.

CheckExpected
Motor stopped0
Motor running slowlyA steady low number
Motor at full speedA higher steady number
Two pieces of tapeSet MARKS to 2 for the right value
Hand over the sensorThe 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 →

Ask Lark AI

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.