No errors but project doesn't work

Hi everyone. My project involves moving a servo motor 90 degrees when pressing a button. There doesn't seem to be any problem with the code and the stimulation works when I test it out but when I plug my Arduino into my computer there is just buzzing from the servo and it sometimes moves randomly.
Any suggestions on what's wrong or what I should do would be much appreciated!

#include <Servo.h>

float sensor1 = 0;
float sensor2 = 0;
float sensor3 = 0;
float sensor4 = 0;
int button1 = 0;
int button2 = 0;
int button3 = 0;

Servo servo_2;
Servo servo_1;
Servo servo_0;

long readUltrasonicDistance(int triggerPin, int echoPin)
{
    pinMode(triggerPin, OUTPUT);  // Clear the trigger
    digitalWrite(triggerPin, LOW);
    delayMicroseconds(2);
    // Sets the trigger pin to HIGH state for 10 microseconds
    digitalWrite(triggerPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(triggerPin, LOW);
    pinMode(echoPin, INPUT);
    // Reads the echo pin, and returns the sound wave travel time in microseconds
    return pulseIn(echoPin, HIGH);
}

void setup()
{
    pinMode(12, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(13, OUTPUT);

    pinMode(A0, INPUT);
    pinMode(A1, INPUT);
    pinMode(3, INPUT);

    servo_2.attach(2, 500, 2500); 
    servo_1.attach(4, 500, 2500); 
    servo_0.attach(5, 500, 2500); 
}

void loop()
{
    sensor1 = 0.01723 * readUltrasonicDistance(9, A4);
    if (sensor1 < 20) {
        digitalWrite(12, HIGH);
    } else {
        digitalWrite(12, LOW);
    }

    sensor2 = 0.01723 * readUltrasonicDistance(7, A5);
    if (sensor2 < 20) {
        digitalWrite(11, HIGH);
    } else {
        digitalWrite(11, LOW);
    }

    sensor3 = 0.01723 * readUltrasonicDistance(6, A2);
    if (sensor3 < 20) {
        digitalWrite(10, HIGH);
    } else {
        digitalWrite(10, LOW);
    }

    sensor4 = 0.01723 * readUltrasonicDistance(8, A3);
    if (sensor4 < 20) {
        digitalWrite(13, HIGH);
    } else {
        digitalWrite(13, LOW);
    }

    delay(10); // Delay a little bit to improve performance

    button1 = digitalRead(A0);
    if (button1 == HIGH) {
        servo_2.write(90);
        delay(5000); // Wait for 5000 millisecond(s)
    } else {
        servo_2.write(0);
    }

    button2 = digitalRead(A1);
    if (button2 == HIGH) {
        servo_1.write(90);
        delay(5000); // Wait for 5000 millisecond(s)
    } else {
        servo_1.write(0);
    }

    button3 = digitalRead(3);
    if (button3 == HIGH) {
        servo_0.write(90);
        delay(5000); // Wait for 5000 millisecond(s)
    } else {
        servo_0.write(0);
    }
}

This is my circuit:

You cannot power the servos from the 5V output of an Uno.
You need a separate 5V supply for the servos
What type of servos are you using?

2 Likes

You have the wrong drawing and sketch, which shows ten devices, three of which require a dedicated power supply.

1 Like

Implied by @xfpd's superior power provision, but not explicit, is the fact that running higher current s through your bread boards is a bad idea.

a7

3 Likes

Hi all. Thank you for your help! I am using three sg90 micro servos. Please don't mind the other components, they work okay. Would something like this be better?

It would work for a while but 4 AAs would last longer and you only need 1 battery pack

Thank you! I've tried using 4 AAs but the micro servos still won't turn. Any suggestions would be greatly appreciated! :slight_smile:

Get one servo to work first then work your way up from there.
Cannot see any aa battery common to other commons.

1 Like

Disconnect the batteries immediatly. You may have damaged the Uno and sensors.

ONLY the servos should be powered by the batteries, everything else should be powered by 5V

Oh yes, thank you I didn't realise that. I tried the following circuit but other than the micro servos randomly turning, it doesn't seem to work any better. I also changed the micro servos to connect them to PWM pins on Arduino.

#include <Servo.h>


float sensor1 = 0;
float sensor2 = 0;
float sensor3 = 0;
float sensor4 = 0;
int button1 = 0;
int button2 = 0;
int button3 = 0;

Servo servo_2;
Servo servo_1;
Servo servo_0;

long readUltrasonicDistance(int triggerPin, int echoPin)
{
    pinMode(triggerPin, OUTPUT);  // Clear the trigger
    digitalWrite(triggerPin, LOW);
    delayMicroseconds(2);
    // Sets the trigger pin to HIGH state for 10 microseconds
    digitalWrite(triggerPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(triggerPin, LOW);
    pinMode(echoPin, INPUT);
    // Reads the echo pin, and returns the sound wave travel time in microseconds
    return pulseIn(echoPin, HIGH);
}

void setup()
{
    pinMode(12, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(13, OUTPUT);

    pinMode(A0, INPUT);
    pinMode(A1, INPUT);
    pinMode(4, INPUT);

    servo_2.attach(6, 500, 2500); 
    servo_1.attach(3, 500, 2500); 
    servo_0.attach(5, 500, 2500); 
}

void loop()
{
    sensor1 = 0.01723 * readUltrasonicDistance(9, 9);
    if (sensor1 < 9) {
        digitalWrite(12, HIGH);
    } else {
        digitalWrite(12, LOW);
    }

    sensor2 = 0.01723 * readUltrasonicDistance(7, 7);
    if (sensor2 < 9) {
        digitalWrite(11, HIGH);
    } else {
        digitalWrite(11, LOW);
    }

    sensor3 = 0.01723 * readUltrasonicDistance(2, 2);
    if (sensor3 < 9) {
        digitalWrite(10, HIGH);
    } else {
        digitalWrite(10, LOW);
    }

    sensor4 = 0.01723 * readUltrasonicDistance(8, 8);
    if (sensor4 < 9) {
        digitalWrite(13, HIGH);
    } else {
        digitalWrite(13, LOW);
    }

    delay(10); // Delay a little bit to improve performance

    button1 = digitalRead(A0);
    if (button1 == HIGH) {
        servo_2.write(90);
        delay(5000); // Wait for 5000 millisecond(s)
    } else {
        servo_2.write(0);
    }

    button2 = digitalRead(A1);
    if (button2 == HIGH) {
        servo_1.write(90);
        delay(5000); // Wait for 5000 millisecond(s)
    } else {
        servo_1.write(0);
    }

    button3 = digitalRead(4);
    if (button3 == HIGH) {
        servo_0.write(90);
        delay(5000); // Wait for 5000 millisecond(s)
    } else {
        servo_0.write(0);
    }
}




Still wrong.
You have the pushbuttons and one ultrasonic sensor powered by 6V.
I hope nothing has been burned out.

You state: No errors but project doesn't work. I disagree.
Try posting a real schematic, we are trying to troubleshoot, not wire it; from what I see you have connected the batteries 6V output to the Arduino's 5V bus, this is a bad thing and tends to destroy parts.

Here's the schematic.
Thanks for all your help I'm still a beginner :sweat_smile:. Hopefully, this is better??
STS Schematic.pdf (11.8 KB)

The batteries (4x1.6=6.4V when new) are still in the 5V pin...
Problem is that 6V is too high for 5V pin and too low for vin pin...
Shift the buttons down. Get rid of all small orange wires. (Orange in your schematic!)
I must admit: it looks really neat,but every bridge is a potential loose contact.
Are the buttons working? You might need to turn them by 90 degrees.
Did you print any numbers to serial? Such as button states and return value from ultrasensor? Serial.print() is your best friend for debugging software...
Is orange wire (picture) in ~13?
How is the left upper board powered?
The ground of battery fed board should be connected to the ground of uno.

I only see one error, the battery negative must connect to the Uno GND
Also make the resistors connecting the buttons larger, like 1K.