Skip to content

Instantly share code, notes, and snippets.

Created August 5, 2022 16:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
RPM sensor - first draft
#include <Arduino.h>
#include <U8x8lib.h>
#include <Wire.h>
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* clock=*/PIN_WIRE_SCL, /* data=*/PIN_WIRE_SDA, /* reset=*/U8X8_PIN_NONE); // OLEDs without Reset of the Display
#define REED_SWITCH D10
unsigned long rpms = 0;
void tick()
static uint64_t previousTick = 0;
uint64_t currentTick = millis();
// limit to 12K RPMs (just in case the IRQ handler takes more than 5 millis)
if ((currentTick - previousTick) <5) return;
// Compute RPMs in thousands of RPMs.
rpms = 60 / (currentTick - previousTick);
previousTick = currentTick;
void setup(void)
attachInterrupt(digitalPinToInterrupt(REED_SWITCH), tick, RISING);
u8x8.setFlipMode(1); // set number from 1 to 3, the screen word will rotary 180
void loop(void)
static unsigned long prevRpms = 1;
if (prevRpms == rpms) return;
u8x8.setCursor(0, 0);
u8x8.print(rpms, 10);
prevRpms = rpms;
// Convert in better delay form like in tick().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment