Hi, I have a problem with my project. It consists of a DC motor connected to a potentiometer that controls its speed and a servo that moves left or right by pressing one of two buttons. The servo works fine when the motor is not powered, but when I power the motor the servo starts moving randomly. I am not sure if the problem it’s in the code or in the hardware, I am new to this so any help will be appreciated. There aren’t any errors when verifying the code and the board it’s powered by USB.
It looks to me like you are expecting the Arduino to provide power for the servo and the motor. As you've seen, that doesn't happen. You need to power the servo and motor independently.
Thanks for your help. Now, to power the motor and the servo separately I just need to remove the cables that connect the breadboard with the 5v and ground pins on the arduino and replace them with the two cables from a battery holder. That way the board is powered by USB and the motor and the servo by battery, is that correct?
The motor runs around 1.5-4.5 V and the servo 4.8-6.0V, that means that I should be using 3 AA batteries, so the voltage is 1.5x3=4.5. Or 4 batteries would be better?
Your last post gave me the idea of connecting the motor to the 3.3v pin and to a separate ground pin on the board. Now it works as expected and the servo doesn’t move when it shouldn’t. I have also noticed that the velocity on the motor drops when the servo is moving, which I guess is caused by the lack of a separate power supply that zoomkat and PaulS mentioned. Next week I will add a 4 AA battery pack to power the servo and connect the motor to the 5V pin and it should work without velocity drops.
Thanks to everyone that commented, you have been very helpful.
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.
You can't use pin 9 for anything when using the servo lib.
Thanks for the tip on the use of the servo library, I didn't notice that when I turned the potentiometer the motor just when to max rpm. I have changed the pin to number 6 and now I can control the rpm.