How to: Power Module

Power module provides two features - you can control high power device with robust relay (230 V / 16 A) and you can also connect 5V addressable LEDs (WS2812B) and control them.

Relay Control

Controlling the relay is very simple. First you have to initialize the power module in your application_init function by calling twr_module_power_init(). It is always good to set an initial state immediately after the initialization, like twr_module_power_relay_set_state(false).

When the relay is switched on, you will see a small green LED light on the Power module.

You can set the relay state anywhere in your code by calling twr_module_power_relay_set_state(bool state). The SDK holds an actual state internally, you can always retrieve it by calling twr_module_power_relay_get_state(). So if you are going to switch relay state, you don’t have to create any variable, just use this function within function: twr_module_power_relay_set_state(!twr_module_power_relay_get_state())

In the example below we set the relay to off state after initialization. To switch the state, just use the button.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <application.h>

twr_button_t button;

void button_event_handler(twr_button_t *self, twr_button_event_t event, void *event_param)
{
    (void) self;
    (void) event_param;

    if (event == TWR_BUTTON_EVENT_PRESS)
    {
        twr_module_power_relay_set_state(!twr_module_power_relay_get_state());
    }
}

void application_init(void)
{
    twr_module_power_init();
    twr_module_power_relay_set_state(false);

    twr_button_init(&button, TWR_GPIO_BUTTON, TWR_GPIO_PULL_DOWN, false);
    twr_button_set_event_handler(&button, button_event_handler, NULL);
}

LED / LED Strip Control

We have separate tutorial on how to control our LED strip.