Arduino code and Video for PCA9685 16 channel 12 bits servo controller V1
This is the Arduino code for PCA6985 16 channel 12 bits servo controller V1
This video shows you how to use PCA6985 servo controller module for arduino.this is the code is V1 to controll one or more servos using PCA6895. Modify it so it is used for your purpose. This code have a mappting methode so you enter your desired angle and it will convert it to appropriate pulse width. you will use pwm.setPWM(servo_number, 0, angleToPulse(angle) );
replace servo_number with the connector number like 0 ,2, up to 15 and the pulse is the value between 150 to 600 in this example. The angle is the a value between 0 to 180 degrees.
Other Videos on PCA9685
-V1 Video and code on PCA9685 (introduction and calibration) -V2-1 Video and code on PCA9685 Servo Control ONE-By-ONE -V2-2 Video and code on PCA9685 -V2-3 Video and code on PCA9685 -V3 Contro 32 Servo Motor with PCA9685 and Arduino -V4 Video and code on PCA9685 32 Servo with ESP32Resources for this sketch
- Download PCA6985 Datasheet (pdf)
- PAC6985 Library (zip file) (from Robojax.com)
- Extra information on PCA9685 (pdf)
- PAC9685 Library (from GetHub)
- Leran Arduino in 30 Minuetes (video)
- Learn Arduino step by step from beginner to Advanced (Course)
- Get Early Access to my videos via Patreon and your name appears on all my videos
/*
* Original sourse: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
* This is the Arduino code PAC6985 16 channel servo controller
* watch the video for details and demo http://youtu.be/y8X9X10Tn1k
* *
* Watch video for this code:
*
* Related Videos
V5 video of PCA9685 32 Servo with ESP32 with WiFi https://youtu.be/bvqfv-FrrLM
V4 video of PCA9685 32 Servo with ESP32 (no WiFi): https://youtu.be/JFdXB8Za5Os
V3 video of PCA9685 how to control 32 Servo motors https://youtu.be/6P21wG7N6t4
V2 Video of PCA9685 3 different ways to control Servo motors: https://youtu.be/bal2STaoQ1M
V1 Video introduction to PCA9685 to control 16 Servo https://youtu.be/y8X9X10Tn1k
* Written by Ahmad Shamshiri for Robojax Video channel www.Robojax.com
* Date: Dec 16, 2017, in Ajax, Ontario, Canada
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: this code is "AS IS" and for educational purpose only.
* this code has been downloaded from http://robojax.com/learn/arduino/
*
*/
/***************************************************
This is an example for our Adafruit 16-channel PWM & Servo driver
Servo test - this will drive 16 servos, one after the other
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/815
These displays use I2C to communicate, 2 pins are required to
interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
#define SERVOMIN 125 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 575 // this is the 'maximum' pulse length count (out of 4096)
// our servo # counter
uint8_t servonum = 0;
void setup() {
Serial.begin(9600);
Serial.println("16 channel Servo test!");
pwm.begin();
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
//yield();
}
// the code inside loop() has been updated by Robojax
void loop() {
for( int angle =0; angle<181; angle +=20){
delay(500);
pwm.setPWM(0, 0, angleToPulse(angle) );
}
delay(1000);
}
/*
* angleToPulse(int ang)
* gets angle in degree and returns the pulse width
* also prints the value on seial monitor
* written by Ahmad Nejrabi for Robojax, Robojax.com
*/
int angleToPulse(int ang){
int pulse = map(ang,0, 180, SERVOMIN,SERVOMAX);// map angle of 0 to 180 to Servo min and Servo max
Serial.print("Angle: ");Serial.print(ang);
Serial.print(" pulse: ");Serial.println(pulse);
return pulse;
}
PCA6985 16 channel 12 bits servo controller simple code
this is the code to controll one or more servos using PCA6895. Modify it so it is used for your purpose. This code doesn't give you the option of selecting angle. you will enter the pulse width in the method pwm.setPWM(servo_number, 0, pulse );
replace servo_number with the connector number like 0 ,2, up to 15 and the pulse is the value between 150 to 600 in this example.
http://robojax.com/learn/arduino/
/*
* Original sourse: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
* This is the Arduino code PAC6985 16 channel servo controller
* watch the video for details and demo http://youtu.be/y8X9X10Tn1k
* *
* Written by Ahmad Nejrabi for Robojax Video channel www.Robojax.com
* Date: Dec 15, 2017, in Ajax, Ontario, Canada
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: this code is "AS IS" and for educational purpose only.
*
*/
/***************************************************
This is an example for our Adafruit 16-channel PWM & Servo driver
Servo test - this will drive 16 servos, one after the other
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/815
These displays use I2C to communicate, 2 pins are required to
interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
#define SERVOMIN 125 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 575 // this is the 'maximum' pulse length count (out of 4096)
// our servo # counter
uint8_t servonum = 0;
void setup() {
Serial.begin(9600);
Serial.println("16 channel Servo test!");
pwm.begin();
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
//yield();
}
// the code inside loop() has been updated by Robojax
void loop() {
pwm.setPWM(0, 0, 125 );
delay(500);
pwm.setPWM(0, 0, 255 );
delay(500);
pwm.setPWM(0, 0, 450 );
delay(500);
pwm.setPWM(0, 0, 575 );
delay(500);
}