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 the LDR connected to my Mega.



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