Book Image

Building Wireless Sensor Networks Using Arduino

By : Matthijs Kooijman
Book Image

Building Wireless Sensor Networks Using Arduino

By: Matthijs Kooijman

Overview of this book

Table of Contents (13 chapters)

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:


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

void switchHeating(bool state) {
  digitalWrite(RELAY_PIN, state);
  heating_state = state;
  publish(F("House/Heating"), state);

This version of the sketch is available in the code bundle as Coordinator_Relay.ino.

Things become a little more complicated if...