Servo and dc motor together , motor stop working

I am doing a little school project. I have an arduino uno, sg90 servo, 1 small dc motor(the little ones on the kits) and L298N motor controller.

I have a function to move the motor and a function to move the servo(sweep demo). They work perfect when I do one or the other. When I call one after the other, the dc motor never runs.,

I am using pin 7,8,9 for motor controller and 6 for the servo. I am running the motor controller from a 12V power adapter and the arduino from the USB cable.

Basically, if I comment the servo code, the motor works.

boat.txt (1.24 KB)

Well done posting a photo of your setup.

Some tips:
1: your code goes in code tags - see the icon above that looks like this </>

2:

I have an arduino uno, sg90 servo, 1 small dc motor(the little ones on the kits) and L298N motor controller.

provide links to the components you are ACTUALLY using - eg

L298N motor controller

3: Use variable names to help comment your code - eg

in1 isnt really a helpful name for a digital pin used as an output.

Why it isnt working:

The Servo library uses a timer

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.

So you need to use a different pin to provide PWM for your motor speed

Using the servo library disables PWM on pins 9 & 10, use pin 3 for motor PWM.

Don't try to power the servo from the Arduino 5V. That will cause the Arduino to malfunction and may even damage it.

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