Difference between revisions of "Bicycle speedometer"

From ShawnReevesWiki
Jump to navigationJump to search
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
When a student in my circuits class asked if they could build a speedometer, I began to seek a simple circuit that would include a switch, a frequency meter, some way to convert that to a speed, and a display.
+
==About making a bicycle speedometer==
 +
When a student in my circuits class asked if one could build a speedometer, I began to seek a simple circuit that would include a switch, a frequency meter, some way to convert that to a speed, and a display.
  
 
===Specifications===
 
===Specifications===
*Use a magnetic reed switch attached to the frame/fork of a bicycle.
+
*Use a magnetic reed switch attached to the frame/fork of a bicycle, or a Hall sensor switch.
 
*Run on a battery.
 
*Run on a battery.
*Include a two-digit display.
+
*Include at least a two-digit display. An LCD will save power over an LED display.
 
*Include a power switch to save the battery.
 
*Include a power switch to save the battery.
 
*Weigh as little as possible.
 
*Weigh as little as possible.
 
*Use a breadboard so it can be modified.
 
*Use a breadboard so it can be modified.
 +
*Optionally include a switch to allow changing tire diameter.
  
===Circuit===
+
==Versions==
 +
===AVR===
 +
[[File:Bicycle-speedometer-Arduino-on-a-board-4-bit-LCD.png|256px|thumb|right|Circuit diagram]]
 +
This version is similar to my [[frequency meter]], but includes a variable wheel-radius, adjustable by button. It is built around an Atmel 328P AVR micro-controller, programmed using the Arduino IDE.
 +
 
 +
See [[Arduino program for bicycle speedometer]] for a working code.
 +
 
 +
====Components====
 +
See [[Media:Bicycle-speedometer-Arduino-on-a-board-4-bit-LCD.pdf]] for a printable diagram.
 +
;Atmel 328P AVR micro-controller:
 +
;TT electronics OH090U, Hallogic Hall-effect sensor:This sensor is uni-polar, meaning it turns on only when it senses a south pole facing its symbol-face, with a strength of about 90 Gauss. A hysteresis of about 10 Gauss helps reduce bounce in the switch.
 +
;Lumex 1601 character LCD:This LCD operates internally as an 8x2 character display, so the program needs to send characters to the second line for them to appear in the rightmost eight spaces. We use 4-bit mode, so data goes over four lines, and  a total of nine separate lines go from the circuit board to the LCD, including a middle-voltage that controls the contrast.
 +
;10kΩ resistor:This resistor pulls the micro-controller RESET pin high for normal operation.
 +
;Two momentary-on push-button switches: One pulls reset low, and the other changes the wheel-diameter.
 +
;16Mhz crystal:For accurate timing, we use an external 16MHz crystal. A lower frequency would save power, something for a future version.
 +
;Two 20pF, +/-2pF capacitors:These capacitors allow the crystal to oscillate.
 +
;0.1µF electrolytic or ceramic capacitor:This capacitor helps smooth the voltage on power busses so switches and signals don't trip each other.
 +
====Power supply====
 +
The LCD is intended to operate between 4.7V and 5.3V. A battery case with a switch and three alkaline AAA cells works well. A 9V battery with a 5V regulator would also work, but waste more energy.
 +
 
 +
We could save energy by putting the micro-controller to sleep if the sensor doesn't send a signal for more than, say, 30 seconds. See http://playground.arduino.cc/Learning/ArduinoSleepCode .
 +
 
 +
===2051===
 
[[File:Speedometer-Diagram1.png|256px|thumb|right|Circuit diagram for a bicycle speedometer]]
 
[[File:Speedometer-Diagram1.png|256px|thumb|right|Circuit diagram for a bicycle speedometer]]
 +
 +
This version is built around an Atmel 89C2051 micro-controller, driving a two-digit LED display.
 +
 
The circuit uses a common-anode layout since the 2051's output ports can sink up to 20mA of current.
 
The circuit uses a common-anode layout since the 2051's output ports can sink up to 20mA of current.
  
Line 17: Line 44:
 
