Introduction
Obstacle avoiding robots are the perfect first project for anyone diving into STEM kits Pakistan, IoT, or basic robotics kits. They combine sensor reading, motor control, and simple decision‑making in a compact platform that you can expand later. In this tutorial we’ll walk you through a beginner‑friendly roadmap using the popular ESP32 (or ESP32‑S3) and classic Arduino Uno / Arduino Nano boards. All components are readily available in Pakistan, whether you shop at the Electronic Market Pakistan, Hall Road Lahore, Digilog, Electrobes, or online at Robotics Kits.
1. Choose Your Controller: ESP32 vs Arduino
| Feature | ESP32 / ESP32‑S3 | Arduino Uno / Nano | |---------|------------------|--------------------| | Wi‑Fi & Bluetooth | Built‑in, ideal for IoT extensions | Requires external module | | Processing Power | Dual‑core, up to 240 MHz | 16 MHz single‑core | | GPIO Pins | 30+ (including ADC, DAC) | 14‑22 (depending on board) | | Price (online) | Competitive, especially on Robotics Kits | Very cheap, widely stocked |
For a pure obstacle avoiding robot you can start with either board. If you plan to add remote monitoring or cloud logging later, the ESP32 gives you Wi‑Fi out of the box. If you prefer a classic Arduino experience, the Arduino Nano fits nicely on a small chassis.
2. Core Components List
- Microcontroller – ESP32 (or ESP32‑S3) or Arduino Uno/Nano
- Ultrasonic sensor – HC‑SR04 (detects obstacles up to 4 m)
- Motor driver – L298N dual H‑bridge (drives two DC motors)
- DC motors & wheels – 6 V geared motors (commonly sold in kits)
- Chassis – Acrylic or 3D‑printed frame (many local sellers provide ready‑made kits)
- Battery – 7.4 V Li‑Po or 4×AA NiMH pack (choose based on motor voltage)
- Jumper wires & breadboard – for prototyping
- Optional – ESP32‑S3 for extra AI edge, Raspberry Pi for vision, STM32 for advanced control
All of these parts are listed in the Robotics Kits online catalog with live PKR prices, COD option, and an AI‑powered Build‑to‑Buy assistant that suggests the exact SKU you need.
3. Wiring the Robot
3.1 Connect the Ultrasonic Sensor (HC‑SR04)
- VCC → 5 V on Arduino or 3.3 V on ESP32 (HC‑SR04 works on 3.3 V but 5 V is safer).
- GND → Ground.
- Trig → Any digital pin (e.g., D5 on Arduino, GPIO 18 on ESP32).
- Echo → Any digital pin (e.g., D6 on Arduino, GPIO 19 on ESP32).
3.2 Hook Up the L298N Motor Driver
- IN1‑IN4 → Four digital pins (e.g., D7‑D10 on Arduino or GPIO 21‑24 on ESP32).
- ENA / ENB → PWM pins for speed control (e.g., D3 & D11 on Arduino, GPIO 25 & 26 on ESP32).
- Motor A → Left motor terminals.
- Motor B → Right motor terminals.
- +12 V (or motor voltage) → Battery positive.
- GND → Battery negative and Arduino/ESP32 ground.
3.3 Power Considerations
- Keep the microcontroller voltage separate from the motor supply to avoid noise.
- Use a DC‑DC buck converter if you need 5 V for the ESP32 from a 7.4 V Li‑Po pack.
4. Programming the Logic
Below is a concise Arduino‑style sketch that works on both Arduino and ESP32 (with minor pin adjustments). It reads the distance from the HC‑SR04, decides whether to turn, and drives the motors via L298N.
// Pin definitions (adjust for ESP32 if needed)
const int trigPin = 5; // HC‑SR04 Trig
const int echoPin = 6; // HC‑SR04 Echo
const int in1 = 7, in2 = 8, in3 = 9, in4 = 10; // L298N inputs
const int enA = 3, enB = 11; // PWM enable pins
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(in1, OUTPUT); pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT); pinMode(in4, OUTPUT);
pinMode(enA, OUTPUT); pinMode(enB, OUTPUT);
Serial.begin(115200);
}
long getDistance() {
digitalWrite(trigPin, LOW); delayMicroseconds(2);
digitalWrite(trigPin, HIGH); delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2; // cm
}
void forward() { digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
digitalWrite(in3, HIGH); digitalWrite(in4, LOW);
analogWrite(enA, 200); analogWrite(enB, 200); }
void turnLeft() { digitalWrite(in1, LOW); digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH); digitalWrite(in4, LOW);
analogWrite(enA, 180); analogWrite(enB, 180); }
void loop() {
long dist = getDistance();
Serial.print("Distance: "); Serial.println(dist);
if (dist < 15) { // obstacle within 15 cm
turnLeft();
delay(300);
} else {
forward();
}
}Tip: On ESP32 replace analogWrite() with ledcWrite() and set up a PWM channel first.
5. Testing & Calibration
- Bench test the motor driver without the chassis – ensure forward/reverse works.
- Place a wall in front of the robot and watch the serial monitor. Adjust the distance threshold (15 cm) if needed.
- Mount the components on the chassis, secure wires, and run a few obstacle courses.
- For smoother turns, experiment with differential speeds on the two motors.
6. Where to Buy the Parts in Pakistan
| Component | Popular Offline Market | Online Option | |-----------|-----------------------|---------------| | ESP32 / ESP32‑S3 | Electronic Market Pakistan, Hall Road Lahore | Robotics Kits – live PKR price, COD, AI Build‑to‑Buy | | Arduino Uno/Nano | Hallroad, Digilog | Robotics Kits – instant stock check, AI suggestions | | HC‑SR04 Ultrasonic | Electrobes, local electronics stalls | Robotics Kits – bundled with motor driver for discount | | L298N Motor Driver | Hall Road Lahore, Electronic Market Pakistan | Robotics Kits – includes heat‑sink and wiring kit | | Batteries & Chassis | Hall Road, Digilog | Robotics Kits – COD, warranty, AI Lab for custom chassis |
While the traditional markets like Hall Road Lahore and Electronic Market Pakistan still offer great deals, buying online at Robotics Kits saves you the trip, gives you live PKR pricing, and lets the AI assistant automatically add compatible accessories (e.g., jumper wires, buck converter). Plus, you can pay cash on delivery (COD) across major Pakistani cities.
7. Extending the Project
- Add Wi‑Fi telemetry: Use the ESP32’s built‑in Wi‑Fi to send distance data to a simple web dashboard.
- Integrate a camera: Pair a Raspberry Pi Zero with the robot for visual obstacle detection.
- Upgrade to STM32: For higher‑precision motor control, replace the L298N with a STM32‑based driver board.
- AI Lab on Robotics Kits: Upload your code to the platform’s AI Lab to get automated suggestions for power budgeting and sensor placement.
8. Troubleshooting Common Issues
| Symptom | Likely Cause | Fix | |---------|--------------|-----| | Motors jitter when sensor reads close | PWM conflict or noise from ultrasonic sensor | Add a small capacitor (0.1 µF) across motor driver supply, or use separate power rails | | No distance reading (always 0) | Echo pin not connected or wrong voltage level | Verify wiring, use a level shifter if using 3.3 V ESP32 | | Robot veers to one side | Unequal motor speed or wheel friction | Calibrate PWM values, check wheel alignment |
9. Final Thoughts
Building an obstacle avoiding robot is an excellent way to grasp the fundamentals of IoT, sensor fusion, and motor control. By following this roadmap you’ll have a working robot in a few hours and a clear path to add more sophisticated features later. Remember to leverage the Robotics Kits AI Build‑to‑Buy tool for a hassle‑free parts list, live PKR pricing, and COD delivery across Pakistan.
Ready to start?
Visit Robotics Kits today, use the AI Lab to generate your exact BOM, and get your components shipped straight to your doorstep. Happy building!