Ruuvi Firmware – Part 3: LED Blinking

Ruuvi Firmware series intro image

In this part of the tutorial we’ll use GPIO drivers made in part 1 to finally make our RuuviTag blink LEDs. Final code of this blog post can be downloaded at Ruuvi GitHub in the ruuviblog-branch, tag 3.3.0-alpha. Please follow part 1 of the series for details on how to clone the repository and compile the code. If you want to download compiled hex, you can get it from http://jenkins.ruuvi.com/job/ruuvi.firmware.c/13/

Tasks

As our program becomes more complicated, we’ll split it into separate independent tasks rather than keep everything in main.c. As before, previously introduced parts are in blue, modified parts have gradient and new parts are in green.

ruuvi firmware.c architecture 3.3.0
Architecture as of 3.3.0

We add 3 tasks for LEDs. Init configures the pins, write sets an LED to desired state and cycle goes through the LEDs turning them on and then off in sequence.

task_led.h
task_led.h

In addition to the function definitions we’ll add enum for the led state. We could use RUUVI_BOARD_LEDS_ACTIVE_STATE and !RUUVI_BOARD_LEDS_ACTIVE_STATE directly just as well. Actual implementation is extremely simple, we just call the GPIO driver implemented in part 1 of the posts:

task_led.c
task_led.c

Finally we’ll call these tasks in the main.c:

main.c
main.c

The flow of program is similar to the current official RuuviTag firmware. We’ll boot up logging and peripherals, and then turn the RED LED on. Once self-test has passed, we turn the RED LED off and turn GREEN LED on. After one second we start the main loop, LED blinking in this case.

Power Consumption

Our delay implementation does not sleep the tag, it rather keeps the MCU active until the timeout occurs. Therefore our power consumption is several milliamperes, absolutely not feasible for battery powered operation.

Power profile of Ruuvi Firmware 3.3.0-alpha
Power profile of Ruuvi Firmware 3.3.0-alpha

It is easy to see the LEDs turning on and off in the steps of the profile. Right now our power consumption is approximately 1750 times the consumption we had in previous post, so obviously this is not acceptable solution for the long run.

In the next part of Ruuvi Firmware blog we’ll add interrupts from the button to program flow, which will allow us to sleep until the next button press occurs.

Stay tuned and follow @ojousima and @ruuvicom on Twitter for #FirmwareFriday posts!

Featured Products

Find the right products for your measuring needs
  • On backorder
    RuuviTag Pro is an environmental sensor that fits perfectly...
    Read more

    RuuviTag Pro (2-in-1, breathable, estimated shipping in middle of December)

    49,90
  • RuuviTag temperature sensorIn stock
    This RuuviTag model is without an air pressure sensor....
    Read more

    RuuviTag – Wireless Temperature Sensor (3-in-1)

    38,90
  • In stock
    With Ruuvi Gateway, you can read your Ruuvi sensors...
    Read more

    Ruuvi Gateway

    199,00
  • On backorder
    RuuviTag Pro is an environmental sensor that fits perfectly...
    Read more

    RuuviTag Pro (3-in-1, breathable, estimated shipping in middle of December)

    49,90