We could use PNP transistors instead of MOSFETs but we'd need a resistor between the micro-controller and the base of the transistor.
 
We could use PNP transistors instead of MOSFETs but we'd need a resistor between the micro-controller and the base of the transistor.
  
If the power supply can go above 6V, for example if it is four AA or AAA cells, then a voltage regulator or a step-down power diode will be necessary, which will be a waste.
+
If the power supply can go above 5.5V, for example if it is four AA or AAA cells, then a voltage regulator or a step-down power diode will be necessary, which will be a waste.
  
===Components===
+
====Components====
 
;Atmel 89C2051:This is a very cheap (~$1) micro-controller that can run on 2.7 to 6 volts. To program the C variant requires a flash programmer with a 12V power supply. There is an S variant that can be serially programmed in-circuit.
 
;Atmel 89C2051:This is a very cheap (~$1) micro-controller that can run on 2.7 to 6 volts. To program the C variant requires a flash programmer with a 12V power supply. There is an S variant that can be serially programmed in-circuit.
;Lumex LDD-M512RI-RA:This is a dual-digit, seven-segment display, where the LED's cathodes for each digit are tied together to reduce the number of pins to 10. This means that unless both digits are the same, we need to alternate rapidly between showing one and the other by allowing current into one anode or the other. The LEDs are green.
+
;Lumex LDD-M512RI-RA:This is a dual-digit, seven-segment display, where the LED's cathodes for each digit are tied together to reduce the number of pins to 10. This means that unless both digits are the same, we need to alternate rapidly between showing one and the other by allowing current into one anode or the other. The LEDs are green for excellent visibility in daylight.
 
;Fairchild BS270:This is an N-channel MOSFET suited to switching at logic levels (3-5V). The current that goes through depends on the voltage between the gate and source, and the switching happens in nanoseconds. When the voltage at the gate is equal to or lower than the source, no current is allowed from drain to source. Since more current is allowed when the voltage at the gate is much higher than at the source, this N-channel MOSFET is better suited to a common cathode display. I'd like to find a better MOSFET for this common-anode display, and I welcome suggestions on the [[Talk:Bicycle_speedometer|discussion]] page.
 
;Fairchild BS270:This is an N-channel MOSFET suited to switching at logic levels (3-5V). The current that goes through depends on the voltage between the gate and source, and the switching happens in nanoseconds. When the voltage at the gate is equal to or lower than the source, no current is allowed from drain to source. Since more current is allowed when the voltage at the gate is much higher than at the source, this N-channel MOSFET is better suited to a common cathode display. I'd like to find a better MOSFET for this common-anode display, and I welcome suggestions on the [[Talk:Bicycle_speedometer|discussion]] page.
 
;11MHz crystal:The 2051 can run up to 24MHz. We should use the slowest possible crystal to save energy. The 2051 uses half as much energy running at 8MHz as it does running at 24MHz. At 5V with an 11MHz crystal, the chip will use about 40mW, plus whatever it takes to drive the LEDs, which is about another 40mW with the initial circuit shown.
 
;11MHz crystal:The 2051 can run up to 24MHz. We should use the slowest possible crystal to save energy. The 2051 uses half as much energy running at 8MHz as it does running at 24MHz. At 5V with an 11MHz crystal, the chip will use about 40mW, plus whatever it takes to drive the LEDs, which is about another 40mW with the initial circuit shown.
Line 31: Line 58:
 
;Power switch:Without a power switch the circuit would consume a battery in a few days or less. It should be attached to one of the wires from the battery.
 
;Power switch:Without a power switch the circuit would consume a battery in a few days or less. It should be attached to one of the wires from the battery.
  
===Power supply===
+
====Power supply====
 
We can use three AAA or AA cells which would provide 3.6V to 5.1V, or one lithium 3.7V cell. Or, we can use a 9V battery with a voltage regulator. A coin battery would not last a day if we left the switch on.
 
We can use three AAA or AA cells which would provide 3.6V to 5.1V, or one lithium 3.7V cell. Or, we can use a 9V battery with a voltage regulator. A coin battery would not last a day if we left the switch on.
  
