# Ultrasonic sensor - problem with distance measurement

#### saboteur

Joined Jan 6, 2023
3
Hello

Here I am again with the car, but now there is no problem with control . I just added an ultrasonic sensor to the front of the car, with the fact that if the distance is less than 20 cm, it should stop. When the car is running, it works once or twice, but on the third attempt, the car won't move in any direction and it has to be restarted. If anyone knows where the problem could be hiding, I will be very happy.

Here is the code:

(The sensor is attached to the servo motor due to the fact that if the car goes to the left, for example, the sensor turns to the left, but when it stops, it returns to its original position.)

Code:
#include <AFMotor.h>
#include <SoftwareSerial.h>
#include <Servo.h>

#define trigPin 2
#define echoPin 13
#define maxDistance 20

AF_DCMotor motor_1(1); //lavy predny
AF_DCMotor motor_3(3); //predny pravy
Servo servo;
SoftwareSerial bluetoothSerial(0, 1); // RX, TX
const int ledPin = 7;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
bluetoothSerial.begin(9600);
motor_1.setSpeed(150);
motor_2.setSpeed(150);
motor_3.setSpeed(150);
motor_4.setSpeed(150);

pinMode(ledPin, OUTPUT);
servo.attach(10);
}

void loop() {
if (bluetoothSerial.available()) { // ak je k dispozícii niečo na bluetoothSerial
switch (znak_citany) {
case 'F':
motor_1.run(FORWARD);
motor_2.run(FORWARD);
motor_3.run(FORWARD);
motor_4.run(FORWARD);
break;
case 'B':
motor_1.run(BACKWARD);
motor_2.run(BACKWARD);
motor_3.run(BACKWARD);
motor_4.run(BACKWARD);
break;
case 'L':
motor_1.run(BACKWARD);
motor_2.run(FORWARD);
motor_3.run(BACKWARD);
motor_4.run(FORWARD);
servo.write(120);
delay(50);
break;
case 'R':
motor_1.run(FORWARD);
motor_2.run(BACKWARD);
motor_3.run(FORWARD);
motor_4.run(BACKWARD);
servo.write(60);
delay(50);
break;
case 'A':
motor_1.run(FORWARD);
motor_3.run(FORWARD);
servo.write(120);
delay(50);
break;
case 'C':
motor_2.run(FORWARD);
motor_4.run(FORWARD);
servo.write(60);
delay(50);
break;
case 'E':
motor_2.run(BACKWARD);
motor_4.run(BACKWARD);
break;
case 'G':
motor_1.run(BACKWARD);
motor_3.run(BACKWARD);
break;
case 'H':
motor_1.run(BACKWARD);
motor_2.run(FORWARD);
motor_3.run(BACKWARD);
motor_4.run(BACKWARD);
break;
case 'I':
motor_1.run(BACKWARD);
motor_2.run(BACKWARD);
motor_3.run(FORWARD);
motor_4.run(FORWARD);
break;
case 'U':
digitalWrite(ledPin, LOW);
break;
case 'D' :
digitalWrite(ledPin, HIGH);
break;
case 'S':
motor_1.run(RELEASE);
motor_2.run(RELEASE);
motor_3.run(RELEASE);
motor_4.run(RELEASE);
servo.write(90);
delay(50);
break;

}
}

long duration, distance;
digitalWrite(trigPin, LOW); // vyslanie trig signálu
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // meranie času odozvy echo signálu
distance = duration / 58.2;       // výpočet vzdialenosti z echo signálu

if (distance < maxDistance && distance > 0) { // ak je vzdialenosť od objektu menšia ako maxDistance
motor_1.run(RELEASE);
motor_2.run(RELEASE);
motor_3.run(RELEASE);
motor_4.run(RELEASE);

}
}

#### michael_lee1988

Joined Jan 24, 2023
4
Does the ultrasonic sensor work normally?

#### KeithWalker

Joined Jul 10, 2017
2,724
This is probably a power wiring problem. R/C servos can draw a fairly high instantaneous current when they are energized. It can cause enough local voltage drop in the wiring to reset a microcontroller. To minimise this effect, make sure that all power supply wires and common return wires from all devices each separately connect directly to their supplies. You may need to add extra supply decoupling for some of the devices.

#### saboteur

Joined Jan 6, 2023
3
This is probably a power wiring problem. R/C servos can draw a fairly high instantaneous current when they are energized. It can cause enough local voltage drop in the wiring to reset a microcontroller. To minimise this effect, make sure that all power supply wires and common return wires from all devices each separately connect directly to their supplies. You may need to add extra supply decoupling for some of the devices.
Ok, I try it.

#### saboteur

Joined Jan 6, 2023
3
Ok, I try it.
It is exactly this problem, but could you perhaps be more specific about how the problem with the power supply could be solved.

#### KeithWalker

Joined Jul 10, 2017
2,724
It is exactly this problem, but could you perhaps be more specific about how the problem with the power supply could be solved.
I hope this explains it to you: