Arduino Intelligent Brake Light
by chienline in Circuits > Arduino
9952 Views, 59 Favorites, 0 Comments
Arduino Intelligent Brake Light
Having a cool bike Should you have a cool light You can abandon the rusty But you should care about safety
Considering safety on the street you should have a brake light on your bike. The population of vehicles are getting higher and higher every year. You can horn the vehicles in front of you, but a sudden brake can make you hit by the vehicles behind you.
Mostly, brake light is triggered from the brake levers. With an Arduino, we can make it intelligent. It will blink when you slow down. It will light up when you brake. When you stop for a long time, it will turn off the light at desired time coded in it. This project is based on the Arduino Bike Speedometer project by Amanda Ghassaei so it is a speed sensor that turn on or off the LED.
Previously I've build this PVC Bike Wheel Stand intentionally for this project. I need to get the rear wheel off the floor so that I can turn the wheel for testing.
Pretty cool right? Let's check it out.
What You Need
- An Arduino (I use Sparkfun RedBoard, which is an Arduino compatible board).
- Magnetic reed switch.
- A red LED.
- 300 ohm resistor.
- 10K ohm resistor.
- Some cable zip ties.
- Some wires.
Magnetic Reed Switch
If you have a sealed magnetic reed switch, then you can skip this step. I only have a very fragile glass magnetic switch, so I need to make a case for it. What I find is an old universal charger connector and a female USB. Female USB case is cooler, but it is hard to take off the part inside. Then I pick the charger connector which is easy to take apart by unscrewing two screws on it and two little clips by its sides.
I solder the magnetic switch to a pair of cables. Wrap it with heat shrink tube. Glue it on the case with a hot glue gun. This case has three holes on its sides so be aware on rainy days. You can choose a better case for it then :)
Wiring
The wiring in this project is very simple. We have two wires from the magnetic reed switch. They have no polarity. I use my Wire Quick Joint and two breadboard jumper wires so that it is easier to plug on Arduino pin since it is in testing phase. You can then solder a male pin header directly on the wire if you want to. Plug one wire to Arduino 5V and the other to Arduino analog pin A0. Add a 10K pull down resistor from analog pin A0 to GND. For saving a breadboard you can use this trick : turn one lead of resistor to the A0 jumper wire, and plug the other lead to the GND.
Coding
I do not re-write this code from the beginning, I just revise the Arduino Bike Speedometer codes by Amanda Ghassaei. What I change is using meter per second (m/s) instead of mile per hour (mph). Bike do not goes that fast so reading meter per second is easier I think, but you can change it back to mph or kph if you like to. The other thing is set the speed to zero when we stop at the position where the magnet keeps the reed switch closed.
Then I add the speed reading to turn on or off the light. When the speed is slowing down, the LED will blink so that vehicles at the back will notice and be aware. When the bike stop totally or the brake lever is pressed so that the wheel is not spinning, the LED will turn on. The light will be off on certain time, you can set this in the code namely lightOffDelay and lightOnCounter.
//arduino bike speedometer w serial.print() //by Amanda Ghassaei 2012 //https://www.instructables.com/id/Arduino-Bike-Speedometer/ // Revise by Chienline @2016 // Showing Zero instead of Max Speed when reed switch stay closed on stop. // Change from MPH (mile per hour) to m/s (meter per second). // Add functionality of Intelligent Stop Light on Digital PIN 13 /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * */ //calculations //tire radius ~ 13.5 inches //circumference = pi*2*r =~85 inches //max speed of 35mph =~ 616inches/second //max rps =~7.25 #define reed A0 // pin connected to read switch #define stopLight 13 // pin connected to stop light //storage variables int reedVal; int lightOffDelay = 4; // stop light off delay time int lightOnCounter = 5; // turn on stop light for certain time; initially set higher than lightOffDelay long timer; // time between one full rotation (in ms) float mps; // speed in m/s float prevmps = 0; // previous speed in m/s float radius = 13.5;// tire radius (in inches) float circumference; int maxReedCounter = 100;//min time (in ms) of one rotation (for debouncing) int reedCounter; int i; void setup(){ reedCounter = maxReedCounter; circumference = 2*3.14*radius; pinMode(reed, INPUT); pinMode(stopLight, OUTPUT); // TIMER SETUP- the timer interrupt allows precise timed measurements of the reed switch //for more info about configuration of arduino timers see <a href="http://arduino.cc/playground/Code/Timer1"> http://arduino.cc/playground/Code/Timer1 </a> cli();//stop interrupts //set timer1 interrupt at 1kHz TCCR1A = 0;// set entire TCCR1A register to 0 TCCR1B = 0;// same for TCCR1B TCNT1 = 0; // set timer count for 1khz increments OCR1A = 1999;// = (1/1000) / ((1/(16*10^6))*8) - 1 // turn on CTC mode TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler TCCR1B |= (1 << CS11); // enable timer compare interrupt TIMSK1 |= (1 << OCIE1A); sei();//allow interrupts //END TIMER SETUP LED_OFF(); Serial.begin(9600); } ISR(TIMER1_COMPA_vect) {//Interrupt at freq of 1kHz to measure reed switch reedVal = digitalRead(reed);//get val of A0 if (reedVal){//if reed switch is closed if (reedCounter == 0){//min time between pulses has passed mps = (25.4*float(circumference))/float(timer);//calculate meter per second if (mps > 21){ // this is when the reed keeps closing when stop; you probably don't go that fast. mps=0; } timer = 0;//reset timer reedCounter = maxReedCounter;//reset reedCounter } else{ if (reedCounter > 0){//don't let reedCounter go negative reedCounter -= 1;//decrement reedCounter } } } else{//if reed switch is open if (reedCounter > 0){//don't let reedCounter go negative reedCounter -= 1;//decrement reedCounter } } if (timer > 2000){ mps = 0;//if no new pulses from reed switch- tire is still, set mps to 0 } else{ timer += 1;//increment timer } } void LED_ON(int state){ // 0 = certain time has passed; turn LED OFF; lightOnCounter set higher than lightOffDelay // 1 = turn LED ON and reset lightOnCounter // 2 = turn LED ON and increase lightOnCounter if (state == 0){ digitalWrite(stopLight, LOW); lightOnCounter = lightOffDelay + 1; } if (state == 1){ digitalWrite(stopLight, HIGH); lightOnCounter = 0; } if (state == 2){ digitalWrite(stopLight, HIGH); lightOnCounter += 1; } } void LED_BLINK(){ int j; for (j=1;j<10;j++){ digitalWrite(stopLight, HIGH); delay(90); digitalWrite(stopLight, LOW); delay(90); } } void LED_OFF(){ digitalWrite(stopLight, LOW); } void displaySpeed(){ Serial.println(mps); // Serial.println(" m/s"); // Serial.println(" "); } void loop(){ //print speed once a second displaySpeed(); //-- if (mps == 0){ if (prevmps != 0){// Brake ON; LED_ON for certain delay LED_ON(1); } else{ // prevmps = 0; not moving if (lightOnCounter <= lightOffDelay){ // Light ON time has not passed LED_ON(2); } else{ // Light ON time has passed; turn light OFF LED_ON(0); } } } else if (mps != 0 && mps < prevmps){ LED_BLINK(); } else{ LED_OFF(); } //-- prevmps = mps; delay(1000); }
Prepare the LED
I have an old brake light. There is an on/off switch, a LED that is always on when we switch it on, and two flashlight bulbs which will be on when the brake lever is triggered. Now I modded it to a very simple circuit as you can see in my drawing. It is now simply a LED with 300 ohm resistor that we are going to plug on the Arduino pin 13 and GND.
I will then switch to Arduino Pro Mini so that everything can be put into the brake light case. But that will be done later. I need to pimp up my whole bike starting from repainting it ;)
For testing, you can simply plug a LED on pin 13. Why I choose pin 13? Because there is a GND next to it, so you can just plug a LED there. If you are using Arduino Uno, it has a built-in LED on pin 13 and you even need no LED for testing :)
The Setup
Now you can see the advantage of the charger connector case I use. After removing the entire inner parts, I have holes through the sides. I use those holes to fasten the magnetic reed switch to the chain stay. Then I put two small neodymium magnets on the spoke. It simply stick there, but to have it remained in position you should wrap it with tape. My Arduino simply hanging on the top tube for testing. I think it is not so hard for you to find a case and a mounting place for it. You can mount it as close as possible to the brake light to reduce messy wires or you can hook up an LCD to show the speed and mount it on the stem handle bar.
One thing should go to your consideration upon setup mounting place. If you are using it as speedometer, then it is better mounted on the front wheel. Why? Because when we are speeding and then we brake, well I mean rear brake because we never brake the front wheel in high speed, the bike still slide with little speed on it. If you mount it at the rear wheel, the brake will stop the wheel from spinning and the magnet will not trigger the reed switch then the speedometer will reads zero. But in this brake light project you should mount it at rear wheel for sure. When you brake, the light should be on no matter if your bike is sliding and still has some speed.
Well, it is just a matter of little accuracy on the speedometer anyway :D
Safety Ride
For now, please do not comment on my torn saddle and my flat tire yet (-_-") This is a real messy bike which needs miracle touches here and there. After you put your Arduino in a case and mount it well, you can power it with a 5V powerbank for smartphone. Your bike are now ready to go. Show it off on your neighborhood :D