====Saving battery-life====
+
=====Saving battery-life=====
 
If we used an LCD display instead, we'd double or triple the life of the battery.
 
If we used an LCD display instead, we'd double or triple the life of the battery.
  
Line 44: Line 71:
  
 
Combining all of the above might increase the battery-life more than ten-fold. Note that commercial bike-computers use LCDs, coin-sized batteries, and smart power-down modes.
 
Combining all of the above might increase the battery-life more than ten-fold. Note that commercial bike-computers use LCDs, coin-sized batteries, and smart power-down modes.
 +
 +
We could replace the electrolytic capacitors with film capacitors.
  
 
To get the micro-controller to enter a power-down mode would save the most energy. It would require a logic circuit that allowed the reed switch to raise the voltage on the reset pin only if the program isn't running already.
 
To get the micro-controller to enter a power-down mode would save the most energy. It would require a logic circuit that allowed the reed switch to raise the voltage on the reset pin only if the program isn't running already.
 
*Special attention must be paid to ports P1.0 and P1.1, which can sink current, wasting power. From the datasheet: "The P1.0 and P1.1 should be set to '0' if no external pull-ups are used, or set to '1' if external pull-ups are used."
 
*Special attention must be paid to ports P1.0 and P1.1, which can sink current, wasting power. From the datasheet: "The P1.0 and P1.1 should be set to '0' if no external pull-ups are used, or set to '1' if external pull-ups are used."
 
*We need to pay attention to what happens to the ports that control the transistors that control the common anodes.
 
*We need to pay attention to what happens to the ports that control the transistors that control the common anodes.
*The reed-switch should be in it's own serial circuit with a resistor to V+, so that it creates a signal usable by the logic controlling reset no matter the status of the input pin to which it is normally connected. So, RST = (C-R voltage) OR ( ( REED SWITCH ) NOR ( CONTROL PIN) ). That logic can be achieved with NOR gates only if desired, considering the part before OR as A and the part after OR as B, as ( A NOR B ) NOR ( A NOR B ). Thus, we can use a chip with five NOR gates.
+
*The reed-switch should be in it's own serial circuit with a resistor to V+, so that it creates a signal usable by the logic controlling reset no matter the status of the input pin to which it is normally connected. So, RST = (C-R voltage) OR ( ( REED SWITCH ) NOR ( CONTROL PIN) ). That logic can be achieved with NOR gates only if desired, considering the part before OR as A and the part after OR as B, as ( A NOR B ) NOR ( A NOR B ). Thus, we can use a chip with five NOR gates. I am also investigating using a transistor-based logic, where the software-controlled pin and reed switch NOR through two transistors, then that is OR with the reset capacitor through two more transistors.
  
===Program===
+
If we used an 89S2051 instead of an 89C2051, the micro-controller would use half the power when active, and we could use the reed switch as an interrupt to wake the machine from power down mode. An 89S2051 costs $1.80 and an 89C2051 costs $0.90. If the user is silly enough to use non-rechargeable batteries, they certainly should opt for the more expensive yet more efficient micro-controller. Also, the higher price would pay for itself if it replaced the extra circuitry required to wake the C variant from power-down if that was part of the program.
====Watching a magnet go around and around====
+
 
 +
====Program====
 +
=====Watching a magnet go around and around=====
 
My program running on the 2051 runs a timer that counts up every machine cycle, which is once every 12 oscillations of the crystal. When it senses a grounded voltage on the pin (6) attached to the reed switch, it enters an "interrupt" subroutine that checks to see how much time has passed. If only a few microseconds has passed since the last grounding, it knows the reed switch is just bouncing from the last hit. If a reasonable amount of time has passed, between a few tens of milliseconds and a few seconds, it calculates the speed in miles per hour by dividing the circumference of the wheel by the time elapsed since the magnet last passed. While waiting for the next interrupt, it displays the two digits of the speed. If many seconds pass with no grounding by the reed switch, the program figures the bike is not moving or is being walked, so it shuts off the display.
 
