Hi, This is my first post here. I've taken a Arduino beginners course a while ago and have made some useful (and some not so useful) projects since I've finished the course.
My car (from the 1960's) does not have a tachometer (REV counter). So one of my latest projects is to make a digital tachometer. The idea is to use the signal from the negative side of the ignition coil. To lower the voltage I've made a voltage divider. The signal then connects to the interrupt pin on the Arduino and the ground of the Arduino is connected to the negative battery terminal of the car. To display the RPM a 4x7segment display is connected to the Arduino.
I'm using the following code to measure and display the RPM:
#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 3
#define DIO 4
TM1637Display display(CLK, DIO);
const uint8_t SEG_ERR[] = {
0x0,
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G,
SEG_E | SEG_G,
SEG_E | SEG_G
};
const uint8_t SEG_HOI[] = {
0x0
SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,
SEG_E | SEG_F
};
float rev=0;
int c=8; //number of cylinders
int rpm;
int oldtime=0;
int time;
void isr(){ //interrupt service routine
rev++;
}
void setup() {
display.setBrightness(4); //0-4
display.clear();
display.setSegments(SEG_HOI); //display welcome message
delay(2000);
attachInterrupt(0,isr,RISING); //attaching the interrupt
}
void loop() {
delay(100);
detachInterrupt(0); //detaches the interrupt
time=millis()-oldtime; //finds the time
rpm=((rev/time)*60000)/c; //calculates rpm
oldtime=millis(); //saves the current time
rev=0;
if (rpm<0){
rpm=0;
}
if (rpm>7000){
rpm=7000;
}
if (rpm=<7000){
display.showNumberDec(rpm, false);
}
else{
display.setSegments(SEG_ERR); //Show error message if rpm > 7000
}
attachInterrupt(0,isr,RISING);
}
It however does not give the RPM I was expecting. Is was more like double of what I was expecting. So I've connected my oscilloscope. And here is the result;
Oscilloscope connected directly to the ignition coil: directly connected.jpg
The video shows the signal with the voltage divider (the signal the Arduino receives)
Video: MOV 1025 - YouTube
I'm guessing the reason it's not showing the correct RPM has to do with the ugly signal. It looks like the Arduino most of the time may detect not 1 but 2 pulses per "ignition spark".
Does anybody have any suggestions on how to make this work? Simply dividing the RPM by 2 in the code is not the right way because it sometimes also detects just 1 pulse per spark.