【Digi-Key Electronics Follow me Issue 3】Task Completion Summary 2
[Copy link]
Follow me Mission 3
video:
//Content 2: Task/Project Summary Report//
Task 1: Use MicroPython system (required task)
introduce:
MicroPython is a stripped-down version of the Python programming language that can run on microcontrollers and embedded systems. To use the MicroPython system, you need to do the following:
- Hardware preparation: ESP32_c3 .
- Install MicroPython:
Thonny, Python IDE for beginners
https://thonny.org /
Or download directly from the repository:
- Write code: Write code in the THONNY editor and then transfer the code to the device.
- Connect the device: First burn the BIN of ESP32 C3 that supports thonny, and connect the device to the computer with a USB data cable.
- Transfer code: Use Thonny to upload the code to the device and execute it.
- Run the code: The code is successfully uploaded to the device, and the output results can be viewed through the serial port or OLED screen.
Task 2: Drive the OLED screen on the expansion board (mandatory task)
Using Thonny to drive the OLED screen on the expansion board, the connection method is to drive the OLED screen through IIC. The OLED screen is connected to the ESP32 C3 through the I2C interface. The specific pins are the clock and data pins 6 ( SDA ) and 7 ( SCL ) respectively. The screen is ssd1306, and the library used is the ssd1306 library
Here are some general steps to help you drive the OLED screen on the expansion board in Thonny:
- Confirm the model and connection method of the OLED screen. Check the specification or manual of the OLED screen to understand its communication interface, pin definition, etc.
- Connect the OLED screen. According to the corresponding pins of C3's 6th pin (SDA) and 7th pin (SCL) of the OLED screen, you can directly snap them on and use them.
- Import the necessary SSD1306 libraries in Thonny.
- Write the driver and test the driver. Upload the driver to the C3 development board and run the program in Thonny. Observe whether the OLED screen displays normally and check whether the driver works properly.
Display circle:
Chinese:
Task 3: Control the buzzer to play music (mandatory task)
Reference material, this gave me some inspiration, so I posted it here.
1,
import time
from time import sleep
import machine
from machine import Pin, SoftI2C
# Buzzer settings
buzzer_pin = machine.Pin(5, machine.Pin.OUT)
buzzer = machine.PWM(buzzer_pin)
buzzer.freq(1047)
# Buzzer working
while True:
buzzer.duty(10)
time.sleep(1)
buzzer.duty(0)
time.sleep(1)
2,
import machine
import time
# Buzzer settings
buzzer_pin = machine.Pin(5, machine.Pin.OUT)
buzzer = machine.PWM(buzzer_pin)
buzzer.freq(1047)
# Defining frequency of each music note
NOTE_C4 = 262
NOTE_D4 = 294
NOTE_E4 = 330
NOTE_F4 = 349
NOTE_G4 = 392
NOTE_A4 = 440
NOTE_B4 = 494
NOTE_C5 = 523
NOTE_D5 = 587
NOTE_E5 = 659
NOTE_F5 = 698
NOTE_G5 = 784
NOTE_A5 = 880
NOTE_B5 = 988
# Music notes of the song, 0 is a rest/pulse
notes = [
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
NOTE_C5, NOTE_A4, NOTE_B4, 0,
NOTE_A4, NOTE_A4,
#Repeat of first part
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
NOTE_C5, NOTE_A4, NOTE_B4, 0,
#End of Repeat
NOTE_E5, 0, 0, NOTE_F5, 0, 0,
NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
NOTE_D5, 0, 0, NOTE_C5, 0, 0,
NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4,
NOTE_E5, 0, 0, NOTE_F5, 0, 0,
NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
NOTE_D5, 0, 0, NOTE_C5, 0, 0,
NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4
]
# Durations (in ms) of each music note of the song
# Quarter Note is 250 ms when songSpeed = 1.0
durations = [
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 125, 250, 125,
125, 125, 250, 125, 125,
250, 125, 250, 125,
125, 125, 250, 125, 125,
125, 125, 375, 375,
250, 125,
#Rpeat of First Part
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 125, 250, 125,
125, 125, 250, 125, 125,
250, 125, 250, 125,
125, 125, 250, 125, 125,
125, 125, 375, 375,
#End of Repeat
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 125, 125, 125, 375,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 500,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 125, 125, 125, 375,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 500
]
def play_song():
total_notes = len(notes)
for i in range(total_notes):
current_note = notes[i]
wait = durations[i]
if current_note != 0:
buzzer.duty(512) # Set duty cycle for sound
buzzer.freq(current_note) # Set frequency of the note
else:
buzzer.duty(0) # Turn off the sound
time.sleep_ms(wait)
buzzer.duty(0) # Turn off the sound
while True:
# Play the song
play_song()
The location of the buzzer:
Task 4: Connect to WiFi network (mandatory task)
Let’s look at the picture first:
The antenna must be connected, otherwise it won't work.
The following is a reference example:
-
Import the wifi library. In Thonny's code editor, enter the following code and run:
import network
Set WiFi parameters. The name (SSID) and password of the WiFi network to connect to. In Thonny's code editor, enter the following code and run it:
-
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(SSID, PASSWORD)
Replace the real name and password of the WiFi network SSID in PASSWORD the house. My name is CMCC_....), mei1372823.......
-
Wait for the connection to succeed. You can use the following code to wait for the connection to succeed:
while not wlan.isconnected():
time.sleep(1)
print('connected to network')
-
Test the network connection. Use the wlan.ifconfig() command to view detailed information about the network connection, including the IP address, subnet mask, etc. In the Thonny code editor, enter the following code and run it:
print(wlan.ifconfig())
- View output similar to the following:
The actual code used:
# Execute the functions
scan_and_connect()
# 从时间服务器获取时间
ntptime.settime()
while True:
# 获取当前时间戳,此处加了8小时的时区差
current_time = utime.time() + 8 * 60 * 60
print("Current Time:", current_time)
# 将时间戳转换为本地时间
local_time_value = utime.localtime(current_time)
print("Local Time:", local_time_value)
# 根据日期时间转换,便于打印和显示
local_data = "{:02d}-{:02d}-{:02d}".format(local_time_value[0], local_time_value[1], local_time_value[2])
local_time = "{:02d}:{:02d}:{:02d}".format(local_time_value[3], local_time_value[4], local_time_value[5])
print(local_data)
print(local_time)
# 更新显示内容
oled.fill(0)
oled.text(local_data, 10, 25)
oled.text(local_time, 10, 40)
oled.show()
# 延时1秒
utime.sleep(1)
Task 5: Using External Sensors (Required)
Connection method:
Get the value of the ambient light sensor or temperature and humidity sensor in Thonny. The following are the general steps to get the sensor value:
- Import the required libraries. In Thonny's code editor, import the sensor-related libraries.
Library address: https://pypi.org/search/?q=aht20
- Set the sensor pins.
Connect the temperature and humidity sensor module to the I2C interface of the expansion board (next to the switch). Connect the light sensor module to the A0-D0 interface of the expansion board (next to the buzzer).
- Initialize the sensor. Initialize the sensor first, then connect to the sensor pins and set the sensor parameters.
- Read the sensor value. Use the corresponding function to read the sensor value.
from machine import Pin, SoftI2C, ADC
import ssd1306
import utime
import time
from ahtx0 import AHT20
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# 清空屏幕,并显示任务要求
oled.fill(0)
oled.text("ESP32 _ C3 fifth", 0, 0)
oled.text("ATH20:", 0, 16)
oled.text("Humi:", 0, 32)
oled.text("Light:", 0, 48)
oled.show()
# aht20
aht = AHT20(i2c)
# 光照部分
adc = ADC(Pin(2))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_12BIT) #4095
while True:
temp = aht.temperature
humi = aht.relative_humidity
light_adc = adc.read()
# 计算光照强度单位Lux
light_lux = light_adc * 350 * 1.0 / 4095
# 算出电阻值单位K
light_res = (4095 - light_adc) * 10.0 / light_adc
print("Temp(°):\n");
print('{:.2f}'.format(temp))
print("Humi(%):\n");
print('{:.2f}'.format(humi))
print("Light(lux)\n");
print('{:.2f}'.format(light_lux))
print("Light(K)\n");
print('{:.2f}'.format(light_res))
# 清除变化部分的内容
oled.fill_rect(64,16,64,48,0)
oled.text('{:.2f}'.format(temp), 64, 16)
oled.text('{:.2f}'.format(humi), 64, 32)
oled.text('{:.2f}'.format(light_lux), 64, 48)
oled.show()
# 延时1秒
time.sleep(1)
//Content 3: Compilable and downloadable code//
屏.rar
(3.58 KB, downloads: 0, 售价: 5 分芯积分)
蜂鸣器.rar
(1.15 KB, downloads: 0, 售价: 5 分芯积分)
wifi.rar
(1.58 KB, downloads: 0, 售价: 5 分芯积分)
Summarize:
Summary of programming the SSD1306 OLED display, buzzer, and WiFi connectivity using Thonny and ESP32 development board.
- SSD1306 OLED display:
Use the SSD1306 OLED display on ESP32 and use the I2C interface to communicate. First, initialize the I2C code, then set the address and pin assignment of the SSD1306. In Thonny, use the built-in I2C library to perform these operations. Complete image, text, English, Chinese display, etc.
- buzzer:
The buzzer on the ESP32 driver expansion board can usually be controlled through GPIO pins. In Thonny, the GPIO library is used to control the GPIO pins. By changing the level state of the GPIO pins, the buzzer can make sounds and sounds of different frequencies.
- WiFi connection:
ESP32 C3 has a built-in WiFi module, which is connected to a WiFi network. In Thonny, first, initialize WiFi in the code, and then enter the SSID and password of the WiFi network. If you connect to the WiFi network, you can send and receive data over the Internet.
In summary, programming the ESP32 C3 with Thonny makes it easy to control a variety of peripherals, including the SSD1306 OLED display, buzzer, and WiFi connection. By using it, you can better understand how to develop IoT projects using microcontrollers and the Internet.
|