1

I need a fairly deterministic PWM duty cycle. I don't believe it's rocket science to use hardware PWM. Numerous sources tell us that we can modify /boot/config.txt by adding

dtoverlay=pwm-2chan

which defaults to PWM0 = GPIO18 and PWM1 = GPIO19. We also know that we can use

dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4

to set PWM0 = GPIO12 and PWM1 = GPIO13.

My application has been using software PWM and coincidentally I've elected GPIO13 for this. So it should be quite easy to switch to hardware. GPIO 12, 18, and 19 are already allocated to other functions on a little "hat" I produced, which would be a real pig to modify.

The latter above allows my test code to work, but I am concerned that it will also convert GPIO12 to PWM.

I thought I might be able to use

dtoverlay=pwm,pin=13,func=4

in order to use GPIO13 as PWM1, leaving the other IOs available for normal GPIO, but the pin doesn't seem to produce any output when used in conjunction with the library at [https://pypi.org/project/rpi-hardware-pwm/][1] .

Can you tell me if what I'm trying to achieve is possible, and if I'm going about it the right way? What could I do differently to get a result?

My hardware is B+ v1.2 and my preference is to work in Python. [1]: https://pypi.org/project/rpi-hardware-pwm/

Thanks.

KDM
  • 718
  • 1
  • 9
  • 24

2 Answers2

2

It's probably simplest to set up PWM on GPIO 12/13 with

dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4

and then to use your Python library to override the setting for GPIO 12 by opening it for input or output (whichever you need).

joan
  • 71,852
  • 5
  • 76
  • 108
0

Original poster has made an error. In fact,

dtoverlay=pwm,pin=13,func=4

works perfectly well in order to use GPIO13 as PWM1, and works perfectly in conjunction with the library at https://pypi.org/project/rpi-hardware-pwm/ .

The problem is that the device being controlled is a Solid State Relay (SSD). The SSD only changes output state when the AC voltage being controlled is zero-crossing. The UK has a nominal mains frequency of 50Hz. For the purposes of our library, that's a duty cycle of 20ms where the AC crosses zero twice in a cycle, so the SSD output is only updated every 10,000,000ns. This means that for certain selections of duty cycle and PWM period, the PWM "on" time could be shorter than the AC halfwave. Nyquist effect means that if the PWM switching happens during this halfwave, this may manifest as the output not changing for long periods of time.

...coupled with the fact that the SSD's built-in LED seems to be dead.

I'm not sure whether to delete the question or leave it as a lesson to the community.

KDM
  • 718
  • 1
  • 9
  • 24