Close

Frobo V2 programming lesson- Part 5

Hope you are enjoying this series of Froboduino/Arduino tutorials. In this Frobo V3 programming lesson Part 4, we will look at LDR CDS Photosensor closely. LDR is light dependent resistor. CDS LDR sensors have been around for a while and are widely used by robotic and electronic hobbyists and professionals. These sensors use a special property of cadmium sulfide to change its electrical resistance when light intensity changes. We measure this changing resistance using what is known as "voltage divider circuit". We connect another fixed value resistance in series with this sensor. When connected to power (5V), with the change in light the LDR sensor resistance will change causing current ampere change in circuit. According to"Ohm's law" when resistance in circuit changes, the current flowing will change in inverse proportion.

Ohm's law states-

V = I X R,

Where V is supply voltage, I is current in ampere and resistance in "Ohms ( A unit of resistance named after German scientist Georg Simon Ohm)"

Look at the typical voltage divider circuit. In the electronics world, we can't directly read current(amperes), but can read voltages. A voltage divider circuit helps in reading changing current as a changing voltage across a variable resistance when another fixed value resistance is attached in series. When light intensity changes, LDR resistance changes inversely. With this change overall resistance of circuit changes. With change in total resistance, the total current will also change as per Ohm's law as power supply voltage is constant. For two resistance in series, the current flowing through both is the same.

Let's take an example.

V= 5V = 5000 mV (milli volt)

LDR resistance in one light condition is RL = 3000 Ohms,

Fixed Value resistance Rf = 2000 Ohms

Total resistance R1 in one light condition is = Rf+ Rl = 3000+2000 = 5000 Ohms

Current I1 = V/R = 5000/5000 = 1 mA (milli amperes),

With 1 mA current in the circuit, the voltage difference across fixed value resistance Rf is Vl = I1 X Rf = 1 X 2000 mV = 3000 mV

if light becomes brighter, LDR resistance changes from 3000 to 2000 Ohms.

Now the Current I2 = V/R2 = 5000/(2000+2000) = 1.2 mA,

With 1.2 mA current, te voltage difference across fixed value resistor Rf is V2 = I X Rf = 1.2 X 2000 = 2400 mV

We use this difference if voltage change across Rf to measure light changes and use it in our application.

Let's write our froboduino code for reading values from LDR sensor with changes in ambient light.

 /* On your froboduino board, connect one of the  LDRs to LD1 +ve and -ve ports. LDR is non-directional, so you can put any leg to +ve or -ve. LD1 port is connected on control PCB A4 pin. A4 is is an analog pin abd can read voltage as values.  Let us first define the pin for LDR.*/ const int LDR = A4; //We need not define pinMode as all pins are taken as INPUT by default. Setup code runs only once when program starts. void setup() { // Let us open the serial ports with data rate 9600 bits /second Serial.begin(9600); } //This loop code runs repeatedly void loop() { //Record the analog value at pin A4 int ldrValue = analogRead(A4); // Froboduino analog reference voltage is 5V. // Analog pins read voltage between 0 to 5V. Arduino 328P has an 10 bit analog to digotal converter (ADC) on 6 pins from A0 to A5.  // The ADC converts voltage from 0 to 1023, i.e. 2^10-1 (Being a 10 bit ADC), so that each value corresponds to 5000mV/1024 = 4.88mV // Let us convert ldrValue to voltage. We will use "float" variable for the decimal value. float ldrVoltage = ldrValue*4.88 ; // We use Characters within "Characters" in Serial.print to show characters in serial monitor as "Characters" Serial.print("LDR analog reading is "); Serial.print(analogRead(A4)); Serial.print("LDR volatge is "); Serial.print(ldrVoltage); Serial.println("milli volts"); }

Here is the Video for your reference...

Copy the code above and paste it in Arduino text editor and upload to froboduino as explained earlier in programming lesson part 1.

Now, Let us try to use LDR sensor in our Frobo to do something useful. We will use two LDR sensors, mounted right in the front. How about making Frobo follow light! Can you imagine, how we can do that?

There are two LDRs hole mounts in the front part of Frobo. See picture. Connect the left LDR legs to LD1+ and LD1-  and right LDR legs to LD2+ and LD2- ports on the Froboduino board.

