I'm trying to get my raspi zero to move an SG90 PWM driven servo. I have achieved this with python already using the gpiozero library, so I know my hardware works but I am trying to do it with C using the wiringPi library. when I compile and run the executable (using sudo) I get no errors and everything seems to work, but the servo doesn't move at all.

I assumed I was using the wrong pin number at first. I found this post Raspberry pi Software Driven PWM using C++ and I used the table offered in one of the answers. The pin I'm using is physical pin 11, the 6th pin up from the square pin 1, aka GPIO17. I've tried using 11 with wiringPiSetupPhys(), 17 with wiringPiSetupGpio(), and 0 with wiringPiSetup(), getting the same result each time. I initially tried writing 50 and then changed it to 10000, but both times got nothing

here's my code:

#include <stdio.h>
#include <wiringPi.h>
#include <softPwm.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#define PWM_PIN_NUM 17

int main(){

    printf("Failed to setup wiringPi\n");


  if(softPwmCreate(PWM_PIN_NUM, 0, 100)){
    printf("Failed to init pwm pin\n");

  printf("Successfully setup wiringPi and init pwm pin\n");

  softPwmWrite(PWM_PIN_NUM, 10000);

  printf("%s: %d\n", strerror(errno), errno);

it always outputs "sucessfully setup etc etc" and errno is always 0

  • 1
    A servo requires pulses at 50 Hz. Each pulse should be between say 600 to 2400 µs long for a SG90. How are you ensuring these values?
    – joan
    Commented Jan 10, 2021 at 20:01
  • Thanks for responding! No Idea, I'm definitely not an expert at the nitty gritty details of servo control, but I have a standard pi zero, and I was hoping the wiringPi library would be able to handle those details. I wrote 10000 because someone on the linked post said that softPwmWrite() clips values to the highest possible value to be written to the servo, in this case 100, which if I understand correctly, would encode a 2400 µs pulse and cause the servo to rotate 180 degrees.
    – Dyskord
    Commented Jan 11, 2021 at 0:05
  • 1
    I'm not a fan of software timed PWM for servos. If you must use software timed PWM from C I suggest you use my lgpio library and lgTxServo. For hardware timed PWM use pigpio (e.g. sudo pigpiod then pigs s 17 1000, pigs s 17 2000 to move your servo.
    – joan
    Commented Jan 11, 2021 at 9:02
  • 1
    I'd have to read the wiringPi documentation for those calls to see what you re doing wrong. You can do that as easily as me.wiringpi.com/reference/software-pwm-library
    – joan
    Commented Jan 11, 2021 at 18:12
  • 1
    If pigs works then the servo is okay and any library will work. lgTxServo is part of the lgpio library.
    – joan
    Commented Jan 11, 2021 at 20:44

1 Answer 1


I was able to figure something out, however I was not able to get it to work with the wiringPi library. I used the linux sysfs interface to control the gpio pin directly and then I created my own sofware timed pwm. I used the information in this link https://www.ics.com/blog/how-control-gpio-hardware-c-or-c. I hope this doesn't become a dead link in the future, it is super helpful information. I created a c++ class that resembled the wiringPi library and used sysfs to implement the functions myself. If anyone needs more information on this, leave a comment or ask a question and tag me and I will be happy to elaborate and show my code.

  • If you "have achieved this with python already using the gpiozero library" which is by default based on RPi.GPIO there seems little reason to write new code based on the deprecated sysfs interface or attempting to emulate the deprecated WiringPi code.
    – Milliways
    Commented Jan 12, 2021 at 6:51
  • I have made my own version of RPi.GPIO and am in the process of extracting the underlying code to make a C library. This is working, but incomplete. There are other PWM libraries, including lgpio which is a new C library for Linux Single Board Computers (including the Pi) which allows control of the GPIO.
    – Milliways
    Commented Jan 12, 2021 at 6:51
  • Awesome! I just wanted to implement it in C++ because I am trying to learn embedded programming with c++. Do you know if the igpio library uses the sysfs interface under the hood?
    – Dyskord
    Commented Feb 3, 2021 at 1:44
  • AFAIK No current libraries use the deprecated sysfs interface. Most use gpiomem to access the hardware registers. Joan's new code uses the gpiochip interface.
    – Milliways
    Commented Feb 3, 2021 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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