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

  1. 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.

  2. 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 ...

Get Raspberry Pi Zero Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.