Book Image

Arduino Networking

By : Marco Schwartz
Book Image

Arduino Networking

By: Marco Schwartz

Overview of this book

Table of Contents (13 chapters)

Testing the relay


We are now going to build a very simple sketch to test the hardware connections we just made. The sketch will simply switch the relay on and off every second.

The sketch starts by declaring the right pin for the relay:

const int relay_pin = 7;

Then, we set this pin as an output:

pinMode(relay_pin,OUTPUT);

Then, in the loop() function of the sketch, we set the relay to a HIGH state:

digitalWrite(relay_pin, HIGH);

We wait for 5 seconds:

delay(5000)

Then, we switch the relay pin to a LOW state again:

digitalWrite(relay_pin, LOW);

We then wait again for 5 seconds:

delay(5000);

Note

The code for this section can be found in the GitHub repository for this chapter at https://github.com/openhomeautomation/arduino-networking/tree/master/chapter4.

You can now upload the code to the Arduino board. You should hear the relay switching on and off every second. If you connect a lamp to the project, for example, you should also see it switching on and off every second.