Chapter 3 LED Bar
We have learned how to control a LED blinking, next we will learn how to control a number of LEDs.
Project 3.1 Flowing Light
In this project, we use a number of LEDs to make a flowing light.
Component List
ESP8266 x1
|
USB cable
|
||
Breadboard x1
|
|||
LED bar graph x1 |
Resistor 220Ω x1
|
Jumper wire M/M x3
|
|
Component knowledge
Let’s learn about the basic features of these components to use and understand them better.
LED bar
A LED bar graph has 10 LEDs integrated into one compact component. The two rows of pins at its bottom are paired to identify each LED like the single LED used earlier.
Circuit
Schematic diagram |
|
Hardware connection If you need any support, please feel free to contact us via: support@freenove.com |
|
If LED bar does not work, try to rotate it for 180°. The label is random.
Code
This project is designed to make a flowing water lamp. Which are these actions: First turn LED #1 ON, then turn it OFF. Then turn LED #2 ON, and then turn it OFF… and repeat the same to all 10 LEDs until the last LED is turns OFF. This process is repeated to achieve the “movements” of flowing water.
03.1_FlowingLight
Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP8266/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “03.1_FlowingLight” and double click “FlowingLight.py”.
Click “Run current script” shown in the box above, LED Bar Graph will light up from left to right and then back from right to left.
Press the “RST” button on the ESP8266 development board and exit the program. You can also click “Run current script” again.
If you have any concerns, please contact us via: support@freenove.com
The following is the program code:
1import time
2from machine import Pin
3
4pins=[13,12,14,16,5,4,0,2,15]
5
6def showled():
7 length=len(pins)
8 for i in range(0,length):
9 led=Pin(pins[i],Pin.OUT)
10 led.value(1)
11 time.sleep_ms(100)
12 led.value(0)
13 for i in range(0,length):
14 led=Pin(pins[(length-i-1)],Pin.OUT)
15 led.value(1)
16 time.sleep_ms(100)
17 led.value(0)
18
19while True:
20 showled()
Use an array to define 10 GPIO ports connected to LED Bar Graph for easier operation.
1pins=[13,12,14,16,5,4,0,2,15]
Use len() function to obtain the amount of elements in the list and use a for loop to configure pins as output mode.
1length=len(pins)
2for i in range(0,length):
3 led=Pin(pins[i],Pin.OUT)
Use two for loops to turn on LEDs separately from left to right and then back from right to left.
1for i in range(0,length):
2 led=Pin(pins[i],Pin.OUT)
3 led.value(1)
4 time.sleep_ms(100)
5 led.value(0)
6for i in range(0,length):
7 led=Pin(pins[(length-i-1)],Pin.OUT)
8 led.value(1)
9 time.sleep_ms(100)
10 led.value(0)
Reference
- for i in range(start,end,num: int=1)
For loop is used to execute a program endlessly and iterate in the order of items (a list or a string) in the sequence
start: The initial value, the for loop starts with it
end: The ending value, the for loop end with it
num: Num is automatically added each time to the data. The default value is 1







