As a hobbyist and and electronic enthusiast, you might have heard of Servo Motors and their application. Servo Motors are used in robotics as well as automation projects in home and offices. They are used in door locks, aeroplane wings, robotic hands and they have countless other applications. Using Servo Motors you will have precise control over the movement.
The PCA9685 is a 16 Channel 12 Bit PWM I2C-bus controlled Servo motor Driver. The Driver can very easily be connected to your Arduino, Raspberry Pie and easily programmed to control single or multiple servo motors and make your own RC plane, car, ship, quadruped, hexapod, or anything you want.
Easily Control Multiple Servo Motors using PCA9685 Servo Motor Driver Module and an Arduino
Watch this Video to Set this up.
Components Required
- Arduino (Arduino Nano)
- PCA9685 Servo Motor Driver Module
- Servo Motor
- Power Source
- A Regulator (Optional)
What is a Servo Motor?
So what is a Servo Motor? Servo Motor is a motor which is designed for specific precise angular protection. they have gears and have an internal circuit which will provide feedback and helps in precisely adjusting the movement of the shaft.
There are different variants of Servo Motors available in the market. depending upon the type of voltage applied, there are AC and DC Servo Motors. Depending upon the type of gears – Metal Geared and non-metal geared for plastic geared Servo Motors. Depending upon the range of motion, there are 180 degree and 360 degree Servo Motors. And there are analog and digital servo motors.
How Do We Control Servo Motors?
In the case of a DC motor it will be having two terminals. in one terminal we will apply a positive potential and in the other we will provide a negative potential. once the circuit is complete the DC motor will start to rotate. when we reverse the terminal the DC motor will start to rotate in the opposite direction.
Now let’s take a closer look at the Servo Motor. If you look carefully, you will see three wires coming out from the back of the servo motor. Usually it will be orange, red and brown; but it varies from motor to motor. One is for providing a positive voltage (+5 V, usually the red one) and black one is for 0 V. But that is not enough we have to tell the motor in which angle it should rotate. For that we will use the third pin – the orange one. This is done by providing PWM signals through the signal Pin.
What is Pulse Width Modulation?
This is a way of getting analogue output using digital means. I will make it a little bit clear. in digital world there are only two states – 0 and 1 or on and off or 5 volt or 0 volt. Usually, using digital methods the only waveform or signal we can create is a square wave of varying On time and Off. But this can be used to simulate and analogue waveform by adjusting the time the wave stays On and the time It stays Off.
One way to do that is to make use of servo library which is available online. This library can be used to create PWM signals by writing analog values to one of the PWM pins. All you have to do is enter the value of the angle of rotation of the servo motor (between 0 and 180) and the library will take care of the rest of the hard work.
But the problem here is there is a limit to the number of PWM pins available in the Arduino board. If you want to control more than that, you will have to use another Arduino Board.
So how can we overcome this issue? Behold PCA9685 16 Channel Servo Motor Driver.
PCA9685 16 Channel 12 Bit PWM I2C-bus controlled Servo motor Driver
The PCA9685 is a 16 Channel 12 Bit PWM I2C bus controlled Servo motor Driver. The Driver can very easily be connected to your Arduino, Raspberry Pi and easily programmed to control single or multiple servo motors and make your own RC plane, car, ship, quadrapod, hexapod or anything you want.
Here the Arduino and PCA9685 16 Channel Servo Motor Driver communicate with each other using I2C protocol and the PCA9685 Servo Motor Driver will send PWM signals to the servo motor. Thus you can control multiple servo motors the way you want using a few lines of code.
You can buy it from BangGood for a cheap price. Click Here to buy.
Get Started
Here, I used an easy to use voltage regulator circuit for hobbyist. This can be used in arduino, Raspberry Pi projects. Easy and Efficient Voltage Adjustment.
The video will show you how to setup your own voltage regulator.
After setting up the regulator, Follow the below diagram and connect the Arduino board and PCA9685 16 Channel Servo Motor Driver.
Schematics
There are two pins on the driver board for power; VCC and V+. It is important that, for the working of the IC inside the motor driver, 5V power supply is required. This 5 V can be provided from arduino’s 5 V pin as I did in the video.[AdSense-C]
Arduino’s 5 V should be connected to VCC only and not V+. V+ is used to power the motor. It will take more current than VCC.
Mega256020 —– (SDA), 21 (SCL)
Leonardo —– 2 (SDA), 3 (SCL)
Due —– 20 (SDA), 21 (SCL), SDA1, SCL1
Download and install adafruit library from here and launch arduino IDE.
Code
#include <Wire.h> #include <Adafruit_PWMServoDriver.h> Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); #define MIN_PULSE_WIDTH 650 #define MAX_PULSE_WIDTH 2350 #define DEFAULT_PULSE_WIDTH 1500 #define FREQUENCY 50 uint8_t servonum = 0; void setup() { Serial.begin(9600); Serial.println("16 channel Servo test!"); pwm.begin(); pwm.setPWMFreq(FREQUENCY); } int pulseWidth(int angle) { int pulse_wide, analog_value; pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096); Serial.println(analog_value); return analog_value; } void loop() { pwm.setPWM(0, 0, pulseWidth(0)); delay(1000); pwm.setPWM(0, 0, pulseWidth(120)); delay(500); pwm.setPWM(0, 0, pulseWidth(90)); delay(1000); }
Rate the Project
Did you find this page useful? Help us to improve by rating this page.
Can we have a generic tutorial not using adafruits code as that does not compile under all Arduino platforms? they built it to support hardware they make so will not compile under digispark attiny85 and pro
Did some further hacking and turns out that it is possible to mould the library for digispark Arduino platform.
Add to the Adafruit_PWMServoDriver.h header file:
#ifdef ARDUINO_AVR_DIGISPARK
#define TwoWire USI_TWI
#endif
And since the USI chip does not clock the I2C in hardware it does not have a need for
Wire.setClock(400000);
so does not include the method in its Wire.cpp and Wire.h
One can either conditionally comment out the above statement in the examples OR add dummy method to the digistump Wire.cpp and Wire.h
void USI_TWI::setClock(uint32_t clock) { // digispark Attiny boards do not have this feature!
// twi_setFrequency(clock);
}
Obviously it is likely some syntax may get trashed by comment filtering rules but hopefully enough information will survive to be usable without too much thought.