Hardware PWM in python??? - Raspberry Pi Forums - 图1

OutoftheBOTS

Posts: 711

Joined: Tue Aug 01, 2017 10:06 am

Hardware PWM in python???

As far as I can find from googling there isn’t any easy way to use hardware PWM from python.

There does seem to be a few ways like PiGPIO, WiringPi and RPIO from the ZeroGPIO library all of them seem to need a root user and or running a deamon or installing packages not part of a standard OS install.

I coach beginners and teenage kids so prefer not to use root user or try to get them installing extra packages.

We have been playing with IR transmitters and receivers. The IR receiver will only receive IR light flashing at 38khz at 30% duty so the transmitter must be able to switch on/off at 38khz or the receiver just won’t see it.

For the moment I have got around the problem by using a 555 timer to generate the 38khz square wave to send to the IR LED, see below pic. This make it easy to program sending the NEC protocol code in python(just switch the wire connected to the rest pin of the 555 timer on and off using time.sleep to generate the code).

I do find it frustrating that the RPi has got hardware PWM but I can’t find a beginner way of using it. Am I missing something ????

Hardware PWM in python??? - Raspberry Pi Forums - 图2

20171007_184817.jpg (89.5 KiB) Viewed 13328 times


ghans

Posts: 7882

Joined: Mon Dec 12, 2011 8:30 pm

Location: Germany

Re: Hardware PWM in python???

Sat Oct 07, 2017 9:03 am

The GPIO libraries have been made to work without root and come preinstalled for years IIRC. Which distro are you using ?

ghans

• Don’t like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don’t like to search the forum BEFORE posting ‘cos it’s useless ? Try googling : yoursearchtermshere site:raspberrypi.org


Hardware PWM in python??? - Raspberry Pi Forums - 图3

OutoftheBOTS

Posts: 711

Joined: Tue Aug 01, 2017 10:06 am

Re: Hardware PWM in python???

Sat Oct 07, 2017 11:19 am

The RPi.GPIO library can’t control the hardware PWM, it only creates software PWM that certainly won’t create a stable 38khz square wave, putting it on the scope the RPi.GPIO PWM won’t get anywhere near to freq that high and anything it creates is not stable and all over the shop.

The PiGPIO library will control the hardware PWM and as far as I can tell can create a stable 38khz PWM with 30% duty cycle see http://abyz.co.uk/rpi/pigpio/python.html#hardware_PWM

But the PiGPIO library needs to run a deamon as a sudo user to work.

All the other libraries that can control hardware PWM need to be installed and need to be run as a sudo user.

Setting up and using the LIRC is far from a beginner python programming option, most higher end programmer struggle to get it to work.

Again I was wondering is there a simple way of controlling hardware PWM in python without having to run a sudo deamon or run programs as a sudo user or install new packages.



Hardware PWM in python??? - Raspberry Pi Forums - 图4

OutoftheBOTS

Posts: 711

Joined: Tue Aug 01, 2017 10:06 am

Re: Hardware PWM in python???

Sat Oct 07, 2017 12:06 pm

The vast majority of IR remotes follow the NEC protocol, certainly the half dozen different ones that I have played with do.

It was quite easy to connect an IR receiver to the RPi and write a very short program to receive and decode the incoming IR codes from all the buttons on all the remotes and get the 2 bytes of info that is send with each button press.

NEC protocol just sends 2 bytes, first byte is the address byte (each remote has it’s own address byte but every button on that remote sends the same address byte as it is designed to send to the 1 receiver address), the second byte is the command byte (each button has a different command byte). The whole signal train is longer because there is a long start bit and a short stop bit, also both bytes r send twice, once as the byte and a second time as the inverse as a sort of checksum.

The NEC protocol also as a very short repeat signal that is a shorter start bit followed by a stop bit that is sent when you hold a button down on a remote.

My 12yr old son wrote a very simple program that can switch a GPIO pin on/off as needed to send the NEC code providing that the 555timer was creating the 38khz square wave to switch on/off. He enjoyed changing channels on our TV from the RPi using his program.

Now we wouldn’t need the 555timer hardware to create the 38khz square wave if it was simple to set the hardware PWM to 38khz with 30% duty. Is there a simple way to do this that doesn’t require running a sudo daemon or installing packages. Using both LIRC and PiGPIO requires this.



DirkS

Posts: 10169

Joined: Tue Jun 19, 2012 9:46 pm

Location: Essex, UK

Re: Hardware PWM in python???

Sat Oct 07, 2017 12:25 pm

OutoftheBOTS wrote:

Sat Oct 07, 2017 12:13 pm

Pigpio is pre-installed on RPF images. The daemon just needs to be enabled e.g. with raspi-config or the GUI config utility

Where in the GUI config utility can the daemon just needs to be enabled??

Tab ‘Interfaces’, Option ‘Remote GPIO’.
After that you will have to reboot the Pi to start the daemon.



Hardware PWM in python??? - Raspberry Pi Forums - 图5

OutoftheBOTS

Posts: 711

Joined: Tue Aug 01, 2017 10:06 am

Re: Hardware PWM in python???

Sat Oct 07, 2017 1:05 pm

Tab ‘Interfaces’, Option ‘Remote GPIO’.
After that you will have to reboot the Pi to start the daemon.

I already had this enabled but the when I try to run the code it says that can’t connect to pigpio daemon but after I run “sudo pigpio” it will work

I found the only way for me to switch it on and off is using this code but it won’t do it with consistent timing needed compared to switching a GPIO pin HIGH/LOW using RPI.GPIO

Code: Select all

  1. import pigpio
  2. import time
  3. pi = pigpio.pi()
  4. short = 0.0005
  5. long = 0.00168
  6. pi.hardware_PWM(18, 0, 0)
  7. time.sleep (0.1)
  8. pi.hardware_PWM(18, 38000, 300000)
  9. time.sleep (0.009)
  10. pi.hardware_PWM(18, 0, 0)
  11. time.sleep (0.0045)
  12. pi.hardware_PWM(18, 38000, 300000)
  13. time.sleep (short)
  14. pi.hardware_PWM(18, 0, 0)
  15. time.sleep (short)
  16. pi.hardware_PWM(18, 38000, 300000)
  17. time.sleep (short)
  18. pi.hardware_PWM(18, 0, 0)
  19. time.sleep (short)
  20. pi.hardware_PWM(18, 38000, 300000)
  21. time.sleep (short)
  22. pi.hardware_PWM(18, 0, 0)
  23. time.sleep (long)

Return to “General discussion”