0

Here's what caused the original confusion. As one know, Arduino nano and UNO use analog pin as PWM pins, while Arduino Mega use digital pin output as the PWM pins, which this webpage ignored to mention. (https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/) But by reading the pin diagram here, https://www.arduino.cc/en/Hacking/PinMapping2560 the PWM pins were then specified.

However, I experienced the following things which didn't make quite sense. The board I used was a Mega board. First I tried to use this code to control an ESC, where I assumed 6 to be A6, the same as that's being used for nano.

#include <Servo.h>
Servo ESC;
ESC.attach(6, 1000, 2000); 

As you could thought of, this resulted multiple failure, eventually I though of trying another pin(A8), so I write.

ESC.attach(8, 1000, 2000); 

Guess what, it worked! Which was more crazy, when I switched the code back to

ESC.attach(6, 1000, 2000); 

It also worked! So I bumped into an assumption that the pin 6 here automatically selected analog pin! I uploaded the code several times with minor adjustments, it all worked.

But then, when I tried to add more ESC. It just suddenly stopped working. After two days, when I eventually realized that pin 6 might not meant pin 6, I adjusted the code into

ESC.attach(A6, 1000, 2000); 

all the sudden, it worked again!

Could you explain to me what exactly was happening? How does the Servo select the pin on different boards? (i.e. on a nano board,

ESC.attach(6, 1000, 2000);

seemed to be locking into analog pin directly. But, on Mega board, what exactly was it doing?) Was it necessary to specify analog pin as A# at all? Why

ESC.attach(6, 1000, 2000);

worked for A6 on a Mega board on the first place?

  • UNO use analog pin as PWM pins ... they are not really analog ... they are PWM – jsotola Feb 5 at 2:51
  • I don't think that the very same source code at one time compiles into something working and at another time compiles differently into something not working. You will get the same binary each time from the same source. -- So it must be something else, like a logical/semantical error in the source code, or a hardware problem. -- The pin names like A6 are mapped on integers constants, it does not have to be 6 for A6. And these integer constants may map to different pin numbers on different boards. – the busybee Feb 5 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.