My task this week is to create Build and Program a smart circuit using an Arduino UNO which reads a signal from an LDR Photoresistor to control a servo motor to direct it to the sun using Code BlocksĀ
Ā I'll use the Tinkercad web app to simulate and test the circuit before building the prototype with cardboardĀ
Then copy the code to real Arduino UNO board using Arduino IDE softwareĀ
join me in the upcoming steps to learn more about how I accomplished this task.
In this project, I'll use the following componentsĀ
2 LDR photoresistorĀ
2 resistors 1K Ohm
1 Servo motorĀ
1 small BreadboardĀ
1 Arduino boardĀ
some wires for connectionĀ
In this step, I'll connect the photoresistor with GND and 5V pins to the Arduino and the ANALOG IN input A5
Note: the analog input for the photoresistor scale is ( 0,1023)
The Next step is, connecting the servo motor with GND and 5V pins and connecting the servo motor signal pin to the analog output pin " pin 3"
Note: the analog output has the sign ( ~ ) beside itĀ
Now let's test the coding part before installing the rest of the photoresistors š«£
to do that click on the code button at the top right of the screen
Because we used a photoresistor as an input, we will first choose an input, then read the analog pin, then choose A5 pinĀ Ā
Now from the Math section we will choose the comparison function to set the condition for the photoresistorĀ
I will set the condition as greater than 500, action will happenĀ
and from the output section choose the action that will happen to the servo motorĀ
set the pin to 3 and the action will be rotating for 45 degreeĀ
Now from the control section insert the " If, Then, Else " function, then insert the input and output previous function as shown on the left imageĀ
then insert 1 sec wait after itĀ
finally at the else set the servo motor to 0 degrees if the photo resistor read is lower than 500Ā
Now let's test our simulation circuitĀ
from the top right click on start simulation, then change the light brightness on the photoresistorĀ
As you can see the servo moved 45 degreesĀ
To try it by yourself click on the next button šš½
Now let's install the other photoresistor and connect it to the second analog input A4 to make it rotate to the other sideĀ
for sure I tried to replicate the code for the other pin A4Ā
But it didn't work š
So for this challenge, I had to start coding using blocks again from scratchĀ
I Used the function ANDĀ from the math section to set each condition for each statusĀ
as you can see in the final code
If the light at the left sensor the servo motor will rotate 45 degrees
If the light is at the right sensor the servo motor will rotate 135 degrees
If the light at both sensors the servo motor will rotate 90 degrees
If there is no light on sensors the servo motor will stay still " 0 degrees"
As you can see the simulation works perfectly š„³
and you can try it from here šš½
to copy the code to the Arduino boardĀ
click on the code button
choose blocks + Text
Select the text and copy itĀ
But the challengeĀ I don't have all these components in my provided basic kit and the servo motor was broken ššš
So I have to start again with another sensĀ
I'll use the LDR sensor and I'm going to simulate it with the PIR sensor on TinkercadĀ
The LDR sensor that will be used as the input device has 4 pinsĀ
VCC, connected toĀ the voltage pinĀ
GND, connected to the ground pinĀ
DO, digital pin, and connected to digital inputĀ
AO, Analog pin, and connected to the analog input pinĀ
we will use the digital pin DO in our device and will be connected to pins 9 & 10
The Servo motor Ā that will be used as the action device has 3 pinsĀ
Signal, for the signal and its analogĀ signal and will be connected to pin 11
GND, will be connected with ( - )Ā
VCC, will be connected with ( + )
Then I changed all input blocks from read analog pin to read digital pin, and the digital pin didn't need to enter value, Just HIGH and LOW
After thatĀ
copy the code again to past it to Arduino IDEĀ
Now connect the Arduino to your PCĀ
from the tool menu choose portĀ
your board should appear in the serial port section as shown in the left imageĀ
delete any code shown in the code areaĀ
past your codeĀ
click on the upload button as shown in the top imageĀ
you will see an" Uploading Done " message if everything is OK
In the next step we will try to build the physical circuit using these componentsĀ
As you can see it works perfectly as the simulationĀ