Problem with servo.attach!

Hello, first timer here!

I am using my arduino uno to drive a servo and 2 DC motors. I am using the seeed studio motor shield. They 2 DC motors and the servo work seperately, but together they are not working.

The electronics should all be working, and I am driving the whole thing with a big battery.
When I program to move the DC motors, everything works. My setup function uses pins 8,11,12,13 as digital outputs and pins 9 and 10 as analog output for the pwm. When i enter my function forward(), the DC motors both rotate forward.

The servo motor is attached to pin 6. I used servo.attach(6). When i put servo.write(90) or any angle, it goes to that angle successfully. So the Servo part is working.

Now, I take the code of the 2 DC motors, a code that is perfectly working, and I add to it at the top of the program
#include <Servo.h>
Servo servoMain;

and in the setup I add servoMain.attach(6);

I compile and upload, but now the DC motors stop turning. The DC motors don't use pin 6 at all, so what could be the problem??

I would appreciate any help! Thanks!

Can you post the code that doesn't work (between code tags, please - the # button above the edit window).

...R

Hello, first timer here!

I am using my arduino uno to drive a servo and 2 DC motors. I am using the seeed studio motor shield. They 2 DC motors and the servo work seperately, but together they are not working.

The electronics should all be working, and I am driving the whole thing with a big battery.
When I program to move the DC motors, everything works. My setup function uses pins 8,11,12,13 as digital outputs and pins 9 and 10 as analog output for the pwm. When i enter my function forward(), the DC motors both rotate forward.

The servo motor is attached to pin 6. I used servo.attach(6). When i put servo.write(90) or any angle, it goes to that angle successfully. So the Servo part is working.

Now, I take the code of the 2 DC motors, a code that is perfectly working, and I add to it at the top of the program
#include <Servo.h>
Servo servoMain;

and in the setup I add servoMain.attach(6);

I compile and upload, but now the DC motors stop turning. The DC motors don't use pin 6 at all, so what could be the problem??

I would appreciate any help! Thanks!

Christopher414:
#include <Servo.h>
Servo servoMain; // Define our Servo

int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead =180;//define the spead of motor

void setup()
{

pinMode(pinI1,OUTPUT);
pinMode(pinI2,OUTPUT);
pinMode(speedpinA,OUTPUT);
pinMode(pinI3,OUTPUT);
pinMode(pinI4,OUTPUT);
pinMode(speedpinB,OUTPUT);
servoMain.attach(6); //If i just remove this line, the loop works and wheels rotate, if this is here, then the wheels don't move.
}

void forward() //GO FORWARD
{
digitalWrite(pinI4,HIGH);
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,LOW);
digitalWrite(pinI1,HIGH);
analogWrite(speedpinA,spead);
analogWrite(speedpinB,spead);

}

void backward() //GO BACK
{
digitalWrite(pinI4,LOW);
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,HIGH);
digitalWrite(pinI1,LOW);
analogWrite(speedpinA,spead);
analogWrite(speedpinB,spead);

}

void left() //TURN LEFT
{
analogWrite(speedpinA,spead);
analogWrite(speedpinB,spead);
digitalWrite(pinI4,HIGH);
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,HIGH);
digitalWrite(pinI1,LOW);
}

void right() //TURN RIGHT
{
digitalWrite(pinI4,LOW);
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,LOW);
digitalWrite(pinI1,HIGH);
analogWrite(speedpinA,spead);
analogWrite(speedpinB,spead);

}
void carstop() //STOP CAR
{
digitalWrite(speedpinA,LOW);
digitalWrite(speedpinB,LOW);
delay(1000);

}

void loop() //GO FORWARD A BIT THEN STOP.
{
forward();
delay(200);
while(1);
}

Disclaimer: I never had this problem. However I know the Servo library messes up the PWM on the Arduino.

From the Servo lib reference page:

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

So you want to change the DC motors PWM pins.

1 Like

Aha I guess that makes sense. Is there any way to turn on and off the servo library through code, since the motor driver specifically attaches to pins 9 and 10.

I tried using servo.detach() before I run the DC motors but that doesn't work.

Disclaimer: I never had this problem. However I know the Servo library messes up the PWM on the Arduino.

I think the work 'messes up' is ill chosen. PWM output pins require hardware timers. The servo requires a hardware timer. The 328P has three timers available and the first is used by the millis() function. That leaves two timers to support the pwm output pins. If you use the servo library then that takes up a timer leaving only one left to support some of the PWM output pins. You just have to review the timer usage in your specific sketch and be aware of such conflicts in timer usage. If you use the servo library then you can't use pins 9 and 10 for PWM outputs.
From the servo reference page:

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.

Thank you all for the answers. I rewired my PWM pins to digital outputs 2 and 3(instead of 9 and 10) and it is working!

Christopher414:
I rewired my PWM pins to digital outputs 2 and 3(instead of 9 and 10) and it is working!

I had a similar issue with the Uno and seeed motor shield 1.0, when you rewired for digital output 2 and 3 did you have an issue where the dc motors were acting oddly? I tried that and got mixed results - how did you rewire to those outputs?

You can also go into the motordriver.h file and where it maps the speed_pins to 9 and 10 change the values to 3 and 5. Then attach your leads to 3 and 5. My servo is on 7 and ping is on 6. They all work great.

In my situation i got a mega 2560, and 44,46 PWMs are not working for l298n motor shield. should i change the pins for motor shield or pins for the servo??(I got two servos connected to 6,7)

@Sewmina, I have suggested to the Moderator to move your question to its own Thread as this one is very old.

Please post the program you are having trouble with.

On a Mega the PWM pins are 2 to 13. Check with the servo library documentation to see which PWM pins are unusable when using the Servo library.

...R

(I got two servos connected to 6,7)

changes this pins to PWM signal pins.

[u]Arduino Mega PWM pins - Troubleshooting - Arduino Forum

nielyay:
changes this pins to PWM signal pins.

I'm not sure what that is intended to mean but a servo can be controlled from any I/O pin.

...R

Christopher414:
Aha I guess that makes sense. Is there any way to turn on and off the servo library through code, since the motor driver specifically attaches to pins 9 and 10.

I tried using servo.detach() before I run the DC motors but that doesn't work.

Hack it? You've got the source code...