members
Project 19e Raspberry PI Zero W board - Breathing LED
of Acoptex.com in Raspberry Pi Zero W
Raspberry basics: Project 19e
Project name: Raspberry PI Zero W board - Breathing LED
Tags: Raspberry, Raspberry PI Zero W board, vers 1.1, v 1.1, LED, Breathing LED, PWM, Pulse Width Modulation, Fading an LED with PWM, LED’s brightness, RPi.GPIO library, time library
Attachments: ledbright.py
In this project, you needed these parts (Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you):
1. Raspberry PI Zero W board 1 pc
2. Micro SD card with NOOBS and SD card adapter 1 pc
3. Micro USB power supply (2 A 5V or 5V 3A) 1 pc
4. USB keyboard 1 pc
5. USB mouse 1 pc
6. TV or PC monitor 1 pc
7. HDMI cable 1 pc
8. T-Cobbler Breakout and GPIO Cable 1 pc
9. Micro USB 2.0 OTG Cable 1 pc
10. Mini HDMI to HDMI Adapter (HDMI to Mini HDMI Adapter) 1 pc
11. 4-Port USB 2.0 Hub 1 pc
12. Breadboard 1 pc
13. LED 1 pc
14. Resistor 1 pc (220 Ohm)
15. Jumper cables F-M, M-M
General
We will learn how gradually increase and decrease the LED’s brightness with PWM, just like breathing. We will do it with Raspberry PI Zero W board.
An LED’s brightness is determined by controlling the amount of current flowing through it, but that requires a lot more hardware components.
By controlling the amount of time the LED is on versus off, we can change its perceived brightness. This is known as pulse width modulation (PWM).
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 (3.3 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 pulse width. To get varying analog values, you change, or modulate, that width. If you repeat this on-off pattern fast enough with some device, an LED for example, the result would be like this: the signal is a steady voltage between 0 and 3.3v controlling the brightness of the LED. (See the PWM description on the official website of Arduino)
We have two separate PWM channels for our use: PWM0 and PWM1. We can output a PWM signal on PWM0, which will show up on GPIO12 and GPIO18. Additionally, PWM1 controls the signal for GPIO13 and GPIO19.
Understanding the LED
Semiconductor light-emitting diode is a type of component which can turn electric energy into light energy via PN junctions. According to its wavelength, semiconductor lightemitting diode can be categorized into laser diode, infrared light-emitting diode and visible light-emitting diode which, called light-emitting diode for short, is usually known as LED. When we supply 2V-3V forward voltage to an LED, it will blink if only forward currents flow through the LED. Usually we have red, yellow, green, blue and color-changing LED which can change its colors with different voltages. LEDs are widely used due to its low operating voltage, low current, luminescent stability and small size.
LEDs are diodes too. Hence they have a voltage drop which usually varies between 1V-3V depending on their types. Likewise, LEDs usually can emit light if supplied 5mA-30mA current, and generally we use 10mA-20mA. So when an LED is used, it is necessary to connect a current-limiting resistor to protect the LED from over-burning.
The operating voltage of the LED is 1.8V and the operating current is 10mA-20mA. The Arduino Uno board can supply 5V or 3.3V power. We will use 5V for this project, so the minimum resistance of the current limiting resistor should be (5 V to 1.8 V)/20 = 160 Om. The 220 Om offered in the kit is suitable and you can also choose other resistors that meet the condition. The larger the resistance is, the dimmer the LED will get.
Understanding the Raspberry PI Zero W board
You can read more about it here.
Signals and connections of the Raspberry PI Zero W board



We recommend using a high-performance SD card for increased stability as well as plugging your device into an external display to see the default application booting up.
1. Setup and preparation
We assume that you have Windows 10 installed on your PC and Raspbian OS installed on your Raspberry Pi Zero W board.
- Do wiring.
- Insert your micro SD card with Raspbian OS into the TF card slot on the Raspberry Pi Zero W board. It will only fit one way.
- Connect Raspberry PI Zero W board mini HDMI port to your TV or Monitor HDMI (DVI) port (use HDMI cable and mini HDMI to HDMI adapter and/or HDMI to DVI adapter).
- Make sure that your monitor or TV is turned on, and that you have selected the right input (e.g. HDMI/DVI, etc).
- Plug in micro USB 2.0 OTG Cable to USB data port of Pi Zero and 4-Port USB 2.0 Hub to micro USB 2.0 OTG Cable.
- Plug in your USB mouse and USB keyboard to 4-Port USB 2.0 Hub.
- If you intend to connect your Raspberry Pi Zero vers 1.2 or vers 1.3 to the internet, connect a WiFi dongle to one of the 4-Port USB 2.0 Hub ports.
- Connect Micro USB power supply to Raspberry PI Zero board micro USB input.
- The Raspberry PI desktop will start up.
- Open Terminal window and type the command: sudo apt-get update
- Then type the command: sudo apt-get install
2. Making the program
- Type this command in the Terminal: sudo nano ledbright.py
- Copy and paste the code from ledbright.py to opened window. Since it is a Python code, you need to be careful with the Tab characters as it is important to group the instruction as blocks in Python.
- Press Ctrl+X, Y, Enter buttons to save the file.
- Type the command in the Terminal: sudo python ledbright.py
- You will see the gradual change of the LED's brightness between bright and dim.
Code
The code is well explaned. Please check the comments in the code.
Summary
We have learnt how gradually increase and decrease the LED’s brightness with PWM, just like breathing. We will do it with Raspberry PI Zero W board.
Libraries in use
- RPi.GPIO
- time
Script
- Look for attached code on the begining of this project
Other projects of Acoptex.com










Viewed: 1863 times