Skip to content

Instantly share code, notes, and snippets.

@Injac
Last active February 5, 2021 22:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Injac/ddf7459038bd2e4a6bd5 to your computer and use it in GitHub Desktop.
Save Injac/ddf7459038bd2e4a6bd5 to your computer and use it in GitHub Desktop.
Drive the SM-S2309S (the one coming with the Arduino Kit) Servo using the Adafruit 16x12 bit Servo Shield
/***************************************************
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 110 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 520 // this is the 'maximum' pulse length count (out of 4096)
// our servo # counter
uint8_t servonum = 0;
uint16_t pulsedegree = 0;
//Run the show by pulse between SERVOMIN and SERVOMAX or by pulse/degree
//#define BYDEGREE
#define BYPULSE
void setup() {
Serial.begin(9600);
Serial.println("16 channel Servo test!");
#ifdef ESP8266
Wire.pins(2, 14); // ESP8266 can use any two pins, such as SDA to #2 and SCL to #14
#endif
pwm.begin();
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
yield();
}
// 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. its not precise!
void setServoPulse(uint8_t n, double pulse) {
double pulselength;
pulselength = 1000000; // 1,000,000 us per second
pulselength /= 60; // 60 Hz
Serial.print(pulselength); Serial.println(" us per period");
pulselength /= 4096; // 12 bits of resolution
Serial.print(pulselength); Serial.println(" us per bit");
pulse *= 1000;
pulse /= pulselength;
Serial.println(pulse);
pwm.setPWM(n, 0, pulse);
}
void loop() {
#ifdef BYPULSE
Serial.println("Degrees counting up:");
for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
delay(5);
pwm.setPWM(0, 0, pulselen);
}
Serial.println("Degrees pulse counting down:");
for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) {
delay(5);
pwm.setPWM(0, 0, pulselen);
}
delay(500);
#endif
#ifdef BYDEGREE
for(uint8_t degrees = 1; degrees <=180;degrees++)
{
pulsedegree = map(degrees, 0, 180, SERVOMIN, SERVOMAX);
pwm.setPWM(0, 0, pulsedegree);
delay(1);
Serial.println(degrees);
}
for(uint8_t degrees = 180; degrees >=1;degrees--)
{
pulsedegree = map(degrees, 0, 180, SERVOMIN, SERVOMAX);
pwm.setPWM(0, 0, pulsedegree);
delay(1);
Serial.println(degrees);
}
delay(500);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment