Project Description

TUTORIAL

How to connect and control multiple Servo Motors using a Joystick and an Arduino

How to connect and control multiple Servo Motors using a Joystick an Arduino and the PCA9685 PWM Module

VIDEO

Code version 1

Code version 2

PARTS USED

Available in the Brainy-Bits store

OVERVIEW

We have seen in prior tutorials how to connect and control Stepper Motors.

 

Stepper Motors are great for many projects but can get expensive when your projects needs a bunch of them.

 

So let’s see how to connect and control Servo Motors, which are a little cheaper.

 

Servo Motors are great when your project just need to make some mechanical movements that don’t need to be as precise, like those achieved by Stepper Motors.

 

Normally to control a Servo you need 1 pin on your Arduino, so if you have 5 of them, then you would require 5 pins.

 

Depending on your project, that means that you could run out of pins for other components.

 

In this tutorial we will be using the PCA9685 PWM module, which enable us to control up to 16 Servos and only use 2 pins on our Arduino UNO, since it uses the I2C communication protocol.

 

You can even link multiple PCA9685 modules together to control even more if needed.

SERVO MOTORS INTRODUCTION

A Servo Motor can be either a DC, AC or other type of motor and includes a device to know it’s position (ex.: potentiometer, digital encoder…).

 

Servo Motor Inside View

 

Inside most Servo Motors you will find: A motor, gears, some type of limit stops that will limit the movement of the shaft, a potentiometer of some kind for position feedback and some integrated circuit to move the servo to a specific position.

 

A Standard Servo has around 180 degrees of motion. These can be modified to make them rotate 360 degrees or you can buy them already made this way.

 

The modification involves opening the Servo case, removing the limiting device and disconnect the potentiometer from the shaft.  Once done the Servo will rotate in either direction endlessly since it has no way of knowing it’s position anymore and there are no limit switches to stop it.

 

Most Servo Motors have three wires:

Black: Ground

Red: Voltage

White or Yellow: Control Wire.

 

To move a Servo you send a pulse to the control wire.  This process is referred to Pulse Coded Modulation.

 

Standard Servos expects to see a pulse every 20ms.  Depending on the length of this pulse the Servo will move to a specific angle.

 

For example a 1.5ms pulse will make the Servo move to the 90 degree position (which normally is the neutral or middle position).

 

A pulse shorter than 1.5ms will be move the Servo closer to 0 degrees and a longer one will move closer to 180 degrees.

 

Servos can have a lot of torque for their size and they also draw power in proportion to how hard they are working.  So if you project is not moving much weight than the Servo will not consume much energy.

SCHEMATIC

 PCA9685-Schematic

Using the PCA9685 PWM Servo module makes connections very easy as you can see.

 

Each of the Servo motors connectors are connected to the PCA9685 bottom connections:

*In this tutorial we are using connector 1-4-8-12.

 

V+ is the RED wire of the Servo motor

GND is the Black wire

PWM is the White or Yellow wire

 

Then to power the Servo Motors we connect an External 5V power supply to the screw terminal V+ and GND.

*Note the PCA9685 screw connector is protected in case you wire it backward.

 

Then we have to make the connection from the UNO:

 

5V and GND from the UNO are connected to VCC and GND of the PCA9685 module.

*Note V+ on the side of the PCA9685 Module is used for powering the Servo Motors if you are not using the Screw Terminal connector.  It is better to use the Screw Terminal connector since it is protected in case you invert GND and Voltage by mistake.

 

A4 (SDA) and A5 (SLC) of the UNO are connected to the corresponding pins of the PCA9685 module.

 

*Note If your are using VERSION 2 of the code you also need to connect the Joystick Switch to Pin 4 of the UNO.

THE CODE

We are using the HDPC9685 library to communicate with the PCA9685 PWM Module.

You can find a link to download the library at the bottom of this tutorial page.

 

As you can see from the codes below, using the library makes it very easy to move the Servo Motors to a position relative to the position of our Joystick.

 

We are reading the X axis of the Joystick to move Servo Motors number 0 and 12 and the Y axis to move the 4 and 8 Servos.

 

Below you will find 2 different ways (code) to move the servos:

 

The first one will be proportional to the movement of the joystick: Since the analog value from the Joystick varies from 0 to 1023 and the movements of the Servo Motors are from 0 to around 420, we are using the MAP function to map the values of the Joystick to reflect the Servos limits.

 

The second one moves the servos until the joystick is released and the servo position can be reset to the middle by clicking the joystick switch.

 

As always, Don’t forget to watch our Tutorial videos on the left side of this page for more information.

 

DOWNLOAD

Copy the above Sketch code in your Arduino IDE software to program your Arduino board.

 

Used Libraries:

PCA9685 PWM Module Library created by Andrew Davies

 

Download The PCA9685 Library

 

Once downloaded, just extract the content of the zip files inside your “arduino/libraries” folder, and make sure to restart the Arduino IDE (Close and Reopen) software so it detect this newly installed library.