# RPM

I’m looking into using my arduino to monitor rpm.

The system counts the sparks, It outputs a very high voltage about 100v.

I made this circuit to bring down the voltage and protect the arduino with an optocoupler.

I’m new to this and could use some help with the coding, there is some counter signals info in attachments.

Thanks

JasonBoyd:
I'm looking into using my arduino to monitor rpm.

The system counts the sparks, It outputs a very high voltage about 100v.

I made this circuit to bring down the voltage and protect the arduino with an optocoupler.

I'm new to this and could use some help with the coding, there is some counter signals info in attachments.

Thanks

How many Sparks per Revolution

Basic Tachometer code

you need to set the pulses per revolution to match your tachometer

My Encoder has 400 Clock pulses per revolution
note that 150000.0 = (60 seonds * 1000000 microseconds) microseconds in a minute / 400 pulses in 1 revolution)

If your device only pulses once per revolution the multiplier needs to be 60000000.0

Change the math to get the proper multiplier for RPM for your encoder
#define Multiplier 60000000.0 // don't forget a decimal place to make this number a floating point number

``````#define ClockPin 2 // Must be pin 2 or 3
//#define DataPin 9 // can be any other pin

// My Encoder has 400 Clock pulses per revolution
// note that 150000.0 = (60 seonds * 1000000 microseconds)microseconds in a minute / 400 pulses in 1 revolution)
// change the math to get the proper multiplier for RPM for your encoder
#define Multiplier 150000.0 // don't forget a decimal place to make this number a floating point number
volatile long count = 0;
volatile long EncoderCounter = 0;
volatile float SpeedInRPM = 0;

void onPin2CHANGECallBackFunction(){
static uint32_t lTime; // Saved Last Time of Last Pulse
uint32_t cTime; // Current Time
cTime = micros(); // Store the time for RPM Calculations
int32_t dTime; // Delt in time
/*
// Encoder Code
// We know pin 2 just went high to trigger the interrupt
// depending on direction the data pin will either be high or low
EncoderCounter += (DataPinVal) ? 1 : -1; // Should we step up or down?
// End Encoder Code
*/
// calculate the DeltaT between pulses
dTime = cTime - lTime;
lTime = cTime;
//    SpeedInRPM = Multiplier / ((DataPinVal) ? dTime: (-1 * dTime)); // Calculate the RPM Switch DeltaT to either positive or negative to represent Forward or reverse RPM
SpeedInRPM = Multiplier / dTime; // Calculate the RPM Switch DeltaT to either positive or negative to represent Forward or reverse RPM
}

void setup() {
Serial.begin(115200); //115200
// put your setup code here, to run once:
pinMode(ClockPin, INPUT);
//  pinMode(DataPin, INPUT);
attachInterrupt(digitalPinToInterrupt(ClockPin),onPin2CHANGECallBackFunction,RISING);
}

void loop() {
//  long Counter;
float Speed;
noInterrupts ();
// Because when the interrupt occurs the EncoderCounter and SpeedInRPM could be interrupted while they
// are being used we need to say hold for a split second while we copy these values down. This doesn't keep the
// interrupt from occurring it just slightly delays it while we maneuver values.
// if we don't do this we could be interrupted in the middle of copying a value and the result get a corrupted value.
//  Counter = EncoderCounter;
Speed = SpeedInRPM;
interrupts ();

// use the speed and counter values for whatever you need to do.

static unsigned long SpamTimer;
if ( (unsigned long)(millis() - SpamTimer) >= (100)) {
SpamTimer = millis();
//   Serial.print(Counter );
//   Serial.print("\t");
Serial.print(Speed , 3);
Serial.print(" RPM");
Serial.println();
SpeedInRPM = 0; // if no pulses occure in the next 100 miliseconds then we must assume that the motor has stopped
}
}
``````

Z