Skip to content

Relays

Description

SensorIO has 2 on-board, normally opened (1-Form-A) photorelays that can be used as programmable switches for devices with power supply up to 26Vdc or 18Vac and up to 2A.

Part details

Manufacturer: Toschiba Corporation

Part number: TLP241A

Datasheet: link

Code example

Compiling example

This example is based on Mbed OS and requires a simple setup. For full description check Code Setup page.

Relay driver

Relays do not need external driver - only Mbed OS built-in DigitalOut class is required.

Code

Pin definitions

1
2
3
    /**** Relay pins ****/
    RELAY_1 = PG_2,
    RELAY_2 = PG_3,

Example

In this example user button is used to toggle state of RELAY_1.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "mbed_events.h"
#include "DigitalOut.h"
#include "InterruptIn.h"
#include "Serial.h"

mbed::Serial console(SERIAL_TX, SERIAL_RX, 115200);
events::EventQueue scheduler;
mbed::DigitalOut relay(RELAY_1);

void relayStatus(int status)
{
    console.printf("Relay is %s\r\n", (status == 1) ? "ON" : "OFF");
}

void buttonPressed()
{
    // toggle relay state
    relay = !relay;

    // log relay state via Serial (via EventLoop)
    scheduler.call(&relayStatus, relay);
}

int main(int argc, char **argv)
{
    console.printf("This is SensorIO\r\n");

    mbed::InterruptIn button(USER_BUTTON);
    // attach function to button press
    button.fall(&buttonPressed);

    // running event loop
    scheduler.dispatch_forever();
}

Example main.cpp file can be downloaded from here.