Servo with analogWrite()?

Hi,
Can servos(or any other device that requires PWM) be controlled with the analogWrite() function?

Thanks!

Also, can an Arduino spin two continuous micro servos at once? For, like, I don't know, robot wheels?

analogwrite() is PWM, an RC servo does NOT use PWM. It uses a pulse of length 1ms to 2ms, and it does not respond to the off time. The timing from leading edge to leading edge can be from 10 to 30ms.

Yes, you can send signals to more than one servo at a time.

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.

A servo certainly does use PWM, but of a low duty cycle and long period - consider the meaning
of the words "pulse" "width" and "modulation"... We use PWM rather informally when refering
to the pins controlled by analogWrite, since its really only the duty-cycle that matters.

Its just that the analogWrite controlled pins are setup all wrong for Servo signals.

2 Likes

I think it muddies the meaning of PWM to use it in that way. But I'm not the boss of you, so use it however you'd like.

polymorph:
I think it muddies the meaning of PWM to use it in that way. But I'm not the boss of you, so use it however you'd like.

One could argue that the term "PWM" makes more sense in the servo context than it does for analogWrite() since servos care about pulse width and are relatively insensitive to duty cycle. In any case PWM is common usage in the servo context and has been since before the advent of microcontrollers.

Just use the Servo library for servos and be aware that it steals on of the timers to do its magic...

Since you asked about other devices too. Most new hobby speed controllers on the market can take a regular servo signal 1-2ms, 50hz. Or a 1000us-1900us 500hz pulse or a 125-250us 500hz signal. Or a 5-25us up to 32khz pulse. There are other protocols too. Analogwrite works nicely if you use a 500hz pin and a range of 125-225 ( 1000-1800us) but you lose resolution.

It's all PWM.

I noticed the standard 50hz PWM is actually disabled by default on the latest blheli (most popular for multirotors) firmware versions so we can expect people asking why the servo library doesn't work with their esc's.

Pls am new to arduino relating to rc signal stuff, but I understand a bit of analogwrit() function basically for pwm and servo.write acting as ppm for servos but how can I implements these two functions in one code for my project which involve one brushed DC motor and one servo?

Which Arduino do you have? Use the Servo library for your servo and PWM for speed control of your brushed DC motor.
NOTE: If using the Servo library, it disables PWM for pins 9 & 10 on a UNO, Nano, ProMini and other Arduinos that use the Mega328 chip, but you still have pins 3, 9, 10, & 11.
Also, you cannot connect a motor directly to Arduino pins, you will need a driver board depending on the size and voltage of your motor.