This idea was inspired by the Arduino Solar tracker. You would all know the fact that Sunflowers 'follow' the Sun. Similar to the Sunflower, my light tracker will follow the light.
- Arduino Uno/ Nano
- Solderless Breadboard - Mini
- Servo Motor SG-90
- LDR (x2)
- Resistor (x2) - 10kΩ
- Male-to-Male Jumper wire (x9) - 10cm
- 2 pin Female-to-Female Jumper wire (x2) - 70cm
- 3D printed/ Cardboard barrier (Please refer to the 'Setup' section)
Your setup must look somewhat similar to the one in the image above.
ConnectionsServo Motor (SG-90)
- S (Yellow/ Orange wire) - D9
- (+) (Red wire) - 5V
- ( - ) (Black/ Brown wire) - Ground (GND)
LDR
- First - A0
- Second - A1
Connect one end of the LDR to 5V and the other to 10kΩ resistor. The other end of the resistor must be connected to ground. Connection to the port must be where the resistor and LDR are connected.
CodingFor this project, you will need to include the 'Servo' library by Michael Margolis in your sketch. If you are new to this library, and need reference to the functions, please feel free to follow the hyperlink in this section.
Create Servo object to control a servo and a variable named 'pos' to store the servo position. The initial position of the servo motor must be 90°, so assign the 'pos' variable to 90. Create global variables for your LDRs, and a variable named 'limit'. Assign the 'limit' variable to 2.
Within void setup( ), use the pinMode( ) function to configure the LDR pins as INPUT. Use attach( ) function to attach the Servo variable to a pin, and use write( ) function to set the position of the servo at 90°. Set a delay of one second before moving on to void loop( ).
Within void loop( ), create local variables to store the digital values of the two LDRs. You must use analogRead( ) in this case. Calculate the absolute difference between the two values by using the abs() function. If the absolute difference between the two values exceeds the 'limit' (2), the servo position must change. If the value of the first LDR is greater than that of the second, the value stored in the 'pos' variable must reduce. If the value of the second LDR is greater than that of the first, the value stored in the 'pos' variable must increase.
If the value stored in the 'pos' variable exceeds 180, it must be reset to 180. Repeat the same if the value stored in 'pos' decreases below 0. Finally use write( ) function, to set the position of the servo to the value, now stored, in 'pos' variable. Set a delay of 50 microseconds.
If anyone has doubts with the coding, please don't hesitate to ask them in the comments below.
Final LookIf anyone has questions, or suggestions, about this project, please feel free to comment below.
Comments