PWM/SERVO DRIVER SHIELD Sketches
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- Olie426
- Posts: 8
- Joined: Fri Jul 20, 2018 4:50 pm
PWM/SERVO DRIVER SHIELD Sketches
Hello all. I am new to Arduino and have only used my Uno for very basic tasks. I am trying to run two servos that are controlled by an on-off-on toggle via the PWM/Servo shield. I am able to successfully run them straight from the Uno board but I wanted to add the shield so down the road I can run more servos and LEDs. I installed theAdafruit_PWMServoDriver.h and tried to make sense of it, I'm lost. Can someone point me in a direction to where I might be able to see or use as a template sketch that runs this shield? I have been unable to find anything searching the forums that made sense to me. I know using the shield is overkill but I need to learn this stuff for future upgrades.
- Franklin97355
- Posts: 23940
- Joined: Mon Apr 21, 2008 2:33 pm
Re: PWM/SERVO DRIVER SHIELD Sketches
Have you read through this tutorial yet?
- Olie426
- Posts: 8
- Joined: Fri Jul 20, 2018 4:50 pm
Re: PWM/SERVO DRIVER SHIELD Sketches
Yes, that is where I started.
- Franklin97355
- Posts: 23940
- Joined: Mon Apr 21, 2008 2:33 pm
Re: PWM/SERVO DRIVER SHIELD Sketches
What do you get when you run the servo example?
- Olie426
- Posts: 8
- Joined: Fri Jul 20, 2018 4:50 pm
Re: PWM/SERVO DRIVER SHIELD Sketches
The servo sweeps. If I load the sketch that works with the switch, nothing happens but that is to be expected because it's not set up for the servo shield.
- Franklin97355
- Posts: 23940
- Joined: Mon Apr 21, 2008 2:33 pm
Re: PWM/SERVO DRIVER SHIELD Sketches
Post your code and we will have a look.
- Olie426
- Posts: 8
- Joined: Fri Jul 20, 2018 4:50 pm
Re: PWM/SERVO DRIVER SHIELD Sketches
Code: Select all
#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
// Use single on-off-on toggle to control servo position.
// First position moves from X to Y degrees
// Second position moves from Y to X degrees
int pos = 0; // variable to store the servo position and starting position
int buttonApin = 4;
int buttonBpin = 8;
bool buttonAstatus; // stores button state
bool buttonBstatus;
void setup()
{
Serial.begin(9600);
Serial.print("Running v2.0 of Servo controlled by toggle and Nano.");
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
buttonAstatus = digitalRead(buttonApin);
buttonBstatus = digitalRead(buttonBpin);
Serial.println(buttonAstatus);
Serial.println(buttonBstatus);
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}
void loop()
{
buttonAstatus = digitalRead(buttonApin);
buttonBstatus = digitalRead(buttonBpin);
Serial.println(buttonAstatus);
Serial.println(buttonBstatus);
if (buttonAstatus == LOW)
{
servoLeft.write((digitalRead(10)) ? 75: 145);
servoRight.write((digitalRead(9)) ? 75: 145);
{
Serial.print(pos);
}
}
if (buttonBstatus == LOW)
{
servoLeft.write((digitalRead(10)) ? 145 : 75);
servoRight.write((digitalRead(9)) ? 145 : 75);
{
Serial.print(pos);
}
}
}
- Franklin97355
- Posts: 23940
- Joined: Mon Apr 21, 2008 2:33 pm
Re: PWM/SERVO DRIVER SHIELD Sketches
But have you tried this?
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>
// 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);
// you can also call it with a different address and I2C interface
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(&Wire, 0x40);
// 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 150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 600 // this is the 'maximum' pulse length count (out of 4096)
// our servo # counter
uint8_t servonum = 0;
void setup() {
Serial.begin(9600);
Serial.println("8 channel Servo test!");
pwm.begin();
pwm.setPWMFreq(60); // Analog servos run at ~60 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. 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 *= 1000000; // convert to us
pulse /= pulselength;
Serial.println(pulse);
pwm.setPWM(n, 0, pulse);
}
void loop() {
// Drive each servo one at a time
Serial.println(servonum);
for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
pwm.setPWM(servonum, 0, pulselen);
}
delay(500);
for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) {
pwm.setPWM(servonum, 0, pulselen);
}
delay(500);
servonum ++;
if (servonum > 7) servonum = 0;
}
- Olie426
- Posts: 8
- Joined: Fri Jul 20, 2018 4:50 pm
Re: PWM/SERVO DRIVER SHIELD Sketches
Yes. The servo sweeps. What I want to do is control when, how far and how fast when the on-off-on switch is moved to another position. On the PWM sketch it sweeps the servos one at a time without input from an outside source. Is it simply placing the code:
int buttonApin = 4; int buttonBpin = 8; (I know there's more, just wanted to keep it simple) then soldering pins on the PWM shield to 4 and 8 for the switch? Obviously the servos will no longer be on pin 9 and 10, are they just named servonum 1 and servonum 2?
Sorry for all the questions, once the light bulb goes off and I get the gist of PWM sketches I should be good, just really confusing right now. I'm probably over thinking it.
Thanks again for your help.
int buttonApin = 4; int buttonBpin = 8; (I know there's more, just wanted to keep it simple) then soldering pins on the PWM shield to 4 and 8 for the switch? Obviously the servos will no longer be on pin 9 and 10, are they just named servonum 1 and servonum 2?
Sorry for all the questions, once the light bulb goes off and I get the gist of PWM sketches I should be good, just really confusing right now. I'm probably over thinking it.
Thanks again for your help.
- Olie426
- Posts: 8
- Joined: Fri Jul 20, 2018 4:50 pm
Re: PWM/SERVO DRIVER SHIELD Sketches
I hope this thread hasn't died.....still looking for some guidance.
- Olie426
- Posts: 8
- Joined: Fri Jul 20, 2018 4:50 pm
Re: PWM/SERVO DRIVER SHIELD Sketches
Anyone have any guidance?
- adafruit_support_bill
- Posts: 88155
- Joined: Sat Feb 07, 2009 10:11 am
Re: PWM/SERVO DRIVER SHIELD Sketches
It looks like you have code to read the buttons already working with the Arduino Servo library.
You can replace those calls to Servo.write() with calls to pwm.setPWM(). There is no need to 'attach' a servo to a pin. Simply plug it into the servo header on the shield. The PWM channel for your setPWM calls will be the same as the header position your servo is plugged into.
You can use the Arduino "map()" function to convert between degrees and pulse length:
https://learn.adafruit.com/16-channel-p ... ength-6-12
You can replace those calls to Servo.write() with calls to pwm.setPWM(). There is no need to 'attach' a servo to a pin. Simply plug it into the servo header on the shield. The PWM channel for your setPWM calls will be the same as the header position your servo is plugged into.
You can use the Arduino "map()" function to convert between degrees and pulse length:
https://learn.adafruit.com/16-channel-p ... ength-6-12
- Olie426
- Posts: 8
- Joined: Fri Jul 20, 2018 4:50 pm
Re: PWM/SERVO DRIVER SHIELD Sketches
Sweet! Thank you. I will try it out this evening and see what I can do.
Please be positive and constructive with your questions and comments.