# Obstacle avoidance car esp32 based

**URL:** <https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475>\
**Category:** General Guidance\
**Created:** [August 7, 2024, 1:28pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475 "2024-08-07T13:28:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![zuczac12](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zuczac12](https://forum.arduino.cc/u/zuczac12)\
**Post date:** [August 7, 2024, 1:28pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/1 "2024-08-07T13:28:57Z")

</div>

```arduino
#include <ESP32Servo.h>
Servo servo; // create servo object to control a servo

#define trigger_pin 5
#define echo_pin 18
#define SERVO_PIN 12 //seeting servo pin 

// Setting Motor A
int enable1Pin = 14;
int motor1Pin1 = 27;
int motor1Pin2 = 26;

//Settin Motor B
int enable2Pin = 32;
int motor2Pin3 = 25;
int motor2Pin4 = 33;

// Setting PWM properties
const int frequency = 3000;
const int pwm_channel = 0;
const int resolution = 8;

long t; //var time to calculate distance
int distance; //var to store diatnce

void setup() {
  servo.attach(SERVO_PIN); // attaches the servo on pin 12 to the servo objectư
  pinMode(trigger_pin,OUTPUT); // attaches the trigger pin to 5 pin
  pinMode(echo_pin,INPUT); // attaches echo pin to 18 pin

   //modding motor A
  pinMode(enable1Pin, OUTPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT); 

  //modding Motor B
  pinMode(enable2Pin, OUTPUT);
  pinMode(motor2Pin3, OUTPUT);
  pinMode(motor2Pin4, OUTPUT);

   // configure LEDC PWM
  ledcAttachChannel(enable1Pin, frequency, resolution,pwm_channel);
  ledcAttachChannel(enable2Pin, frequency, resolution,pwm_channel);

}

void loop() {
  //setting hc-sro4
  digitalWrite(trigger_pin,LOW);
  delay(5);
  digitalWrite(trigger_pin,HIGH);
  delay(10);
  digitalWrite(trigger_pin,LOW);

  //getting distance from hc-sr04
  t = pulseIn(echo_pin, HIGH);
  distance = (t*0.034)/2;

  //default moving forward
  MoveForward();
  delay(200);

  //centering servo
  servo.write(90); 
  delay(500);

  if(distance<20){
    int distanceLeft = 0; //setting var for left and right
    int distanceRight = 0;
    delay(200);

    distanceLeft = ServoLookLeft(distance);
    delay(200);
    distanceRight = ServoLookRight(distance);
    delay(200);

     StopCar();
     delay(200);
     MoveBackward();
     delay(200);
     StopCar();
     delay(200);

     //calcauting shortest distance
     if(distanceRight <= distanceLeft){
           TurnLeft();
      }
     else{
             
          TurnRight();
        }
  

  }

}

//servo seek clear path

//servo turn left
int ServoLookLeft(int distance){
  int ditanceLeft = 0;
  delay(500);

  servo.write(180); //lefting servo and taking value
  delay(2000);
  

  return distance;
}

//servo turn right
int ServoLookRight(int distance){
  int ditanceRight = 0;
  delay(500);

  servo.write(0); //lefting servo and taking value
  delay(2000);
  

  return distance;
}

int SeekClearPath(){
  int ditanceRight = 0;
  int ditanceLeft = 0;
  delay(500);

  servo.write(0); //righting servo
  delay(2000);
  ditanceRight=distance;

  servo.write(180); //lefting servo
  delay(2000);
  ditanceLeft = distance;
  //lcd.clear();

  //centering servo back
  servo.write(90); 
  delay(1000);

}

//car movement functions

//move forward
void MoveForward(){
  
  
  //Set speed motor A and B
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
  //lcd.clear();
  
}

//move backward
void MoveBackward(){
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
  //lcd.clear();
  delay(700);

}

//turn car left
void TurnLeft(){

   //Set speed motor A and B
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, LOW);
  //lcd.clear();
  delay(850);

   //Stop motor for half second
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(500);

}

//turn car right 
void TurnRight(){
   //turn right
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, HIGH);
  //lcd.clear();
  delay(850);

   //Stop motor for half second
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(500);
}

// stop car
void StopCar(){
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(700);
}

```

type or paste code here

```arduino

```

thats my attempt for obstacle avoidance car.  
the thing is , the engine wont move.

when i try this-

```arduino

```

// Motor A  
int enable1Pin = 14;  
int motor1Pin1 = 27;  
int motor1Pin2 = 26;

//Motor B  
int enable2Pin = 32;  
int motor2Pin3 = 25;  
int motor2Pin4 = 33;

// Setting PWM properties  
const int frequency = 3000;  
const int pwm\_channel = 0;  
const int resolution = 8;

void setup() {  
//modding motor A  
pinMode(enable1Pin, OUTPUT);  
pinMode(motor1Pin1, OUTPUT);  
pinMode(motor1Pin2, OUTPUT);

//modding Motor B  
pinMode(enable2Pin, OUTPUT);  
pinMode(motor2Pin3, OUTPUT);  
pinMode(motor2Pin4, OUTPUT);

// configure LEDC PWM  
ledcAttachChannel(enable1Pin, frequency, resolution,pwm\_channel);  
ledcAttachChannel(enable2Pin, frequency, resolution,pwm\_channel);

}

void loop() {  
//forward

analogWrite(enable1Pin,255);  
analogWrite(enable2Pin,255);

digitalWrite(motor1Pin1, LOW);  
digitalWrite(motor1Pin2, HIGH);  
digitalWrite(motor2Pin3, HIGH);  
digitalWrite(motor2Pin4, LOW);  
delay(700);

//Stop motor  
analogWrite(enable1Pin,0);  
analogWrite(enable2Pin,0);

digitalWrite(motor1Pin1,LOW);  
digitalWrite(motor1Pin2, LOW);  
digitalWrite(motor2Pin3,LOW);  
digitalWrite(motor2Pin4, LOW);  
delay(700);

}

```arduino

```

i get a normal movement forward.  
what am i missing?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 7, 2024, 5:28pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/4 "2024-08-07T17:28:42Z")

