Introduction: Arduino Relay Activated Light Dependent Lamp

This instructable will show you how you can create a lamp that will turn on automatically when it gets too dark within the room its located. Essentially the way the lamp would work is that when the LDR (Light Dependent Resistor) attached to the lamps circuit detects darkness in its immediate area, it would command the light bulb to turn on through the help of the Arduino microcontroller also apart of the circuit. Then when the LDR detects an adequate amount of light in the room, it would instruct the light bulb to stop shining light as the room is dark no more.

In this circuit, I am also including a slide switch which would allow one to easily switch between which power source the light bulb receives its energy from. However, this component is completely optional, if you wish to just include one power source and have your lamp run off of that, then you are free to do so.

Now I will be creating this circuit within TinkerCAD, a free-to-use online modelling program that allows you to create circuits electronically. However, if you wish to physically create this circuit, you could do so by using this same instructable as all the steps done in TinkerCAD can also be executed with a real circuit board and wiring, as TinkerCAD follows the same logic as a physical circuit.

Supplies

In order to construct this circuit would require the following:

I have attached a link to where you can purchase these components if you would like to physically build this circuit. However, if you are building this circuit on TinkerCAD then you do not need to purchase anything, as all of these components are available on the program.

Step 1: Setting Up the LDR

First, we are gonna set up the Light Dependent Resistor (LDR) as that is the main component which is gonna sense for light and send the order through the Arduino to the light bulb to power it on. The LDR has two legs, the first one being called Terminal 1, and the other being Terminal 2. First, attach a red coloured wire going from the 5V socket of the Arduino to the power rail marked with a red '+' sign. Then attach a black coloured wire going from the GND socket of the Arduino referred to as the ground socket into the ground rail of the breadboard marked with a black '-' symbol. The colours of the wire don't really influence the circuit in any way, they just allow your circuit to look more organized and easier to follow. Once the wiring is complete, place the LDR on to the breadboard, make sure the legs are in two different columns of the breadboard. Connect a black wire from the column in which the Terminal 1 leg of the LDR is positioned to the ground rail of the breadboard. Then, connect one leg of the 1000 Ohm resistor to the column in which the Terminal 2 leg of the LDR is placed, and the other in any other column in the breadboard. Connect one end of a wire into the column the second end of the resistor is placed, and the other end of the wire into the power rail on the breadboard. Lastly, connect a wire going from the 'A0' socket of the Arduino to the column of the breadboard where the Terminal 2 leg of the LDR is placed. The LDR is now fully operational!

Step 2: Setting Up the SPDT Relay

Now setting up the relay is rather simple, you first want to place the relay on to the very middle of the breadboard where half of the relay is on one side of the breadboard while the other half is on the other. The reason for doing this is that you do not want the pins of the relay to be in the same columns of the breadboard as that could cause a short-circuit and would not allow the relay to work properly. Then, once the relay is placed on the circuit as intended where none of its pins are in the same column as one another, we can start attaching the relay to the other components of the circuit. First, use a ground wire to connect the Terminal 8 pin of the relay to the ground railing of the breadboard. Then, use any coloured wire and connect the Terminal 5 pin of the relay to any numbered pin socket on the right side of the Arduino except for pin sockets 1 and 0. Now your relay is wired and ready to go!

Step 3: Setting Up the Light Bulb and Power Source

Now it is time to set up the core component of the circuit and the project, the light bulb and its power source. First, let us set up the light bulb, the light bulb has two sockets called much like the LDR, Terminal 1 and Terminal 2. To start wiring, connect one end of a wire to the Terminal 1 socket of the light bulb, and the other end to the breadboard column in which the Terminal 7 pin of the SPDT relay is connected to. Let's leave the Terminal 2 socket of the light bulb empty for now and move on to the power source. The power source I am using for the circuit is the 5V power supply, however, you could also use the three AAA batteries as well, they both work fine with the main light bulb. I will explain how to switch between the power sources using a slide switch later on in the instructable. However, if you do not want to switch and are content with one power source, then that is completely fine as well. The power source should have two sockets, a positive one coloured red and a negative one coloured black. The red one is for supplying power while the black one is used for ground. Use a black wire and connect one end of it to the negative socket of the power source and the other end to the breadboard column which has the Terminal 1 pin of the SPDT relay connected to it. Now using a red wire, connect the positive socket of the power supply to the Terminal 2 socket of the light bulb. Now when you start the power supply your light bulb should turn on, if it does not please review all the steps carefully.