We will compare the light falling on each LDR and use left and right LEDs to indicate the  direction of more light. Interesting! isn't it!

Here is the Froboduino/Arduino code for making a light indicating robot.

 #include //declare servo as object. Servo steeringServo; const int LD1 = A4; const int LD2 = A5; const int leftLed = 9; const int rightLed = 10; // accelerator is used to control Frobo speed. Remember PWM! const int accelerator = 5; const int leftMotorFwd = A0; const int leftMotorRev = A1; const int rightMotorFwd = A2; const int rightMotorRev = A3;; int servoPos; // Setup code runs only once when program starts. void setup() { //declare Leds as outputs. pinMode(leftLed, OUTPUT); pinMode(rightLed, OUTPUT); pinMode(accelerator, OUTPUT); pinMode(leftMotorFwd, OUTPUT); pinMode(leftMotorRev, OUTPUT); pinMode(rightMotorFwd, OUTPUT); pinMode(rightMotorRev, OUTPUT); steeringServo.attach(6); steeringServo.write(90);; //We need not define LD1 and LD2 as inputs pinMode as all pins are taken as INPUT by default // Let us open the serial ports with data rate 9600 bits /second Serial.begin(9600); } //This loop code runs repeatedly void loop() { digitalWrite (accelerator, HIGH); //Record the analog value at pin A3 int ldrValue1 = analogRead(A4); int ldrValue2 = analogRead(A5); Serial.println(ldrValue1); Serial.println(ldrValue2); delay(500); // Let us try to do something useful with light reading of both LDR sensors. //Let us compare both readings of LDRs and then light up left or right Leds if left and right LDR shows // LDR value goes down when it sees more light. // this code will light up left Led if value of left LDR is lower. //Frobo will turn where it sees more light, until both LDRs  start getting close enough values. if (ldrValue2- ldrValue1 > 25){ digitalWrite(leftLed, HIGH); digitalWrite(rightLed, LOW); digitalWrite (accelerator, HIGH); //Frobo sees more light on left side, so we will turn servo wheel leftwards. Serial.println("turning left");     steeringServo.write(75);     digitalWrite(leftMotorFwd, LOW);     digitalWrite (leftMotorRev, LOW);      digitalWrite(rightMotorFwd, HIGH);     digitalWrite (rightMotorRev, LOW); } // this code will light up right Led and turn right if value of right LDR is higher.  if (ldrValue1- ldrValue2 > 25){ digitalWrite(leftLed, LOW); digitalWrite(rightLed, HIGH);  //Frobo sees more light on right side, so we will turn it leftwards. /*for (servoPos = 90; servoPos <= 135; servoPos += 1) {// goes from 0 degrees to 180 degrees     // in steps of 1 degree     steeringServo.write(servoPos);              // tell servo to go to servoPosition in variable 'servoPos'     delay(15);                       // waits 15ms for the servo to reach the servoPosition   }*/     Serial.println("turning right");     steeringServo.write(105);     digitalWrite(leftMotorFwd, HIGH);     digitalWrite (leftMotorRev, LOW);      digitalWrite(rightMotorFwd, LOW);     digitalWrite (rightMotorRev, LOW); } // this code will light up blink left and right Led if value of both LDR is almost equal. int X = ldrValue1 - ldrValue2; int Y = abs(X); if (Y < 25 ){   //Frobo sees almost equal light on both side, so we will it forwards.   Serial.println("Moving Straight");  steeringServo.write(90);      digitalWrite(leftMotorFwd, HIGH);     digitalWrite (leftMotorRev, LOW);      digitalWrite(rightMotorFwd, HIGH);     digitalWrite (rightMotorRev, LOW); digitalWrite(leftLed, HIGH); digitalWrite(rightLed, LOW); delay(300); digitalWrite(leftLed, LOW); digitalWrite(rightLed, HIGH); delay(300); } }

Here is the video for your reference....

Copy and upload this code in your Froboduino and test it by flashing a torch. Is your Frobo now seeing light? We are excited to see you bringing more and more sense and action to your Frobo. It will get more and more exciting from here onwards.

Our Frobo is now doing multiple tasks all at once. It is time that we learn to make it even smarter. In next lesson, we will combine a few more features to make Frobo a great robot.

If any questions, please write in comment section below.