PWM Servo Controls

Help and questions on programming the codebug
Post Reply
Posts: 1
Joined: Mon Oct 12, 2015 9:46 am

PWM Servo Controls

Post by Spanner »

Hi guys,

I'm having some difficulty with driving a servo, and thought you might be able to provide a bit of feedback on the code?

So, it's supposed to work like this. Press button A, and the servo goes to position 170 (the "bottom"). Press A again, and it goes to position 60 (the "top"). Continuing to press button A switches the servo between these two positions.

It works, for the most part. But if I leave the button unpressed for a short while (say, 10 seconds or longer), the whole thing becomes unresponsive. The servo does nothing until I've pressed A multiple times, or held it down for a while, and then it all comes back to life (until I leave things unpressed for a while again). It seems to work fine in the emulator, however.

Also, initially the code is set to switch the servo to 170 when it's first powered up, but this never seems to work.

Furthermore, even with the working code, if I add in a couple of other functions such as displaying a sprite, the servo control stops working.

If you guys would be good enough to take a look and let me know if I'm missing something (or maybe trying to do something the CodeBug can't handle), that's be very much appreciated.

Here's the code I've been using: ... vo-driver/

Thanks in advance!
Posts: 86
Joined: Fri Jul 31, 2015 10:37 am

Re: PWM Servo Controls

Post by mikerr »

couple of things that may help:

- you have no delay for the "off" part of the pulse, try putting a 19ms delay after the off signal:

(also see graham's code

- using "sleep until button changes" makes codebug go into a deep sleep with no display or output,
the servo will be powered down (not holding position) in that state.

- codebug goes to sleep by itself after 3 mins
to change this click the cog on the start icon,then select "sleep after" and "never".
Twitter @mikered codebug profile mikerr
Posts: 67
Joined: Sat Aug 15, 2015 1:41 pm

Re: PWM Servo Controls

Post by pigraham »

You do need to keep sending the control pulse out to the servo. It doesn't have to be precise, but something around every 20 ms is normal.
If you expect to set a position then leave it alone for a long time (e.g. a door latch) then you should probably use a different approach.

You could try using a leg to switch power to the servo so that the servo is off in the idle state.

It will take some time to initialise control when you turn on and start sending control pulses, as you have found.

You will see that in my code I output a PWM pulse every time around the main loop, with a delay that gives about 20ms interval. That gives stable control. You can do other things from that loop even if they delay the PWM pulses a bit, but keep the pulses coming.
Posts: 1
Joined: Mon Dec 07, 2015 5:48 pm

Re: PWM Servo Controls

Post by NattyAlway »

I have gone through your problem details but i need some more information about your setup?
Can you please share all the details of all of your hardware and its specifications?
Also share your firmware?
Post Reply