Trouble understanding control code for servo (PWM code)

I’m working with a continuous rotation servo and I’m not quite sure that I understand how the IMP example code works ( I am trying to get my servo motor to rotate at intervals of 90 degrees at a time. And when I disable the sweep() function and input statements with setServo() and setServoDegrees(), but it results in my servo spinning constantly. The servo should rotate 90 degrees with a 1.5 ms pulse (according to the product page), but I’m not sure where that information needs to go. In servo.configure(…)? What are the servo’s min and max constants? And what do I need to put in a function so that the servo rotates 90 degrees for an input?

Any help would be greatly appreciated.

This is the Servo motor that I am using:

For a single pulse, you don’t want to be using PWM. You likely want to do something like:

local e = hardware.micros()+1500;
while(hardware.micros() < e);

…which will generate a ~1.5ms pulse; this is a non-ideal way of doing it though, the more accurate way (hardware, vs software timed) would be to use SPI or the FFDAC to generate the output.

How many servos?

I think your problem is with the “servo” itself. From that product page: “Position “90” (1.5ms pulse) is stop, “180” (2ms pulse) is full speed forward, “0” (1ms pulse) is full speed backwards.” Sounds to me like it’s not really a servo that can be commanded to a particular angle, but more like just a continuously-running motor that’s controlled a bit like a servo.

If you want a motor that rotates 90 degrees at a time (but can go all the way round and carry on), look into stepper motors.


I’d second what Peter is suggesting. When a standard servo is modified, it is going from a position controlled device to a speed control device. The potentiometer and feedback circuit inside the servo keeps hunting for its position but it can’t get there because of the continuous rotation capabilities and disabling of the potentiometer.

Yes, my bad Peter actually read the datasheet and I jumped in with useless info :slight_smile:

That was my fear, but I think you’re right. Thank you Peter, Hugo and Swieter!

I do see that stepper motors are not very power efficient, especially when not operating. This is a pretty big problem for the system that we are making, which is a solar powered parking meter. So we have a limited battery life. Are there other types of servo motors out there that can rotate 270 degrees at 90 degree intervals?

Googling “270 degree servo” shows some options, eg

It’s worth noting that servos aren’t really low power and you will probably need to gate power to them when they are not in use if you are trying to get to a low power state. They will hold their position due to friction, but without being energized they won’t return to the set location.

Stepper motors are actually very efficient when not being driven because they are completely disconnected so use no power. If you are trying to hold a load in place then you have to continue to run current through them to hold that position so they would be a poor choice for that (but so would a servo), but if you are just trying to turn a knob or you have sufficient friction then you can complete deenergize the motor and it will stay in place.