So Easy MicroPython -NTP Clock, Counter, Timer, …, and IoT Display
*** It can be very easy to develop a script which can be used to a digital LED Clock, Counter, Timer, Scoreboard or even as an IoT Global Weather Station, if you use MyKitNeoPixel7seq library like I did. Just few lines a MCU (ESP8266 or ESP32) can become them extremely easy and fast. ***
Step 1: Upload libraries to your MCU
First, download the libraries by its link and upload it to your MCU !
- MyKitNeoPixel : A basic library for controlling WS2812B LEDs
- MyKitNeoPixel7Seq : A special library for display LED strip as clock, timer, counter, … etc
*** If no idea how to upload files to your MCU board, you can read my other article “So Easy — ESP8266/ESP32 File Management” first. ***
*** If you are interested in how easy I control my LED strip, you are welcome to read my other article “So Easy MicroPython — NeoPixel (WS2812B) LED Strip” first. ***
Step 2: How a digital LED clock works ?
- 1 Digital number is made of 7 LED pixels
- 1 Clock has 4 digitals plus 2 dots between the digitals of hour and minute
- 1 Clock is totally made of 30 LEDs, and LED pixels are indexed 0 to 29
- Controlling the ON/OFF value of the LED pixels, corresponding to what number 0~9 to be display for a bits array. The lucky thing is you don’t need to worry about how it works, if you use the MyNeoPixel7Seq library I designed. It will handle everything for those troubles for you by itself.
One question here, can I use my own design while the sequence of my clock’s LEDs are different ? And if I could use digitals more or less than 4 LED digitals ? My answer is “YES”, because the order of 7 pixels of LEDs or the size of digitals inside a clock is not limited by only 4 digitals since the library was flexible and designed to handle those differences, so you can change them if you like.
Step 3: Lets’ learn some examples
First, include the following statements onto the top of your script as below:
import utime as time
from MyKitNeoPixel7Seq import myKitNeoPixel7SeqLED_PIN = 14 # ESP8266: D5
LED_PIXELS = 30 # 30 LEDs for a digital LED clock
LED_BRI = 10 # Brightness set to 10%, to avoid dazzling
np = myKitNeoPixel7Seq(LED_PIN, LED_PIXELS, LED_BRI, text=[0, 7, 16, 23], dots=[14, 15])
*Basic Display
Example 1:Show Numbers
for n in range(4):
for i in range(10):
np.showChar(i, n) # 0~9 of each 4 digitals
time.sleep_ms(50)
Example 2:Show Dots (blinking)
for _ in range(6):
np.showDots()
np.dot_sw = not np.dot_sw
time.sleep_ms(500)
*** np.dot_sw is the switch to toggle LED’s ON/OFF ***
Example 3:Marquee Text
text = "0123456789-"
temp = text
for _ in range(len(text)):
np.showText(temp[:len(np.text)])
temp = np.strRotate(temp, 1)
time.sleep(0.5)
Example 4:Paging Text
text = "1314520-487-"
for _ in range(6):
np.showText(text[:len(np.text)])
text = np.strRotate(text, 4)
time.sleep(1)
*Counter Display
Example 5:Show Numbers without leading zeros
for num in range(11):
np.showText(str(num), 4, ' ')
Example 6:Show Numbers with leading zeros
for num in range(11):
num_str = np.strFiller(num, 4, '0')
np.showText(num_str)
*Timer Display
Example 7:Count down: every 10 ms
np.showTimer(1111, "c", True). # count 1111 milliseconds
Example 8:Count : every 10 ms
np.showTimer(1111, "c") # count 1111 milliseconds
Example 9:Count down : every second
np.showTimer(11, "s", True) # count 11 seconds
Example 10:Count : every second
np.showTimer(11, "s") # count 11 seconds
Example 11:Count down : every minute
np.showTimer(61, "m") # count 61 minutes
Example 12:Count : every minute
np.showTimer(61, "m") # count 61 minutes
*Clock Display
Example 14:Digital Clock
while True:
np.showClock()
np.dot_sw = not np.dot_sw
time.sleep(0.5)
Example 15:Streamer Clock
while True:
np.showClock360(8) # skip 8 colors in 360 color wheel
np.dot_sw = not np.dot_sw
time.sleep_ms(100)
*Scoreboard Display
Example 16:Two-teams Match
np.showScoreboard(scoreA, scoreB, colorA, colorB)
*** This demo is a simulation for recording 2 teams’ score randomly***
DEMO : Network NTP Streamer Clock
This example shows how easy for just few lines of statements, it makes a MCU to complete tasks of WiFi connection, date-time synchronized using NTP network, and combined with the streamer light effect, change the original monochrome electronic clock into more lively and interesting one.
np.updateMachineTime() # NTP synchronized MCU
while True:
np.showClock360(8) # skip 8 colors in 360 color wheel
time.sleep_ms(100)
*** Please remember since it is an internet NTP clock, your MCU should connect to network as an IoT device first. ***
*** If you want to know how easy I can simply add 2~3 lines to the script, have my MCU connect to WiFi , then you might be interested to refer my other article “So Easy MicroPython — WiFi Connection, MCU as IoT” . ***
That’s all, so easy right ? Hope this helps !
Posted by Yungger
If it really help you, you are welcome to clap your hands clicking on top-left icon, or want to buy me a coffee to encourage me to write more, I thank you too 😘 😘 !!