Silly little project - bicycle rear light

Discussion in 'The Completed Projects Collection' started by Robartes, Dec 3, 2014.

  1. Robartes

    Thread Starter Member

    Oct 1, 2014
    57
    13
    This 'project' (it's not really big enough to qualify as a full project IMO, hence the quotes) came about when my son (8 yo) came home from school with yet another light missing off of his bicycle -- I swear he or his classmates eat the things.

    Anyway, this time instead of being him another set of lights (although in the end that's probably cheaper, but money is never the reason we do these things), I decided to build my own. The design parameters for this project were:

    • the light is battery powered, so low power consumption is required
    • the circuit would need to be powered from 3V (either a stepped down/regulated LiIon or 2 plain 1.5V AA batteries)
    • the circuit would need to fit inside the casing of a bicycle rear light I had lying around from one of his previous escapades
    • the circuit would need to reproduce the behaviour of my own bicycle's rear light -- it works with a button (momentary pushbutton), which cycles the light between off, blinking (at about 2Hz) and on

    With these parameters in mind, I came up with the following design, based on individual IC's & components:

    ic_schematic.jpg

    The basic idea behind this circuit is that the 'cycling' behaviour of the pushbutton is implemented by a 2-bit counter built from 2 D-type flipflops (the 74HC74 chip), with the fourth state of this counter (11) resetting the entire thing back to the initial state using a simple NAND gate built out of two transistors ('manual TTL' so to speak :) ). The rest of the circuit has some transistors to switch two LEDs and a 7414 inverting Schmitt trigger IC that does the debouncing of the pushbutton and provides an astable multivibrator for the 'blinking' state.

    The circuit when built resulted in this rat's nest of cabling (I really should work on my point to point protoboard wiring skills):

    IMG_1737.jpg IMG_1736.jpg


    And of course, this first attempt did not work at all (as in no light whatsoever). While I had built it successfully on a breadboard, transferring it to protoboard presented some challenges. One was the wiring rat's nest shown above, which undoubtedly would have introduced lots of fun issues. However, it never came to that because of another issue the keen eyed among you might have already spotted: I soldered the IC's directly to the board, without sockets. This, combined with the rat's nest above, resulted in my soldering iron hanging around some of these pins for far too long and destroying the 74HC74 chip. When I had finished the circuit and tested it, I noticed it did not do anything and started debugging with multimeter & scope. I found that the fist Schmitt trigger did present the correct pulse on the first D-flop's clock pin, but then the 74HC74 never did anything with it (Q0 remained low). After rereading the 74HC74's datasheet, I am now fairly sure I exceeded the 'maximum soldering temperature' (around 250 degrees C for 10 seconds) on a number of the 74HC74 pins. I probably destroyed the connection between the IC's die and the pin by brute thermal force :).

    In addition to the circuit not working when built on protoboard (which is a pretty big show stopper by itself :) ), I was not really happy with the power consumption of it in idle state when I measured it on the breadboard version -- it still drew a significant fraction of a milliamp, which with batteries of a few hundred mAh would mean lots of battery replacements.

    So, back to the drawing board. This time, instead of going the hardcore TTL logic (well, high speed CMOS, to be pedantic :) ) route, I decided to use this as an opportunity to brush up on my C skills and implement the entire thing with an Atmel ATtiny microprocessor. The circuit for this is simplicity itself, of course:

    avr_schematic.jpg IMG_1734.jpg

    (please take note of the professional way of fixing the board in the case)

    All of the good stuff in this version is in the code, which is included below.

    This is the version that is going into my son's bicycle (well, actually my own, as I gave him my rear light to tide him over and he's not keen on giving it back :) ). In idle state, it consumes less than my multimeter can measure on its lowest setting (microamps) so it should keep going for over a year before the battery needs replacing, depending on how much bicycle riding at night I do of course: in 'on' mode it draws just over 20mA, which, incidentally, is the reason I did not include any current limiting resistors for the LEDs. Next step is to power this from a LiIon battery charged by the dynamo on my bicycle, but that's another project.

    Thanks for reading this far! Code for the ATTiny included below:

    Code (Text):
    1. /* Name: fietslicht.c
    2. * Author: Bart Vetters <robartes@nirya.be>
    3. */
    4.  
    5. #define F_CPU 1000000
    6.  
    7. #include <avr/io.h>
    8. #include <avr/interrupt.h>
    9. #include <avr/power.h>
    10. #include <avr/sleep.h>
    11. #include <util/delay.h>
    12. #include <stdint.h>
    13.  
    14. #define LED                  PB0
    15. #define BUTTON               PB1
    16. #define BUTTON_INTERRUPT_PIN PCINT1
    17. #define MIN_DEBOUNCE_COUNT   15
    18.  
    19. typedef enum {
    20.     OFF,
    21.     ON,
    22.     BLINKING,
    23. } led_status_t;
    24.  
    25. #define NUM_LED_STATUS 3
    26.  
    27. /* button_press_counter
    28.    Keeps track of button presses for debouncing purposes
    29. */
    30. volatile unsigned char button_press_counter = 0;
    31.  
    32.  
    33. /* Interrupt handler for button press. This does not actually
    34.    do anything, as its only function is to wake up the processor
    35. */
    36. EMPTY_INTERRUPT(PCINT0_vect);
    37.  
    38.  
    39. /* She's got no more power, cap'n */
    40. void init_power_save(void) {
    41.  
    42.   /* Shut down stuff we do not need */
    43.   power_adc_disable();
    44.   power_timer1_disable();
    45.   power_usi_disable();
    46.  
    47. }
    48.  
    49. /* Interrupt initialisation */
    50. void init_interrupt(void) {
    51.  
    52.     // Enable pin change interrupt
    53.     GIMSK |= (1 << PCIE);
    54.  
    55.     // Set BUTTON pin mask for pin change interrupt
    56.     PCMSK |= ( 1 << BUTTON_INTERRUPT_PIN);
    57.  
    58.     // Enable interrupts
    59.     sei();
    60.  
    61. }
    62.  
    63. /* Timer utility functions */
    64. void clear_timer_output(void) {
    65.  
    66.     // Set COM0Ax to 0 to disconnect timer from LED pin
    67.     TCCR0A &= ~( 1 << COM0A1 | 1 << COM0A0 );
    68.  
    69. }
    70.  
    71. void hold_timer(void) {
    72.  
    73.     // Assert TSM & PSR0 bits to stop timer
    74.     GTCCR |= (( 1 << TSM ) | ( 1 << PSR0 ));
    75.  
    76. }
    77.  
    78. /* Set up timer for PWM. Aim is about 2Hz with 70% duty cycle */
    79. void start_timer(void) {
    80.  
    81.     // Hold timer
    82.     hold_timer();
    83.  
    84.     // Set OCR0A for 70% duty cycle (0.7 * 255 =~ 178);
    85.     OCR0A = 178;
    86.  
    87.     // Set TCCR0A for non-inverting PWM on OC0A ( OC0A = PB0 = LED)
    88.     TCCR0A |= ( 1 << COM0A1 );
    89.     TCCR0A &= ~( 1 << COM0A0);
    90.  
    91.     // Set TCCR0A & TCCR0B for Phase correct PWM, count to 0xFF
    92.     TCCR0B &= ~( 1 << WGM02 | 1 << WGM01 );
    93.     TCCR0A |= ( 1 << WGM00 );
    94.  
    95.     // Set timer prescaler to glacial (CLKio / 1024)
    96.     // 512 timer clock ticks is a full period, timer clock is at 1E6/1024, so
    97.     // we end up just shy of 2Hz
    98.     TCCR0B |= (1 << CS00 | 1 << CS02);
    99.     TCCR0B &= ~( 1 << CS01 );
    100.  
    101.     // Set TSM to 0 to start timer. Hardware resets PSR0 automatically
    102.     GTCCR &= ~( 1 << TSM );
    103.  
    104. }
    105.  
    106.  
    107.  
    108. /* Initiate sleep mode */
    109. void go_to_sleep(void) {
    110.  
    111.    // Set Power-Down sleep mode
    112.    MCUCR |= ( 1 << SM1 );
    113.    MCUCR &= ~( 1 <<SM0 );
    114.  
    115.    // Go to sleep
    116.    sleep_enable();
    117.    sleep_cpu();
    118.  
    119.  
    120. }
    121.  
    122. /* Check button press (includes debouncing) */
    123. uint8_t check_button_pressed(void) {
    124.  
    125.     if ( bit_is_clear(PINB, BUTTON) ) {
    126.       // Increase button loop counter
    127.       button_press_counter++;
    128.     } else {
    129.       // No more button pressed
    130.       button_press_counter=0;
    131.     }
    132.  
    133.     if ( button_press_counter >= MIN_DEBOUNCE_COUNT ) {
    134.       // We have a live one. Wait a while to make sure user
    135.       // releases button
    136.       _delay_ms(500);
    137.       button_press_counter=0;
    138.       return 1;
    139.     } else {
    140.       return 0;
    141.     }
    142.  
    143. }
    144.  
    145. void set_led_status (led_status_t expected_led_status) {
    146.  
    147.     switch ( expected_led_status ) {
    148.    
    149.         case OFF:
    150.             hold_timer();
    151.             clear_timer_output();
    152.             PORTB &= ~(1 << LED);
    153.             go_to_sleep();
    154.             break;
    155.  
    156.         case BLINKING:
    157.             start_timer();
    158.             break;
    159.  
    160.         case ON:
    161.             hold_timer();
    162.             clear_timer_output();
    163.             PORTB |= ( 1 << LED );
    164.             break;
    165.     }
    166.    
    167. }
    168.  
    169.  
    170.  
    171. int main(void)
    172. {
    173.  
    174.     led_status_t current_led_status = OFF;
    175.  
    176.     /* Set LED as output */
    177.     DDRB |= ( 1 << LED);
    178.  
    179.     /* Enable pull up on all unused pins (datasheet best practice) */
    180.     PORTB = (0x3f & ~( 1 << LED ));
    181.  
    182.     /* The above two commands also take care of BUTTON correctly, so no
    183.        need to repeat ourselves here */
    184.  
    185.     /* Put chip in minimum power consumption mode */
    186.     init_power_save();
    187.  
    188.     /* Enable pin change interrupt on BUTTON */
    189.     init_interrupt();
    190.  
    191.     /* Turn off LED & go to sleep */
    192.     PORTB &= ~( 1 << LED );
    193.     go_to_sleep();
    194.  
    195.     /* Event loop. Wait for button presses to toggle leds around */
    196.     for(;;) {
    197.         if ( check_button_pressed() ) {
    198.           if ( ++current_led_status == NUM_LED_STATUS ) {
    199.             // Reset back to 0. c enums aren't smart enough to have ++ operator wrap around
    200.             current_led_status = OFF;
    201.           }
    202.           set_led_status(current_led_status);
    203.         }
    204.     }
    205.     return 0;   /* never reached */
    206. }
    207.  
     
  2. #12

    Expert

    Nov 30, 2010
    16,278
    6,791
    So...no help needed here?
     
  3. Robartes

    Thread Starter Member

    Oct 1, 2014
    57
    13
    No, it's working, thanks. I just wasn't sure where to put this (here or the Completed Projects bit) so I chose here :).
     
  4. #12

    Expert

    Nov 30, 2010
    16,278
    6,791
    I already told the moderators to consider this for the Completed Projects forum. Right now, you are where most of the, "need help" posts arrive.
     
  5. Robartes

    Thread Starter Member

    Oct 1, 2014
    57
    13
    Yes, it was moved - thanks! I initially chose the Projects forum instead of this one as the 'How to post a project here' topic at the top there suggested to do so. I'll remember for next time :)
     
Loading...