Saturday, 25 May 2013

Basic Sensors Used in Robotics

                In my previous post Getting Started with Robotics we have seen a brief introduction about basic sensors used in the field of Robotics. In this post I am going to write How to use these sensors i.e., application of these sensors in the field of Robotics with circuit diagram and programming code of Arduino Microcontroller.In this post I will show you how to use and program Temperature sensor and Photoresistor

            Before entering into the project's, let us have a look at the Arduino UNO board. The Arduino UNO Microcontroller is based on ATmega328. It has 14 digital input/output pins numbered from 0 to 13. In these 14 pins we have 6 PWM pins (Pulse Width Modulation outputs). Arduino uno has 6 analog input pins numbered from A0 to A5. The analog pins maps the input voltage between 0 and 5 Volts into integer values between 0 and 1023. You can learn more about Arduino in the official Arduino website http://www.arduino.cc.



Temperature Sensor:

Temperature sensor            The name itself indicates that this sensor is used to sense the temperature. This sensor has three legs like a Transistor, first leg is Vin second one is Vout and the third one is Ground. we have to supply the voltage to Vin and Ground and take the output voltage at Vout which tells us the temperature in terms of voltage.

Temperature Sensor Project:

            In this project we are going to take the temperature readings from our Temperature Sensor LM35 for every second and we are going to use the Serial monitor of Arduino IDE to display the temperature readings taken from the Temperature sensor.

Materials Required:

 1) Arduino               2) Temperature Sensor LM35              3) Solid core wires.

Circuit:

            The 5 V pin of Arduino UNO goes to the Vin leg of Temperature sensor (LM35), the second leg of Temperature sensor (GND) goes to one of the three Ground pins of Arduino UNO and the third leg of Temperature sensor (Vout) goes to the Analog input pin A0 of Arduino UNO. The circuit is as shown below.
LM35 circuit with Arduino

Program:                    

 int temp;                           //declaring temp variable to read values from temperature sensor  
 int senPin = 0;                     //initialising the sensor to 0  
 void setup()  
 {  
 Serial.begin(9600);                //initialising serial port with a default baund rate of 9600  
 }  
 void loop()  
 {  
 temp = analogRead(senPin);         //read the value from the sensor  
 float degC = (5.0 * temp * 100.0)/1024.0;     //convert the analog data to temperature  
 Serial.print((byte)degC);          //prints the temperature in Serial monitor  
 delay(1000);                       //wait one second  
 }  

How Program works:

            The code starts with the initialisation of variable like 'temp' which is used to read the integer value from the Analog pin and the 'senPin' variable is set to 0 which indicates the temperature sensor connected to Analog pin 0. In the 'void setup()' method Serial port is initialised with a default baund rate of 9600. The 'void loop()' method is used to execute the body inside this method continuously. In the 'void loop()' method the variable 'temp' reads the data from the Analog pin 0, the next step is conversion of analog data to temperature. The 'Serial.print((byte)degC)' method writes the temperature (which is measured in previous step) in the Serial monitor after type casting. The 'delay(1000)' method holds the execution process for 1000 milli seconds (or) 1 second

                      If you have any trouble related to the code of any thing please feel free to comment.

Photoresistor:

Photoresistor circuit diagram with arduino
            Photo resistor is also called as Light Dependent Resistor (LDR). It is made up of high resistance semi-conductor. It is a sensor whose resistance changes according to the strength of light that falls on its surface. The resistance offered by this sensor decreases with the increase in the amount of light falls on its surface and vice-verse. This sensor is used to track the light or to know the strength of the light.


Photoresistor Project:

           In this project we are going to measure the intensity of light in terms of 5 grades i.e.,grade 0= No light, 1=Darker, 2=Dark, 3=Normal, 4=Bright, 5=Brighter. We are going to use a series of 5 LED’s to display the grades i.e., for grade 1 one LED will be in ON state, for grade 2 two LED’s will be in ON state and so on.

Materials Required:

  1)Arduino Microcontroller          2) Photoresistor               3) Resistors 4.5 k, 100 k ohms            
  4) 5 LED's                                 5) Solid core wires.

Circuit:

          Connect the 4.5 k ohms Resistor in series with the Photoresistor across Vin and Ground pins of Arduino Microcontroller. Take the input value in between the 4.5 k ohm resistor and Photoresistor to Analog pin A0 of Arduino.

How it the circuit works:

                          The circuit works as a voltage divider. A voltage divider is a circuit consisting of two resistances across a voltage supply. An output between the two resistances will give a lower voltage depending on the values of the two resistors. The output is taken between the two resistors. The output voltage can be calculated using the formula Vout = [R2/(R1+R2)]*Vin. Where, R1 is 4.5 k ohms and R2 is the resistance offered by the Photoresistor whose value is depends on the Intensity of the light falls on it. If the Intensity of light on the Photoresistor is more, then the resistance offered by the Photoresistor will be less and vice-versa. If the resistance of the Photoresistor is more, then Vout (which is taken in between the 4.5 k ohm resistor and Photo resistor) will be more and vice-versa. 

