Pi IoT In Python Using Linux Drivers - PWM
Written by Harry Fairhead & Mike James   
Monday, 11 October 2021
Article Index
Pi IoT In Python Using Linux Drivers - PWM
PWM Driver
PWM Class
Number of Duty Cycles

You can easily work out the number of duty cycles available at any given frequency:

PWM Frequency

Number of clock pulses

Number of different duty cycles

Resolution in bits

10MHz

2

3

1.6

5MHz

4

5

2.3

2.5MHz

8

9

3.2

1.25MHz

16

17

4.1

625kHz

32

33

5.0

312.5kHz

64

65

6.0

156.25kHz

128

129

7.0

nkHz

20000/n

20000/n - 1

Log2(20000/n -1)

 

In many applications 8-bit resolution for the duty cycle is considered the minimum acceptable and this sets the highest frequency to about 75kHz, which is high enough for most things.

For example, if you want to control a servo motor, see later, then you need a PWM signal with a frequency of 50Hz and at this frequency you can specify the duty cycle down to about 18 bits or around 250 thousand increments – more than enough for any real servo motor.

In Chapter But Not In This Extract

  • Controlling An LED
  • How Fast Can You Modulate?
  • Controlling a Servo
    servosg90
  • What Else Can You Use PWM For?
  • Complete PWM Program

Summary

  • PWM, Pulse Width Modulation, has a fixed repetition rate but a variable duty cycle, i.e. the amount of time the signal is high or low changes.

  • PWM can be generated by software simply by changing the state of a GPIO line correctly, but it can also be generated in hardware, so relieving the processor of some work.

  • Hardware PWM can generate high speed pulses, but how quickly you can change the duty cycle is still software-limited.

  • All versions of the Pi have two hardware PWM channels which can be used and configured using Linux drivers.

  • The PWM drivers do not provide control over the PWM clock frequency which determines how accurately you can set the duty cycle.

  • A typical use of PWM is to control a servo and this only requires a PWM frequency of 50Hz. The position of the servo depends on the duty cycle.

  • You can easily invert the sense of the PWM signal, which is useful when the device is being driven by a single transistor.

  • As well as being a way of signaling, PWM can also be used to vary the amount of power or voltage transferred. The higher the duty cycle, the more power/voltage.

  • In the same way, by varying the duty cycle, you can dim an LED. As the brightness of an LED is not linear with applied voltage, you have to modify the output using a cubic law to get linear changes in brightness.

 

Raspberry Pi IoT In Python Using Linux Drivers

By Harry Fairhead & Mike James

driversPython360

Buy from Amazon.

Contents

  1.  Choosing A Pi For IoT

  2. Getting Started With Python

  3.  Drivers: A First Program

  4.  The GPIO Character Driver

  5.  GPIO Using I/O Control

  6.  GPIO Events

  7.  The Device Tree
       Extract: The DHT22   

  8. Some Electronics

  9. Pulse Width Modulation
       Extract: PWM 

  10. SPI Devices

  11. I2C Basics
       Extract: I2C ***NEW!!

  12. The I2C Linux Driver

  13. Advanced I2C

  14. Sensor Drivers

  15. 1-Wire Bus
       Extract 1-Wire And The DS18B20 

  16. Going Further With Drivers

  17. Appendix I

 <ASIN:187196265X>

<ASIN:B08YXJ743K>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


A Time To Enrol, A Time To Save
26/09/2022

September is the traditional back to school month and right now is a good time to do something that most developers find rewarding - sign up to learn something new. Whether you are embarking on a new  [ ... ]



AI-Based Artwork Wins Prize
11/09/2022

An art prize has been won by an artwork created using a combination of AI and PhotoShop. While the judges didn't know the origins of the work, they have said they'd still have given it first prize had [ ... ]


More News

pythondata

 



 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:1871962587>

<ASIN:B07S1K8KLW>

<ASIN:1871962455>

<ASIN:1871962463>



Last Updated ( Monday, 11 October 2021 )