Setting up brightness using PWM
Pulse wave modulation, or PWM is a technique where the power is oscillated between on and off at a certain frequency. There are several applications for this. In this recipe, we will start off easy and see how it can control the perceived brightness of an LED. By changing the oscillation frequency using PWM, we can flicker an LED to give the effect of changing brightness. Let's try it out!
Getting ready
Check the current board and mode configuration.
Note
If you have SPI or I2C enabled, make sure not to use the pins that are reserved for that mode.
The following example uses pins still available while in SPI mode, and it should work for just about any configuration that you have for your GPIO as long as you aren't using GPIO 22, 23, and 24.
We're going to be using Python and the RPi.GPIO library. If you haven't set that up, you'll find all of the information you need in Chapter 5, Getting Your Hands Dirty Using GPIO Header.
How to do it...
Here is the circuit to...