Program:


   
 int ledPin[5] ={1,4,7,10,13};                   //initialising 5 LED's with pins  
 int pr=0;                                       //initialising Photoresistor to pin 0  
 void setup()  
 {  
   for(int i=0;i<5;i++)                         //setting pin mode to 5 LED's  
   {  
    pinMode(ledPin[i], OUTPUT);  
   }  
 }  
 void loop()  
 {  
   int pr_input=0;                             //initial value of Photoresistor is set to 0  
   pr_input = analogRead(pr);                  //reading value from analog inut pin A0  
    
   for(int j=0;j<5;j++)                        //setting all LED's to OFF state  
   {  
     digitalWrite(ledPin[j],LOW);  
     delay(2000);                              //wait for 2 seconds
   }  
    
   if(pr_input<=32)        
   {  
    for(int j=0;j<5;j++)                       //Setting 5 LED's to ON state  
    {  
     digitalWrite(ledPin[j],HIGH);  
     delay(2000);                              //wait for 2 seconds

    }  
   }  
   else if(pr_input>32 && pr_input<=76)   
   {  
    for(int j=0;j<4;j++)                       //Setting 4 LED's to ON state  
    {  
     digitalWrite(ledPin[j],HIGH);  
     delay(2000);                              //wait for 2 seconds
    }  
   }  
   else if(pr_input>76 && pr_input<=184)  
   {  
    for(int j=0;j<3;j++)                       //Setting 3 LED's to ON state  
    {  
     digitalWrite(ledPin[j],HIGH);  
     delay(2000);                              //wait for 2 seconds
    }  
   }  
   else if(pr_input>184 && pr_input<=235)  
   {  
    for(int j=0;j<2;j++)                       //Setting 2 LED's to ON state  
    {  
     digitalWrite(ledPin[j],HIGH);  
     delay(2000);                              //wait for 2 seconds
    }  
   }  
   else if(pr_input>235 && pr_input<=655)  
   {  
     digitalWrite(ledPin[1],HIGH);            //Setting 1 LED to ON state  
     delay(2000);                             //wait for 2 seconds
   }  
    
 }  


How program works:

                The code starts form initialising the variables i.e., the 5 LED's and the Photoresistor. I have used an array of five variables 'ledPin[5]' which indicates 5 LED's which are connected to the output pins 1, 4, 7, 10 and 13 of Arduino. The Photoresistor variable 'pr' is set to 0. After that I had set the pin mode for the five LED's using a 'for' loop inside the 'void setup()' method. Then, to read the intensity of light from an Photoresistor and to output the result using 5 LED's continuously, I had used 'void loop()' method which executes continuously. Inside the 'void loop()' method I have initialized a new variable 'pr_input'  with the value 0 and it can be used to take the input from the analog pin 0. After that I had set all the five LED's in the OFF state (initial condition). An if-else ladder is used to divide the intensity of light measured by photoresistor into 5 grades (grade 1, grade 2 ... grade 5). 
                How I got the values used in if-else ladder? The answer is, I had calculated the Resistance of Photoresistor, Vout from the voltage divider circuit with the use of an Multimeter, then the Analog input value related to the output voltage from the voltage divider circuit is calculated in the following cases
case 1: When Photoresistor is in a Dark room  R2 = 8 K ohms =>  Vout = 3.2 V => Analog input value=655
case 2: When Photoresistor is normal room  R2=1K ohms => Vout=0.9V => Analog input value=184
case 3: When Photoresistor is under a bright light R2=150 ohms => Vout=0.16V => Analog input value=33
               By using the values between the above three cases the grades are divided ino -
Grade 0 i.e., non of the LED's are in ON state if Analog input value is grater than 655.
Grade 1 i.e., one LED is in ON state if Analog input value is in the range of 235 to 654.
Grade 2 i.e., two LED's are in ON state if Analog input value is in the range of 184 to 234.
Grade 3 i.e., three LED's are in ON state if Analog input value is in the range of 76 to 183.
Grade 4 i.e., four LED's are in ON state if Analog input value is in the range of 32 to 75.
Grade 5 i.e., five LED's are in ON state if Analog input value is less than 31.
            This process i.e., reading the intensity of light using Photo resistor and display the output using 5 LED's continues for every 2 seconds.

              If you have any problem related to the code or any thing please feel free to comment.

Related posts: 
1) How to use and program IR LED and Photodiode pair with Arduino

2) Getting Started with Robotics - This post is for those people who want to get into the field of Robotics but don't know How to start. You can find some useful pdf files also.

3) List of Robotics/Embedded online stores in INDIA.

3 comments:

  1. Well presented.

    The Sensio shield from eNTesla has all and more to what you have explained.
    http://entesla.com/microcontroller-development-boards/arduino/entuino-kit-arduino

    ReplyDelete
  2. why did you used // if(pr_input<=32) //
    i mean how did you come to know that we have to put 32 ??

    ReplyDelete