Introduction: AUTOMATIC LIGHT SOURCE TRACKING WITH ARDUINO UNO R3
In this lesson, we will use a servo motor, a photoresistor and a pull-down resistor to assemble an automatically tracking light source system.
Step 1: Components
- Arduino Uno board * 1
- USB cable * 1
- Servo motor * 1
- photoresistor * 1
- Resistor (10k) * 1
- Breadboard * 1
- Jumper wires
Step 2: Principle
The servo motor and the photoresistor scan and look for light source in 180 degree and record the location of light source. After finishing scanning, the servo motor and the photoresistor stop at the direction of light source.
Step 3: Schematic Diagram
Step 4: Procedures
Step1:
Build the circuit.
Step 2:
Download the code from https://github.com/primerobotics/Arduino
Step 3:
Upload the sketch to the Arduino Uno board
Click the Upload icon to upload the code to the control board.
If "Done uploading" appears at the bottom of the window, it means the sketch has been successfully uploaded.
Now, if you use a flashlight to shine the photoresistor, you will see the servo motor and the photoresistor rotate, and finally stop at the direction of light source
Step 5: Code
/********************************************************************
* name : Automatically Tracking Light Source
* function : if you use a flashlight to shine the photoresistor,
* you will see the servo motor and the photoresistor rotate,
* and finally stop at the direction of light source.
***********************************************************************
/Email: info@primerobotics.in
//Website: www.primerobotics.in
#include
const int photocellPin = A0;
/************************************************/
Servo myservo;//create servo object to control a servo
int outputValue = 0;
int angle[] = {0,10, 20, 30, 40, 50, 60,70, 80, 90, 100,110,120,130,140,150,160,170,180};
int maxVal = 0;
int maxPos = 0;
/*************************************************/
void setup()
{
Serial.begin(9600);
myservo.attach(9);//attachs the servo on pin 9 to servo object
}
/*************************************************/
void loop()
{
for(int i = 0; i < 19; i ++)
{
myservo.write(angle[i]);//write the angle to servo
outputValue = analogRead(photocellPin);//read the value of A0
Serial.println(outputValue);//print it
if(outputValue > maxVal)//if the current value of A0 is greater than previous
{
maxVal = outputValue;//write down the value
maxPos =i;//
}
delay(200);
}
myservo.write(angle[ maxPos]);//write the angle to servo which A0 has greatest value
while(1);
}