I am trying to control frequency and duty cycle through c-program based on wiringPi library.

Here is the code:

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

int main (void)
  printf ("Raspberry Pi wiringPi test program\n") ;

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

  pwmSetRange (10) ;
  pwmWrite (1, 30);

When I execute it by:

sudo gcc -Wall -o sh pwm.c -lwiringPi
sudo chmod 777 sh

I see on my oscilloscope just single impulse when I run my program and then nothing, dead silence.. nothing like pwm and LED is just lighting

I cannot explain it.. I understand it this way. When I execute my program, pin number 12 (number 1 according to wiringPi library) must be configurated to some frequency and duty call and stands the same till the moment when I change some parameters in my code and run this program again.. but this behavior I cannot explain, especially when commands on bash like:

gpio mode 1 pwm
gpio pwm-ms
gpio pwmr 200     
gpio pwm 1 30    
gpio pwmc 1920 # and then I modified pwmc and see frequency being changed as it should be!

when I change gpio pwmc 2000 to gpio pwmc 40 and gpio pwmc 4000 I see pwm on my oscilloscope and see my LED blinking...

What is wrong with my understanding, why it is working okay on bash and does not appropriate work on C? How whould I change my code on C to achieve the same ?

Thank you

  • the wiringpi library could be cleaning up on exit? you could try adding a simple delay before your program exits to see if thats the case? – James Kent Aug 8 '17 at 12:31

As @joan said, wiringPi has different pin numbering schemes. wiringPiSetupGpio() uses the GPIO pin scheme, whereas wiringPiSetup() uses the wPi pin scheme.

You either need to change your init call to wiringPiSetup() or change the pin number to 18 (18 is the GPIO representation for physical pin 12 (PWM pin), and 1 is the wPi representation).

Your wiring is definitely correct, as the gpio command uses the GPIO pin scheme, and will automatically use 18 as the pin number and that's why it works on the command line.

| improve this answer | |

wiringPi supports two types of PWM.

  1. software - available on all GPIO but subject to jitter and has limited frequencies (and possibly limited duty cycles, I do not remember).
  2. hardware - available on GPIO 18 (pin 12) and possibly on GPIO 12/13/19 (I have not checked the code/documentation).

Given the commands you are using you are using hardware PWM . It will only be effective on GPIO 18 (and possibly 12/13/19).

wiringPi can use GPIO numbers or wiringPi numbers. Check the actual number you are using is correct for the GPIO numbering scheme you are using.

| improve this answer | |
  • You hit the nail on the head here. OP is using wiringPiSetupGpio(), but is specifying the wPi number as opposed to the GPIO one. wPi number uses pin number 1 for GPIO 18, so either the OP has to change to using wiringPiSetup(), or change the pin number to 18 if they wish to continue to use wiringPiSetupGPIO(). Either one of those changes will rectify the issue. – stevieb Aug 8 '17 at 12:35
  • To further, the gpio program uses the GPIO pin scheme, which tells me OP has correctly wired things to the real PWM pin (phys 12, GPIO 18, wPi 1). – stevieb Aug 8 '17 at 12:39

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.