0

Simple code for Adafruit 16-channel PWM/Servo Shield
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Simple code for Adafruit 16-channel PWM/Servo Shield

by Mihailo on Mon Aug 02, 2021 12:43 pm

Hi guys,

I'm trying to find a simple arduino code for moving a servo attached to one of the 16 connectors left and right?

This shield is what I'm talking about - https://www.adafruit.com/product/1411.

The example 'servo' from the library is soooooo complicated.

I don't want to calculate the frequency and pulse length.

All I want is to move it left, then right :)

Do you know where I can find the simplest version of the code for moving the servo (cheap one - SG90), all the way to the right, then all the way to the left, for example.

I've tried the example servo sketch and I think it fried one of my servos (will confirm later).


Thanks, Mihailo :)

Mihailo
 
Posts: 3
Joined: Mon Aug 02, 2021 12:41 pm

Re: Simple code for Adafruit 16-channel PWM/Servo Shield

by dastels on Mon Aug 02, 2021 1:15 pm

Yes, you have to be careful about the limits of movement. Servos differ a bit from one to another about what pulse widths are at either end. Over drive it and there could be damage.

You fail to say what MCU/board/language you are using.

Dave

dastels
 
Posts: 7229
Joined: Tue Oct 20, 2015 3:22 pm

Re: Simple code for Adafruit 16-channel PWM/Servo Shield

by Mihailo on Mon Aug 02, 2021 1:21 pm

Oh, sorry, thought I wrote it.

I'm using this with Arduino Uno, in Arduino IDE.

There is an example sketch 'servo' that comes with the library, but it's a bit of an overkill. Testing 8 servos simultaneously. Lots of variables,parameters and calculations...

All I want is one servo to move. Left. Then right.

?

Thanks :)

Mihailo
 
Posts: 3
Joined: Mon Aug 02, 2021 12:41 pm

Re: Simple code for Adafruit 16-channel PWM/Servo Shield

by Mihailo on Tue Aug 03, 2021 6:05 am

Here, I've found a solution myself (combining what I've found online)

Why nobody wrote it simple like this ???

This is for Arduino IDE, using SG90 small blue cheap servo (parameters for all the way to the left, then wait 1 sec, then all the way to the right, wait 1 sec).

Servo is attached to the port 0 on the shield. For other ports just change first 0 in pwm.setPWM(0, 0,100) to the port you want.

Hope this helps someone! :)

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

void setup() {
  Serial.begin(9600);
  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
}

void loop() {
    pwm.setPWM(0, 0,100);
    delay(1000);
    pwm.setPWM(0, 0, 650);
    delay(1000);
}


Take care, everyone! :)

Mihailo
 
Posts: 3
Joined: Mon Aug 02, 2021 12:41 pm

Please be positive and constructive with your questions and comments.