Skip to content

Instantly share code, notes, and snippets.

@aarongough
Created September 10, 2013 13:36
Show Gist options
  • Save aarongough/6509501 to your computer and use it in GitHub Desktop.
Save aarongough/6509501 to your computer and use it in GitHub Desktop.
A simple Tachometer written for the Arduino. Uses a 'photo interruptor' on pin 3 as the input. The photo interruptor used was: https://www.sparkfun.com/products/9299 In combination with this breakout board: https://www.sparkfun.com/products/9322
#include <SoftwareSerial.h>
#include <serLCD.h>
int lcdTxPin = 2;
int tachPin = 3;
volatile int pulseCount = 0;
volatile unsigned long samplePeriodStart = 0;
volatile unsigned long pulsePeriod = 0;
unsigned long rpm = 0;
int pulsesPerRevolution = 50;
int pulseSampleSize = 20;
int minRpm = 10;
serLCD lcd(lcdTxPin);
void setup(){
lcd.clear();
lcd.setBrightness(30);
pinMode(tachPin, INPUT);
attachInterrupt(1, registerTachPulse, FALLING);
samplePeriodStart = micros();
}
void loop(){
if(pulsePeriod > 0){
rpm = 60000000 / pulsePeriod;
rpm = rpm / pulsesPerRevolution;
}
if((micros() - samplePeriodStart) > ((60000000 / (minRpm * pulsesPerRevolution)) * pulseSampleSize)){
rpm = 0;
}
lcd.clear();
lcd.print("RPM: ");
lcd.print(rpm);
delay(100);
}
void registerTachPulse(){
pulseCount++;
if(pulseCount == pulseSampleSize){
pulsePeriod = micros() - samplePeriodStart;
pulsePeriod = pulsePeriod / pulseSampleSize;
pulseCount = 0;
samplePeriodStart = micros();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment