Course Content
Hands-On ESP32 Robotics: Build Smart Robots Step by Step

💻 Programming Code (Limit Switch Module)

🎯 Lesson Objective

In this lesson, students will learn:

• How to program the Single Limit Switch Bumper Robot using a limit switch module
• How the ESP32 reads the signal from the module
• How the robot detects a collision
• How the ESP32 controls motors through the L298N motor driver
• How the robot moves backward and changes direction after hitting an obstacle

By the end of this lesson, students will be able to upload the program and run the Single Limit Switch Bumper Robot.


1️⃣ Complete Copy-Paste Code

Students can copy and paste the following code directly into Arduino IDE.

 
 
int bumperSwitch = 32;

int motor1A = 26;
int motor1B = 27;
int motor2A = 14;
int motor2B = 12;

int switchState;

void setup()
{
pinMode(bumperSwitch, INPUT);

pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);

Serial.begin(9600);
}

void loop()
{
switchState = digitalRead(bumperSwitch);

Serial.print("Switch State: ");
Serial.println(switchState);

if(switchState == HIGH)
{
moveForward();
}
else
{
avoidObstacle();
}

delay(100);
}

void moveForward()
{
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);

digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}

void moveBackward()
{
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);

digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}

void turnRight()
{
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);

digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}

void avoidObstacle()
{
moveBackward();
delay(500);

turnRight();
delay(500);
}

2️⃣ Program Logic

The robot follows this behavior:

1️⃣ Robot moves forward normally
2️⃣ When the robot hits an obstacle, the limit switch module is pressed
3️⃣ The ESP32 detects the signal from the module
4️⃣ The robot moves backward for a short time
5️⃣ The robot turns right
6️⃣ The robot continues moving forward

This allows the robot to avoid obstacles after physical contact.


3️⃣ Pin Declaration

 
int bumperSwitch = 32;
 

This pin reads the signal from the limit switch module.

Connection:

GPIO 32 → OUT pin of the limit switch module


 
int motor1A = 26;
int motor1B = 27;
int motor2A = 14;
int motor2B = 12;
 

These pins control the L298N motor driver inputs.

Motor mapping:

motor1A & motor1B → Left motor
motor2A & motor2B → Right motor


4️⃣ Variable Declaration

 
int switchState;
 

This variable stores the current state of the limit switch module.

Possible values:

HIGH → switch not pressed
LOW → switch pressed


5️⃣ Setup Function Explanation

 
void setup()
{
pinMode(bumperSwitch, INPUT);
 

The limit switch module already contains a pull-up resistor, so we use INPUT mode instead of INPUT_PULLUP.

Signal behavior:

Switch not pressed → HIGH
Switch pressed → LOW


 
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
 

These pins are configured as OUTPUT to control the motors.


 
Serial.begin(9600);
 

This starts serial communication for debugging.

Students can see the switch state in the Serial Monitor.


6️⃣ Reading the Limit Switch Module

Inside the loop function:

 
switchState = digitalRead(bumperSwitch);
 

This reads the output signal from the limit switch module.

The value will be:

HIGH → robot free to move
LOW → obstacle detected


7️⃣ Decision Making Logic

 
if(switchState == HIGH)
{
moveForward();
}
else
{
avoidObstacle();
}
 

If the switch is not pressed:

➡ Robot moves forward

If the switch is pressed:

➡ Robot performs obstacle avoidance behavior


8️⃣ Forward Movement Function

 
void moveForward()
 

Both motors rotate forward.

The robot moves straight ahead.


9️⃣ Backward Movement Function

 
void moveBackward()
 

Both motors rotate backward.

The robot moves backward.


🔟 Turning Function

 
void turnRight()
 

• Left motor → forward
• Right motor → backward

The robot turns right.


1️⃣1️⃣ Obstacle Avoidance Function

 
void avoidObstacle()
 

When the robot hits an obstacle:

1️⃣ It moves backward
2️⃣ It turns right
3️⃣ Then it continues moving forward

This helps the robot escape from obstacles.


1️⃣2️⃣ Final Working Process

The robot works as follows:

1️⃣ Robot moves forward
2️⃣ If it hits an obstacle, the limit switch module is pressed
3️⃣ ESP32 detects the LOW signal
4️⃣ Robot moves backward and turns
5️⃣ Robot continues moving forward

This creates a simple obstacle-avoidance robot using contact detection.


1️⃣3️⃣ Common Mistakes

Students may face the following issues:

⚠ Robot always reversing → check switch module wiring
⚠ Switch not detected → verify OUT pin connection
⚠ Motors not moving → check motor driver wiring
⚠ Robot turning wrong direction → swap motor wires

Always verify wiring before debugging the code.


🚀 What Happens Next

Now that the Single Limit Switch Bumper Robot program is uploaded, the final step is to test the robot and explore improvements.

Scroll to Top