Friday, April 28, 2017

Controlling relays with the Arduino

Today we are learning how to control relays with a microcontroller, using an Arduino. In this easy project we will program an Arduino board to switch On or Off any device you want, for example, an electrical appliance like a lamp, a heater or a fan. This is a simple but useful project, perfect if you are new to Arduino. Some knowledge of electronics and programming is a plus, but anyone can succeed in the Arduino world. So, what's an Arduino?

The Arduino platform
Arduino is an open-source electronics platform based on cheap but powerful and easy-to-use hardware and software. Arduino designs and manufactures microcontroller kits distributed as open-source hardware and software, permitting the manufacture of Arduino boards and distribution of the software by anyone. Being an easy tool for learning and fast prototyping, Arduino quickly gained success and has been used in thousands of different projects and applications by both beginners and advanced users.

To program the boards the Arduino project provides an IDE (integrated development environment), which includes a code editor, compiler and debugger. Arduino uses a custom programming language that is very similar to C and C++.

Arduino boards
There are several Arduino boards available, using mainly ATMEL and ARM processors. They vary in processing power, capabilities and  size. Usually Arduino boards are used together with other electronic devices like sensors, switches, displays, etc. These are manufactured in such a way as to easily connect with the Arduino boards. Many expansion shields are also available.

Arduino UNO, MEGA 2560, Nano and Lilypad
Popular Arduino boards (UNO, Nano, Mega 2560 and Lilypad).

Arduino relay control project
This project consists on controlling a relay with an Arduino board and a button switch. Once the board is programmed, pressing the button switch will turn the relay to the On or Off state, switching on or off whatever device is controlled by the relay. I will be using an Arduino UNO, but other Arduino boards (Nano, Leonardo, MEGA, etc) can be used.

Arduino relay control interconnects

Relay, button and resistor
Hardware parts:
1x Arduino UNO board
1x Relay module (5V)
1x tactile button switch
1x 10KOhm resistor
Breadboard
Breadboard jumper wires

Notes:
This project uses a relay module that can be directly interfaced by an Arduino board. The relay used is not critical but it must be capable of handling the power that the appliance draws. In this tutorial a 10A relay module is used which can handle up-to 2400W (240Vx10A resistive loads) appliances. Relay coil is 5 Volts. This relay module has an led that lights up when the relay is switched on. Info on where you can buy the relay module is at the end of this post.

A breadboard makes hooking up the circuit easier, but is not essential.

How does it work?
Pressing the button switch momentarily will put pin 2 of the Arduino in HIGH (ON) state, with 5 Volts. This voltage will put the transistor in the relay module in the ON (conducting) state and that will then power the relay. When the relay is powered, the relay contacts will close and let mains power flow to our appliance. The opposite happens when the button is pressed again, pin 2 is put in LOW (OFF) state with 0 Volts and the relay is switched OFF. The controlled appliance is connected to two of the three terminal blue connector (the one with screws) on the relay module. 

Relay circuit


Step 1 - Download and install the Arduino IDE
There are two versions of the IDE, the offline one and the Arduino Web Editor. The traditional offline IDE will let you work without Internet connection. The Arduino Web Editor saves your work in the cloud and requires an Internet connection. In this tutorial I will be using the offline IDE. It can be downloaded here.  

Step 2 - Connect the circuit according to the schematic.
The circuit is really simple and all pins are labelled, both on the Arduino board and the relay module. It can all be put together using a small breadboard and some breadboard jump wires. The relay has 3 pins labelled -, + and S. Pin S is connected to an output pin on the Arduino, pin 2. The button switch has 4 pins, but 2 pairs are connected together! Check with a multimeter or, if the switch has no effect after the code is uploaded, try connecting to the other pins by rotating the switch 90º.

Step 3 - Connect the board to your computer.
Arduino boards use USB connection. If you are buying an Arduino board don't forget the correct USB cable. The Arduino UNO uses USB A to B cable (standard USB printer cable) and the Arduino Nano uses mini B USB plug.

Step 4 - Start the Arduino IDE
When the IDE is stated it will automatically create and name a file called a sketch. Sketch files is where we enter our code. Ignore the blank sketch file and download the project sketch file here. Then open the file in the IDE.

Step 5 - Select the board
Before uploading the sketch to the board we must make sure the correct type of board is selected in the IDE. Go to Tools > Board and select the correct board matching the board you are using.

Step 6 - Select the Port
Before uploading the sketch to the board select the correct communications port for the board. In the Tools menu, in Board, select the correct COM port.

Step 7- Upload the code
To upload the code to the Arduino board go to File and select Upload, or press the Upload button on the IDE toolbar. In a few seconds the code will be compiled and then uploaded to the board.

Arduino IDE

Testing
Once the code upload is completed you can test the circuit by pressing the switch button. You should hear the relay click on and off as you press the button.  If you use the suggested relay module, an led will also light up when the relay is switched on. If the switch has no effect, check the connections for errors or bad contacts and try rotating the tactile switch by 90º.

That's it! You now know how to use and program an Arduino to switch relays. You can use this project as it is or you can make changes to the code, like adding more relays or switching the relay On or Off at predetermined programmed times. Have fun!

Buy the parts and support MakerCorner!
I may get a small fee if you use the following links to buy the parts. Thank you for your support!


No comments:

Post a Comment