Feedback!

Bike Speedometer

Views: 8851 Difficulty: 4 Status: Development
Screen_shot_2012-12-21_at_7.15.15_pm

A hall effect sensor and an LCD screen gives you a DIY bike speedometer.

We put a magnet in our spokes and sense bicycle tire revolutions with a hall effect sensor. We output speed and and distance to an alpha-numeric LCD screen. We also throw in a temperature sensor for good measure.

Board FIle

Screen_shot_2013-03-08_at_2.31.29_am
An Image of the EAGLE board file.

FFW Speedometer Movie

The making of the through hole component speedometer in fast forward. The actual process takes about 45 minutes but here you can watch it in 45 seconds!
J0308903 ako: ultra speed !

Speedometer Code

Speedometer code keeps track of tire revolutions using a hall effect sensor. Code assumes 26 inch tires but this can be tweaked to your tire size by changing the variable:
float tire_diameter = 26.2; // put in your tire dimaeter in inches
The code that does the heavy lifting is the function:
void updateSpeedAndDistance(){
  distance = tire_circumference * revolutions;
  distance /= miles_in_inches;
  float ipm = tire_circumference / (float)last_interval;
  mph = ipm * k_ipm_2_mph;
}
This code translates from the number of tire resolutions to the speed you are going and the distance you have travelled. We know the tire revolutions by checking the hall effect sensor as we do in the following code found in the loop function:
int hall_val = digitalRead(hall_pin);
  if (hall_state != hall_val && hall_val == LOW) {
    revolutions++;
    last_interval = millis()-last_fall;
    last_fall = millis();
  }
  hall_state = hall_val;
So we check to see if the state of the hall pin has changed, and if it has changed to low so we are on the falling edge of the clock. Then we know the tire has made a complete revolution so we increment the revolutions variable. Then we calculate how long this revolution took and we will use that number to derive our speed.
/* LucidTronix bike Speedometer
 * For instructions, details and schematic, See:
 * http://www.lucidtronix.com/tutorials/13
 * Hall effect sensor on pin 16, 
 * doesn't use hardware interrupt.
 */
#include <LiquidCrystal.h>

LiquidCrystal lcd(4, 5, 0, 1, 2, 3);
String message = "Luf";

int mode = 1;
int num_modes = 2;
int cur_scroll = 0;
int cur_msg_length = 0;
int cursor_index = 0;
int char_index = 0;
int btn = 3;
int hall_pin = 16;
byte backlight_pwm = 200;
int hall_state = 1;
int revolutions = 0;
float miles_in_inches = 63360;
float mph = 0.0;
float distance = 0.0;
float tire_diameter = 26.2;
float k_ipm_2_mph;
int tire_circumference;
unsigned long last_fall = 0;
unsigned long last_interval = 0;

int backlight_pin = 9;
int cur_x = 8;
int cur_y = 1;
void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  delay(500);
  k_ipm_2_mph = 3600000 / miles_in_inches;
  tire_circumference = tire_diameter*3.14159;
  pinMode(btn, INPUT);
  pinMode(hall_pin, INPUT);
  pinMode(backlight_pin, OUTPUT);
  analogWrite(backlight_pin , backlight_pwm);
  lcd.blink();
  lcd.print("hello, world!");
  delay(1000);
}

void loop() {
  int hall_val = digitalRead(hall_pin);
  if (hall_state != hall_val && hall_val == LOW) {
    revolutions++;
    last_interval = millis()-last_fall;
    last_fall = millis();
  }
  hall_state = hall_val;
  updateSpeedAndDistance();
  
  lcd.setCursor(0, 0);
  lcd.print("Hall:" );
  lcd.print( hall_state);
  lcd.print("Mph:");
  lcd.print(mph);
  lcd.setCursor(0, 1);
  lcd.print("Miles:");
  lcd.print(distance);

  backlight_pwm = analogRead(0)/4;
  analogWrite(backlight_pin , backlight_pwm);

}

void updateSpeedAndDistance(){
  distance = tire_circumference * revolutions;
  distance /= miles_in_inches;
  float ipm = tire_circumference / (float)last_interval;
  mph = ipm * k_ipm_2_mph;
}

Parts

Title Description # Cost Link Picture
LCD HD44780 16x2 characters, White Characters Blue Backlight Value: HD44780 1 $9.95 Link Screen_shot_2012-12-28_at_7.44.53_pm
PCB Bike Kit The circuit board for the bike computer 1 $8.0 Link Screen_shot_2013-01-26_at_4.13.47_pm
ATMEGA168A-PU IC MCU AVR 16K FLASH 28PDIP Value: 16KB (8K x 16) 1.8 V ~ 5.5 V 1 $2.54 Link Screen_shot_2012-12-28_at_7.40.44_pm
Hall Effect Sensor Magnetic Hall Effect, 30mT Trip, 9.5mT Release, 3.5 V ~ 24 V Unipolar Switch, TO-92 Value: 3.5 V ~ 24 V 1 $1.08 Link 413-to-92
Potentiometer POT ROTARY, Linear 10K OHM 9MM SNAPIN Value: 10k 1 $0.76 Link Screen_shot_2012-12-28_at_7.41.04_pm
Crystal CRYSTAL 16.0 MHZ 18 PF FUND Value: 16MHz 1 $0.35 Link Screen_shot_2012-12-28_at_7.30.51_pm
Button Tactile switch SMD SPST 0.05A 12V Value: SPST 2 $0.2 Link Fsm4jsma
Transistors (BJT) - Single IC TRANS NPN SS GP 200MA TO-92 Value: 40V 300mV @ 5mA, 50mA 100 @ 10mA, 1V 1 $0.18 Link Screen_shot_2012-12-28_at_7.39.40_pm
Resistor RES 10K OHM 1/4W 5% CF MINI Value: 10k 4 $0.08 Link Screen_shot_2012-12-28_at_7.28.15_pm
Permalink: http://lucidtronix.com/tutorials/13
Keyboard kit using CD4021 shift registers and big through hole buttons....
A QWERTY keyboard you build yourself....
RGB Flashlight, wearable sound art, rainbow lamp....
Display numbers in big bright red LEDs use only 3 micro-controller pins and the 74HC595 shift registe...
Two LED matrices give 70 LEDs to light up a necklace with messages....
The heartduino is a heart-shaped arduino clone complete with onboard sensors and a 70 pixel LED matri...
Shift Registers help you multiply your inputs and outputs....
Make an interactive light show using a proximity sensor, and a bunsh of shift registers and LEDs....