Step 4: Coding the Arduino

Now the LDR would have not effect on the light bulb turning on or off without this code informing the Arduino on how it should react to the information the LDR sends it and how it should communicate with the light bulb. Really what the code does is that it reads the value that comes from LDR and then compares it to the parameters we have set. So if the value that comes from the LDR is below 500, that tells the Arduino that there is not enough light in the room and that it needs to turn on the light bulb. However, if the value from the LDR is more than 500, then that means there is more than enough light in the room and the light bulb does not need to be on, so it turns it off if it is on. The code can be found above and below.

void setup()
{

pinMode(A0, INPUT);

Serial.begin(9600);

pinMode(2, OUTPUT);

}

void loop()

{ Serial.println(analogRead(A0));

if (analogRead(A0) > 500)

{

digitalWrite(2, LOW);

}

else

{

digitalWrite(2, HIGH);

}

delay(10);

}

If you are coding in TinkerCAD then you will just need to click on the "Code" button in the top left corner and write the code there. However, if you are creating this circuit physically, then you will need to open a C++ coding software onto the computer which your Arduino is connected to. You will have to set the output source of the code to the Arduino and then send the instructions to the Arduino once you have completed the code.

Once you have gotten the coding component done on either TinkerCAD or the coding software, the LDR should be controlling the light bulb. So, if you cover the LDR with your hand or such, the light bulb should turn on and if you apply light to the LDR, it should turn off.

Step 5: Setting Up Dual Power Sources [Optional]

Adding the dual power sources is a completely optional feature, if you are content with the circuit thus far, then you do not have to read this step. Adding a dual power source to the circuit is rather simple and does not take a lot of rewiring. Let's start with the slide switch as that is what's going to allow us to switch between the two power sources. The slide switch has three pins that attach to the breadboard, the pins on the far right and left are referred to as Terminal 1 and Terminal 2, while the pin in the centre is referred to as the common pin. When placing the slide switch on to the breadboard, make sure each of the pins is in a different column of the breadboard. Then once you have placed the slide switch, unplug the positive (red) wire on the light bulb that connects the power source to the light bulb. Once unplugged from the light bulb, connect the positive power source wire to the breadboard column in which the terminal one pin of the slide switch is connected to. Now bring in the new power source that you want to be able to switch between. The new power source should have two sockets, a red positive one and a black negative one, much like the preexisting power source. First, connect the negative socket on the new power source to the breadboard column in which the Terminal 1 pin of the SPDT relay is connected to using a black hook-up wire. Then connect the positive socket on the new power source to the breadboard column in which the Terminal 2 pin of the slide switch is connected to using a red hook-up wire. Now you have completed the wiring for the new power source, all you must do now is connect one end of a red hook-up wire to the breadboard column in which the common pin of the slide switch is connected to and connect the other end of the wire to the Terminal 2 socket of the light bulb, the socket from which we disconnected the previous power source. Now when you supply power, you should easily be able to switch between the power sources every time to use the slide switch!

Step 6: Enjoying the Fruits of Labor

Congratulations! You have just completed the Arduino Relay Activated Light Dependent Lamp (Phew, that's a big name)! However, the fun does not stop here, if you wish to further modify the lamp or add your own personal touch to it, you absolutely could! If you completed this circuit in TinkerCAD, give a try to creating this circuit physically! If you have physically created the circuit, maybe try implementing it around your house! However, either way, thank you for following this instructable!