Crazy problem with servos

I am using official servo library with 6 servos.

I have got several models of Servos (MG996R, Hitec HS-422, and many more)

Servos attached to pins 6,9,10,11 work OK, any model.
Servos attached to pins 3,5 do not work, except curiously for model HS-422.

Power supply is not problem 5V, 30A. Besides I test little servos, very low amps, that work without problem in pins 6,9,10 11, but they refuse to move in pins 3,5.

If pins 3,5 never worked, maybe the problem would be more traceable, but they work only with hitec servos.

So, I am crazy with this strange problem.

Board Arduino Duemilanove
Shield
smart hammer

Need to see code and wiring diagram

  • servos not powered from the Arduino 5v terminal I hope …. Or directly from a shield without a separate power connection to your external 5v supply ??
1 Like

Shield is simple. 5V input from external power supply for servos and it also powers arduino.

Code is also easy.

#include "Servo.h"
#include <EEPROM.h>


Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;

int potpin1 = 0;//A0
int potpin2 = 1;//A1
int potpin3 = 2;//A2
int potpin4 = 3;//A3
int potpin5 = 4;//A4
int potpin6 = 5;//A5


int va1,vs1;
int va2,vs2;
int va3,vs3;
int va4,vs4;
int va5,vs5;
int va6,vs6;

byte  mode ;
void setup()
{

  servo1.attach(11);               
  servo2.attach(10);  
  servo3.attach(9);               
  servo4.attach(6);
  servo5.attach(5);               
  servo6.attach(3);

  pinMode(potpin1, INPUT); 
  pinMode(potpin2, INPUT); 
  pinMode(potpin3, INPUT); 
  pinMode(potpin4, INPUT); 
  pinMode(potpin5, INPUT); 
  pinMode(potpin6, INPUT); 

  Serial.begin(9600);
}
void loop()


 
  {
    
    va1 = analogRead(potpin1);
    vs1 = map (va1, 0, 1023, 0, 179);
    servo1.write(vs1);
    delay(10);


    va2 = analogRead(potpin2);
    vs2 = map (va2, 0, 1023, 0, 179);
    servo2.write(vs2);
    delay(10);  

    va3 = analogRead(potpin3);
    vs3 = map (va3, 0, 1023, 0, 179);
    servo3.write(vs3);
    delay(10);

    va4 = analogRead(potpin4);
    vs4 = map (va4, 0, 1023, 0, 179);
    servo4.write(vs4);
    delay(10);

    va5 = analogRead(potpin5);
    vs5 = map (va5, 0, 1023, 0, 179);
    servo5.write(vs5);
    delay(10);

    va6 = analogRead(potpin6);
    vs6 = map (va6, 0, 1023, 0, 179);
    servo6.write(vs6);
    delay(10);

  }

You are not defining the pins for analog inputs correctly .

Should be “int potpin1 = AO “ ; same for the others . So in your code , it happens by chance that “3” and “5” are used twice - but you intend one as digital 3 and the other as A3.

Have a look at the analog example in the IDE

1 Like

Exactly. It works now.
However, analog inputs were reading OK when mapping as 0-5. Maybe the problem was pinMode(x, INPUT) that declares as Digital Inputs 3 and 5, instead A3 and A5.
Thank You.

@liken perhaps this is just test code that was made quickly and you are aware it is not optimised, but... you really need to learn to use arrays. Your code is 6 times longer than it should be.

1 Like

This article confirms that what @liken 's code was previously doing (using analogRead(0) rather than analogRead(A0)) does indeed work:

However, I could find nothing on any official Arduino page that describes it, so I feel it should be considered an "undocumented feature" and therefore using it should be strongly discouraged. Clearly it results in issues, as this topic demonstrates!

1 Like

We have not solved the mystery of why one particular model of servo continued to work with this code!

1 Like

Interesting stuff !!

Maybe defining the PIN number in a variable creates the issue ??
Always best to try and stick with the rules and avoid unexpected problems ?

2 Likes

A0 has integer value 14 in Arduino Uno, Duemilanove,...
You can use 0,1,2,... for analogread() instead 14,15,16,... and it works.
But what you must not do is a pinmode(3,INPUT) affecting digital pin 3 you are using for another stuff. It must be pinmode (17,INPUT), or pinmode (A3,INPUT) to avoid errors.

About the mistery of HITEC servo working nonetheless, I think something in servo library is overwritten if you attach a servo on pin 3 and after you do a pinmode (3, INPUT). This causes most servos to stop working. However the hitec servo must have something different in its control loop that makes it tolerant.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.