Arduino & 16 Channel servo shield

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
simsj2
 
Posts: 7
Joined: Mon Oct 15, 2018 1:55 pm

Arduino & 16 Channel servo shield

Post by simsj2 »

This is my very first electronics project, so please make no mistake, I don't really know what I am doing. Keeping things simple and to the point will go a long way.

I want to make a pumpkin that has multiple eyes that move back and forth randomly using servos. I have made some progress in completing this project. However I have a concern about the Arduino & servo shield interaction.

Are these plug and play? Meaning after uploading the code to the Adruino will it automatically tell the servo shield what to do? Or will I need to tell Arduino to recognize the shield then tell it what to do, therefore changing the structure of my coding?

Here is what I have done so far:

1) I have run my coding through tinker cad circuit simulator and everything has worked out so far.
2) Put my coding into the Arduino Online Code Editor
3) Checked to make sure there were no errors in the code, everything came up green.
4) Uploaded the code to the Arduino.
5) Waiting for my power packs to be delivered.


Here are the Items I have aquired:

(1) Arduino R3
(1) Adafruit 16 channel servo shield
(1) 4 AA Powerpack with Leads / (1) 9v battery hookup with leads
(10) Servos
The random degree generator coding
All of the other physical components.

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino & 16 Channel servo shield

Post by adafruit_support_bill »

will I need to tell Arduino to recognize the shield then tell it what to do, therefore changing the structure of my coding?
Since you have not posted your code, we have no idea what the structure of it is. But like any piece of hardware, your program needs to tell the processor what device to send the commands to.

The best place to start is by reading the guide: https://learn.adafruit.com/adafruit-16- ... it-library

User avatar
Franklin97355
 
Posts: 23911
Joined: Mon Apr 21, 2008 2:33 pm

Re: Arduino & 16 Channel servo shield

Post by Franklin97355 »

Here is the tutorial for the servo shield that should get you started. https://learn.adafruit.com/adafruit-16- ... rvo-shield

User avatar
simsj2
 
Posts: 7
Joined: Mon Oct 15, 2018 1:55 pm

Re: Arduino & 16 Channel servo shield

Post by simsj2 »

Thanks for the replies, I felt that is how the two boards would communicate. But am unsure of my ability to tell them what to do. I looked at the guide you linked me, I will look at it some more this evening. Here is the code that I have. To help you better understand what I am working with. Thank you for your time.

Code: Select all

#include <Servo.h>  //this is a servo library 

Servo servo1; // create servo objects to control multiple servos
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;
Servo servo8;
Servo servo9;
Servo servo10;
Servo servo11;
Servo servo12;           
int pos = 90;    // variable to store the servo position with a starting position of 90 degrees
                       // so the eyes go to the middle at startup  

 void setup() 
{ 
  servo1.attach(2);               // attaches the servos on pins 2 - 13 to the servo objects 
  servo2.attach(3);
  servo3.attach(4);
  servo4.attach(5);
  servo5.attach(6);
  servo6.attach(7);
  servo7.attach(8);
  servo8.attach(9);
  servo9.attach(10);
  servo10.attach(11);
  servo11.attach(12);
  servo12.attach(13);
  servo1.write(pos);              // tell all servos to go to middle position (90 degrees)
  servo2.write(pos);
  servo3.write(pos);
  servo4.write(pos);
  servo5.write(pos);
  servo6.write(pos);
  servo7.write(pos);
  servo8.write(pos);
  servo9.write(pos);
  servo10.write(pos);
  servo11.write(pos);
  servo12.write(pos);
  delay(700);  
} 
    void loop()                           // for each servo, generate a random number between 10 and 170 and 
    {                                            // assign to pos then write the pos to the servo.  This is done in three groups 
    pos =random(10,170);      // with a delay to make it appear the eyes are not all starting at the same time.
    servo1.write(pos);             //  I use 10 and 170 because the last 10 deg on either side jammed my
    pos = random(10,170);     // mechanism.   Depending on you mech, these might need to be other limits
    servo2.write(pos);             // They don't need to be all the same, each eye can have its own range
    pos = random(10,170);
    servo3.write(pos);
    delay(200);
    pos = random(10,170);
    servo4.write(pos);
    pos = random(10,170);
    servo5.write(pos);
    pos = random(10,170);
    servo6.write(pos);
    pos = random(10,170);
    servo7.write(pos);
    pos = random(10,170);
    servo8.write(pos);
    delay(300);
    pos = random(10,170);
    servo9.write(pos);
    pos = random(10,170);
    servo10.write(pos);
    pos = random(10,170);
    servo11.write(pos);
    pos = random(10,170);
    servo12.write(pos);
    delay(200);
   }

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino & 16 Channel servo shield

Post by adafruit_support_bill »

Code: Select all

#include <Servo.h>  //this is a servo library 
That is the Arduino servo library for controlling servos directly from Arduino pins. That library does not know how to communicate with the servo shield.

