eltoroit / eltoroit.github.io

Diary of a Salesforce developer on the magical world of electronics. Join me as I share my experiences. Let's learn together!
2 stars 0 forks source link

PWM with PIGPIO #4

Closed fivdi closed 3 years ago

fivdi commented 3 years ago

This article mentions that the code presented uses software-based PWM via the pigpio npm package. It also mentions that software-based PWM uses the CPU to continuously put either a HIGH or LOW value on the output pin, depending on the state in the duty cycle, but we can use any GPIO pin!

This isn't actully how pigpio functions. Under the covers, pigpio used the DMA (Direct Memory Access) peripheral on the Raspberry Pi rather than the CPU to generate PWM pulses. This means that it doesn't block the CPU requiring it to continuously put either a HIGH or LOW value on the output pin. It leaves the CPU free to perform other tasks while the DMA peripheral takes care of the PWM pulses in the background.

eltoroit commented 3 years ago

@fivdi, first of all, thank you so much for writing an amazing library! As you may have noticed on my blog, I am new to the Raspberry Pi world and electronics in general, so I appreciate your comments.

I am learning from a book that came with a kit that I purchased from Amazon.ca. This is what the book says

The hardware PWM only needs to be configured, does not require CPU resources and is more precise in time control. The software PWM requires the CPU to work continuously by using code to output high level and low level. This part of the code is carried out by multi-threading, and the accuracy is relatively not high enough.

So I understood the CPU was busy putting those signals. Good to know how PIGPIO works, and nobody knows it better than you, the creator. I am going to read up on DMA and update my blog.

Thank you.

fivdi commented 3 years ago

There are software based implementations of PWM that are implemented the way the book says.

eltoroit commented 3 years ago

Thanks for your comment, I have learned something new. There are actually 3 ways of performing PWM: Hardware, Software and DAM. I will be updating the PWM articles.