I'm trying to add a servo to a project that already has a dc motor and the motor doesn't work if I call myServo.attach(servoPin). It seems like attaching the servo has an effect on the analogWrite call on the motor enable pin. If I don't call attach on the servo, then I get the appropriate voltage out of the enable pin which is 5v for analogWrite(enablePin, 255); and 2.5v for analogWrite(enablePin, 128);. If I do call attach on the servo, I get 5v with analogWrite(enablePin, 255); but 0v for analogWrite(enablePin, 128);. It's as if attaching the servo disables PWM for the enablePin. Here's my code:
#include<Servo.h>
const int controlPin1 = 2;
const int controlPin2 = 3;
const int enablePin = 10;
const int servoPin = 6;
Servo myServo;
void setup() {
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// Set to LOW so motor starts off
digitalWrite(enablePin, LOW);
pinMode(servoPin, OUTPUT);
myServo.attach(servoPin);
}
void loop() {
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
analogWrite(enablePin, 255); // This produces 5v output on the enablePin
//analogWrite(enablePin, 128); // This produces 0v output on the enablePin if the Servo 'attach' is called
}
The wiring for the dc motor basically follows project 10 in the projects book using the h-bridge chip as the controller. As I said, the dc motor works fine by itself, it's only when I introduce the servo that I have problems. I'm a newb so maybe this is just something obvious to someone with more experience. Thanks for any help.
When you attach the servo, Arduino begins sending the control signal to it. This causes the servo motor to draw current and hold its position. Your power supply seems not to be able to power both motors at the same time.
Thanks. I'm using the Arduino's 5v for the servo (it's the one that comes with the UNO kit) and a 9v battery for the DC motor. Does that seem like it would be a problem?
That is one problem. The Arduino is not a power supply. You can actually damage the Arduino by attempting to power a motor or servo from the 5V pin. Ignore any instructions or tutorials that tell you this is OK!
a 9v battery for the DC motor
That is the other problem. 9V batteries are for low power devices like smoke alarms.
For the servo, use a 5V power supply capable of providing at least one Ampere for a small (SG-90) servo, and at least 2.5 Amperes for a large servos like MG996R, or use 4XAA battery pack.
The motor power supply may need to be a different voltage. Tell us what motor and motor driver you have.
The first thing I would is file the battery in file 13 and get a decent power supply. You can get an adjustable Lab Power supply for less than $50 us. Many times a second is very helpful as well.
The motor is the one the comes with the Uno starter kit:
It's a small motor. Sorry, I don't know the specs and there's nothing printed on it. Thanks for the advice on not using the arduino as a power supply. The project book that comes with the starter kit does use the arduino's 5v for the servo project. I'm using the H-bridge chip that came with the kit to control the motor.
Sorry for the slang, file 13 is another name for the trash. Search for "lab power supply", you will find a lot in a large range of prices. Key features is both current and voltage meters. Be sure you have voltage and current adjustments, mine have both with course and fine settings. 15V at about 3A would be a good starting one, higher rating would be OK. Nice thing is you set the current limit and if you ooops it will limit the current and many times save your from frying your parts. Have fun, the skill set you gain here will be with you the rest of your life.
Another suggestion is download KiCad, it is a full blown electronic computer aided design (CAD) package. It is free but they ask for a donation. As you will find schematics is the language of electronics, frizzing etc is useless when you get more than a few parts involved.
A 4xAA battery pack should work well with that small motor(*), and will power the servo as well. You do not need a lab power supply at this stage. Follow this scheme for the servo.
(*) However, I can't tell what motor driver the kit uses. I'll bet it is L298 or L293, which are very outdated, very inefficient chips that require at least 8V to run. In that case, the 4xAA (6V) battery pack probably won't work. This modern motor driver from Pololuwill work with 4xAA. If you buy it, follow Pololu's clear instructions for wiring it up.
It is really too bad that Arduino CC gives you such terrible, unprofessional advice on using their products, but one could take the cynical view that they will be happy to sell you another Arduino if you destroy yours, following their poor directions.
Thanks. This is going to be for a Halloween decoration if it works out so I don't think a lab power supply would work in this case anyway. The controllers I bought from Amazon are L298N. Do you know if those work with 4xAA or would a 9v be enough?
analogWrite() works by sending a PWM sequence to the selected pin.
In servo.h you can find the following:
A servo is activated by creating an instance of the Servo class passing the desired pin to the attach() method. The servos are pulsed in the background using the value most recently written using the write() method.
Note that analogWrite of PWM on pins associated with the timer are disabled when the first servo is attached.
It looks like the selected pin for your servo affects the pins you are using for analogWrite. You should be able to fix the problem by choosing different pins.
Definitely! The servo library uses timer 1 on UNO to create the servo signals. PWM on pins 9 and 10 also uses timer 1. So you have a timer conflict. Use another PWM pin as 9 0r 10 for your motor..
Thanks! I switched the enable pin from 10 to 11 and now both the servo and motor work as expected. Is there any documentation for the UNO as to which PWM pins interfere with each other?