I'm using wiringPi for PWM on BCM18. I tried to use this to generate a STROBE-equivalent signal to turn ON the LED for the Camera Capture. My intention is to synchronize the LED-ON to shutter opening (note: LED-ON is BCM18 = 0). In order to do that, I need to re-sync the PWM reference point whenever I detect it going off-sync. For that I use pwmWrite() to redefine the new ON time. However, after several experiments I discovered that pwmWrite() does not change the duty-cycle immediately (i.e., does not restart PWM with new reference); instead it waits for the on-going cycle to be complete and then the new value is effected!! Using pwmSetClock() after pwmWrite() also did not yield the result I wanted - i.e., set the new PWM-Start reference with immediate effect on calling pwmWrite() WiringPi does not have a pwmDisable() and pwmEnable() calls that I can make use of. Anybody has tried this scheme and found any workaround ? Please please let me know of the tricks to circumvent the problem.

Thanks in advance - HC Srinivasa

  • How do you perform shutter opening? Can't you do both actions using the same pin? Also check out this. Apr 8, 2019 at 15:30
  • 1
    Dmitry, shutter opening timing is not under software control; I can only specify the exposure duration, but when the shutter opens is controlled by the Camera. Ican only get an indirect indication of 'when the shutter closed' through encoder-callback in piCamera. So, I'm trying to synchronize LED-ON timing based on encoder-callback time. Apr 9, 2019 at 4:50
  • Why do you need to use PWM? Are you trying to react to a GPIO status change by sending PWM? Or are you setting a GPIO and then sending PWM?
    – joan
    Apr 9, 2019 at 8:32

1 Answer 1


I would not use PWM for this, as it creates more problems that it solves. Since you have a callback (from the shutter) which is called each time you need to generate a new LED pulse, you could generate a new pulse on demand every time, either in software or in hardware. If the duty cycle is essentially constant, you can use a pulse generator (AKA monostable multivibrator), which can be implemented with either an Arduino or with a dedicated chip like the famous 555. If the duty cycle has to be adjusted, I would just toggle the pin in software, perhaps from a separate thread.

If you wish to keep using PWM, you'll have to implement a control algorithm which estimates the delay between the LED pulse and the stutter opening window (e.g. from picture brightness) and gradually adjusts the PWM period to minimize that delay.

  • Dmitry, I used GPIO - turn ON at start of capture and turn OFF in encoder callback. But here, I cannot use continuous capture. Because of this, the LED-ON time is coming to about 60ms for a framerate of 10. If I were to use continuous capture, then I do not know the shutter-open time to turn-ON the LED. So I use encoder-callback as a reference point that indicates shutter-close, and estimate the time at which the LED shall be ON for the next frame and start a PWM for that. Apr 9, 2019 at 9:41
  • 1
    "estimate the time at which the LED shall be ON for the next frame and start a PWM for that" - my point is that you should generate a single pulse rather than starting a PWM. You get a callback for every frame, right? Apr 9, 2019 at 9:50
  • Yes I get a callback every frame. How do I program a GPIO to go active in future - say, after 90ms? Using a timer is more error prone because of its own jitter. That's the reason I used a PWM. You have any idea for future-setting of a GPIO? Apr 10, 2019 at 5:59
  • Really having hardtime in restarting PWM from a new sync point. When I change duty-cycle, it gets applied only after the current PWM-cycle is complete; so, I just cannot change the reference starting point after starting it. I even tried modifying wiringPi code to do a PWM0_DISABLE and re-enable it inside pwmWrite() function. But somehow it doesn't oblige to start a renewed PWM cycle from the time I write a new value. It always waits for the current cycle to be over. I'm not getting any idea as to how to make it forget about the ongoing cycle and start a new one from NOW. !! Apr 11, 2019 at 9:08

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.