My program running on the 2051 runs a timer that counts up every machine cycle, which is once every 12 oscillations of the crystal. When it senses a grounded voltage on the pin (6) attached to the reed switch, it enters an "interrupt" subroutine that checks to see how much time has passed. If only a few microseconds has passed since the last grounding, it knows the reed switch is just bouncing from the last hit. If a reasonable amount of time has passed, between a few tens of milliseconds and a few seconds, it calculates the speed in miles per hour by dividing the circumference of the wheel by the time elapsed since the magnet last passed. While waiting for the next interrupt, it displays the two digits of the speed. If many seconds pass with no grounding by the reed switch, the program figures the bike is not moving or is being walked, so it shuts off the display.
  
====Displaying the digits on a shared-pin LED display====
+
=====Displaying the digits on a shared-pin LED display=====
 
Since the two digits share the same cathode-pins, we can only show one at a time. We flip back and forth so quickly, about five thousand times a second, that it's imperceivable. The value of each digit is stored in a byte of memory, actually not the value [0-9] but the combination of bits to make the right LEDs on the display light up. For example, '8' requires all seven bits, while '7' requires just the first three.
 
Since the two digits share the same cathode-pins, we can only show one at a time. We flip back and forth so quickly, about five thousand times a second, that it's imperceivable. The value of each digit is stored in a byte of memory, actually not the value [0-9] but the combination of bits to make the right LEDs on the display light up. For example, '8' requires all seven bits, while '7' requires just the first three.
  
===Datasheets===
+
==Datasheets==
 
;Atmel 89C2051:http://www.atmel.com/Images/doc0368.pdf
 
;Atmel 89C2051:http://www.atmel.com/Images/doc0368.pdf
 
;Lumex LDD-M512RI-RA:http://www.lumex.com/specs/LDD-M512RI-RA.pdf
 
;Lumex LDD-M512RI-RA:http://www.lumex.com/specs/LDD-M512RI-RA.pdf
  
===See also===
+
==See also==
[[PG302]]
+
See [[PG302]] for discussion on programming the 2051.
 +
 
 +
See [[frequency meter]] for more discussion on timing.
 +
 
 +
See "Bicycle Freewheeling with Air Drag as a Physics Experiment" by Paul Janssen and Ewald Janssens in The Physics Teacher, volume 53, number 25 (2015): http://dx.doi.org/10.1119/1.4904237 (subscription or purchase required for full text access). The publisher provides a supplementary document about the circuit in a guest-accessible FTP server: ftp://ftp.aip.org/epaps/phys_teach/E-PHTEAH-53-010501
  
 
[[Category:Electronics]]
 
[[Category:Electronics]]

Latest revision as of 11:04, 28 June 2023

About making a bicycle speedometer

When a student in my circuits class asked if one could build a speedometer, I began to seek a simple circuit that would include a switch, a frequency meter, some way to convert that to a speed, and a display.

Specifications

  • Use a magnetic reed switch attached to the frame/fork of a bicycle, or a Hall sensor switch.
  • Run on a battery.
  • Include at least a two-digit display. An LCD will save power over an LED display.
  • Include a power switch to save the battery.
  • Weigh as little as possible.
  • Use a breadboard so it can be modified.
  • Optionally include a switch to allow changing tire diameter.

Versions

AVR

Circuit diagram

This version is similar to my frequency meter, but includes a variable wheel-radius, adjustable by button. It is built around an Atmel 328P AVR micro-controller, programmed using the Arduino IDE.

See Arduino program for bicycle speedometer for a working code.

Components

See Media:Bicycle-speedometer-Arduino-on-a-board-4-bit-LCD.pdf for a printable diagram.

