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 keeps standing out from the crowd of all-around development boards due too its ease of use and budget price. As a result, this has enabled almost anyone to get his own idea into life no matter how crazy or even useless to the whole world it was, except for its maker! Also, it has …
Arduino robots are always funny and definitely more enjoyable when combined with robotic arm! And today, I will walk you through the making of a simple Arduino Robot Arm that’s made of cardboard and how you can attach it over your Arduino Bluetooth RC Car / Robot that you have made so far through this series. …
Arduino Line Following Robot is one of the easiest and most well-known projects that anyone can make to learn the basics of programming, electronics, and mechanics all in one project. It’s known that this project is common for most of the makers and tech students and today I am going to give it a revisit …
Back again with another Arduino Bluetooth RC Car tutorial but with an advanced feature that makes your car autonomous by adding ultrasonic sensors to the RC Car you have made before!. Yeah, I know that you may have seen other tutorials using ultrasonic to make the car/robot avoid obstacles ahead, but I am going to …
Cardboard crafts are one of the most popular and easy to make DIY stuff. Mixing this with the unmatchable enjoyment of RC Cars and the ease of using Arduino, I am going to show you how to make a Cardboard Arduino Bluetooth RC car that you can control via Bluetooth from your smartphone. …
Arduino Security Camera Have you ever wanted to check your home in real time? Afraid that maybe someone has stormed in, don’t remember if you shut the door or not or maybe wanna check if your Hyper-energetic dog has broken any of your dishes AGAIN!! 🙂 Then this is the perfect place for you cause today …