What is a relay
For beginner, Arduino is about processing input from sensor and output to LED, servo, buzzer and etc. What if you like to control the real electrical devices at your house, let say a desktop light or a Kettle? Then, you will need a relay.
In short, a relay is an electrically operated switch. Most relays use an electromagnet to operate a switching mechanism mechanically.
Where to get one
I do most of my shopping at
dx.com. A decent relay cost as low as USD2.40 and it is free shipping! (The only problem is it takes quite some time to reach my doorstep).
There are many type of relays available out there. You must get the right one to works. Get a 5V one due to your arduino board power output is usually 5v or 3.3v. If you got a
9v relay like this one, then you will need to connect the power source from an external 9v battery.
5V relay
Notice the word down there: SRD-
05VDC-SL-C. This is a 5v DC relay which you can connect to your arduino board directly for power source to control.
12v Relay
These are the 12V relay which required external power source to control.
How to connect
Normally, a relay has 6 pins, 3 on the A side (IN, VCC, GND) , 3 on the B side (NO, COM, NC) , as shown in the picture below. A side connect to your arduino board, while the B side connect to the ‘real devices’.
For testing, we always start connecting the
A side.
VCC to your 5V power output pin,
GND to GND pin, while
IN to your Digital Output pin. Like this circuit:
Then, load the following code to test:
|
int Relay = 8; void setup() { pinMode(Relay, OUTPUT); } void loop() { digitalWrite(Relay, HIGH); // turn on relay delay(2000); digitalWrite(Relay, LOW); // turn off relay delay(2000); } |
You have done it correctly when you hear the clicking sounds. For those relay module that come with built in LED, it’s easier to tell it works or not.
Connecting the ‘real device’
You have to be very careful when connecting a relay as you are dealing with real AC power source most of the time. You could burnt your arduino board or worst yourself if connected wrongly. For the learning and safety, I will use an external 6v battery source to represent the power supply and LED for the lights/electrical device.
The diagram below shows how you connect the
B side:
The full circuit would be:
Note: connect a resistor to your LED as 6V battery could be too large for it to handle.
Your LED on/off is now control by your relay.
Chinese References
Most products sellings on dx.com sourced from china and so their components have chinese writing character. It’s not too hard to read them. The following table helps:
- NO – 常开 – where you connect with + of light bulb
- COM – 公共端 – where you plugged + power source in
- NC – 常关 – seldom use
Have Fun!
Some Photos