Controlling a relay using the Arduino

First of all you will need the following; an Arduino, Arduino Relay Module, Wires  and a 240v electrical appliance you wish to control the power to. ( You may notice the Ethernet Module of my Arduino) This is for the next guide which will show you how to control the relay using ethernet but whether you have this module or not this guide will still apply.

IMG_2528

 

Okay no that we have our kit, the first thing we will need to do is connect the relay to the Arduino as shown below

IMG_2529

You will need 3 cables.

Cable 1 Goes from GND on the Arduino to GND on the relay

Cable 2 Goes from Pin 7 to IN1 on the relay

Cable 3 Goes from 5V on the Arduino to VCC on the relay

 

Once that is done you will need to upload the following code to your Ardwino Uno (Don’t forget to select the COM port if you get an error with the upload)

 

#define RELAY1 7
void setup()

{
Serial.begin(9600);
pinMode(RELAY1, OUTPUT);

}

void loop()

{

digitalWrite(RELAY1,0); // Turns ON Relay
Serial.println(“Light ON”);
delay(3000); // Wait 3 seconds

digitalWrite(RELAY1,1); // Turns Relay Off
Serial.println(“Light OFF”);
delay(3000);

}

 

This is the main part completed now and will now require us to wire up our AC appliance.

 

 

Loading Codes onto the Arduino

If you have followed my previous guide you should have the Arduino software up and running on your computer.

When first opening the Arduino software it’ll have some basic instructions on how to get started with coding

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}

Above is some basic code to turn an LED off and on again. You can see from the code that it is only using a few functions to do this task, which are;

void loop (which runs the code over and over again anything which is between the { and } which come after it

digitalWrite (which is used for sending a command to a pin it is formatted in the brackets (pinnumber,COMMAND)

delay follow by the time before running the next command (this is measure in 1/1000th of a second)

Another useful thing to know “/” is used to comment out text or numbers which don’t need to be run by the Arduino when it is processing the code.

Once you are happy with your code use the “Tick” to verify it followed by the “Arrow” to upload it to the Arduino chip

Arduino Download and software configuration

The Arduino software is a very powerful piece of kit which not only works with the Arduino Uno but other devices such as clone’s of the Arduino and every other edition’s on the Arduino. It is Mac, Windows and Linux compatible and I’ve included the download links for each installer at the bottom of the page.

Once Installed you will find that it is a very simple piece of software with a few buttons. It has a range of pre installed code’s which can be loaded onto the Arduino for testing, examples etc.

On Mac you will find that you need to select which COM port is being used you can see how to do this from the image below

Screen Shot 2015-04-15 at 19.14.06

 

Once you have selected the port you should be good to go on to the next step of loading code’s onto the Arduino

Windows Download

Mac Download