Skip to content

Commit 40fa216

Browse files
committed
updated servodemo
1 parent ded0d04 commit 40fa216

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

examples/servo/servo.ino

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
3333
// have!
3434
#define SERVOMIN 150 // This is the 'minimum' pulse length count (out of 4096)
3535
#define SERVOMAX 600 // This is the 'maximum' pulse length count (out of 4096)
36-
#define USMIN 611 // This is the rounded 'minimum' microsecond length based on the minimum pulse of 150
37-
#define USMAX 2441 // This is the rounded 'maximum' microsecond length based on the maximum pulse of 600
38-
#define FREQ 60 // Analog servos run at ~60 Hz updates
36+
#define USMIN 600 // This is the rounded 'minimum' microsecond length based on the minimum pulse of 150
37+
#define USMAX 2400 // This is the rounded 'maximum' microsecond length based on the maximum pulse of 600
38+
#define SERVO_FREQ 60 // Analog servos run at ~60 Hz updates
3939

4040
// our servo # counter
4141
uint8_t servonum = 0;
@@ -45,8 +45,11 @@ void setup() {
4545
Serial.println("8 channel Servo test!");
4646

4747
pwm.begin();
48-
49-
pwm.setPWMFreq(FREQ); // Analog servos run at ~60 Hz updates
48+
// In theory the internal oscillator is 25MHz but it really isn't
49+
// that precise. You can 'calibrate' by tweaking this number till
50+
// you get the frequency you're expecting!
51+
pwm.setOscillatorFrequency(27000000); // The int.osc. is closer to 27MHz
52+
pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~60 Hz updates
5053

5154
delay(10);
5255
}
@@ -57,7 +60,7 @@ void setServoPulse(uint8_t n, double pulse) {
5760
double pulselength;
5861

5962
pulselength = 1000000; // 1,000,000 us per second
60-
pulselength /= FREQ; // Analog servos run at ~60 Hz updates
63+
pulselength /= SERVO_FREQ; // Analog servos run at ~60 Hz updates
6164
Serial.print(pulselength); Serial.println(" us per period");
6265
pulselength /= 4096; // 12 bits of resolution
6366
Serial.print(pulselength); Serial.println(" us per bit");
@@ -94,6 +97,6 @@ void loop() {
9497

9598
delay(500);
9699

97-
servonum ++;
100+
servonum++;
98101
if (servonum > 7) servonum = 0; // Testing the first 8 servo channels
99-
}
102+
}

0 commit comments

Comments
 (0)