Photoresistors, Servo and Arduino


Photoresistors, Servo and Arduino


ABSTRACT

In this exercise the movement of the servo depends on the output value of the photoresistors. The impedance of the sensors varies with the light that incident on them. The position of the servo always changes toward the sensor with less light. There are four light sensors. One Up, one right, one down and another on the left. The servo can only move 180 degrees. I‘ve divided this range in three part. The positions are Up, Right and Down. 0 degrees is the position Up, 90 degrees is the position on the right and finally, 180 degrees is the inferior position Down. Therefore, the code programmed here moves the servo Up if the upper sensor or the left sensor have less light than the others. It goes to the right if the Right sensor has less light and it changes his position to the inferior position if the Down sensor has less light than the other.

On the picture below you can appreciate the light sensors on the sides of the box and the servo in the center with his stick looking down. The USB cable goes out from the inferior side of the box.

Figure 1: shadows detector. The box with the servo on the center and four photoresistors on the sides.


       
COMPONENTS

1 x Arduino Uno
4 x Photoresistors
4 x Resistors of 10k
1 x Servo
Other: wires, a box and tools. The Arduino is powered by the USB cable that goes out from the box. The picture below shows the components inside the box.

Figure 2: Interior of the box where is all the circuit.



             
DEVELOPMENT

In the Figure 1 and 2  you can see how I've held the servo on the box, how the wires have been placed and the position of the photoresistors .The components are assembled on Arduino as in the schematic below.


Figure 3: Schematic of the circuit with photoresistor, servo and Arduino.




          
RESULTS

The best way to see the results is to watch it in a video. In this video you are going to watch how the stick of the servo responds when I cover the light from a photoresistor.


The Arduino's code of this exercise is provided in the Appendix.

PROPOSAL OF IMPROVEMENT

I was thinking in using a big servo controlled by a 12V relay with the purpose of moving heavy stuff. For instance, a solar panel that always moves toward the greatest source of light.

REFERENCES


APPENDIX


//Libraries
#include <Servo.h>
#include <arduino.h>

// create servo object to control a servo
Servo myservo; 

//Photoresistor analog pin
static int LDRnorte=0,LDReste=1,LDRsur=2,LDRoeste=3,ServoPin=9;

//ServoMotor digital Pin
int LDR=0;//Degrees of the servo movement

//Functions used
int  choiceLDR(int norteLevel,int esteLevel,int surLevel,int oesteLevel);

void setup()
{
  Serial.begin(9600);//Initializes the USB communication
  myservo.attach(ServoPin);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
  char levels[100];
 
  //Read the light level of the four photoresistors
  int LDRnorteLevel=analogRead(LDRnorte);
  int LDResteLevel=analogRead(LDReste);
  int LDRsurLevel=analogRead(LDRsur);
  int LDRoesteLevel=analogRead(LDRoeste);
 
  //Shows in the screen the levels of the photoresistors
  sprintf(levels,"\nLDRnorte=%d;LDReste=%d;LDRsur=%d;LDRoeste=%d",
LDRnorteLevel,LDResteLevel,LDRsurLevel,LDRoesteLevel);
  Serial.println(levels);
 
  //This function decides who photoresister has the highest level
  LDR=choiceLDR(LDRnorteLevel,LDResteLevel,LDRsurLevel,LDRoesteLevel);

  //Moves the servomotor depending on the light levels
  myservo.write(LDR);

  // The program checks the light each five seconds
  delay(1000);
}

int  choiceLDR(int norteLevel,int esteLevel,int surLevel,int oesteLevel)
{
    int LDRlevel=0;
  
    if(norteLevel<=esteLevel) LDRlevel=norteLevel;
    else LDRlevel=esteLevel;
    if(LDRlevel<=surLevel) LDRlevel=LDRlevel;
    else LDRlevel=surLevel;
    if(LDRlevel<=oesteLevel)  LDRlevel=LDRlevel; 
    else LDRlevel=oesteLevel;
    //LDRlevel has the lowest level of the photoresistors
    //That means that this photoresistor has less light than the others
   
    if(LDRlevel==norteLevel)
    {
      LDRlevel=10;//North
      Serial.print("LDRNorth has the lowest level= ");
      Serial.println(norteLevel);//For debugging
      Serial.print("The servo movement will be: ");
      Serial.print(LDRlevel);Serial.println(" Degrees");
    }
    else if(LDRlevel==esteLevel)
    {
      LDRlevel=85;//East
      Serial.print("LDREast has the lowest level=");
     Serial.println(esteLevel);//For debugging
     Serial.print("The servo movement will be: ");
     Serial.print(LDRlevel);Serial.println(" Degrees");
    }
    else if(LDRlevel==surLevel)
    {
      LDRlevel=179;//South
      Serial.print("LDRSouth has the lowest level=");
      Serial.println(surLevel);//For debugging
      Serial.print("The servo movement will be: ");
      Serial.print(LDRlevel);Serial.println(" Degrees");
    }
    else
    { 
     LDRlevel=0;//West
     Serial.print("LDRWest has the lowest level=");
     Serial.println(oesteLevel);//For debugging
     Serial.print("The servo movement will be: ");
     Serial.print(LDRlevel);Serial.println(" Degrees");    
    }
   
   //Now, LDRlevel has the degrees that the servo will move his motor
 return LDRlevel;
}