Distance Sensors + Servos

Hello! I am trying to connect two servos to act as wheels and a distance sensor. For some reason, the servos work perfectly fine and can move when the distance sensor isn't connected, but the second the distance sensor is added the servos no longer move. Not sure why this is happening.

Pictures:


Code:

#include <Servo.h>

Servo myservoR;
Servo myservoL;

const int trigPin = 13;
const int echoPin = 12;

void setup() {
  Serial.begin(9600);
  myservoR.attach(7);
  myservoL.attach(6);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Perform distance measurement
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);
  int duration = pulseIn(echoPin, HIGH);
  int distance = microsecondsToCentimeters(duration);

  // Control the robot based on the distance
  if (distance < 100) {  // Adjust this threshold based on your requirements
    // Move forward
    myservoR.write(80);
    myservoL.write(100);
  } else {
    // Stop
    myservoR.write(90);
    myservoL.write(90);
  }
}

// Function to calculate distance from duration
int microsecondsToCentimeters(int microseconds) {
  return microseconds / 29 / 2;
}

Welcome! Thanks for following the guidelines and posting your code accordingly. Nice picture but I cannot determine how everything is connected. Can you post a schematic showing exactly how you have wired this.

sure, here it is:

D7 is not a PWM pin.
Pin # 3, 5, 6, 9, 10, and 11 are.

It looks like your 'distance sensor' is a HC-SR04 which requires a
minimum trigger pulse of 10 us for reliable operation. With your pulse of 5us, pulsein might be returning an erratic value over 100.

I changed it to pin 9 instead - still doesn't work. I also updated my code according to @yamdaka

new code:

#include <Servo.h>

Servo myservoR;
Servo myservoL;

const int trigPin = 13;
const int echoPin = 12;

void setup() {
  Serial.begin(9600);
  myservoR.attach(9);
  myservoL.attach(6);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Perform distance measurement
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Adjusted to meet the HC-SR04 requirements
  digitalWrite(trigPin, LOW);
  int duration = pulseIn(echoPin, HIGH);
  int distance = microsecondsToCentimeters(duration);

  // Control the robot based on the distance
  if (distance < 100) {  // Adjust this threshold based on your requirements
    // Move forward
    myservoR.write(80);
    myservoL.write(100);
  } else {
    // Stop
    myservoR.write(90);
    myservoL.write(90);
  }
}

// Function to calculate distance from duration
int microsecondsToCentimeters(int microseconds) {
  return microseconds / 29 / 2;
}

Try printing 'distance' to the serial monitor maybe that will reveal
something?

It only prints out "distance:0" and never changes

Ok you found the problem. Double check your connections and
wiring to the sensor and arduino. Hopefully you will find the problem there. If not I would suggest you try a new sensor.

2 Likes

Look at your simulated code here:
Work well.

" US_Servos - Wokwi ESP32, STM32, Arduino Simulator

Recommendation:
Do not use Arduino power to power the servos.
Servos demand a lot of current.
The Arduino is not a power supply.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.