Arduino ESP32 All in One Robot (Obstacle Avoiding, Line Tracking, Following, Real Time Video)

by c.iurisci in Circuits > Arduino

11311 Views, 14 Favorites, 0 Comments

Arduino ESP32 All in One Robot (Obstacle Avoiding, Line Tracking, Following, Real Time Video)

IMG_20220325_081418.jpg

Hi, after several night spent to do robots I decided to share with you my last job. An Arduino/ESP32 based robot that is able to avoid obstacle, track lines, follow and show real-time video from a camera.

This robot cab be controlled through Bluetooth (connected to Arduino) or WIFI (ESP32 board connected to Arduino).

I hope to do a step by step guide on making this robot in a very easy way.


Video tutorial available at: https://www.youtube.com/watch?v=S48q2WzM1kc

 

An obstacle avoiding robot is a fully autonomous robot which can be able to avoid any obstacle which it faces when it moves. Simply, when it met an obstacle while it’s moving forward, automatically stop moving forward and makes a step back. Then it looks it's two sides left & right and starts to move the best possible way, which means either in left direction if there is another obstacle in right or in right direction if there is another obstacle in left side. The obstacle avoiding robot is very helpful and it is the base of many large projects such as Automatic cars, robots used in Manufacturing factories, even in robots used in spacecrafts.

 

A line tracking robot use some infrared proximity sensors to detect the line and based on input received from the sensors, the Arduino will direct the motors to move with the help of a motor drive.

 

A Following robot use proximity sensors that can detect any object near it and can follow this object. If you come in front of the robot it will start following you. this robot consists ultrasonic sensor and ir sensor which help to follow the object. This is like the obstacle avoiding robot only but opposite in the working. It used 2 IR sensors and an ultrasonic sensor. IR sensors used to follow the human or object and ultrasonic sensor is used to move back the robot.

 

A Bluetooth Robot can be controlled through a Bluetooth device connected to Arduino. I developed an Android App that can control robot direction and activate action mode.

 

ESP32 board can transmit real-time video through WIFI. I developed an Android App where you can see real-time video during robot movement. You can use the robot as an IOT device. 

Supplies

IMG_20220323_204221.jpg

Arduino Uno + cable               1 pc        https://www.ebay.it/itm/393696348236

Arduino Sensor Shield V5.0  1 pc        https://www.ebay.it/itm/272475905095

Dual H Bridge L298N               1 pc        https://www.ebay.it/itm/174530182679

DC 3V-6V Motor                       4 pcs    https://www.ebay.it/itm/193732734105

Plastic wheels 65x26mm      4 pcs      https://www.ebay.it/itm/273552851532

Tracking Module 5V 5-way    1 pc        https://www.ebay.it/itm/164310317627

IR Sensor                                    1 pc        https://www.ebay.it/itm/322526594796

Buzzer module                          1 pc        https://www.ebay.it/itm/324275042391

Ultrasound sensor HC-SR04  1 pc        https://www.ebay.it/itm/393185981726

Micro servo SG90                    1 pc        https://www.ebay.it/itm/164409797808

Resistors 100kΩ and 1MΩ                     https://www.ebay.it/itm/175213569707

Esp32 Cam WiFi                       1 pc        https://www.ebay.it/itm/255283289447

Bluetooth module HC-06 1 pc https://www.ebay.it/itm/111691804445

Mini Breadboard                     1 pc        https://www.ebay.it/itm/133588032841

Battery holder                           1 pc        https://www.ebay.it/itm/174942264104

Female-Female Cable            20 pc      https://www.ebay.it/itm/133416936217

Standoff M3x50mm               4 pc        https://www.ebay.it/itm/362093544564

Screw M2-M2.5                                          https://www.ebay.it/itm/275100688634


3D printed part at https://www.thingiverse.com/thing:5330810

1 Chassis Top (2 part glued)

1 Chassis Bottom (2 part glued)

1 HC-SR04/ESP32 Face Holder

1 ESP32 clip

2 Spacers


If you want you can buy directly the robot at https://www.ebay.it/itm/165403121965

Assembling the Bottom Chassis

IMG_20220323_205852.jpg
IMG_20220323_205909.jpg
IMG_20220323_210420.jpg
IMG_20220323_211806.jpg
Schema_MotorDriver.jpg
IMG_20220323_214237.jpg
IMG_20220323_215122.jpg
IMG_20220323_220633.jpg
IMG_20220323_220609.jpg
IMG_20220323_220615.jpg
IMG_20220323_220629.jpg
IMG_20220323_220633.jpg

Place the bottom chassis on a plane and mount the Dual H Bridge L298N on it (use 4 screw).

Take care of the Dual H Bridge orientation (see image for reference).

Mount the 4 DC Motor on the chassis using the longer screw and nut (see image for reference).

