I have a Python code which controls an ESC & servo fine. Now, I'm trying to develop a similar code in C language using wiringPi. However, the C codes don't work. The Python & C codes are attached. So, where is the problem?

  • I am using pin#11 or GPIO_17, which should be pin#0 on wiringPi.

The working fine Python:

import RPi.GPIO as GPIO
GPIO.setup(11, GPIO.OUT)

dc = 5

servo = GPIO.PWM(11,50)
print('***Connect Battery & Press ENTER to start')
res = raw_input()
print('***Press ENTER to start')
res = raw_input()

print ('increase > a | decrease > z | save Wh > n | set Wh > h|quit > 9')

cycling = True
    while cycling:
        res = raw_input()
        if res == 'a':
            dc = dc + 0.05
        if res == 'z':
            dc = dc - 0.05
        if res == 'h':
        if res == '9':
            cycling = False
    # shut down cleanly
    print ("dc var setting is: ")
    print (dc)

print('***Press ENTER to quit')
res = raw_input()

A very simple C code#1 I developed:

#include <stdio.h>
#include <wiringPi.h>
#include <softPwm.h>

int main(void)

if (wiringPiSetup() < 0) return 1;

pinMode(11, OUTPUT);
digitalWrite(0, LOW);
softPwmCreate(0, 0, 500);
softPwmWrite(0, 300);

return 0;

Another simple C code#2 :

 * test2.c:
 *      Simple test program to test the wiringPi functions
 *      PWM test

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main (void)
  int pin=0 ;
  int l ;

  printf ("Raspberry Pi wiringPi PWM test program\n") ;

  if (wiringPiSetup() == -1)
    exit (1) ;

    pinMode (pin, OUTPUT) ;
    digitalWrite (pin, LOW) ;

  pinMode (0, PWM_OUTPUT) ;

  for (;;)
    for (l = 0 ; l < 1024 ; ++l)
      pwmWrite (0, l) ;
      printf ("pwm=%i  \n"), l;
      delay (1) ;

    for (l = 1023 ; l >= 0 ; --l)
      pwmWrite (0, l) ;
      printf ("pwm=%i  \n"), l;
      delay (1) ;

  return 0 ;

Thank you in advance.

  • Are you asking how to do all the things in C that you do in the python code that you aren't currently doing in C, e.g. raw_input()? – goldilocks Apr 13 '15 at 13:58

ESCs and servos need precisely timed pulses at 50Hz+. Generally each pulse will be between 1000µs to 2000µs in length.

Neither wiringPi (unless using the hardware PWM function) nor RPi.GPIO are suitable for driving ESCs/Servos with any degree of accuracy. They may be ok if your application doesn't mind a lot of jitter (software timed pulses will vary a lot in time).

If you are doing any serious work you need to be using one of the modules which provides hardware (DMA) timed pulses, or use one or both of the hardware PWM channels, or use external hardware.

Servoblaster, RPIO.GPIO (not RPi.GPIO), and my pigpio, all provide DMA timed PWM.

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.