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#include <application.h>
 3twr_button_t button;
 5void button_event_handler(twr_button_t *self, twr_button_event_t event, void *event_param)
 7    (void) self;
 8    (void) event_param;
10    if (event == TWR_BUTTON_EVENT_PRESS)
11    {
12        twr_module_power_relay_set_state(!twr_module_power_relay_get_state());
13    }
16void application_init(void)
18    twr_module_power_init();
19    twr_module_power_relay_set_state(false);
21    twr_button_init(&button, TWR_GPIO_BUTTON, TWR_GPIO_PULL_DOWN, false);
22    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.