Sunday, February 28, 2016

Using the Light Sensor

Sunday, Feb. 28th, 2016

It is Sunday morning, and I could not sleep so I popped out of bed, made some coffee, and started thinking about mounting the head of the robot.  Then I realized that I completely forgot a couple of sensors.  One is the light sensor mounted on the front.



Here you can see the little light sensor opening just below the sonar sensor (more on that one later).

So, the light sensor is really a photocell, or light detecting type of resistor.  It should be simple to hook up as there are only 2 wires.  The easiest way to deal with this type of sensor is to connect one end to 5V and the other to Analog 0 and a resistor then to ground.

  Here is a layout of an Arduino board with the LDR connected to A0, ground, and 5V.  By the way the tool I used for this diagram is called Fritzing and you can download it from the web (http://fritzing.org/home/) and use it to make wiring diagrams, and eventually to make circuit boards from.  Making a schematic diagram is also in Fritzing and so I made one up like the one below.

  So, armed with the diagram, I hooked up my board and created a little code to read the values from A0 and try to detect light.  The results were good, and so now the Light Detector is available.  I will connect it to the same board as the keypad, LED's, Motion Sensor, etc.  This will be a sensor board connected on top of the Arduino Mega as a shield.  But more on that later.

Here is the LDR connected to my Mega.

  And with a little bit of code, I can detect either light or dark.  I used ambient light for the standard (dark) and then shone a flashlight into the sensor for light.



Having used a smaller resistor in the circuit, ambient light is 0 and bright light goes up to about 127 on the values.  This is a pretty standard scale that we can use and it can be varied by changing the resistor in the circuit to use different values.

Here is the test code for the LDR in the circuit above:
/* Hero Jr. Light Sensor
   This is a simple light sensor test
   of the LDR mounted on the front of
   the robot.
   GSS 2/28/2016
*/

int LDR = 0;                  //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0;             //that’s a variable to store LDR values
int light_sensitivity = 10;   //This is the approx value of light surrounding your LDR

void setup()
  {
    Serial.begin(9600);      //start the serial monitor with 9600 buad
    pinMode(13, OUTPUT);     //we mostly use 13 because there is already a built in yellow LED in arduino which shows output when 13 pin is enabled
  }

void loop()
  {
    LDRValue = analogRead(LDR);     //reads the ldr’s value through LDR 
     delay(50);                      //This is the speed by which LDR sends value to arduino

    if (LDRValue > light_sensitivity) 
      {
        //It saw light, so do something.
        Serial.println("Bright Light! Bright Light!");
        Serial.println(LDRValue);
        digitalWrite(13, HIGH);
      }
    else
      {
        //It is dark, do something else.
        Serial.println("Sure is dark in here...");
        Serial.println(LDRValue);
        digitalWrite(13, LOW);
      }
  }

No comments:

Post a Comment