RC Servo PWM control.

  • Hello.

    Please, I need to control a common RC servo to operate a valve. These servos uses 50Hz cycle and the the width of pulses vary from 1ms to 2ms - the center being 1.5ms.

    How can I configure a digital output to control it? The example file from the software does not uses the PWM block and I could not figure out how to use it.


  • Hi Bruno,
    the Monarco HAT has hardware PWM and therefore does not need to create PWM in software using a PWM block. Documentation to the PWM on Monarco HAT can be found on page 16 in Monarco technical specification https://www.monarco.io/docs/Monarco-HAT-Hardware-Reference-Manual.pdf.
    All you need to do is set the PWM frequency (in your case to 50 Hz) and calculate the actual duty cycle for one channel according to the equation:

    Duty_cycle = pulse_width/period_length

    Take a look at example 0121-05_Monarco_HAT_PWM_output to find out how to set the frequency and duty cycle for PWM1 (for example).

    Hope, this will be helpfull.

  • @reitinge Thank you.

    So, the duty cycle value for 50hz would be from 0.1 to 0.05 (1ms to 2ms pulse width) and for 100Hz would be from 0.2 to 0.1 for example.


    But I could not make it work...

    Edit: With 500Hz it moved so... Ok 😄

    Thank you

  • @reitinge said in RC Servo PWM control.:

    Duty_cycle = pulse_width/period_length

    Period length? 1s/Hz?

  • The length of the period for 50 Hz is 0.02 s.
    For pulse width 0.001 s:
    Duty cycle = 0.001/0.02 = 0.05
    Pulse width 0.002 s:
    Duty cycle = 0.002/0:02 = 0.1

    Are you sure about the frequency and width of the pulses? I would expect a duty cycle range from 0 to 1.

  • @reitinge Yes but these values of duty cycle does not work as calculated.

  • @reitinge And I am not sure...

    At 50hz the servo just ignores the DOUT signal. It started working at 500Hz up to 5000Hz.

    And the Duty cycle calculation does not create the expected response from the servo at all. The width I am sure (but it is around 0.8 to 2.2ms)

    So I am confused. I could make the servo works but this formula for the duty cycle and pulse width does not works.

  • Hi Bruno,
    I have connected my Monarco HAT to the oscilloscope and the PWM behaves as expected. I set the frequency to 50 Hz and on the first measurement I set the duty cycle to 0.5 (Figure 1). Figure 2 shows the measurement at a duty cycle = 0.1. Please note that Monarco HAT has open-drain outputs, as shown in Figure 2.
    Can you send a wiring diagram to your RC servo?

  • @reitinge To be more specific, for an open-drain digital output you will need to subtract the calculated duty cycle value from 1:

    Duty_cycle = 1 - Calculated_duty_cycle

    For 1 ms width pulse and frequency 50 Hz:

    Duty_cycle = 1 - 0.05 = 0.95

    For 2ms width pulse and frequency 50 Hz:

    Duty_cycle = 1 - 0.1 = 0.90

    If you are using a DO as an 5V voltage source, see the Using digital outputs in 5V logic circuits chapter here:
    The logic is "reversed":
    If DOUT=OFF, no connection to ground exists and you will measure 5V at the output. If DOUT=ON, connection to ground is made and there will be 0V at the output. That's how you can control devices which use 5V logic.

    I apologize for the mystification in previous posts.

  • @reitinge said in RC Servo PWM control.:

    Can you send a wiring diagram to your RC servo?

    The signal wire of the servo is connect to the DOUT1. The servo is powered by a dedicated BEC to provide 6v that have a common ground with the Monarco.

  • @reitinge With the "Duty_cycle = 1 - Calculated_duty_cycle" it worked like a charm. THANK YOU!

  • You are welcome 🙂

Log in to reply