Arduino Relay Lamp

Arduino Relay

Arduino Relay

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


Mechanical 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).


Electronic switches:

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.


Electromechanical switches:

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.


Relay Application

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.

All the sensor work has been done by another guy, the 1Sheeld proximity shield!

Hence, you are going to just connect the Arduino to the 1Sheeld and the lamp to the relay. And you are ready to go…

Note: I am going to use a relay module since it has a soldered transistor with some safety components and pin headers to make it easy for you to use the relay.


Getting started:

If this is your first time to deal with 1Sheeld or you want to learn more about it, I recommend checking this quick and easy getting started tutorial.

Now, after you’ve become a little bit familiar with 1Sheeld, let’s start!


Step 1: Hardware components

–       Arduino Uno.

–       1Sheeld+ board.

–       Relay module.

–       AC lamp.

–       3 * Male to female wires.

–       Arduino USB cable or 9-12v battery.

–       Android/iOS phone with 1Sheeld App installed on it.


Step 2: Software components:

Step 3: Connection and Schematic:

  • Plug the 1Sheeld board into your Arduino as this:

  • 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:

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.

Stay tuned for the upcoming tutorials  😉

Related Blogs

Arduino Security Camera

Arduino Security Camera System (Arduino Home Hacks #4)

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 …

Arduino IoT

Arduino IoT Shield Functions Explainer

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 …

Arduino Temperature Humidity System

IoT Arduino Temperature and Humidity System

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 …

Arduino Door Lock

IoT Arduino Door Lock using 1Sheeld and Smartphone

  “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, …

IoT Fan Control

Arduino IoT Fan Control using 1Sheeld and Smartphone

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 …

Arduino Light Control

IoT Arduino Light Control using 1Sheeld and Smartphone

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. …