Hello guys, today we are going to create a project where we control a light bulb based on the surrounding lighting. We are going to use PICO and a Light Dependent Resistor (LDR), to detect light, and turn on or off a light bulb depending on how intense the lighting is around it.
- Arduino PICO
- LDR 12mm,
- 2-channel Relay module or 1-channel Relay Module,
- 10k ohm resistor,
- Mini breadboard, a bundle
- Male - male jumber wires, a bundle of 40
- Male - female jumber wires, a bundle of 40
- 220v AC lamp
- 9 volt battery
CONNECTING THE LDR TO PICO
Light Dependent Resistors are variable resistors that change their resistance depending on the amount of light falling on them. Their relationship is inversely proportional, meaning that the resistance increases as the lighting decreases, and decreases when the lighting increases.
We will use this property to change the voltage that our PICO reads, and act depending on it. We have to create a voltage divider using our LDR to be able to do so, and this is how we create one:
- We connect the first side of the LDR to PICO's Vc
- Connect the other side of the LDR with both A0 and a 10K ohm resistor
- Connect the other side of the resistor to PICO's GND
We now have a voltage divider, where the signal that reaches our PICO's A0 depends on the resistance of our LDR. The signal out of a voltage divider is represented by: Vout = (R2/(R1+R2)) * Vin. In our case
- Vin = The power source (Vc)
- Vout = A0
- R1 = The resistance of the LDR
- R2 = 10k ohm (our fixed resistance)
Lets now see how it acts under different lighting conditions.
First test: A lit room
The LDR's resistance decreases and almost reaches 1K ohm, let's try that in our equation:
A0= (10000/(1000+10000)) * 5 = 4.54v
PICO's ADC will convert this voltage to a digital value of 928.
Second test: A dark room
The LDR's resistance increases and almost reaches 10K ohm, let's try that again in our equation:
A0= (10000/(9000+10000)) * 5 = 2.63v
PICO's ADC will convert this voltage to a digital value of 532.
Now that we can get readings from our LDR, let's connect an LED to our PICO and use it to test our work.
CONNECTING AN LED AND TESTING OUR WORK
We now want the LED to turn off and on depending on the reading of our LDR. This means that we need to grab the reading from our LDR, and program a breakpoint for our LED to turn on and off at.
You will need your program to do the following:
- Take an input signal from the LDR at A0
- Have D2 as an output for our LED
- Define a variable that represents our LDR's reading
- Displaying the LDR's signal to A0 in the serial monitor
- Define a breakpoint for our LED to turn in and off at.
But, before we run our program, let's connect the LED to our PICO like this:
- Connect the LED's long leg (the positive anode) to our PICO's D2 pin
- Connect the LED's short leg (the negative cathode) to PICO's GND
CONNECTING THE RELAY TO PICO
Now that we know that our PICO and program are connected and working properly. We can control our house's lights or any other home equipment. But, we need a relay to do so.
Relays are made up of electromagnets which are used as a switch to open a circuit and close it. We will use PICO to control the switching operation of the relay, to control the delivery of current to the device. And these are the relay's pinouts:
- Vcc (Relay) -> Connected to the 5-volt pin (PICO) to power the coil inside the relay
- GND (Relay) -> Connected to PICO's GND to power the coil inside in the relay
- IN1 (Relay) -> Connects to a digital output pin to send out a signal to the first relay in order to open and close the circuit, in our case it will be D2 (PICO)
- IN2 (Relay) -> This is the same as IN1, but for the second relay, and we are going to leave it empty because we only have one load.
- Common "com" (Relay) -> Common is connected to one end of the load that is to be controlled.
- Normally Closed "NC" (Relay) -> The other end of the load is connected to the NC or NO, if it is connected to the NC the load remains connected before the trigger.
- Normally Open "NO" (Relay) -> The other end of the load is either connected to the NC or NO, if connected to the NO the load remains Disconnected before trigger.
We are now just going to replace the LED with the relay module.
CONNECTING THE AC LOAD AND PROGRAMMING THE RELAY
Now, you only need to connect the AC load to the relay module, and you do so by cutting a single wire from your load in half, then connecting one end to the relay's com, and the other to the NO.
The code will stay the same as it was for the LED because the relay uses a digital signal just like the LED. But, change the led variable to relay, so it stays clear and descriptive.
YOU ARE DONE!
Now, you have an AC light that turns on and off depending on the light that is in the room. You can do this to any house electronics, you just have to be careful with how smart you make them!
Please feel free to give us any suggestions, and ask any questions, we will be more than happy to answer them.
Source: MellBell Electronics