Arduino - Relay
In a previous tutorial, we have learned how to turn on/off an LED. In this tutorial, we are going to learn how to turn on/off some kind of devices that use the high voltage power supply(such as a light bulb, fan, electromagnetic lock, linear actuator...).
The common: Just like controlling LED, we use the Arduino's output pin to turn on/off them.
- For LED, we can use power from the Arduino board (≤ 5v). Therefore, we can connect LED directly to Arduino's pin.
- For the light bulb, we MUST use another power source (high voltage and/or high current), which can burn Arduino. Therefore, we CANNOT connect the light bulb directly to Arduino's pin. We need to use a relay between Arduino's pin and light bulb to protect Arduino from high voltage/current.
|1||×||Arduino UNO or Genuino UNO|
|1||×||USB 2.0 cable type A/B|
|1||×||12V Power Adapter|
A relay is a programmable electrical switch, which can be controlled by Arduino or any micro-controller. It is used to programmatically control on/off the devices, which use the high voltage and/or high current.
It is a bridge between Arduino and high voltage devices.
When you are making projects that are connected to mains voltage, you need to know what you are doing, otherwise, you may shock yourself. This is a serious topic, and we want you to be safe. If you’re NOT 100% sure what you are doing, do yourself a favor and don’t touch anything. Ask someone who knows!
Although some kinds of relays support both DC and AC devices, We highly recommend you to use a DC device (≤24V) for testing.
Relay has two groups of pins: low voltage group and high voltage group.
- Pins in the low voltage group are connected to Arduino, including three pins:
- GND pin needs to be connected to GND (0V)
- VCC pin needs to be connected to VCC (5V)
- IN pin receives the control signal from Arduino
- Pins in the high voltage group are connected to high voltage a device, including three pins (usually in screw terminal):
- COM pin is the common pin. It is used in both normally open mode and normally closed mode
- NO pin is normally open pin. It is used in the normally open mode
- NC pin is normally closed pin. It is used in the normally closed mode
In practice, we usually do NOT use all of the pins in the high voltage group. We use only two of them:
- We use only COM pin and NO pin if we use normally open mode.
- We use only COM pin and NC pin if we use normally closed mode.
※ NOTE THAT:
The order of the relay's pins can vary between manufacturers. ALWAYS use the labels printed on the relay. Look closely!
Some relay module only has two pin in high voltage group.
How to Connect the High Voltage Device to Relay
How It Works
The relay can work with two modes: normally open mode and normally closed mode. These modes are the opposite.
The "normally" means "if IN pin is connected to LOW (0V)".
Normally open mode
To use this mode, we need to connect the high voltage device to the COM pin and NO pin.
- If the IN pin is connected to LOW (0V), the switch is open. The device is OFF (or inactive).
- If the IN pin is connected to HIGH (5V), the switch is closed. The device is ON (or active).
Normally closed mode
To use this mode, we need to connect the high voltage device to the COM pin and NC pin.
- If the IN pin is connected to LOW (0V), the switch is closed. The device is ON (or active).
- If the IN pin is connected to HIGH (5V), the switch is open. The device is OFF (or inactive).
You can choose any of them.
Arduino - Relay
Arduino controls a high voltage device by controlling a relay.
Controlling a relay is simple. We just need:
- Connect an Arduino's pin to the IN pin of the relay
- Control the relay by programming the pin to LOW or HIGH
Image is developed using Fritzing. Click to enlarge image
How To Program For Relay
- Configure an Arduino's pin to digital output mode. For example, pin 3:
- Program the pin to LOW (0V):
- Program the pin to HIGH (5V):
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Open Serial Monitor to See the result:
- Automatically turn on the light when you enter into your room and turn off the light after you leave 30 seconds. Hint: Refer to Arduino - Motion Sensor .
The above code also works with the following relays:
Relay on Commercial Products
- Arduino - Button - Relay
- Arduino - Button Toggle Relay
- Arduino - Potentiometer Triggers Relay
- Arduino - Light Sensor Triggers Relay
- Arduino - Ultrasonic Sensor - Relay
- Arduino - Motion Sensor - Relay
- Arduino - Touch Sensor - Relay
- Arduino - Touch Sensor Toggle Relay
- Arduino - Door Sensor - Relay
- Arduino - Door Sensor Toggle Relay