Atmel 328P AVR micro-controller
TT electronics OH090U, Hallogic Hall-effect sensor
This sensor is uni-polar, meaning it turns on only when it senses a south pole facing its symbol-face, with a strength of about 90 Gauss. A hysteresis of about 10 Gauss helps reduce bounce in the switch.
Lumex 1601 character LCD
This LCD operates internally as an 8x2 character display, so the program needs to send characters to the second line for them to appear in the rightmost eight spaces. We use 4-bit mode, so data goes over four lines, and a total of nine separate lines go from the circuit board to the LCD, including a middle-voltage that controls the contrast.
10kΩ resistor
This resistor pulls the micro-controller RESET pin high for normal operation.
Two momentary-on push-button switches
One pulls reset low, and the other changes the wheel-diameter.
16Mhz crystal
For accurate timing, we use an external 16MHz crystal. A lower frequency would save power, something for a future version.
Two 20pF, +/-2pF capacitors
These capacitors allow the crystal to oscillate.
0.1µF electrolytic or ceramic capacitor
This capacitor helps smooth the voltage on power busses so switches and signals don't trip each other.

Power supply

The LCD is intended to operate between 4.7V and 5.3V. A battery case with a switch and three alkaline AAA cells works well. A 9V battery with a 5V regulator would also work, but waste more energy.

We could save energy by putting the micro-controller to sleep if the sensor doesn't send a signal for more than, say, 30 seconds. See http://playground.arduino.cc/Learning/ArduinoSleepCode .

2051

Circuit diagram for a bicycle speedometer

This version is built around an Atmel 89C2051 micro-controller, driving a two-digit LED display.

The circuit uses a common-anode layout since the 2051's output ports can sink up to 20mA of current.

Since the digits are shown in an alternating fashion, we can use a 10-pin display or tie together parallel pins of an 18-pin display.

We could use PNP transistors instead of MOSFETs but we'd need a resistor between the micro-controller and the base of the transistor.

If the power supply can go above 5.5V, for example if it is four AA or AAA cells, then a voltage regulator or a step-down power diode will be necessary, which will be a waste.

Components

Atmel 89C2051
This is a very cheap (~$1) micro-controller that can run on 2.7 to 6 volts. To program the C variant requires a flash programmer with a 12V power supply. There is an S variant that can be serially programmed in-circuit.
Lumex LDD-M512RI-RA
This is a dual-digit, seven-segment display, where the LED's cathodes for each digit are tied together to reduce the number of pins to 10. This means that unless both digits are the same, we need to alternate rapidly between showing one and the other by allowing current into one anode or the other. The LEDs are green for excellent visibility in daylight.
Fairchild BS270
This is an N-channel MOSFET suited to switching at logic levels (3-5V). The current that goes through depends on the voltage between the gate and source, and the switching happens in nanoseconds. When the voltage at the gate is equal to or lower than the source, no current is allowed from drain to source. Since more current is allowed when the voltage at the gate is much higher than at the source, this N-channel MOSFET is better suited to a common cathode display. I'd like to find a better MOSFET for this common-anode display, and I welcome suggestions on the discussion page.
11MHz crystal
The 2051 can run up to 24MHz. We should use the slowest possible crystal to save energy. The 2051 uses half as much energy running at 8MHz as it does running at 24MHz. At 5V with an 11MHz crystal, the chip will use about 40mW, plus whatever it takes to drive the LEDs, which is about another 40mW with the initial circuit shown.
22pF capacitors
These two capacitors keep the crystal ringing, and need to be between 20pF and 40pF.
10µF capacitors
One electrolytic capacitor is used to smooth the power supply. This is probably unnecessary if hefty batteries are used and all the connections are short and good, but I'm using a solderless breadboard. The other 10µF capacitor runs the reset circuit which ensures that the program doesn't try starting until the crystal is ringing properly.
33kΩ resistor
This resistor can be anywhere in the tens-of-thousands range, and brings the capacitor attached to the reset pin low a few milliseconds after the circuit is powered.
220Ω resistors
This array limits the current passing through each LED, as controlled by the output pins on the 2051. In combination with the BS270, the current through each LED is limited to about 2mA by these resistors. Lower resistors would allow for a brighter display, but use more power.
Reed switch
A reed switch needs to be attached to the frame of the bike so that it senses a magnet attached to a spoke of the wheel once per revolution.
Power switch
Without a power switch the circuit would consume a battery in a few days or less. It should be attached to one of the wires from the battery.

Power supply

