How to make Bike Speedo Meter.

Hi,

I am now sitting bike computer project.
I read a lot about bike computers from internet. Now some topics cleared and some topics confused.

The problem is how to calculate the speed of the vehicle ?.

Hall sensor is mounted at shake absorber.(front fork). magnet is mounted alloy wheel.
Hall sensor is connected with interrupt pin 0(digital 2).

Idea ?.

Speed is distance divided by time. Distance is the circumference of the wheel. Time is the interval between sensor triggers.

Speed is distance divided by time. Distance is the circumference of the wheel. Time is the interval between sensor triggers.

Ok.

Distance is the circumference of the wheel.

I think I done below you said.

Now my bike radius is 27" inch. (Hall is triggered by magnet once per rotation)

r=27
PI=3.1415926535897932384626433832795

Circumference = 2PIr.
23.141592653589793238462643383279527 = 169.64600329384883487698274269709

approximately 170" distance per rotate from A to B.

Time is the interval between sensor triggers.

This only I confused. How to get the triggers between this time to this time(Interval)?.

By recording the time of each trigger, and subtracting.

Ok.

Using timer0 and external interrupt is enough for this ?.

SureshKumar2610:
Ok.

Using timer0 and external interrupt is enough for this ?.

It's too much for this.

An ISR is a good idea, because even though the time between pulses is long, the pulses are short. But all you need to do to get the time is capture the value of millis() in the ISR.

I suggested this code in another recent Thread. It compiles but I have not tested it

byte pulsePin = 2;
unsigned long prevPrintMillis;
unsigned long printIntervalMillis = 1000;

unsigned long prevPulseMicros;
unsigned long newPulseMicros;
volatile unsigned long latestPulseMicros;
volatile boolean newPulse = false;
unsigned long microsBetweenPulses;



void setup() {
    Serial.begin(9600);
    Serial.println("Starting my program");

    pinMode(pulsePin, INPUT_PULLUP);
    attachInterrupt(0, pulseDetect, RISING);
}

void loop() {
    if (newPulse == true) {
        prevPulseMicros = newPulseMicros;
        noInterrupts();
            newPulseMicros = latestPulseMicros;
            newPulse = false;
        interrupts();
        microsBetweenPulses = newPulseMicros - prevPulseMicros;
    }
    if (millis() - prevPrintMillis >= printIntervalMillis) {
        prevPrintMillis += printIntervalMillis;
        Serial.print("Micros Between Pulses ");
        Serial.println(microsBetweenPulses);
    }
}

void pulseDetect() {
    latestPulseMicros = micros();
    newPulse = true;
}

...R

Ok. thanks
I try this and update to you all.

SureshKumar2610:
PI=3.1415926535897932384626433832795

How accurately do you need to know your speed? Because this amount of digits is useful for measuring the radius of an atom in lightyears.

MorganS:
How accurately do you need to know your speed? Because this amount of digits is useful for measuring the radius of an atom in lightyears.

HAHAHAHH That did make me LOL

When I read the OP code, I thought - don't do the floating point math on the arduino!
If the circumference is 170", use that.

MorganS:
How accurately do you need to know your speed? Because this amount of digits is useful for measuring the radius of an atom in lightyears.

Also, PI is already defined in Arduino.h

A nice little project.... but you can buy such a device powered by a coin cell for about £6 from any bike shop..

Allan.

This is the one of the function in my bike computer. I need to know the working concepts. I need to make me one.

allanhurst:
A nice little project.... but you can buy such a device powered by a coin cell for about £6 from any bike shop..

Allan.

Now I am something cleared. But not full. This only makes me confusion

Time is the interval between sensor triggers.

Hi,

Now I think I got it.(I am waiting for yours reply this is rite or wrong).

Circumference of the wheel= 2πr (where ‘r’ is in cm)
= 2×3.14×30
= 188.4 cm or 1.884 metres

Speed. Let’s assume that in 1 second the wheel
completes one revolution. In other words, in one second, the bike has covered 1.88 metres. Therefore the speed in km/hour:

