I connected my Rasp. to an ESC, which I want to control with this part of code:

import RPi.GPIO as GPIO
import time

GPIO.setup(37, GPIO.OUT)

servo = GPIO.PWM(37,60)


while True:

print('***Programm started.')


It worked too but the motors turned On and Off... They didn't run stable. Now i want to know what Frequ. i need and how the code has to be modified so that the motors are running stable (without having to turn On and Off On and Off.....). Maybe somebody knows how i can realize that in Pi4J.

Thanks in advance :D

  • Sorry to ask the obvious :) Are you using stepper motors, and if so, are they wired correctly? – NoChecksum May 13 '16 at 17:41
  • 1
    I m using brushless motors. When i connect them with a normal receiver thy work fine... – cy8berpunk May 13 '16 at 17:43

Thanks for the calculation you have mentioned.

Finally on the basis of these calculations my pi4j code is working.

import com.pi4j.wiringpi.Gpio;

public class App {

public static void main(String[] args) throws Exception {

    //BCM = 12, GPIO 26, in raspberry pi 3 model b
    int n = 12;


    System.out.println("Config Servo PWM with pin number: " + n);
    com.pi4j.wiringpi.Gpio.pinMode(n, com.pi4j.wiringpi.Gpio.PWM_OUTPUT);
    com.pi4j.wiringpi.Gpio.pwmWrite(n, 240);
    System.out.println("Connect your power");
    com.pi4j.wiringpi.Gpio.pwmWrite(n, 120);
    com.pi4j.wiringpi.Gpio.pwmWrite(n, 0);
    com.pi4j.wiringpi.Gpio.pwmWrite(n, 160);
    Thread.sleep(10 * 1000);
    com.pi4j.wiringpi.Gpio.pwmWrite(n, 0);

My range is 2000 and my clock is 192, which is 50Hz or 400Hz calculation give the same result for clock 192.

  • 6% of 2000 = 120
  • 8% of 2000 = 160
  • 12% of 2000 = 240

For arming the ESC, you need these steps:

  1. Disconnect the ESC power, and run the program.
  2. It will set the max value, then connect your power to ESC and press ENTER
  3. It will send min value which is 6% of 2000. Then send 0 after the delay of 1 second.
  4. Calibration is now completed.

Finally press ENTER, it will set the ESC in arming condition with 8% value of 2000.

I believe these calculations are equivalent to the range of 1000 - 2000 to operate the ESC.

I am not a electronics guy so I don't have much information.

  • I've made some serious edits that hopefully makes your post easier to read. Feel free to edit it further, but take note of the edits I've made so your future posts can be even better. – Darth Vader Dec 20 '17 at 17:34

60Hz means 60 PWM slots per second.

Each slot will be 1000000/60 = 16667 µs long.

A dutycycle of 5% will be 5/100 * 16667 µs or 883µs.

5% -> 883 µs
6% -> 1000 µs
8% -> 1500 µs
10% -> 1667 µs
12% -> 2000 µs


while True:
    servo.ChangeDutyCycle(6) # 1000 micros, off
    servo.ChangeDutyCycle(8) # 1500 micros, half throttle
    servo.ChangeDutyCycle(12) # 2000 micros, full throttle

If you want to drive servos/ESCs without jitter you need to use something like (my) pigpio or servoblaster.

  • Thank you very mutch for your fast help :D Do you know how i can realize that in Pi4J. – cy8berpunk May 13 '16 at 18:10
  • @YellowDev As far as I am aware you could use pretty much the same code with Pi4J. But I have never used Pi4J so can't help with the details. Hopefully someone else will give the details. – joan May 13 '16 at 18:57
  • Ok thanks ;D But is the jitter in Pi4J away? – cy8berpunk May 13 '16 at 19:04
  • 1
    @YellowDev I believe Pi4J uses software timed pulses so there will be jitter which will increase with system load. The jitter should be less on a 4-core Pi2 or Pi3. Whether the jitter is a problem or not depends on your application. – joan May 13 '16 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.