Motor shield code: using pins 9 and 10 for NOT servo

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
strictlyrude27
 
Posts: 2
Joined: Fri Oct 28, 2011 6:24 pm

Motor shield code: using pins 9 and 10 for NOT servo

Post by strictlyrude27 »

Hi all -

Sorry if this is in the wrong place - this is more of a software question than a hardware question I suppose, but it fits in with the theme of "shields". Please move if appropriate.

I'm using the ArduinoIO API provided by The MathWorks (http://www.mathworks.com/academia/ardui ... atlab.html) to run two motors and a servo on my Arduino via MATLAB. This package is supposed to be compatible with the Ladyada motor shield.

I hooked up a motor PWM output to pin 9 and a servo to pin 10; I get some strange behavior when I do so - the PWM output on pin 9 always saturates to 5V or 0V, as if it was doing digital writes rather than analog:

Code: Select all

a = arduino('/dev/ttyUSB0');
a.pinMode(9, 'output');
a.analogWrite(9, 255); % 5V output
a.analogWrite(9, 200); % approximately 4V output as seen from a DMM
a.servoAttach(1); % attach servo to PWM pin 10 - suddenly, pin 9's output jumps to 5V
a.analogWrite(9, 200); % instead of 4V output, now gives 0V output
a.analogWrite(9, 255); % 5V output
a.analogWrite(9, 254); % 0V output
a.servoDetach(1); % behavior still stays
One of the maintainers of the ArduinoIO package at The MathWorks said that this is somewhat by design with the shield - the shield was meant to either provide standard PWM outputs via pins 9 and/or 10; OR use a servo on pins 9 and/or 10; but not both. I.e., undefined behavior occurs when I try to use PWM on pin 9 with a servo attached to pin 10.

Is this true, and if so why is this? I have a PCB that is using pin 9 for motor control and would prefer not to cut a trace and solder on extra wires if that's at all possible, so I'd rather fix this in software rather than hardware.

Thanks!

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

Re: Motor shield code: using pins 9 and 10 for NOT servo

Post by adafruit_support_bill »

One of the maintainers of the ArduinoIO package at The MathWorks said that this is somewhat by design with the shield - the shield was meant to either provide standard PWM outputs via pins 9 and/or 10; OR use a servo on pins 9 and/or 10; but not both. I.e., undefined behavior occurs when I try to use PWM on pin 9 with a servo attached to pin 10.
Sorry, but that is not the case. The shield just passes these pins through to the servo headers. There are no active components in the path and nothing on the shield that would create any kind of dependency between them. You can refer the person at the MathWorks to the schematics here: https://github.com/adafruit/Adafruit-Mo ... or-Arduino

strictlyrude27
 
Posts: 2
Joined: Fri Oct 28, 2011 6:24 pm

Re: Motor shield code: using pins 9 and 10 for NOT servo

Post by strictlyrude27 »

adafruit_support wrote:
One of the maintainers of the ArduinoIO package at The MathWorks said that this is somewhat by design with the shield - the shield was meant to either provide standard PWM outputs via pins 9 and/or 10; OR use a servo on pins 9 and/or 10; but not both. I.e., undefined behavior occurs when I try to use PWM on pin 9 with a servo attached to pin 10.
Sorry, but that is not the case. The shield just passes these pins through to the servo headers. There are no active components in the path and nothing on the shield that would create any kind of dependency between them. You can refer the person at the MathWorks to the schematics here: https://github.com/adafruit/Adafruit-Mo ... or-Arduino
Sorry, I should clarify - this is a software issue and not a hardware issue. Something to do with the AFMotor library, perhaps?

I have some other diagnostics to run (gonna try to see if the same behavior happens in C). If this is just an issue with the MATLAB API I can get it fixed.

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

Re: Motor shield code: using pins 9 and 10 for NOT servo

Post by adafruit_support_bill »

Servos are not controlled via the AFMotor library. They use the Servo library that is shipped with the Arduino IDE. I checked further and what you are seeing is a limitation of that library on boards other than the Mega.

From the Servo Library documentation:
On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.

User avatar
mojotexas
 
Posts: 4
Joined: Wed Feb 01, 2012 4:02 pm

Re: Motor shield code: using pins 9 and 10 for NOT servo

Post by mojotexas »

I have a motor shield like this and it requires PWM on pins 9 and 10 to control speed of the motors.
I also want to add a Servo to my project. How can I have a servo and keep PWM on 9 and 10? Surely there is a way for a motor shield to live in harmony with a servo?

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

Re: Motor shield code: using pins 9 and 10 for NOT servo

Post by adafruit_support_bill »

I have a motor shield like this and it requires PWM on pins 9 and 10 to control speed of the motors.
The Adafruit MotorShield does not require PWM in pins 9 and 10 to control the speed of the motors. These pins are available for use with servos.

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

Return to “Arduino Shields from Adafruit”