Connect the 4 DC Motor to the Dual H Bridge L298N following the schema. Remember that the Dual H Bridge L298N is able to control 2 DC motor and we have to connect right motor and left motor together.

Assemble the Tracking Module 5V 5-way in the front position of the bottom chassis using the bore (see image for reference). For this step you have to use the 2 printed spacers and 2 M2 nut/screw.

Screw the 4 Standoff M3x50mm on the bottom chassis using the small M3 screw. This standoff will be used to fix the bottom chassis with the top chassis

Assembling the Top Chassis

IMG_20220323_215349.jpg
IMG_20220323_215946.jpg
IMG_20220323_220216.jpg
IMG_20220323_220319.jpg
IMG_20220323_220328.jpg
IMG_20220323_230150.jpg

Place the Top chassis on a plane and mount the Arduino board on it (use 4 screw).

Place the Arduino Sensor Shield V5.0 on top of Arduino board taking care that the corresponding pin are coupled together.

Mount the 2 IR Sensor on the chassis with the screw. Use the oriented supports on the chassis in order to have the sensor oriented of 30°. This step is important because IR sensors are used to check obstacle on the right and left side of the robot.

Mount the Micro servo SG90 on the chassis sqare hole.Use 2 screw to fix it.

Mount the Buzzer module on the chassis with the screw. Use the support for the right orientation.

Mount the Battery holder on the chassis with the 2 screw. Use the support for the right orientation.


You have now assembled the Top and Bottom chassis. Good job.

Let's go on with other step.

Preparing the Ultrasonic Sensor and ESP32 CAM

IMG_20220323_231006.jpg
IMG_20220323_231013.jpg
IMG_20220323_231111.jpg
IMG_20220323_231148.jpg
IMG_20220323_231957.jpg

Use the 3D printed Face Holder and insert the Ultrasound sensor HC-SR04 in the 2 round hole.

Then mount the Holder on the micro servo which has already installed on the chassis.

Wire the Ultrasound sensor HC-SR04 using 4 female-female cable.


Mount he ESP32 Cam on the 3D printed Face Holder.

Wire the ESP32 Cam using 2 female-female cable (5V and GND).


Wiring Components

Schema_MotorDriver.jpg
Schema_Arduino.jpg
IMG_20220323_221012.jpg
IMG_20220323_221352.jpg
IMG_20220323_221922.jpg
IMG_20220323_222658.jpg
IMG_20220323_223450.jpg
IMG_20220323_221651.jpg
IMG_20220323_225540.jpg

Now we can start wiring the Smart Car.


Before assembling the chassis together we have to wire the component on the bottom chassis in order to use the hole to pass through.

Wire the Tracking Module 5V 5-way on the Bottom chassis using 5 female-female cable.

Wire the Dual H Bridge L298N on the Bottom chassis using 6 female-female cable.

Wire the GND, 5V port using 2 male-male cable.

Wire the GND,12V port on the Dual H Bridge to the battery holder. Important: Put the power switch to OFF.


Now you can use the hole on the Top chassis to pass through the female-female cable wired before. Do not forget the micro servo cable.

Place the Top Cassis on the top and fix it with the Bottom chassis using M3 screw.


Now we can continue wiring other component on the Top chassis.

Wire both IR sensor on the Top chassis using 3 female-female cable for each.

Wire the Buzzer module on the Top chassis using 3 female-female cable.

Try to put all cable together using an elastic or something similar.


Wiring detail:

L298N motor driver:

+12V → Lipo battery (+)

GND → Lipo battery (- ) important: connect the GND to lipo battery (-) and to arduino board any GND pin

+5V → arduino Vin

EnA → arduino digital pin 3

In1 → arduino digital pin 4

In2 → arduino digital pin 5

In3 → arduino digital pin 6

In4 → arduino digital pin 7

EnB → arduino digital pin 11

OUT1 → Motor FWD+BWD Right

OUT2 → Motor FWD+BWD Right

OUT3 → Motor FWD+BWD Left

OUT4 → Motor FWD+BWD Left

 

Buzzer module:

GND → arduino V

VCC → arduino G

I/O → arduino digital pin 13

 

Micro servo:

GND → arduino V

VCC → arduino G

IN → arduino digital pin 12

 

IR right sensor:

GND → arduino V

VCC → arduino G

OUT → arduino digital pin 2

 

IR left sensor:

GND → arduino V

VCC → arduino G

OUT → arduino digital pin 8

 

HC-SR04 Ultrasonic

Sonar sensor:

VCC → Arduino URF +5V

Trig → arduino URF analog pin 2

Echo → arduino URF analog pin 3

GND → Arduino URF GND

 

Tracking Module:

GND → arduino G

5V → arduino V

