A while ago, I wanted to control a 220v AC lamp using my Arduino board. For the code, it was easy, of course. Just send high and low commands to the lamp pin and all will be working okay. But I was pretty sure there is something missing as the lamp operates at high AC voltage and the Arduino operates at low DC voltage! So, there must be a link between them.
So, I went for the transistor because it can drive a high power device with a small power signal. But not only it didn’t work, but also burned out! After a lot of searches, I realized that I need to isolate the AC and DC voltages from each other with a kind of switches called, a relay.
And today, I am going to share what I have learned with you and go through switches types and how they work. Then, I am going to focus on the relay with an Arduino Relay Lamp application.
What is a switch?
Simply, a switch is a device that controls the current flow through a circuit. Therefore we use switches to turn devices on and off.
Of course, each switch differs from the other in the way it opens and closes the circuit. And that’s what you will see in the below section.
Types of electric switches
There are mainly 3 types of switches used in electrical circuits:
1- Mechanical switches
2- Electronic switches
3- ElectroMechanical switches
They are just the normal keys you find in every device. They are operating based on contacts that you can connect physically to close or open the switch circuit. And here are some of the popular mechanical switches:
The mechanical switch may be responsible for turning the device on and off manually. Like the Single Pole Single Throw switch (SPST).
Also, the push button which is a widely used mechanical switch used to make the Arduino take decisions based on the push button readings high (usually 5v) or low (0v).
They don’t have any physical contacts. They are just a semiconductor construction where they can be controlled by controllers, like Arduino.
The simplest electronic switch is the diode. It works like a gate that needs 0.7v or more to allow the current flow to the circuit is open.
Another high level of these switches is the transistor which is completely used in all devices. Transistor works like a switch when it takes a certain voltage and its main advantage is that it allows you to control high current devices by using just a small amount of current.
Imagine that you want to drive 2 Ampere motor with a signal from the Arduino that’s at maximum is about 300 milliAmpere! Of course, you will go for the transistor to avoid burning your Arduino out since it can’t deliver this big current.
You would achieve this by such a circuit:
The Arduino pin gives HIGH and LOW output (low current flow) to the transistor which will act as a switch and allows the current fro the 12v battery (high current flow) to drive the motor. The diode here acts like a short circuit with the motor coil to free all the coil stored energy when the circuit is off. Just for protecting the transistor.
They use both the mechanical contacts and the electrical characteristics. They are used to control devices on and off electrically but with the traditional way, the contacts.
So, to get it clear, it’s designed to isolate the device you want to control from the controller itself. As a result, this allows you to control high voltage AC devices (like a lamp) with a small controller DC signal (like Arduino).
The most common electromechanical switch is the Relay. In addition, it is one of the most important electrical components ever. It’s the reason why you can control your 220v AC lamp with just these tiny controllers, like Arduino.
How relay works
The key concept to understand relay is to think about it as a switch which you can control electrically.
Basically, a relay consists of 2 electrically isolated sides. One is an open switch and the other one is a coil. The coil builds up a magnetic field once a current passes through it.
So, what happens is that once a current passes through the coil, it magnetically pulls the switch into the closed position so that current can pass through that other side where the switch lies.
How to use relays
You just need to have a controller that tells the coil to pull the switch, like Arduino. And on the other side, you connect the high voltage circuit you want to control with the relays’ switch.
The relay has 5 pins. 2 of them are for the coil and they are connected to DC voltage which maybe 5v, 9v or even 12v. It differs from a manufacturer to another.
The other 3 pins are for the circuit you want to control. Usually, we use the COM pin with the NORMAL OPEN pin and connect them to the circuit we want to control. The other pin is the NORMAL CLOSE and you don’t need to connect it to anything.
What will happen is the controller will send a signal to the transistor that’s connected directly with the relay’s coil. Transistor is necessary here since the control signal is too small to generate a magnetic field in the coil. So, you connect the coil to an external voltage source and control the current flow of that source by using a transistor. Just like the transistor motor circuit I have explained above.
So, once the transistor allows the current to pass through the coil. Hence, it will pull the Relay’s switch to close the high voltage circuit on the other side of the relay.
I am going to use the smartphone’s proximity sensor to detect my hand movement and control an Arduino Relay lamp based on that action. So, the lamp will toggle its state on each time I move my hand over the phone.
Connect the Arduino Relay and lamp as illustrated below:
Switch the 1Sheeld power to operate on 5v (Not the 3.3v):
1Sheeld have 2 modes: Uploading mode and Operating mode. You can switch between them using the switch close to the Digital pins and is called “UART SWITCH” on 1Sheeld and “SERIAL SWITCH” on 1Sheeld+.
Firstly, you slide the switch towards the “SWITCH” notation which turns the 1Sheeld board into the Uploading mode to let you upload the Arduino code.
Secondly, after you finish uploading the code, slide the switch towards the “UART” notation (or “SERIAL” at 1Sheeld+ board) which turns the 1Sheeld board into the Operating mode to communicate with your smartphone 1Sheeld App.
Finally, connect the Arduino via your PC using Arduino USB cable.
Step 4: Code:
I would recommend checking the Arduino Proximity Shield documentation to know more about the Arduino Proximity Shield functionalities and how to use them.
Now, switch the 1Sheeld board to the Uploading mode, upload this Arduino Relay code:
Arduino Relay Example using the Proximity Shield from 1Sheeld
By using this example, you can turn on the AC lamp connected to pin 13 if the
smartphone's proximity sensor reports a certain value.
To reduce the library compiled size and limit its memory usage, you
can specify which shields you want to include in your sketch by
defining CUSTOM_SETTINGS and the shields respective INCLUDE_ define.
/* Include 1Sheeld library. */
/* Lamp connected through relay to pin 13 */
/* proximity sensor value initiated with 5 */
/* Start communication. */
/* Set the lamp pin as output. */
/* Always check the value of proximity sensor. */
/* if the lamp was on, turn it off */
/* Turn off the lamp. */
/* Turn on the lamp. */
/* add a 1 sec delay for more stability to the proximity sensor */
Then, Switch the 1Sheeld board to the Operating mode. Open the 1Sheeld app and connect it to the 1Sheeld board via Bluetooth.
Step 5: Run the Arduino Relay lamp:
Firstly, select the proximity shield from the shields list. You will notice that the lamp is restarting. Just move your hand over your phone and it will be stable from now on.
Then, move your hand again and the lamp will turn off. And whenever you move your hand over the sensor the Arduino Relay lamp will toggle its state … and so on…
Here’s the project working 🙂
That’s it for the Arduino Relay lamp tutorial, guys. Hope you enjoyed it.
And if you got any questions, please let me know in the comments below.
Arduino makers …. you can connect your Arduino to MQTT brokers(servers) and launch your IoT with Arduino IoT Shield! It’s the 1Sheeld IoT shield that will do the job for you. It’s one of the most important but recent Arduino shields in 1Sheeld. Simply, it turns your Arduino into MQTT client where you can publish and …
Monitor my home environmental parameters remotely is one of the most desirable things I have ever wanted to make. It’s not about purchasing such a system that can get the job done for me. But, passion to create it myself. So, today, I am going to connect the popular DHT sensor to an online MQTT …
“Oh God! … It seems like I have forgotten the door lock open, again” … the normal saying whenever I am in hurry to go out! Have you encountered this problem before? It always confuses me since I may even think that I have left the door lock opened, and once I go back, …
Here I am with another IoT-based home automation project; Arduino IoT Fan Control. Every day, I get back from work tired and feeling hot. No air conditioner here and the fan takes much time to start cooling the room, sadly! So, I was thinking why not controlling the fan from outside my home with my …
Have you ever forgotten to turn your home lights off when you got outside? Sadly, it happens to me all the time. And this causes confusion once I get my bills! Furthermore, here is one secret about me; I am a lazy man. I always want to control my room lamp from my sweet bed. …
MQTT is one of the most commonly used protocols in IoT projects. It stands for Message Queuing Telemetry Transport. In addition, it is designed as a lightweight messaging protocol that uses publish/subscribe operations to exchange data between clients and the server. Furthermore, its small size, low power usage, minimized data packets and ease of implementation …