Controlling a servo with lopy (library or manually)

  • Hello,

    Was wondering if anyone know of an existing library for controlling a hobby servo with the lopy? Or alternatively could point me in a direction of doing it manually ?


  • @soren Good that it works. But omitting the sleep() commands are hardly the reason. I checked with an oscilloscope that PWM output stays when sleep() is performed. It's more something else you did. I worked with these servos decades ago, and remember that the pulse width range they expect is pretty small, from 1 ms to 2ms, at a 20ms period, resulting at a PWM duty cycle range between 5% and 10%

  • @livius I took everything apart, put it together and double cross checked several times the connections and then tried the pwm_c.duty_cycle() command from console - and it WORKED! @robert-hh I think time.sleep actually does affect pwm. At least it works now for me from console where I'm not using sleep - in contrast to what I did in the code.

  • @robert-hh
    yes, but it is not needed from terminal - this was my intention

  • @livius sleep() does not turn off the PWM.

  • @soren said in Controlling a servo with lopy (library or manually):
    For me setup 1 not worked when i tested it in the past with MG996R.
    It does not react properly on 3v3 range pwm.
    Setup 2 should work but i do not use convertes in chip
    i trust simple design as converter on simple transistor eg. 2N7000.

    and to test if it work - try first run commands from terminal
    pwm_c.duty_cycle(0.127 )
    look at result but do not put sleep command
    and decrese value
    pwm_c.duty_cycle(0.117 )
    pwm_c.duty_cycle(0.107 )
    pwm_c.duty_cycle(0.097 )

    any results?
    P.S. pwm_c.duty_cycle(1) is not good at all ;-)
    i have done same mistake previously - as you can see in my post from the past

  • @soren Switching while you toggle manually? or between duty cycle 0 and 1?
    Edit: I see 74HCT125 has a high input level of 2V, like the 74LS125. It's the 74HC125 with the 3.15 V level. So theer should be no problem with the levels.

  • @robert-hh I can see with my multimeter that it's switching, but maybe that isn't accurate enough?

  • @soren I just looked into the 74HCT125 data sheet. The minimum level for high is 3.15 V, close to the output level of the lopy. In that case I would check with an oscilloscope or logic analyzer whether the output of the gate really switches.

  • @soren Software

  • @robert-hh no worries mate, but yes - i did pull OE low. Connected it directly to GND on the lopy. Maybe a stupid question, but what is "SW" ?

  • @soren said in Controlling a servo with lopy (library or manually):


    I'm a little bit shy to ask, but did you pull the OE input of the gate low?
    Besides that, I checked your SW setting with my LoPy, and it fits. The frequency is 50Hz, and the duty cycles also match precisely the setting by SW.

  • @livius OK here goes:

    My servo is a 4,8v C505 Graupner non continuos. Documents about it are very sparse, but to make sure i wasn't wasting my time - I checked it with an arduino, and it's working just fine.

    I have 2 setups that I'm trying:
    Setup #1:
    pin G28 (P12) on the expansion board is connected directly to sig on the servo
    external voltage source (4.8v) to power, and GND to GND

    Setup #2:
    Same setup, but going through a CD74HCT125E logic level converter in order to step up the 3.3v output to a 5v output. Not 100% sure this makes sense though, but at least I haven't ruined anything because of it. When I measure the output of the converter chip, it hits almost 5v - so i can't see why this shouldn't work..

    pwm = machine.PWM(0, frequency=50)
    pwm_c =, pin='P12', duty_cycle=0.04)

    servo is moving only occasionally and only a tiny tiny bit on the lopy.

  • @soren
    yes, what do you tiried already?
    How your setup looks like?

  • @livius thank you, but I have already glanced at that topic over and over again. It's not working.. did you get it to work?? :)

Log in to reply

Pycom on Twitter

Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.