</div>

You did so well using code tags for the first sketch that you posted then failed to use them for the second sketch, Please add them

---

<div class="post-metadata">

**Author:** ![zuczac12](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zuczac12](https://forum.arduino.cc/u/zuczac12)\
**Post date:** [August 7, 2024, 6:53pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/5 "2024-08-07T18:53:00Z")

</div>

```arduino
// Motor A
int enable1Pin = 14;
int motor1Pin1 = 27;
int motor1Pin2 = 26;

//Motor B
int enable2Pin = 32;
int motor2Pin3 = 25;
int motor2Pin4 = 33;

// Setting PWM properties
const int frequency = 3000;
const int pwm_channel = 0;
const int resolution = 8;

void setup() {
  //modding motor A
  pinMode(enable1Pin, OUTPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT); 

  //modding Motor B
  pinMode(enable2Pin, OUTPUT);
  pinMode(motor2Pin3, OUTPUT);
  pinMode(motor2Pin4, OUTPUT);
  
  
  // configure LEDC PWM
  ledcAttachChannel(enable1Pin, frequency, resolution,pwm_channel);
  ledcAttachChannel(enable2Pin, frequency, resolution,pwm_channel);

}

void loop() {
  //forward movement

  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
  delay(700);

  //Stop motor
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(700);

}

```

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [August 8, 2024, 4:46am UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/6 "2024-08-08T04:46:10Z")

</div>

Your first and second sketch use different devices. The first sketch, you say the motors do not move. I will guess the added servo motor and SR04 are using enough power to brown-out your project. The second project moves, and does not use the other devices. Show your wiring diagram.

The formatting is not right in the first sketch... something is missing (a brace, a parenthesis, a semi-colon?)... and if your SR04 is not working, the function StopCar() will probably stop the motors.

---

<div class="post-metadata">

**Author:** ![davidefa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davidefa/32/1088935_2.png) [@davidefa](https://forum.arduino.cc/u/davidefa)\
**Post date:** [August 8, 2024, 8:14am UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/7 "2024-08-08T08:14:36Z")

</div>

- add a debug print at the beginning of each function so that you can check the flow of your program ( I see you are not using debug prints which are very usefull for debugging ) just print "MoveForward" for example
- add a function to measure distance, so that you can use everywhere ( at the end of the function print the distance too )
- in the function ServoLookLeft/Right you are not measuring anything, you simply return the distance passed in, so in main loop distanceLeft/Right have always the same value

Now put the car on something so that the wheels can move freely ( you have your pc connected to the car and don't whant any 'accident to happen' )  
For example put an obstacle in front of the sensor, is the distance measured correctly ( repeat for several distances )?  
Does the car turn/move correctly to avoid obstacle?  
Then check/report the output of the serial debug ( in the arduino serial debug enable the show timestamp option, very useful )

P.S.  
When describing something 'use one word more' ; - )

---

<div class="post-metadata">

**Author:** ![zuczac12](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zuczac12](https://forum.arduino.cc/u/zuczac12)\
**Post date:** [August 8, 2024, 1:44pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/9 "2024-08-08T13:44:35Z")

</div>

would esp32 wont be enough to power up sr04 and servo?

to show a pic of the project itself  
or use an logic tool for wiring?  
is so is there an easy one to use?

---

<div class="post-metadata">

**Author:** ![davidefa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davidefa/32/1088935_2.png) [@davidefa](https://forum.arduino.cc/u/davidefa)\
**Post date:** [August 8, 2024, 1:45pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/10 "2024-08-08T13:45:52Z")

</div>

Are you using these pins ( I imagine ) to connect motor, servo and sr04?

```arduino
#define trigger_pin 5
#define echo_pin 18
#define SERVO_PIN 12 //seeting servo pin 

// Setting Motor A
int enable1Pin = 14;
int motor1Pin1 = 27;
int motor1Pin2 = 26;

//Settin Motor B
int enable2Pin = 32;
int motor2Pin3 = 25;
int motor2Pin4 = 33;

```

---

<div class="post-metadata">

**Author:** ![davidefa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davidefa/32/1088935_2.png) [@davidefa](https://forum.arduino.cc/u/davidefa)\
**Post date:** [August 8, 2024, 1:50pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/11 "2024-08-08T13:50:50Z")

</div>

The sr04 is not a problem, the servo has high peaks of current, so no better not to connect the servo to the esp32 +5v ( if supplied by usb )  
If the motor controller has a good +5v output you could use that

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [August 8, 2024, 1:51pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/12 "2024-08-08T13:51:26Z")

</div>

> [@zuczac12](#):
>
> would esp32 wont be enough to power up sr04 and servo?

Which servo, SG90 or a MG995?

---

<div class="post-metadata">

**Author:** ![aliarifat794](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@aliarifat794](https://forum.arduino.cc/u/aliarifat794)\
**Post date:** [August 8, 2024, 2:23pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/13 "2024-08-08T14:23:17Z")

</div>

You can use the serial monitor. There you can print the distance of the nearest object. It will show if the SONAR sensor is giving correct data.

---

<div class="post-metadata">

**Author:** ![zuczac12](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zuczac12](https://forum.arduino.cc/u/zuczac12)\
**Post date:** [August 8, 2024, 2:27pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/14 "2024-08-08T14:27:35Z")

</div>

took your advice  
changed to code

```arduino
#include <ESP32Servo.h>
Servo servo; // create servo object to control a servo

#define trigger_pin 5
#define echo_pin 18
#define SERVO_PIN 12 //seeting servo pin 

// Setting Motor A
int enable1Pin = 14;
int motor1Pin1 = 27;
int motor1Pin2 = 26;

//Settin Motor B
int enable2Pin = 32;
int motor2Pin3 = 25;
int motor2Pin4 = 33;

// Setting PWM properties
const int frequency = 3000;
const int pwm_channel = 0;
const int resolution = 8;

long t; //var time to calculate distance
int distance; //var to store diatnce

void setup() {
  servo.attach(SERVO_PIN); // attaches the servo on pin 12 to the servo objectư
  pinMode(trigger_pin,OUTPUT); // attaches the trigger pin to 5 pin
  pinMode(echo_pin,INPUT); // attaches echo pin to 18 pin

   //modding motor A
  pinMode(enable1Pin, OUTPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT); 

  //modding Motor B
  pinMode(enable2Pin, OUTPUT);
  pinMode(motor2Pin3, OUTPUT);
  pinMode(motor2Pin4, OUTPUT);

   // configure LEDC PWM
  ledcAttachChannel(enable1Pin, frequency, resolution,pwm_channel);
  ledcAttachChannel(enable2Pin, frequency, resolution,pwm_channel);
  

  // initaite serial monitor 
  Serial.begin(9600);

}

void loop() {
  //setting hc-sro4
  digitalWrite(trigger_pin,LOW);
  delay(5);
  digitalWrite(trigger_pin,HIGH);
  delay(10);
  digitalWrite(trigger_pin,LOW);

  

  //getting distance from hc-sr04
 

  //default moving forward
  MoveForward();
  Serial.println("move forward");
  delay(200);

  //centering servo
  servo.write(90); 
  Serial.println("servo centering");
  delay(200);
  int CurrenntDistance;
  CurrenntDistance = DistanceMeasure(t, distance);
  Serial.println(CurrenntDistance);
  Serial.println("cm");

  if(distance<20){
    int CalculatedDistanceLeft = 0; //setting var for left and right distances
    int CalculatedDistanceRight = 0;
    delay(200);

     

    //car will stop
    StopCar();
    Serial.println( "car stopping");
    delay(200);
    
   
    //car backwards
    MoveBackward();
    Serial.println( "car backs uo");
    delay(200);

    //left distance calc
    CalculatedDistanceLeft = ServoLookLeft(t, distance);
    Serial.println( CalculatedDistanceLeft);
    Serial.println("cm");
    delay(200);
    
    //right distance calc
    CalculatedDistanceRight = ServoLookRight(t, distance); //right distance calc
    Serial.println( CalculatedDistanceLeft);
    Serial.println("cm");
    delay(200);

     
     //calcauting shortest distance
     if(CalculatedDistanceRight <= CalculatedDistanceLeft){
           Serial.println("turning left");
           TurnLeft();
      }
     else{
          Serial.println("turning right");
          TurnRight();
        }
  

  }

}

//servo seek clear path funcs

//getting distance from hc-sr04
//sro4 calc distance
int DistanceMeasure(int time, int distance){
  //time to singanl get back
  t = pulseIn(echo_pin, HIGH);

  //calculation of distance
  distance = (t*0.034)/2;

  return distance;

}

//servo turn left
int ServoLookLeft(int time, int distance){
  int distanceLeft = 0;
  delay(500);

  servo.write(180); //lefting servo and taking value
  delay(2000);
  distanceLeft = DistanceMeasure(time,distance);
  

  return distanceLeft;
}

//servo turn right
int ServoLookRight(int time, int distance){
  int ditanceRight = 0;
  delay(500);

  servo.write(0); //lefting servo and taking value
  delay(2000);
  ditanceRight = DistanceMeasure(time,distance);
  

  return ditanceRight;
}

int SeekClearPath(){
  int ditanceRight = 0;
  int ditanceLeft = 0;
  delay(500);

  servo.write(0); //righting servo
  delay(2000);
  ditanceRight=distance;

  servo.write(180); //lefting servo
  delay(2000);
  ditanceLeft = distance;
  //lcd.clear();

  //centering servo back
  servo.write(90); 
  delay(1000);

}

//car movement functions

//move forward
void MoveForward(){
  
  
  //Set speed motor A and B
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
  //lcd.clear();
  
}

//move backward
void MoveBackward(){
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
  //lcd.clear();
  delay(700);

}

//turn car left
void TurnLeft(){

   //Set speed motor A and B
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, LOW);
  //lcd.clear();
  delay(850);

   //Stop motor for half second
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(500);

}

//turn car right 
void TurnRight(){
   //turn right
  analogWrite(enable1Pin,255);
  analogWrite(enable2Pin,255);
  
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, HIGH);
  //lcd.clear();
  delay(850);

   //Stop motor for half second
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(500);
}

// stop car
void StopCar(){
  analogWrite(enable1Pin,0);
  analogWrite(enable2Pin,0);

  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3,LOW);
  digitalWrite(motor2Pin4, LOW);
  delay(700);
}

```

and in the serial monitor i am getting a permeant "0 cm "distance  
example of the print

"cm

0

cm

turning left

move forward

servo centering

0

cm

car stopping

car backs uo

0

cm

0

cm

turning left"

---

<div class="post-metadata">

**Author:** ![zuczac12](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zuczac12](https://forum.arduino.cc/u/zuczac12)\
**Post date:** [August 8, 2024, 2:29pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/15 "2024-08-08T14:29:10Z")

</div>

> [@jim-p](#):
>
> MG995

SG90

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [August 8, 2024, 2:29pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/16 "2024-08-08T14:29:35Z")

</div>

> [@xfpd](#):
>
> if your SR04 is not working, the function StopCar() will probably stop the motors.

...

---

<div class="post-metadata">

**Author:** ![qubits-us](https://avatars.discourse-cdn.com/v4/letter/q/5daacb/32.png) [@qubits-us](https://forum.arduino.cc/u/qubits-us)\
**Post date:** [August 8, 2024, 2:30pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/17 "2024-08-08T14:30:10Z")

</div>

> [@zuczac12](#):
>
> ```arduino
> delay(5);
> digitalWrite(trigger_pin,HIGH);
> delay(10);
> 
> ```

shouldn't that be delayMicroseconds instead of milli's??

good luck.. ~q

---

<div class="post-metadata">

**Author:** ![zuczac12](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zuczac12](https://forum.arduino.cc/u/zuczac12)\
**Post date:** [August 8, 2024, 2:30pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/18 "2024-08-08T14:30:42Z")

</div>

will 9v battery to the esp matter?

---

<div class="post-metadata">

**Author:** ![Brazilino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/brazilino/32/1198404_2.png) [@Brazilino](https://forum.arduino.cc/u/Brazilino)\
**Post date:** [August 8, 2024, 2:33pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/19 "2024-08-08T14:33:49Z")

</div>

I think it´s time for you to show your setup/wiring/schematics.  
Have you seen post #17 issue?

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [August 8, 2024, 2:37pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/21 "2024-08-08T14:37:13Z")

</div>

> [@zuczac12](#):
>
> will 9v battery to the esp matter?

Is it an ESP32 devkit V4?

---

<div class="post-metadata">

**Author:** ![zuczac12](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zuczac12](https://forum.arduino.cc/u/zuczac12)\
**Post date:** [August 8, 2024, 2:46pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/22 "2024-08-08T14:46:54Z")

</div>

what is that?

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/6/4/e/64e0a6fb183aab5cd23ea3384aedddbaa31c1912.jpeg)

---

<div class="post-metadata">

**Author:** ![davidefa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davidefa/32/1088935_2.png) [@davidefa](https://forum.arduino.cc/u/davidefa)\
**Post date:** [August 8, 2024, 3:01pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/23 "2024-08-08T15:01:18Z")

</div>

- connect the antenna to the esp32 board ( never leave a transmitter without antenna )
- move the 'pulse generation' inside the DistanceMeasure function ( you can also remove the 2 arguments you are not using ), as noted due delay should be delayMicroseconds

```arduino
  //setting hc-sro4
  digitalWrite(trigger_pin,LOW);
  delayMicroseconds(5);
  digitalWrite(trigger_pin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger_pin,LOW);

```

- move also the debug distance at the end of the function, before return

```arduino
  Serial.print("distance ");
  Serial.println(distance);

```

- in the loop you are using CurrentDistance and distance ( use only CurrentDistance )
- on the arduino serial monitor enable the checkbox 'show timestamp'

---

<div class="post-metadata">

**Author:** ![zuczac12](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zuczac12](https://forum.arduino.cc/u/zuczac12)\
**Post date:** [August 8, 2024, 3:11pm UTC](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475/24 "2024-08-08T15:11:08Z")

</div>

like this?

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/6/0/b/60b0f45f5f13c70eae13da84b6ed88b0df9b3d6f.jpeg)

motor A  
enableA =Pin 14

ini1 = Pin 27

ini2 = pin 26

Motor B

enable B = 32

in3 =Pin 25

in4 = Pin 33

[Next page](https://forum.arduino.cc/t/obstacle-avoidance-car-esp32-based/1289475.md?page=2)
