How to use two servo motors with one switch/button

Hello,
l have create a code where the servo motors are activated and depending on the amount of times the switch has been touched one of the servo motors will rotate. l did this via a counter counting the amount of times the button is pushed. When l activate just the one servo motor the counter works and it rotates depending how many times l push it but when l activate “dos” the secound servo motor the motors are both humming and do not rotate or move when l bush the button. I am not sure why its not working and would appreciate any sort of advice or help. Code is attached below:

#include <Servo.h>

// pushbutton pin
const int buttonPin = 12;

// servo pin

Servo servo;//servo 1

Servo dos;//servo 2

//create a variable to store a counter and set it to 0
int counter = 0;
void setup()
{
pinMode(buttonPin, INPUT_PULLUP);
//servo motors and what pin they are attached to.
servo.attach(3);
dos.attach(2);

}

void loop()
{
// local variable to hold the pushbutton states
int buttonState;

//read the digital state of buttonPin with digitalRead() function and store the //value in buttonState variable
buttonState = digitalRead(buttonPin);

//if the button is pressed increment counter and wait a tiny bit to give us some //time to release the button
if (buttonState == LOW) // light the LED
{
counter++;
delay(150);
}

if(counter == 0)
servo.write (20); // zero degrees
else if(counter == 1)
dos.write(20);
else if(counter == 2)
servo.write (80);
else if(counter == 3)
dos.write (80);
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter = 0;
}

Cheers,
Samantha :slight_smile:

I wonder if you are drawing power for the servos from the Arduino’s 5v pin. That is a bad idea, even with one servo. as it cannot provide enough current. With two servos the voltage probably falls too low for the Arduino to work. That can damage the Arduino.

Give the servos their own power supply with the servo GND connected to the Arduino GND.

…R

PS … when posting code please use the code button </> rather than the quote button.

Hello Robin2,
They way l have it connected is the GND and +5V of the servo motors are connected to a controller and then that controller is connected to to the arduino and a lithium battery. Then the servo motor is connected to the arduino for the pins. the GND and +5V for the servo motor are connected and joined togethour should l maybe sperate them and put one of the servo motors 5V on the arduino and one on the controller?

P.S This component l want to do is part of a project l just want to make sure this part works before l join everything togethour.

Cheers,
Samantha

00_samantha_00:
the GND and +5V for the servo motor are connected and joined togethour

I may be misunderstanding you but GND and 5v MUST ALWAYS be kept separate.

Please make a simple pencil drawing showing how everything is connected and post a photo of the drawing. Drawings are much less confusing than written descriptions. See this Simple Image Posting Guide

...R

00_samantha_00:
They way l have it connected is the GND and +5V of the servo motors are connected to a controller and then that controller is connected to to the arduino and a lithium battery.

What servos? What is the "controller"? What battery voltage and other specifications? Details matter.

Steve

servo motors SG90, stepper motor controller TA0052, CNHL 2200MAH 7.4V 2S 40C LIPO BATTERY these are the components that are being used and the way they are wired is shown in this very basic drawing. if needed l can do a better sketch with components but just to get my point across l think this is enough.

servoMotor_wiring.jpg

(deleted)

yes it is the power supply and l have that type of controller because it is part of a bigger project basically a robot that drvies to certain positions and drops off something and the servo motors help with the drop off points. The wheels for navigating the robot are two stepper motors thats why l use that controller. l just can't seem to get this one component working and thats why l seperated it so l can figure out what was wrong with it and try and debug it but hasnt been going well so far hah.

(deleted)

ahh yes l understand what you mean yes the controller is connected to the ground on the arduino thank you for mentioning that.
servoMotor_wiring_LI.jpg

servoMotor_wiring_LI.jpg

(deleted)

I can't find anything that says how much current the 5V out on that controller can supply. But I'd guess it isn't enough.

Try running the servos from 4 x AA NiMH rechargeable batteries (Eneloop or similar) and if it then all works that more or less proves that you don't have enough current from your existing setup.

Steve

ok l will try running the servos on different power sources so that they both have 5V power sources and l will also try the rechargeable batteries thank you for the advice l appreciate it!!!
@smarts_jb and @slipstick