💻 Programming Code
🎯 Lesson Objective
In this lesson, students will learn:
• How to program the Light Sensitive Robot
• How the ESP32 reads light intensity from the LDR sensor
• How the robot decides when to move
• How the ESP32 controls motors using the L298N motor driver
• How the complete robot automation logic works
By the end of this lesson, students will be able to upload the program and run the Light Sensitive Robot.
1️⃣ Complete Copy-Paste Code
Students can copy and paste the following program directly into Arduino IDE.
int ldrPin = 34;
int motor1A = 26;
int motor1B = 27;
int motor2A = 14;
int motor2B = 12;
int lightValue;
void setup()
{
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
Serial.begin(9600);
}
void loop()
{
lightValue = analogRead(ldrPin);
Serial.print("Light Value: ");
Serial.println(lightValue);
if(lightValue < 1500)
{
moveForward();
}
else
{
stopRobot();
}
delay(200);
}
void moveForward()
{
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}
void stopRobot()
{
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, LOW);
}
2️⃣ Program Logic
The Light Sensitive Robot works using the following logic:
1️⃣ The LDR sensor detects light intensity.
2️⃣ ESP32 reads the sensor value using analogRead().
3️⃣ The program compares the value with a threshold level.
4️⃣ If the light level is strong, the robot moves forward.
5️⃣ If the light level is low, the robot stops.
This process repeats continuously.
3️⃣ Pin Declaration
int ldrPin = 34;This pin reads the analog signal from the LDR sensor.
GPIO 34 is used because it supports analog input on the ESP32.
int motor1A = 26;int motor1B = 27;int motor2A = 14;int motor2B = 12;These pins control the L298N motor driver.
Explanation:
• motor1A and motor1B control the left motor
• motor2A and motor2B control the right motor
4️⃣ Variable Declaration
This variable stores the light intensity value measured by the LDR sensor.
The value will usually range from:
0 → very dark
4095 → very bright
5️⃣ Setup Function Explanation
void setup(){pinMode(motor1A, OUTPUT);pinMode(motor1B, OUTPUT);pinMode(motor2A, OUTPUT);pinMode(motor2B, OUTPUT);
Serial.begin(9600);}
The setup() function runs once when the ESP32 starts.
Pin Mode Configuration
pinMode(motor1A, OUTPUT);pinMode(motor1B, OUTPUT);pinMode(motor2A, OUTPUT);pinMode(motor2B, OUTPUT);These commands configure the motor control pins as OUTPUT.
This allows the ESP32 to send signals to the motor driver.
Serial Communication
Serial.begin(9600);This starts communication with the computer.
It allows the program to display light sensor values in the Serial Monitor.
6️⃣ Reading the LDR Sensor
Inside the loop function:
lightValue = analogRead(ldrPin);This reads the analog signal from the LDR sensor.
The ESP32 converts the voltage into a digital value between 0 and 4095.
7️⃣ Displaying Light Values
Serial.print("Light Value: ");Serial.println(lightValue);This displays the sensor value in the Serial Monitor.
This helps during testing and debugging.
8️⃣ Decision Making Using If Condition
if(lightValue < 1500){moveForward();}else{stopRobot();}This condition checks the light level.
If the value is less than 1500, the robot moves forward.
If the value is higher, the robot stops.
The threshold value can be adjusted depending on the lighting conditions.
9️⃣ Robot Movement Function
void moveForward(){digitalWrite(motor1A, HIGH);digitalWrite(motor1B, LOW);
digitalWrite(motor2A, HIGH);digitalWrite(motor2B, LOW);}
This function controls the motors to move the robot forward.
Motor signals:
Left Motor → Forward
Right Motor → Forward
This causes the robot to move straight.
🔟 Robot Stop Function
void stopRobot(){digitalWrite(motor1A, LOW);digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);digitalWrite(motor2B, LOW);}
This function stops both motors.
When both motor signals are LOW, the robot stops moving.
1️⃣1️⃣ Final Working Process
The Light Sensitive Robot works in the following way:
1️⃣ The LDR sensor detects light intensity.
2️⃣ ESP32 reads the sensor value.
3️⃣ If the light level meets the condition, the robot moves forward.
4️⃣ If the light condition changes, the robot stops.
This process continues while the robot is powered.
1️⃣2️⃣ Common Mistakes
Students may encounter these issues:
⚠ Robot not moving → Check motor driver wiring
⚠ Sensor values always 0 → Check LDR analog pin
⚠ Robot moving continuously → Adjust light threshold value
⚠ Motors spinning wrong direction → Swap motor wires
Always check the circuit connections first when debugging.
🚀 What Happens Next
Now that the Light Sensitive Robot program is uploaded, the next step is to test the robot and explore additional improvements.