OUT2 → arduino analog pin 4

OUT3 → arduino analog pin 1

OUT4 → arduino analog pin 0

 

Bluetooth Module HC-06

GND → arduino G

5V → arduino V

TX → arduino RX

RX → arduino TX

 

ESP32 CAM

GND → arduino G

5V → arduino V

RX → arduino TX important: when connected you must disconnect Bluetooth

Breadboard With Voltage Reader

BatteryVoltage.jpg

Mount the mini breadboard on the Top chassis using the adhesive.

Wire the 12V port on the Dual H Bridge to the breadboard using a male-male cable.

Battery Voltage (100k and 1M resistor serial connection)

100k resistor → 12V

100k/1M resistor → arduino analog pin 5

1M resistor → arduino G


Mini Breadboard can be used also for your own experiment !!

Complete Assembling

IMG_20220325_081430.jpg
IMG_20220325_081440.jpg
IMG_20220325_081453.jpg

Now you can complete the job assembling the 4 plastic wheels and group all the wires together.

Programming Arduino UNO

ArduinoIDE.JPG
Download and Install the Arduino Desktop IDE

windows - https://www.arduino.cc/en/Guide/Windows
Mac OS X - https://www.arduino.cc/en/Guide/MacOSX
Linux - https://www.arduino.cc/en/Guide/Linux




Create a folder callled \Catosci_SmartCar and save all .ino,.cpp and .h files.

Open Catosci_SmartCar.ino file on Arduino IDE and upload to Arduino board.




Important: Remember to disconnect Bluetooth before connect USB cable







/**********************************************************************
  Filename    : Catosci_SmartCar.ino
  Product     : Catosci 4WD Car for UNO
  Description : A Multifunctional Bluetooth Remote Car.
  Auther      : Carlo Iurisci
  Modification: 2022/03/25
**********************************************************************/
#include "Catosci_4WD_Car_for_Arduino.h"
#include "Automatic_Obstacle_Avoidance_Mode.h"
#include "Automatic_Tracking_Line_Mode.h"
#include "BluetoothOrders.h"

#define UPLOAD_VOL_TIME    3000
#define UPLOAD_DATA_TIME   500
#define COMMANDS_COUNT_MAX  8
#define INTERVAL_CHAR '@'


//#define DEBUG


u32 lastUploadVoltageTime;
u32 lastUploadDataTime;
String inputStringBLE = "";
bool stringComplete = false;
int paramters[COMMANDS_COUNT_MAX], paramterCount = 0;
int bleCarMode = MODE_NONE;

void setup() {
  pinsSetup();
  Serial.begin(9600);
  servoSetup();
//  while (!strip.begin());
//  strip.setAllLedsColor(0xFF0000);
}


void loop() {
  // invia misura Vbatteria all'App ogni UPLOAD_VOL_TIME
  upLoadVoltageToApp();
  // invia lettura sensori all'App ogni UPLOAD_DATA_TIME
  upLoadAndReadSonarValueToApp();
  
  if (stringComplete) {         //da eseguiro solo alla ricezione di un comando
    String inputCommandArray[COMMANDS_COUNT_MAX];


    String inputStringTemp = inputStringBLE;
    for (u8 i = 0; i < COMMANDS_COUNT_MAX; i++) {
      int index = inputStringTemp.indexOf(INTERVAL_CHAR);
      if (index < 0) {
        break;
      }
      paramterCount = i;  //
      inputCommandArray[i] = inputStringTemp.substring(0, index);
      inputStringTemp = inputStringTemp.substring(index + 1);
      paramters[i] = inputCommandArray[i].toInt();
    }
    stringComplete = false;
    inputStringBLE = "";


    char commandChar = inputCommandArray[0].charAt(0);
    switch (commandChar)
    {
      case ACTION_MOVE:
        if (paramterCount == 2) {
          motorRun(paramters[1], paramters[2]); //app contol catoscicar_speed is 0 ~ 100 ,pwm is 0~255
        }
        break;
      case ACTION_CAR_MODE:
        if (paramterCount == 1) {
          bleCarMode = paramters[1];
          switch (bleCarMode)
          {
            case MODE_NONE: case MODE_GRAVITY:
              resetCarAction();
              writeServo(OA_SERVO_CENTER);
              break;
            case MODE_ULTRASONIC: 
              oa_CalculateVoltageCompensation();
              break;
            case MODE_ULTRASONIC_NOIR:
              oa_CalculateVoltageCompensation();
              break;
            case MODE_FOLLOW:
              oa_CalculateVoltageCompensation();
              break;           
            case MODE_TRACKING:
              tk_CalculateVoltageCompensation();
              break;
            default:
              break;
          }
        }
        break;
      case ACTION_BUZZER:
        if (paramterCount == 1) {
          setBuzzer(paramters[1]);
        }
        break;
      default:
        break;
    }
  }


  switch (bleCarMode) //loop per modalità di controllo
  {
    case MODE_NONE: case MODE_GRAVITY:
      break;
    case MODE_ULTRASONIC: 
      updateAutomaticObstacleAvoidance();
      break;
    case MODE_ULTRASONIC_NOIR:
      updateAutomaticObstacleAvoidanceNoInfrared();
      break;
    case MODE_FOLLOW:
      UltrasonicFollow();
      break;           
    case MODE_TRACKING:
      updateAutomaticTrackingLine();
      break;
    default:
      break;
  }

void upLoadVoltageToApp() {
  int voltage = 0;
  if (millis() - lastUploadVoltageTime > UPLOAD_VOL_TIME) {
    if (getBatteryVoltage()) {
      voltage = batteryVoltage * 1000;
    }
    String sendString = String(ACTION_GET_VOLTAGE) + String(INTERVAL_CHAR) + String((voltage)) + String(INTERVAL_CHAR);
    Serial.println(sendString);
    
    lastUploadVoltageTime = millis();
  }
}

void upLoadAndReadSonarValueToApp() {
  // invia lettura sensori all'App
  if (millis() - lastUploadDataTime > UPLOAD_DATA_TIME) {
    String sendString = String(ACTION_ULTRASONIC)+String(INTERVAL_CHAR)+String(getSonar());
    sendString += String(INTERVAL_CHAR)+ String(digitalRead(IR_AVOIDANCE_RIGHT_PIN));
    sendString += String(INTERVAL_CHAR) + String(digitalRead(IR_AVOIDANCE_LEFT_PIN)) + String(INTERVAL_CHAR);
    Serial.println(sendString);
    
    lastUploadDataTime = millis();
  }
}


void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputStringBLE += inChar;
    if (inChar == '\n') {
      stringComplete = true;
      Serial.println(inputStringBLE);
    }
  }
}


