0% found this document useful (0 votes)
85 views2 pages

Pca 9685

This document contains code to control a servo motor using an Adafruit PWMServoDriver. It defines constants for the minimum and maximum servo pulse widths, initializes the PWM driver at 50Hz, and includes a function to set the pulse width in microseconds to control servo position. The main loop reads serial input to set the pulse width on a specific servo channel.

Uploaded by

Thinh Hoang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views2 pages

Pca 9685

This document contains code to control a servo motor using an Adafruit PWMServoDriver. It defines constants for the minimum and maximum servo pulse widths, initializes the PWM driver at 50Hz, and includes a function to set the pulse width in microseconds to control servo position. The main loop reads serial input to set the pulse width on a specific servo channel.

Uploaded by

Thinh Hoang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Wire.

h>
#include <Adafruit_PWMServoDriver.h>
int pulse_;
int sv_;
int last_pulse;
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVOMIN 150 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 600 // This is the 'maximum' pulse length count (out of 4096)
#define USMIN 600 // This is the rounded 'minimum' microsecond length based on the
minimum pulse of 150
#define USMAX 2400 // This is the rounded 'maximum' microsecond length based on
the maximum pulse of 600
#define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates

// our servo # counter


uint8_t servonum = 0;

void setup() {
Serial.begin(9600);
Serial.println("4 channel Servo test!");

pwm.begin();
pwm.setOscillatorFrequency(27000000);
pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates

delay(10);
}

// You can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. It's not precise!
void setServoPulse(uint8_t n, double pulse) {
double pulselength;

pulselength = 1000000; // 1,000,000 us per second


pulselength /= SERVO_FREQ; // Analog servos run at ~60 Hz updates
Serial.print(pulselength); Serial.println(" us per period");
pulselength /= 4096; // 12 bits of resolution
Serial.print(pulselength); Serial.println(" us per bit");
pulse *= 1000000; // convert input seconds to us
pulse /= pulselength;
Serial.println(pulse);
pwm.setPWM(n, 0, pulse);
}

void loop() {
// Drive each servo one at a time using setPWM()
if(Serial.available())
{
String c = Serial.readString();
int ind=c.indexOf("s");
if(ind != -1)
{
Serial.println("ind:"+String(ind)+String(c[ind+1]));
int sv_ = c[ind+1]-(char)('0');
int pulse_ = c.substring(ind+2).toInt();
Serial.println("sv"+String(sv_)+" Pulse: "+String(pulse_));
pwm.setPWM(sv_, 0, pulse_);
}
}

You might also like