Connecting a servo to a mega 2560 - a newbie's problem

Hello, I hope someone will read and help... I'm some what new to arduinos, and most of programs have been on the UNO. But since I upgraded to a mega 2560, I've been running onto trouble. Mostly pin based, hopefully.

I am connecting a small EMAX servo to the 2560, with the red connected to 5V, the brown connected to gnd, and the orange connected to analog A0. The program is below. The servo makes the little noise when I connect power, so I assume the 5V and gnd is right... I'm looking at the schematic and I think the pin number is 78. It worked on the UNO with pin 0, so why not now??? =(

#include <Servo.h>

Servo test;
int pos = 0; // Try floats next time.
int PIN = 78;

void setup()
{
test.attach(PIN);
test.write(pos);
}

void loop()
{
pos = 20;
test.write(pos);
pos = -20;
test.write(pos);
}

I believe you are associating the wrong digital pin number for the analog input pin 0 number as used in mega boards.

So instead of int PIN = 78;
you should be using int PIN = 54; if wired to the A0 pin.
But better yet and to avoid errors in the future using the analog pins for digital purposes simply
use int PIN = A0;

Here is the association of analog pin names to there corresponding digital pin names:

static const uint8_t A0 = 54;
static const uint8_t A1 = 55;
static const uint8_t A2 = 56;
static const uint8_t A3 = 57;
static const uint8_t A4 = 58;
static const uint8_t A5 = 59;
static const uint8_t A6 = 60;
static const uint8_t A7 = 61;
static const uint8_t A8 = 62;
static const uint8_t A9 = 63;
static const uint8_t A10 = 64;
static const uint8_t A11 = 65;
static const uint8_t A12 = 66;
static const uint8_t A13 = 67;
static const uint8_t A14 = 68;
static const uint8_t A15 = 69;

These are defined in the specific Arduino core file pins_arduino.h that is used when you select a mega board.

Lefty

For the UNO you should NOT have used pin 0 that's used for serial i/o.

For the mega try setting the PIN to output and pick a pin you are sure you can find.

Mark

// Try floats next time.

Why? The Servo write method takes "int"s.

And not that there is anything wrong with it, but why are you trying to use an analog input pin for digital servo output in the first place? A pin is a pin, and properly set up, it doesn't really matter, but you seemed to pick it specifically for some purpose. Just curious.

To start, let me just say I'm new to forums and wow. Just wow. I left for dinner and so many replies. I'm really surprised and kinda touched. Thank you all who replied. Now back to business.

retrolefty: That worked. Thank you. But out of curiosity, what keeps arduino from freaking out about A0 being used as an int?

holmes4: Thanks for the reply. But the "int PIN = A0;" worked. But curious, why not use serial i/o? Sorry if this is a silly question.

AWOL: I'm trying (eventually) to use the servo as a yaw vane for a UAV and I might need a angle that wasn't an integer. I don't think it makes a difference, but I thought I'd try it. It does work, so good to know.

retroplayer: No real reason for analog vs digital. At least none I know. Its the way the tutorial I was basing my code from was setup. Here's the site if interested. Arduino Playground - SingleServoExample

gln6fgsl:
To start, let me just say I'm new to forums and wow. Just wow. I left for dinner and so many replies. I'm really surprised and kinda touched. Thank you all who replied. Now back to business.

retrolefty: That worked. Thank you. But out of curiosity, what keeps arduino from freaking out about A0 being used as an int?

Because A0 is an int compatible constant which was already assigned the value 54 in that code snippet I posted earlier.

Lefty

holmes4: Thanks for the reply. But the "int PIN = A0;" worked. But curious, why not use serial i/o? Sorry if this is a silly question.

AWOL: I'm trying (eventually) to use the servo as a yaw vane for a UAV and I might need a angle that wasn't an integer. I don't think it makes a difference, but I thought I'd try it. It does work, so good to know.

retroplayer: No real reason for analog vs digital. At least none I know. Its the way the tutorial I was basing my code from was setup. Here's the site if interested. Arduino Playground - HomePage

holmes4: Thanks for the reply. But the "int PIN = A0;" worked. But curious, why not use serial i/o? Sorry if this is a silly question.

Pins 0 and 1 aka D0 and D1 are already being used by Serial. So you can't/should not reuse them.

Mark

retrolefty:

gln6fgsl:
To start, let me just say I'm new to forums and wow. Just wow. I left for dinner and so many replies. I'm really surprised and kinda touched. Thank you all who replied. Now back to business.

retrolefty: That worked. Thank you. But out of curiosity, what keeps arduino from freaking out about A0 being used as an int?

Because A0 is an int compatible constant which was already assigned the value 54 in that code snippet I posted earlier. A0 is the name of the constant, not it's value.

Lefty

holmes4: Thanks for the reply. But the "int PIN = A0;" worked. But curious, why not use serial i/o? Sorry if this is a silly question.

AWOL: I'm trying (eventually) to use the servo as a yaw vane for a UAV and I might need a angle that wasn't an integer. I don't think it makes a difference, but I thought I'd try it. It does work, so good to know.

Actually the servo.write(degrees) is a servo library abstraction for command movement as most/many servos are capable of better resolution then 0-180 degrees. There is a servo.writeMicroseconds(xxxx) command that will allow use of better servo positing command. All servos will respond to xxxx = to 1000 to 2000 range, but most will allow 'over travel' in both lower and upper values, but that is servo model specific and until you test your servo you won't know what it's largest possible physical travel limits are. But to be sure servo.writeMicroseconds(xxxx) is the better method to use with servos if best resolution results are needed.
Lefty

retroplayer: No real reason for analog vs digital. At least none I know. Its the way the tutorial I was basing my code from was setup. Here's the site if interested. Arduino Playground - HomePage

The A0 worked wonderfully. But when I was playing around with it, I noticed the program works for A0-A9, but not A10-A15. So now I'm wondering why not? I don't need it for my project, but I am curious.

I have not checked, but a good look at the ATMega data sheet is in order?

Mark

gln6fgsl:
The A0 worked wonderfully. But when I was playing around with it, I noticed the program works for A0-A9, but not A10-A15. So now I'm wondering why not? I don't need it for my project, but I am curious.

Maybe the servo library being written for the Uno doesn't expect these pins to be present? There is no logical reason they wouldn't work unless you broke something in your code trying to switch to those pins, or the library doesn't acknowledge those pins for some reason.

Scratch that... I don't see anything in the library that would prevent this. Are you using the new version of the library or the old one?

Retroplayer:

gln6fgsl:
The A0 worked wonderfully. But when I was playing around with it, I noticed the program works for A0-A9, but not A10-A15. So now I'm wondering why not? I don't need it for my project, but I am curious.

Maybe the servo library being written for the Uno doesn't expect these pins to be present? There is no logical reason they wouldn't work unless you broke something in your code trying to switch to those pins, or the library doesn't acknowledge those pins for some reason.

Yes, I suspect the answer is buried in the details of the servo library as to which pins a mega can use to drive servos. The reference page only gives partial information:

The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. 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.

Lefty

Yep. Up to 48 servos (the whole groups of 12 thing) there is 54 I/O lines on the mega. And A10-A15 just happens to be 6 pins.

Cool. Thank you all for the help. But which library are you guys getting the pin number from?

The default <Servo.h> doesn't support any pins after A7.

Best is to swap your servo pin with something else so it can be addressed from A0-A7.

That's how I made it to work on my Mega.