To use the Servo shield, you need to use the library designed for it. Links and instructions can be found in the guide: https://learn.adafruit.com/adafruit-16- ... it-library

User avatar
simsj2
 
Posts: 7
Joined: Mon Oct 15, 2018 1:55 pm

Re: Arduino & 16 Channel servo shield

Post by simsj2 »

I had some time to look at the 16 channel servo guide this evening. I downloaded and uploaded the <Adafruit_PWMServoDriver.h> to my sketchbook and included it in my sketch.
After clicking the verify button I am getting a couple or errors. I have included an image of the error and copy of the code.

line 78 & 80 in the code I think... How would you fix that? I attached an image of these at the bottom of the code.

It looks like something is wrong with the original library that was in the code.. should this be deleted?
The errors with servo 11 & 12 should I just delete all instances of those servos?


Again thank you for your time of any input or ideas.

Code: Select all

/* 

Random eye location generator for arduino 

*/

#include <Adafruit_PWMServoDriver.h>

Servo servo1; // create servo objects to control multiple servos
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;
Servo servo8;
Servo servo9;
Servo servo10;
Servo servo11;
Servo servo12;
int pos = 90;    // variable to store the servo position with a starting position of 90 degrees
                       // so the eyes go to the middle at startup
                       
void setup() 
{
  servo1.attach(2);               // attaches the servos on pins 2 - 13 to the servo objects 
  servo2.attach(3);
  servo3.attach(4);
  servo4.attach(5);
  servo5.attach(6);
  servo6.attach(7);
  servo7.attach(8);
  servo8.attach(9);
  servo9.attach(10);
  servo10.attach(11);
  servo11.attach(12);
  servo12.attach(13);
  servo1.write(pos);              // tell all servos to go to middle position (90 degrees)
  servo2.write(pos);
  servo3.write(pos);
  servo4.write(pos);
  servo5.write(pos);
  servo6.write(pos);
  servo7.write(pos);
  servo8.write(pos);
  servo9.write(pos);
  servo10.write(pos);
  servo11.write(pos);
  servo12.write(pos);
  delay(700);  
}
    void loop()                           // for each servo, generate a random number between 10 and 170 and 
    {                                            // assign to pos then write the pos to the servo.  This is done in three groups 
    pos =random(10,170);      // with a delay to make it appear the eyes are not all starting at the same time.
    servo1.write(pos);             //  I use 10 and 170 because the last 10 deg on either side jammed my
    pos = random(10,170);     // mechanism.   Depending on you mech, these might need to be other limits
    servo2.write(pos);             // They don't need to be all the same, each eye can have its own range
    pos = random(10,170);
    servo3.write(pos);
    delay(200);
    pos = random(10,170);
    servo4.write(pos);
    pos = random(10,170);
    servo5.write(pos);
    pos = random(10,170);
    servo6.write(pos);
    pos = random(10,170);
    servo7.write(pos);
    pos = random(10,170);
    servo8.write(pos);
    delay(300);
    pos = random(10,170);
    servo9.write(pos);
    pos = random(10,170);
    servo10.write(pos);
    pos = random(10,170);
    servo11.write(pos);
    pos = random(10,170);
    servo12.write(pos);
    delay(200);
   }
Attachments
arduino help 2.jpg
arduino help 2.jpg (263.75 KiB) Viewed 219 times
arduino help 1.jpg
arduino help 1.jpg (225.56 KiB) Viewed 222 times

User avatar
simsj2
 
Posts: 7
Joined: Mon Oct 15, 2018 1:55 pm

Re: Arduino & 16 Channel servo shield

Post by simsj2 »

I may or may not have solved my error issues. I added the original #include <Servo.h> under the <adafruit_pmwservodriver.h> and that has seemed to have fixed the errors I was getting. Image & code attached.

Code: Select all

/* 

Random eye location generator for arduino 

*/

#include <Adafruit_PWMServoDriver.h>
#include <Servo.h>

Servo servo1; // create servo objects to control multiple servos
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;
Servo servo8;
Servo servo9;
Servo servo10;
Servo servo11;
Servo servo12;
int pos = 90;    // variable to store the servo position with a starting position of 90 degrees
                       // so the eyes go to the middle at startup
                       