N×1.88×3600/1000
= N×6.784 or N×6.8
where ‘N’ is the number of revolutions per second.
‘6.8’ is a constant and only ‘N’ varies; for example, if ‘N’
is 5, the speed equals 5×6.8= 34 km/hour.

I get this calculation from this link. this is correct ?.

I get this calculation from this link. this is correct ?.

You need to determine the circumference of your wheel. Is it 188.4 cm?

Just leave the value. That is not mine. Mine is 170" inches. I am asking about the formula to find the km/hr ?.
Is that formula is correct ?.

(Diameter of wheel in metres x pi x 3.6 ) / (time for 1 revolution in seconds)

Allan

You have a given distance per revolution.
You will measure the time (in milliseconds) of that revolution.
Then calculate the speed.

Here is how I would go for MPH

1 mile = 63360 inches
1 hour = 3,600,000 milliseconds

Let's say you measure 875 milliseconds for a single revolution

speed = (170" / 63360) / (875milli / 3600000)
or
speed = 170 * 3600000 / (875 * 63360)
by my math, that results in 11mph (integer math with its rounding)

For KPH, use
1 kilometer = 39370 inches
speed = 170 * 3600000 / (875 * 39370)
speed = 17kph

Using floating point math, that would be 17.7blahblah kph
But representing the speed in an integer value is probably less confusing.
And the arduino likes integer math much more than floating point.

My math is always suspect. Check for errors. If found, mention it and we will all be better off.

Hope things got settled here.. I would just like to shower some more light on this topic.

I have already made a project in which the speed of a bi-cycle is calculated using Arduino and hall sensor.

The project can be found here.

I have used the ISR and millis() to calculate the time taken for one complete rotation of the wheel. Then using this time taken I have calculated the rpm of the wheel. Then finally after knowing the circumference of the wheel we can calculate its velocity in km/hr.

I have simplified the formulas and have listed them below...

Timetaken = millis() – pevtime;
rpm = (1000/timetaken) * 60;
v= radius_of_wheel * rpm * 0.37699;

The complete code of this project is given below

/*Arduino Code for measuring speed of the Vechile using Hall Sensor
* Coded by Circuitdigest.com
* On 14-04-2017
*/
/*CONNECTION DETIALS
 * Arduino D11 -> RX of BT Module
 * Arduino D12 -> Tx of BT
 * Arduino D2  -> Hall sensor 3rd pin
 */
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial Cycle_BT(11, 12);  // RX, TX
int ledpin=13; // led on D13 will show blink on / off
int BluetoothData; // the data given from Computer
float radius_of_wheel = 0.33;  //Measure the radius of your wheel and enter it here
volatile byte rotation; // variale for interrupt fun must be volatile
float timetaken,rpm,dtime;
int v;
unsigned long pevtime;

void setup()
{
  Cycle_BT.begin(9600); //start the Bluetooth communication at 9600 baudrate
  //pinMode(ledpin,OUTPUT); //LED pin aoutput for debugging
  attachInterrupt(0, magnet_detect, RISING); //secound pin of arduino used as interrupt and magnet_detect will be called for each interrupt
  rotation = rpm = pevtime = 0; //Initialize all variable to zero
}

void loop()
{
 /*To drop to zero if vehicle stopped*/
if(millis()-dtime>1500) //no magnet found for 1500ms
{
 rpm= v = 0; // make rpm and velocity as zero
 Cycle_BT.write(v);
 dtime=millis();
}
v = radius_of_wheel * rpm * 0.37699; //0.33 is the radius of the wheel in meter
}

void magnet_detect() //Called whenever a magnet is detected
{
 rotation++;
 dtime=millis();
 if(rotation>=2)
 {
   timetaken = millis()-pevtime; //time in millisec for two rotations
   rpm=(1000/timetaken)*60;    //formulae to calculate rpm
   pevtime = millis();
   rotation=0;
   Cycle_BT.write(v);
   //Cycle_BT.println("Magnet detected...."); //enable while testing the hardware
 }
}

Aswinth

Aswinth:
Timetaken = millis() – pevtime;
rpm = (1000/timetaken) * 60;
v= radius_of_wheel * rpm * 0.37699;

why 0.37699 is constant ?.

then why 1000/timetaken ?.