Difference between revisions of "Bicycle speedometer"
(→Components: the LEDs are green) |
|||
Line 39: | Line 39: | ||
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. | ||
+ | |||
+ | 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 NOT ( ( REED SWITCH ) AND ( CONTROL PIN) ) | ||
===Program=== | ===Program=== |
Revision as of 14:47, 8 December 2013
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.
Contents
Specifications
- Use a magnetic reed switch attached to the frame/fork of a bicycle.
- Run on a battery.
- Include a two-digit display.
- Include a power switch to save the battery.
- Weigh as little as possible.
- Use a breadboard so it can be modified.
Circuit
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.
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.
- 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.
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 NOT ( ( REED SWITCH ) AND ( CONTROL PIN) )
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.
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