# Hall effect speedometer U8GLIB

How can I attach an interrupt to an hall sensor and read real life speed and at the same time save km in a odometer onto the eeprom?

look at the example

Thank you, but what if I need it in microseconds? Or if i need to count let's say 100 inputs /second, is it still reliable?

interrupt are pretty quick, 100 per seconds is no problem if you don't do too much in the interrupt code... you can easily go to 10,000 interrupts a second

the limit in the code they have is

``````volatile byte half_revolutions;
``````

if your speed is really really fast, then the loop might not catch up that value before it overflows (255) and so you could want to go with

``````volatile unsigned int half_revolutions ;
``````

then your have 65535 ticks before it overflow - should be plenty. don't add anything more into the interrupt. just increase the counter.

and of course if you want microseconds accuracy use the micros instead of millis and adjust the maths

That's awesome. Thank you!

so, after various search i come up with the following code that works fine under serial (except I have to find a way for it to show 0 km/h when the wheel stops spinning.

const byte LED_pin = 13;

// wheel radius in mm * 100 * 2 * ( pi * 10000 ) = 94.248000 mm circumference.
// 6 0's were used in scaling up radius and pi, 6 places are divided in the end
// and the units work out. You can use integers more accurate than float on
// Arduino at greatly faster speed. Both type of long can hold any 9-digits.
// Arduino variable type long long can hold any 19 digits is 19 place accuracy.
// if you work in Small Units and scale back later, integers are plenty accurate.
// remember, this value has to be divided by microseconds per turn.
const unsigned long wheel_circumference = 431800UL * 2UL * 31416UL; // = 94,248,000
// wheel circumference gets divided by microseconds, 1,000,000/sec (usec or us).
// wheel turns once for 94248000 mm/100 in 1000000 usecs =

unsigned long Speed = 0;
unsigned long PrevSpeed = 0;

volatile byte hall_rising = 0; // interrupt flag
volatile unsigned long irqMicros;

unsigned long startMicros;
unsigned long elapsedMicros;

unsigned long ledFlashStartMillis;
const unsigned long ledFlashWaitMillis = 10;

void wheel_IRQ()
{
irqMicros = micros();
hall_rising = 1;
}

void setup()
{
pinMode( 2, INPUT_PULLUP ); // pin # is tied to the interrupt
pinMode( LED_pin, OUTPUT );
Serial.begin( 9600 ); // can this be faster? faster would be better

attachInterrupt( 0, wheel_IRQ, RISING ); // pin 2 looks for LOW to HIGH change
}

void loop()
{
detachInterrupt(0);
elapsedMicros = irqMicros - startMicros;

// print speed 1000/Wait (5 times per second)
if( elapsedMicros != 0 )
{
Speed = (wheel_circumference * 0.0009) / elapsedMicros;

if ( Speed != PrevSpeed )
{
Serial.print( Speed ); // this now shows mm/sec with no remainder
Serial.print( "km/h" );
Serial.println( );
}

PrevSpeed = Speed;
}
startMicros = irqMicros;
attachInterrupt( 0, wheel_IRQ, RISING ); // pin 2 looks for LOW to HIGH change

}

Now except the 0 problems I'm having really hard times trying to integrate this into u8glib code to show the value on a glcd. Has someone any suggestion on how to use these kind of interrupts in u8glib?

Anyone?????