void setup() 
{
  servo1.attach(2);               // attaches the servos on pins 2 - 13 to the servo objects 
  servo2.attach(3);
  servo3.attach(4);
  servo4.attach(5);
  servo5.attach(6);
  servo6.attach(7);
  servo7.attach(8);
  servo8.attach(9);
  servo9.attach(10);
  servo10.attach(11);
  servo11.attach(12);
  servo12.attach(13);
  servo1.write(pos);              // tell all servos to go to middle position (90 degrees)
  servo2.write(pos);
  servo3.write(pos);
  servo4.write(pos);
  servo5.write(pos);
  servo6.write(pos);
  servo7.write(pos);
  servo8.write(pos);
  servo9.write(pos);
  servo10.write(pos);
  servo11.write(pos);
  servo12.write(pos);
  delay(700);  
}
    void loop()                           // for each servo, generate a random number between 10 and 170 and 
    {                                            // assign to pos then write the pos to the servo.  This is done in three groups 
    pos =random(10,170);      // with a delay to make it appear the eyes are not all starting at the same time.
    servo1.write(pos);             //  I use 10 and 170 because the last 10 deg on either side jammed my
    pos = random(10,170);     // mechanism.   Depending on you mech, these might need to be other limits
    servo2.write(pos);             // They don't need to be all the same, each eye can have its own range
    pos = random(10,170);
    servo3.write(pos);
    delay(200);
    pos = random(10,170);
    servo4.write(pos);
    pos = random(10,170);
    servo5.write(pos);
    pos = random(10,170);
    servo6.write(pos);
    pos = random(10,170);
    servo7.write(pos);
    pos = random(10,170);
    servo8.write(pos);
    delay(300);
    pos = random(10,170);
    servo9.write(pos);
    pos = random(10,170);
    servo10.write(pos);
    pos = random(10,170);
    servo11.write(pos);
    pos = random(10,170);
    servo12.write(pos);
    delay(200);
   }
With this I was able to get the Servos to move... some not to move.... and some get hung... Is there an easy way to zero out the servos to find the correct angles the arms rotate? Is there a simulator that has the adafruit 16 channel servo driver I can test on somewhere?

If anyone sees something I do not please let me know. Thanks
Attachments
arduino help 3.jpg
arduino help 3.jpg (237.52 KiB) Viewed 211 times
Last edited by simsj2 on Tue Oct 16, 2018 2:17 am, edited 1 time in total.

User avatar
Franklin97355
 
Posts: 23911
Joined: Mon Apr 21, 2008 2:33 pm

Re: Arduino & 16 Channel servo shield

Post by Franklin97355 »

Have you tried the example code in the adafruit_pwm_servo_driver library? Try that first and get that working.

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino & 16 Channel servo shield

Post by adafruit_support_bill »

I added the original #include <Servo.h> under the <adafruit_pmwservodriver.h> and that has seemed to have fixed the errors I was getting. Image & code attached.
Unfortunately, that will not work with the servo shield. You will need to change all of your code to use the correct library. Please read through the guide linked above and follow the instructions there.

User avatar
simsj2
 
Posts: 7
Joined: Mon Oct 15, 2018 1:55 pm

Re: Arduino & 16 Channel servo shield

Post by simsj2 »

Unfortunately, that will not work with the servo shield.
Yes, all of the code must change.....

User avatar
simsj2
 
Posts: 7
Joined: Mon Oct 15, 2018 1:55 pm

Re: Arduino & 16 Channel servo shield

Post by simsj2 »

franklin97355 wrote:Have you tried the example code in the adafruit_pwm_servo_driver library? Try that first and get that working.
Ok, I have spent some time looking / researching on the internet and I have found some code that is working. I have played around with trying to figure out how to create some random numbers to use as angles in the code and this is where I am now stuck. How do you create a random angle generator to work with the shield / this code? Or can it?

Here is the code I have that now works, condensed to one servo..
Link to short capture of my shield & servo rotating https://drive.google.com/open?id=1R1_3Y ... K3TJX8Z_KU

Code: Select all

/*
  This is an example for our Adafruit 16-channel PWM & Servo driver
  Servo test - this will drive 8 servos, one after the other on the
  first 8 pins of the PCA9685

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/products/815
  
  These drivers use I2C to communicate, 2 pins are required to  
  interface.

  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>


Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();


#define MIN_PULSE_WIDTH      650 
#define MAX_PULSE_WIDTH      2350
#define DEFAULT_PULSE_WIDTH  1500 
#define FREQUENCY            50 // 20 millisecond (50 hz) PWM Period


void setup() {
   pwm.begin();
   pwm.setPWMFreq(FREQUENCY);
}

void loop() {                         //Set servo port numbers and commands for direction of movement.
   pwm.setPWM(0, 0, pulseWidth(140));   //Create a command for each servo motor
   delay(1000);
   pwm.setPWM(0, 0, pulseWidth(80));
   delay(1000);
}

int pulseWidth (int angle)
{
   int pulse_wide, analog_value;
   pulse_wide   = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
   analog_value = int (float (pulse_wide) / 1000000 * FREQUENCY * 4096);
   return analog_value;

User avatar
simsj2
 
Posts: 7
Joined: Mon Oct 15, 2018 1:55 pm

Re: Arduino & 16 Channel servo shield

Post by simsj2 »

How do I get the servos to either fire randomly, or one after the other? Either way is fine with me. I just need to make them look random by physically mounting them in different places if need be.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”