When upload complete the code will start running.

Mini servo will position to 90°.

Important: Re-position the Face Holder on the servo if needed.

You can now test Robot movement and action mode with serial command.


Following instruction for the serial commands:

#define ACTION_MOVE        'A'  //control movement of the smartcar A#speedl#speedr#
#define ACTION_CAR_MODE    'H'  //smartcar mode. 
                  //        H#MODE_NONE->stop
                  //        H#MODE_GRAVITY->stop
                  //        H#MODE_ULTRASONIC->obstacle avoidance with sonar and ir
                  //        H#MODE_ULTRASONIC_NOIR->obstacle avoidance with sonar and no-ir
                  //        H#MODE_TRACKING->tracking with ir          
                  //        H#MODE_FOLLOW->follow with sonar and ir   
#define ACTION_BUZZER      'D'  //control buzzer          D#1# buzzer on / D#0# buzzer off

#define MODE_NONE            0
#define MODE_GRAVITY         1
#define MODE_ULTRASONIC      2
#define MODE_ULTRASONIC_NOIR 3
#define MODE_TRACKING        4
#define MODE_FOLLOW          5

Programming ESP32 CAM

ArduinoIDE_ESP32.JPG

Now it's time to program ESP32 CAM in order to trasmit real time video. Follow this tutorial to connect ESP32 CAM to your laptop/computer USB using a FTDI Programmer.

https://randomnerdtutorials.com/program-upload-code-esp32-cam/


Create a folder called \esp32cam-robot and save app_httpd.cpp ,esp32cam-robot.ino.

Open esp32cam-robot.ino file on Arduino IDE.

Before uploading the code, you need to insert your network credentials in the following variables:

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";


Then, click the Upload button in your Arduino IDE

When you start to see some dots on the debugging window, press the ESP32-CAM on-board RST button.

After a few seconds, the code should be successfully uploaded to your board.

When you see the “Done uploading” message, you need to remove GPIO 0 from GND and press the RST button to run your new code.

On serial monitor you should see something like this. Take note of ESP32 IP address.

20:43:13.775 -> ..........
20:43:18.265 -> WiFi connected
20:43:18.265 -> Camera Ready! Use 'http://192.168.1.27' to connect
20:43:18.359 -> Starting web server on port: '80'
20:43:18.359 -> Starting stream server on port: '81'

Android App

Screenshot_20220325_211838_com.catosci.opencat.jpg
Screenshot_20220325_212002_com.catosci.opencat.jpg

You can now use my App developed to control the Robot via bluetooth and see real time video via wifi.

Download apk file from GitHub https://github.com/catosci/CatosciApp/blob/main/release/catosci.apk

Great !!!

Congratulation !! Now your robot it's an FPV !!


If you want you can buy directly the robot at https://www.ebay.it/itm/165403121965