We can use three AAA or AA cells which would provide 3.6V to 5.1V, or one lithium 3.7V cell. Or, we can use a 9V battery with a voltage regulator. A coin battery would not last a day if we left the switch on.

Saving battery-life

If we used an LCD display instead, we'd double or triple the life of the battery.

If we programmed the 2051 to go into idle mode when the bike wasn't moving after a few seconds, we'd double the life of the battery.

If we used a very slow crystal, sub-MHz, we could almost double the life of the battery.

If we used three 1.2V NiMH batteries or some other 3.6V supply, we could double the life of the battery.

Combining all of the above might increase the battery-life more than ten-fold. Note that commercial bike-computers use LCDs, coin-sized batteries, and smart power-down modes.

We could replace the electrolytic capacitors with film capacitors.

To get the micro-controller to enter a power-down mode would save the most energy. It would require a logic circuit that allowed the reed switch to raise the voltage on the reset pin only if the program isn't running already.

  • Special attention must be paid to ports P1.0 and P1.1, which can sink current, wasting power. From the datasheet: "The P1.0 and P1.1 should be set to '0' if no external pull-ups are used, or set to '1' if external pull-ups are used."
  • We need to pay attention to what happens to the ports that control the transistors that control the common anodes.
  • The reed-switch should be in it's own serial circuit with a resistor to V+, so that it creates a signal usable by the logic controlling reset no matter the status of the input pin to which it is normally connected. So, RST = (C-R voltage) OR ( ( REED SWITCH ) NOR ( CONTROL PIN) ). That logic can be achieved with NOR gates only if desired, considering the part before OR as A and the part after OR as B, as ( A NOR B ) NOR ( A NOR B ). Thus, we can use a chip with five NOR gates. I am also investigating using a transistor-based logic, where the software-controlled pin and reed switch NOR through two transistors, then that is OR with the reset capacitor through two more transistors.

If we used an 89S2051 instead of an 89C2051, the micro-controller would use half the power when active, and we could use the reed switch as an interrupt to wake the machine from power down mode. An 89S2051 costs $1.80 and an 89C2051 costs $0.90. If the user is silly enough to use non-rechargeable batteries, they certainly should opt for the more expensive yet more efficient micro-controller. Also, the higher price would pay for itself if it replaced the extra circuitry required to wake the C variant from power-down if that was part of the program.

Program

Watching a magnet go around and around

My program running on the 2051 runs a timer that counts up every machine cycle, which is once every 12 oscillations of the crystal. When it senses a grounded voltage on the pin (6) attached to the reed switch, it enters an "interrupt" subroutine that checks to see how much time has passed. If only a few microseconds has passed since the last grounding, it knows the reed switch is just bouncing from the last hit. If a reasonable amount of time has passed, between a few tens of milliseconds and a few seconds, it calculates the speed in miles per hour by dividing the circumference of the wheel by the time elapsed since the magnet last passed. While waiting for the next interrupt, it displays the two digits of the speed. If many seconds pass with no grounding by the reed switch, the program figures the bike is not moving or is being walked, so it shuts off the display.

Displaying the digits on a shared-pin LED display

Since the two digits share the same cathode-pins, we can only show one at a time. We flip back and forth so quickly, about five thousand times a second, that it's imperceivable. The value of each digit is stored in a byte of memory, actually not the value [0-9] but the combination of bits to make the right LEDs on the display light up. For example, '8' requires all seven bits, while '7' requires just the first three.

Datasheets

Atmel 89C2051
http://www.atmel.com/Images/doc0368.pdf
Lumex LDD-M512RI-RA
http://www.lumex.com/specs/LDD-M512RI-RA.pdf

See also

See PG302 for discussion on programming the 2051.

See frequency meter for more discussion on timing.

See "Bicycle Freewheeling with Air Drag as a Physics Experiment" by Paul Janssen and Ewald Janssens in The Physics Teacher, volume 53, number 25 (2015): http://dx.doi.org/10.1119/1.4904237 (subscription or purchase required for full text access). The publisher provides a supplementary document about the circuit in a guest-accessible FTP server: ftp://ftp.aip.org/epaps/phys_teach/E-PHTEAH-53-010501