Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building Wireless Sensor Networks Using Arduino

You're reading from  Building Wireless Sensor Networks Using Arduino

Product type Book
Published in Oct 2015
Publisher
ISBN-13 9781784395582
Pages 192 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Matthijs Kooijman Matthijs Kooijman
Profile icon Matthijs Kooijman

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 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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}