Be familiar with the basic operations of Seeed Studio XIAO ESP32C3 development board, install esptool, and flash the MicroPython system to the development board to complete the operation of the entry program
1. Install esptool. If you already have esptool on your computer, you can skip this step.
2. Download MicroPython firmware from:
Be sure to go to the device manager to find out which serial port the device is in advance to avoid wasting time.
Task 2: Driving the OLED screen on the expansion board
Mission objective: Use OLED screen to display text, graphics, pictures and other information
Because the driver chip of the OLED screen is SSD1306, you can find the micropython library of ssd1306 on the Internet.
Display text and graphics at the same time. The following is the code
from machine import Pin, SoftI2C
from ssd1306 import SSD1306_I2C
from font import Font
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
screen = SSD1306_I2C(128, 64, i2c)
f=Font(screen)
screen.fill_rect(0, 0, 128, 64, 1)
screen.fill_rect(2, 2, 124, 60, 0)
f.text("hello,world!",15,25,16)
screen.show()
operation result
Task 3: Control the buzzer to play music
Task objective: Learn to use the buzzer and be able to make the buzzer play the corresponding music according to the music score
A buzzer is integrated on the expansion board. Since we need to use the buzzer to emit sounds of different frequencies, the pwm function is used here to adjust the frequency.
Write the code according to the frequency to query the pitch. The code is as follows
import machine
import utime
# 定义音调频率
tones = {'1': 277, '2': 294, '3': 330, '4': 349, '5': 392, '6': 440, '7': 494,'8':523,'9':587, '-': 0}
# 定义Bad apple!!!旋律
melody = "23456-986-2-654323456-543234321323456-986-2-654323456-543-4-5-6"
pizzo = machine.Pin(5)
pwm1 = machine.PWM(pizzo)
#frep1 = 500
#pwm1.freq(frep1)
#pwm1.duty_u16(32768)
for tone in melody:
freq = tones[tone]
if freq:
pwm1.init(duty=1000, freq=freq) # 调整PWM的频率,使其发出指定的音调
else:
pwm1.duty(0) # 空拍时一样不上电
# 停顿一下 (四四拍每秒两个音,每个音节中间稍微停顿一下)
utime.sleep_ms(400)
pwm1.duty(0) # 设备占空比为0,即不上电
utime.sleep_ms(100)
Please watch the video for demonstration. No more videos will be played here.
Task 4: Connect to WiFi network
Connect Seeed Studio XIAO ESP32C3 to a WiFi network and access Internet information
One of the great advantages of esp32 is that it is connected to the Internet. Next we only need to
It is really useful to use the network library for networking
Call the interface network.WLAN (network.STA_IF) to set the network mode
Call the interface station.scan() to scan for wifi hotspots
Call the interface station.connect(wifi_ssid, wifi_password) to connect to the wifi network
You can happily call the content on the Internet
Here, the weather is displayed on the development board by obtaining Meizu weather data.
code show as below
import network
import urequests as requests
import ujson as json
import utime as time
# 无线连接设置
wifi_ssid = "MERCURY_8A1B"
wifi_password = "060324a0"
def scan_and_connect():
station = network.WLAN(network.STA_IF)
station.active(True)
if not station.isconnected():
print("Scanning for WiFi networks, please wait...")
for ssid, bssid, channel, RSSI, authmode, hidden in station.scan():
print("* {:s}".format(ssid))
print(" - Channel: {}".format(channel))
print(" - RSSI: {}".format(RSSI))
print(" - BSSID: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*bssid))
print()
while not station.isconnected():
print("Connecting...")
station.connect(wifi_ssid, wifi_password)
time.sleep(10)
print("Connected!")
print("My IP Address:", station.ifconfig()[0])
# 联网
scan_and_connect()
import ujson as json
import utime as time
import urequests
import network
from machine import Pin, SoftI2C
import ssd1306
# i2c引脚
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# 获取天津天气信息
response = urequests.get("https://aider.meizu.com/app/weather/listWeather?cityIds=101030100")
res = json.loads(response.text)
temp = res.get("value", [{}])[0].get("realtime", {}).get("temp")
sd = res.get("value", [{}])[0].get("realtime", {}).get("sD")
# 清除屏幕
oled.fill(0)
# 显示内容
oled.text("TianJin", 10, 15)
oled.text(temp+"C "+sd+"%", 10, 25)
oled.show()
Task 5: Using External Sensors
Connect ambient light sensor or temperature and humidity sensor to obtain sensor value and convert it into real physical quantity
This time, follow me selected two external sensor modules for us, namely the temperature and humidity sensor module AHT20 and the ambient light sensor module.
Here is an example of the temperature and humidity sensor module
Through the information query, we know that this is a digital sensor, which transmits data through the I2C protocol. The chip used is AHT20, which can communicate directly through the I2C protocol according to the instructions in the specification. Due to its wide use, there is already a library written on a certain hub, so here we can directly call ahtx0.py.
code show as below
import ahtx0
from machine import Pin, SoftI2C
from ssd1306 import SSD1306_I2C
from font import Font
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
screen = SSD1306_I2C(128, 64, i2c)
sensor = ahtx0.AHT10(i2c)
f=Font(screen)
values = (sensor.temperature, sensor.relative_humidity)
f.text("T:%.2f"%(values[0]),36,25,16)
f.text("H:%.2f"%(values[1]),36,40,16)
screen.show()
sleep(2)
Part 3: Compilable and downloadable code
Summary
First of all, I would like to thank Dejie and EEWorld for giving me, a student, the opportunity to get in touch with the development board and learn Python programming.
Through this opportunity, I learned about the vast world of development boards, which inspired me to learn more about development boards.