【Digi-Key Electronics Follow me 2nd issue】+ Submit homework
[Copy link]
This post was last edited by superw on 2023-10-16 01:06
Content 1. Demonstration Video
Demo Video
Content 2: Project Summary Report
Task 0: Preparation
This issue of the follow me activity uses the ESP32-S3 TFT Feather from Adafruit, which can be developed in the Arduino and CircuitPython environments, or using Espressif's official ESP-IDF. Since CircuitPython itself is also developed and maintained by Adafruit, we choose to use CircuitPython for learning based on adaptability and compatibility.
First you need to flash the board with the circuitpython firmware to get circuitpython running on the development board.
Download the circuitpython UF2 file based on the ESP32-S3 TFT Feather at https://circuitpython.org/board/adafruit_feather_esp32s3_tft/ .
Then double-click the reset button on the board. A disk drive called FTHRS3BOOT will appear on the computer. Drag the UF2 file directly into it. The disk drive will disappear and automatically load the UF2 file. Then a disk drive called CIRCUITPY will appear. This means that the circuitpython firmware has been successfully flashed in, and you can learn and develop circuitpython on the board. The specific flashing process and the changes in the LED status during the process can be operated according to the official documentation.
https://learn.adafruit.com/adafruit-esp32-s3-tft-feather/circuitpython
Task 1: Control the screen to display Chinese
Task requirements: Complete screen control and display Chinese
Circuitpython has only one font built-in, which can be called through the terminalio module to control the TFT screen, but calling this module can only display English. The specific code is as follows:
import board
import terminalio
from adafruit_display_text import label
text = "Hello world"
text_area = label.Label(terminalio.FONT, text=text)
text_area.x = 10
text_area.y = 10
board.DISPLAY.show(text_area)
while True:
pass
Set the font through terminalio.FONT and display the text in text. The phenomenon is as follows:
To further display Chinese characters, a Chinese character library is needed. According to the teacher's introduction and the experience shared by other forum members in the live broadcast, the following code can display the Chinese characters of the first 20 elements in the periodic table on the screen.
import board
import displayio
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
display = board.DISPLAY
font = bitmap_font.load_font("fonts/wenquanyi_13px.pcf")
text_group = displayio.Group()
text = "氢氦锂铍硼\n碳氮氧氟氖\n钠镁铝硅磷\n硫氯氩钾钙"
text_area = label.Label(font,text=text,color=0x00FF00,x=10,y=10)
text_group.append(text_area)
display.show(text_group)
while True:
pass
This code imports a new module bitmap_font, through which the added font file can be loaded and further used. The phenomenon is as follows:
Task 2: Network Function Usage
Task requirements: Complete the use of network functions, be able to create a hotspot and connect to WiFi
According to the official document https://learn.adafruit.com/adafruit-esp32-s3-tft-feather/circuitpython-internet-test, you can use the board to connect to nearby WiFi. In this state, the board acts as a station to connect to the AP of the surrounding area. According to the document of Circuitpython's embedded WiFi module https://docs.circuitpython.org/en/8.2.x/shared-bindings/wifi/index.html , you can set the board to AP mode and wait for the surrounding device to connect. The specific code is as follows:
import board
import time
import digitalio
import wifi
import os
import ipaddress
ssid = "Adafruit"
password = "12344321"
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
button_count = 0
def AP_Mode(status):
if status:
print("ESP32-S3 AP Mode Open")
print(f"My AP MAC address: {[hex(i) for i in wifi.radio.mac_address_ap]}")
#wifi.radio.set_ipv4_address_ap
wifi.radio.start_ap(ssid,password)
if wifi.radio.ap_active:
print("ap_active")
else:
print("restart")
else:
print("ESP32-S3 AP Mode Close")
wifi.radio.stop_ap()
def Station_Mode(status):
if status:
print("ESP32-S3 Station Mode Open")
print(f"My MAC address: {[hex(i) for i in wifi.radio.mac_address]}")
print("Available WiFi networks:")
for network in wifi.radio.start_scanning_networks():
print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
network.rssi, network.channel))
wifi.radio.stop_scanning_networks()
print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}")
print(f"My IP address: {wifi.radio.ipv4_address}")
ping_ip = ipaddress.IPv4Address("8.8.8.8")
ping = wifi.radio.ping(ip=ping_ip)
# retry once if timed out
if ping is None:
ping = wifi.radio.ping(ip=ping_ip)
if ping is None:
print("Couldn't ping 'google.com' successfully")
else:
# convert s to ms
print(f"Pinging 'google.com' took: {ping * 1000} ms")
if wifi.radio.connected:
print("station connected")
else:
print("restart")
else:
print("ESP32-S3 Station Mode Close")
wifi.radio.stop_station()
while True:
if not button.value:
button_count += 1
if button_count%2:
AP_Mode(False)
Station_Mode(True)
else:
Station_Mode(False)
AP_Mode(True)
else:
a=1
led.value = not led.value
time.sleep(0.5)
When the button is pressed for the first time, the board's AP mode will be turned off, the board's station mode will be turned on, and the nearby WiFi will be connected. When the button is pressed for the second time, the board's station mode will be turned off, the board's AP mode will be turned on, and the nearby device will be waiting for connection. The phenomenon is as follows:
Task 3: Controlling the WS2812B
Task requirements: Use buttons to control the display and color switching of the onboard Neopixel LED
Use the button to count. When pressed for the first time, the Neopixel LED will display red. The second time will display green. The third time will display blue. The sequence is red, green, blue, cyan, purple, yellow, white, and black. When the button value exceeds 8, the random module will be used to generate a random number to control the color displayed by the Neopixel LED. The random module will also be used to control the brightness of the Neopixel LED. The specific code is as follows:
import board
import digitalio
import time
import random
import neopixel
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
button_count = 0
pixel = neopixel.NeoPixel(board.NEOPIXEL,1)
pixel.brightness = 0.3
while True:
if not button.value:
button_count += 1
pixel.brightness = random.random()
if button_count == 1:
pixel.fill((255,0,0))
elif button_count == 2:
pixel.fill((0,255,0))
elif button_count == 3:
pixel.fill((0,0,255))
elif button_count == 4:
pixel.fill((0,255,255))
elif button_count == 5:
pixel.fill((255,0,255))
elif button_count == 6:
pixel.fill((255,255,0))
elif button_count == 7:
pixel.fill((255,255,255))
elif button_count == 8:
pixel.fill((0,0,0))
else:
pixel.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
else:
a=1
led.value = not led.value
time.sleep(0.5)
The phenomenon is as shown in the figure:
Task 4: WS2812B Effect Control
Task requirements: Complete a Neopixel (12 or more LEDs) controller and switch the display effects through buttons and screens
The 12 Neopixel lamps are controlled by buttons to display different lighting effects, and the corresponding display is made on the TFT screen. The specific code is as follows:
import time
import board
import digitalio
import neopixel
import terminalio
import displayio
from adafruit_display_text import label
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
button_count =0
BORDER = 20
FONTSCALE = 2
BACKGROUND_COLOR = 0x00FF00 # Bright Green
FOREGROUND_COLOR = 0xAA0088 # Purple
RED_COLOR = 0xFF0000
GREEN_COLOR = 0x00FF00
BLUE_COLOR = 0x0000FF
TEXT_COLOR = 0xFFFF00
display = board.DISPLAY
# On CircuitPlayground Express, and boards with built in status NeoPixel -> board.NEOPIXEL
# Otherwise choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D1
pixel_pin = board.D5
# The number of NeoPixels
num_pixels = 12
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(
pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
)
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos * 3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos * 3)
g = 0
b = int(pos * 3)
else:
pos -= 170
r = 0
g = int(pos * 3)
b = int(255 - pos * 3)
return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)
def rainbow_cycle(wait):
for j in range(255):
for i in range(num_pixels):
pixel_index = (i * 256 // num_pixels) + j
pixels[i] = wheel(pixel_index & 255)
pixels.show()
time.sleep(wait)
def TFT_show(color,color_text):
splash = displayio.Group()
display.show(splash)
inner_bitmap = displayio.Bitmap(display.width - BORDER * 2, display.height - BORDER * 2, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = color
inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER)
splash.append(inner_sprite)
text = color_text
text_area = label.Label(terminalio.FONT, text=text, color=TEXT_COLOR)
text_width = text_area.bounding_box[2] * FONTSCALE
text_group = displayio.Group(scale=FONTSCALE,x=display.width // 2 - text_width // 2,
y=display.height // 2,)
text_group.append(text_area) # Subgroup for text scaling
splash.append(text_group)
def switch_color_show(count):
if count == 1:
pixels.fill((255, 0, 0))
pixels.show()
TFT_show(RED_COLOR,"Red")
elif count == 2:
pixels.fill((0, 255, 0))
pixels.show()
TFT_show(GREEN_COLOR,"Green")
elif count == 3:
pixels.fill((0, 0, 255))
pixels.show()
TFT_show(BLUE_COLOR,"Blue")
elif count == 4:
rainbow_cycle(0.001) # rainbow cycle with 1ms delay per step
#else:
while True:
if not button.value:
time.sleep(0.2)
button_count += 1
print("button_count %d" %button_count)
switch_color_show(button_count)
if button_count == 4:
button_count = 0
else:
pass
Experience
From the first issue of RP2040's micropython to this issue of ESP32-S3's circuitpython, Digi-Key and eeworld are really dedicated to helping everyone lay the foundation. These MCUs are very common on the market, and you can learn a lot of MCU-based micropython languages on them, which is great. I look forward to the new boards in the next issue!
Content 3: Compilable and downloadable code
|