Speedometer

Hi,
I am building a speedometer for my motorcycle. I am using a reed sensor and magnet from an bicycle speedometer to pick up the pulses on an interrupt pin. What is the best approach to convert those pulses to km/h?

This is the code i use now, but i don't get a steady readout.

#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 8, 9, 10, 11);

volatile byte speedcount;
unsigned int intsnelheid;
unsigned long timeold1;
int circumference=134; //circumference in cm


void setup()
{
  
  attachInterrupt(1, speedinterrupt, RISING); 
  speedcount = 0;
  intspeed = 0;
  timeold1 = 0;
  lcd.begin(16, 2);

 
}


void loop()
{
  lcd.setCursor(0,0);
 lcd.print(speed());
 delay(250);
 
}    

//snelheid berekenen
int speed(){
  if (speedcount >= 20) { 
    intspeed = (speedcount*circumference*3,6)/((millis()-timeold1)/100000);  
    speedcount = 0;
    return intspeed; }
}



//Interrupts

void speedinterrupt()
{speedcount++;}

Thanks in advance

record the time difference between successive pulses (use millis() or micros()) and calculate the speed - you need to know/measure the distance covered per pulse.

I've always use Hall effect sensors for this sort of thing - are reed relays capable of switching at the sort of speeds a motorcycle wheel turns at?

It should be possible because i tested the sensor using the original bike speedometer and that one gave a good readout (it can mesure up to 300km/h). But with my code the value changes all the time between 0 and 100 while i was driving +-25km/h

but with my code the value changes all the time between 0 and 100 while i was driving +-25km/h

-25km/h? You have a reverse gear? :slight_smile:

Sounds like your reed is bouncing.

the value on my lcd changed all the time while i was driving approximately 25km/h

How do you debounce an interrupt?

Keep an unsigned long variable 'lastTime' recording the time you last incremented the speed counter. In the ISR, call micros() and subtract lastTime to get the interval since the last update. If it is less than the smallest reasonable interval, ignore the interrupt. Else increment the speed counter and store the value that was returned by micros in lastTime.

dc42 nailed it. You're probably getting multiple interrupts because of contact bounce. Another way to overcome this is to have a "holdoff" time. When the 1st interrupt occurs, disable the interrupt for a small, reasonable amount of time during which the contacts will bounce, and re-enable it later.

And what would be a reasonable time to debounce? 100µs? or would that be too small?

littlethommy:
And what would be a reasonable time to debounce? 100µs? or would that be too small?

What's the shortest time between pulses you expect to get at the maximum speed? Work that out, then try half that time interval.

Humm... I would start by trying 5-10% of the time the wheel takes for a full turn at the "current" RPM, and some 50ms - 100ms near 0 RPM... needs tuning, specially at near stopped.

Reed switches have quote a lot of hysteresis, so I don't think the debounce time needs to be varied. It just needs to be long enough to suppress bounces, but not so long as to treat genuine pulses as bounces at or near maximum speed.

From what I remember from spec sheets assume 5ms to 10ms as a bounce time.