Adafruit PCA9685 PWM Servo Driver Setup, Arduino Library Use Shown With an Example Project - Tutorial

in #utopian-io3 years ago (edited)

Introduction

This is a tutorial of an open source Arduino library, which lets you use multiple (up to 16) servo motors with your Arduino so that you can make high count servo required projects. I will show you how to install the library to Arduino IDE and make an example project with the library, throughout this tutorial while giving information servo motors and their working principle.

What Will I Learn?

In this tutorial you will learn:

  • General knowledge about servo motors.
  • How to install Adafruit PCA9685 PWM Servo Driver library to Arduino IDE.
  • How to use Adafruit PCA9685 PWM Servo Driver library and how to use It’s functions.
  • How to control multiple servo motors using the library functions.

Requirements

This tutorials requirements are:

  • Arduino UNO or equivalent microprocessor.
  • Arduino IDE (Download here)
  • Adafruit PCA9685 PWM Servo Driver Arduino library (Download here)
  • Adafruit PCA9685 PWM Servo Driver .
  • Servo motors (as much as you want, for the example project we’ll use 4), 3 Jumper cables for each servo, 5 Volt power supply.

Difficulty

This tutorials difficulty is:

  • Intermediate


Tutorial Contents

What is a Servo Motor and How It Works

Servo motors are electric motors that are very efficient, compact and low cost. Servo motors are made of a DC motor, potentiometer, a gear mechanism, and a control circuit. A servo motors working principle could be described as this: DC motor attached to the gear mechanism rotates the potentiometers knob. As the motor rotates, so does the potentiometers knob, potentiometers resistance value changes, so the control circuit can regulate the movement, both in terms of speed and position. Servo motors are controlled by pulses. This technique is named “Pulse Width Modulation” (PWM). In PWM there is a minimum pulse, a maximum pulse, and a repetition rate. Minimum pulse indicates servo motors 0 degree position, while maximum pulse indicates servo motors 180 degree position. Servo motors are used in hobby toys, elevators, robots, grippers, conveyor belts.

im.jpg

Servo motors are used commonly in hobby projects. Image Source (Royalty free)


How to Install Adafruit PCA9685 PWM Servo Driver Library to Arduino IDE

1. Download the library from https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library.

indir.png

2. Open Arduino IDE. Then go to and select Sketch->Include Library->Add .ZIP Library.

ekle.png

3. Choose Adafruit-PWM-Servo-Driver-Library-master.zip file and click to Open.


An Example Project Using Adafruit PCA9685 PWM Servo Driver Arduino Library

I'll show you step by step how to use the PCA9685 PWM Servo Driver library with a simple example. In this example project, we will control multiple servo motors up to 16, with an Arduino microcontroller, and a PCA9685 PWM Servo Driver module. In order to make this project first we need to wire the servo motors to the driver. After that we’ll need to write and upload a code to control the servo motors to Arduino. See the steps for how to do these actions. Connections for servo motors to PCA9685 PWM Servo Driver module and Arduino is shown below.
If you use this library in your other projects please tell in the comments.

baglanti.png

Connection diagram PCA9685 PWM Servo Driver module to Arduino and servos. Made with Fritzing.

1. Open a new sketch and save it as "Multiple_Servo_Example”.

2. To add our libraries to our sketch, type #include <Wire.h> and #include <Adafruit_PWMServoDriver.h> at the beginning of the code.

1.PNG

3. Add the command that initiates the library functions. Then define two variables for setting the servo motors minimum and maximum pulse lengths respectively. Check your servo motors datasheets for optimal settings. In this project I’ve used 100 for minimum, 2200 for maximum pulse length. After setting the pulse lengths define a integer variable that we will use later for counting process.

2.PNG

4. In the void setup() function, add the code that starts serial connection at 9600 baud rate. Then add the library function that stars the pulse width modulation (PWM) process. Set the servo refreshing rate to 60 Hz and add a slight delay for making sure that setup is done before any servo movement.

3.PNG

5. In the void loop() function, add two for loops for driving each servo (one at a time) to maximum pulse length then to minimum pulse length. This will move the servos to clockwise direction, then to anti clockwise direction. Add a delay after each for loop to move each servo at different times. Then add the code that proceeds to the next servos once a servo finishes It’s movement. Make that by an If statement. Change the number to add or decrease the servo amount.

4.PNG

6. Click “Verify” and then “Upload” in order to compile and execute your codes. You should see the servo motors running in sequence. Make sure your COM port and board setting is set right and power supply is on. Please double check the ground connections before running the code.
upload.png


Conclusion

In this tutorial I’ve shown how to install “Adafruit PCA9685 PWM Servo Driver” Arduino library, written by GitHub user “adafruit” to Arduino IDE, showing how to use the library functions with an example, while giving information about servo motors.
I hope that you enjoyed this tutorial and the information that I’ve given. Thank you for reading.
If you want more information about the library and the source use the link below.

Github: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library



Code

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();   // Initiates library.

#define SERVOMIN  100  // Minimum pulse length count out of 4096.
#define SERVOMAX  2200 // Maximum pulse length count out of 4096.

int servoNo = 0; // Defines a counter for servos.


void setup() 
{
  Serial.begin(9600);       // Starts serial connecton at 9600 baud rate.
  
  pwm.begin();         // Sends PWM signals.
  pwm.setPWMFreq(60);  // Makes servos run at 60 Hz rate.
  delay(20);
}


void loop() 
{   
  
  for (int pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) // Drives each servo one at a time first                                                                                                
    pwm.setPWM(servoNo, 0, pulselen);                            // to maximum pulse length then to minimum pulse length.
  delay(300);
  
  for (int pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--)
    pwm.setPWM(servoNo, 0, pulselen);
  delay(300);

  servoNo ++;            // Proceeds to next servo.
  if (servoNo > 3) servoNo = 0;
} 


Previous Tutorials



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @drencolha I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.16
TRX 0.03
JST 0.026
BTC 13126.84
ETH 412.83
USDT 1.00
SBD 1.00