Controlling a relay

Controlling a relay module or shield is fairly simple: Connect it to an Arduino using 5V, GND and a single I/O pin. Then write HIGH to that I/O pin to turn the relay on, write LOW to switch it off again. Using the PowerSwitch Tail is identical, except you only connect GND and an I/O pin; the Tail has its own power supply built in.

In these examples, we will assume the relay is controlled through pin 4 (as used by the SparkFun shield) and use a constant to store the pin number:

const uint8_t RELAY_PIN = 4;

Of course, the pin must be configured as an output in the setup() function:

  pinMode(RELAY_PIN, OUTPUT);

Now, if you can attach the relay directly to the coordinator, the switchHeating() function will be rather trivial:

void switchHeating(bool ...

Get Building Wireless Sensor Networks Using Arduino now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.