Data Sheet

UCTRONICS Ultimate Starter Leaning Kit for Raspberry Pi #K0064
72 www.uctronics.com
8.7 Project 7: Breathing LED
8.7.1 Overview
In this lesson, we will learn how to program the Raspberry Pi to generate PWM signal. And
use the PWM square-wave signal control an LED gradually becomes brighter and then
gradually becomes dark like the animal's breath.
8.7.2 Requirement
Raspberry Pi ×1
LED ×1
220Ω Resistor ×1
Breadboard ×1
Several Jumper wires
8.7.3 Principle
1. What
s PWM ?
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means.
Digital control is used to create a square wave, a signal switched between on and off. This
on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by
changing the portion of the time the signal spends on versus the time that the signal spends
off. The duration of "on time" is called the pulse width. To get varying analog values, you
change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an
LED for example, the result is as if the signal is a steady voltage between 0 and 3.3v
controlling the brightness of the LED. In the graphic below, the green lines represent a
regular time period. This duration or period is the inverse of the PWM frequency. In other
words, with Raspberry Pi's PWM frequency at about 500Hz, the green lines would measure 2
milliseconds each. A call to pwmWrite() is on a scale of 0-1023, such that pwmWrite(1023)
requests a 100% duty cycle (always on), and pwmWrite(511) is a 50% duty cycle (on half the
time) for example.
2. Key function
Python user: