Controlling Multiple Servo Motors with Arduino

Controlling one or two servo motors with an Arduino development board is very easy, but what if we want to control multiple servo motors?

In this article, we will introduce how to control multiple servo motors using an Arduino development board. Connecting multiple servo motors to the Arduino development board seems easy, but if we connect all the servos to the power pins of the Arduino, we may not have enough current to drive all the motors, causing them to not work properly. Therefore, you must use a separate power supply for the motors, which can be a power adapter (5V/2A) or a high-quality 9V battery.

Materials Needed

● Arduino UNO development board

● Servo motors

● Power supply

● Breadboard

● Connecting wires

Circuit Diagram

Controlling Multiple Servo Motors with Arduino

Circuit diagram for controlling multiple servo motors with Arduino

What is a Servo Motor?

Before discussing in detail, we should first understand what a servo motor is.

Servo motors come in different shapes and sizes. A typical servo motor has three wires: one for power, one for ground, and one for position. The red wire connects to power, the black wire connects to ground, and the yellow wire connects to the signal.

Controlling Multiple Servo Motors with Arduino

Controlling Multiple Servo Motors with Arduino

A servo motor consists of a DC motor, a position control system, and gears. The position of the DC motor shaft is adjusted by the control circuit of the servo motor according to the PWM signal duty cycle at the signal pin.

In simple terms, the control circuit adjusts the shaft position by controlling the DC motor. The data related to the shaft position is sent through the signal pin. The control position data should be sent to the servo motor’s signal pin in the form of a PWM signal.

The frequency of the PWM (Pulse Width Modulation) signal varies slightly depending on the type of servo motor. The important parameter here is the duty ratio of the PWM signal. Based on the duty ratio, the control circuit adjusts the position of the shaft.

As shown in the figure below, to move the shaft to the 9 o’clock position, the duty ratio must be 1/18, which means 1ms ON time and 17ms OFF time in an 18ms signal.

Controlling Multiple Servo Motors with Arduino

To move the shaft to the 12 o’clock position, the ON time of the signal must be 1.5ms, and the OFF time should be 16.5ms. The control system inside the servo motor decodes this duty ratio and adjusts the position accordingly. The PWM is generated using the ARDUINO UNO.

Arduino Code Explanation

The complete Arduino code for controlling multiple servo motors is provided at the end of this article.

The Arduino IDE comes with a servo motor library that handles all PWM-related tasks to rotate the servo motors. You only need to input the desired angle to rotate, and then use the function servo1.write(angle); to move the servo motor to the desired angle.

So, we first include the servo motor library.

  1. #include <Servo.h>

Copy code

In the code below, we initialize four servo motors as Servo1, Servo2, Servo3 and Servo4.

  1. Servo servo1;

  2. Servo servo2;

  3. Servo servo3;

  4. Servo servo4;

Copy code

Next, we set the input pins for all the servo motors on the Arduino. As shown in the code below, Servo1 is connected to pin 3 of the Arduino. You can change the pin numbers according to your needs, but remember it should be a PWM pin. Using digital pins on the Arduino to control servo motors is unreliable.

  1. void setup() {

  2. servo1.attach(3);

  3. servo2.attach(5);

  4. servo3.attach(6);

  5. servo4.attach(9);

  6. }

Copy code

Now, in the void loop() function, we will rotate all servo motors from 0 degrees to 180 degrees and then back from 180 degrees to 0 degrees. The delay used in the code below is to increase or decrease the rotation speed of the servo motors, as it affects the speed of the variable ‘i‘ increment or decrement.

  1. void loop() {

  2. for (int i = 0; i < 180; i++) {

  3. servo1.write(i);

  4. servo2.write(i);

  5. servo3.write(i);

  6. servo4.write(i);

  7. delay(10);

  8. }

  9. for (i = 180; i > 0; i–) {

  10. servo1.write(i);

  11. servo2.write(i);

  12. servo3.write(i);

  13. servo4.write(i);

  14. delay(10);

  15. }

  16. }

Copy code

Working Process

When using one Arduino development board to control more than two servo motors, we face the issue of insufficient current. The only solution is to connect an external power supply that can provide an adequate current rating (in this article, I use a 9V/2A power supply). You can use an adapter, RPS (regulated power supply), or a high-quality 9V battery as an external power supply. In some cases, you can even power small servo motors using a laptop’s USB port. To use an external power supply, simply connect the ground of the Arduino to the ground of the external power supply.

Controlling Multiple Servo Motors with Arduino

Use the Arduino code provided below to program the Arduino development board and connect all the servo motors as shown in the circuit diagram, providing appropriate power to the motors. This way, all servos can work simultaneously without any interruption.

For more exciting tutorials on Arduino development boards, please click “Read Original”.

Leave a Comment

Your email address will not be published. Required fields are marked *