624|0

172

帖子

3

TA的资源

一粒金砂(高级)

楼主
 

【得捷电子Follow me第3期】+使用ESP32C3完成多个应用体验 [复制链接]

 

本帖最后由 EPTmachine 于 2023-12-17 22:42 编辑

很荣幸能获得Follow Me第三期的活动资格,本次使用的板卡是Seeed Studio XIAO ESP32C3。是一款基于 Espressif ESP32-C3 Wi-Fi/蓝牙双模芯片的 IoT 迷你开发板。ESP32-C3 是一款32 位 RISC-V CPU,具有强大的计算能力,包含FPU(浮点运算单元),可进行32 位单精度运算。它具有出色的射频性能,支持IEEE 802.11 b/g/n Wi-Fi和蓝牙 5 (LE)协议。正如其名,Seeed Studio XIAO ESP32C3具有小巧精致的外形,还可通过表面贴装整合到更复杂的PCB上。

本次活动指定的配件都是Seeed Studio出品的,在其官网上可以找到详细的板卡资料。
XIAO ESP32C3介绍界面

除了XIAO ESP32C3外,本次选购的配件包括了XIAO系列的通用拓展板(板载OLED、蜂鸣器、用户按键、SD卡插槽等外设)、外接天线、AHT20温湿度传感器、光照传感器等模块,可以用于实现常见的IOT应用。

开发板各个组件的实物连接图如下。

开发板的整体设计很紧凑,而且官网提供资料很丰富,便于开发者使用。

主要完成了以下几个任务,相应的代码如下

ESP32_C3.zip

(160.55 KB, 下载次数: 1)



介绍视频:[https://training.eeworld.com.cn/video/38742]

任务一:使用MicroPython系统

安装Thonny

使用MicroPython进行编程需要在开发板和PC上安装和配置MicroPython环境。

在PC端,安装Thonny IDE,安装过程十分简单,安装选项选择默认即可。

Thonny 还提供了免安装的版本,两者的使用方式很简单,大家可以自行选择适合自己的版本。

烧写ESP32C3固件

XIAO ESP32C3需要烧录相应的MicroPython镜像来支持MicroPython程序运行。在win10环境中需要安装Python和esptool支持程序的烧写。

在Python官网下载Python3.11的安装包。在命令行中输入python --version查看安装的Python版本。

接下来安装esptool工具

  1. pip install esptool

从github上拉取esptool的仓库(这里感觉有点怪,win10的Python环境太乱了,我的电脑安装了很多Python版本,这里虽然运行成功了,不保证能复现。。。)

  1. git clone https://github.com/espressif/esptool.git

在第一次烧录MicroPython的镜像时,需要进行全片的flash擦除,这里的COM23根据自己系统和端口不同更换为合适的参数。

  1. ./esptool.py --chip esp32c3 --port COM23 erase_flash

从MicroPython的官网下载ESP32C3的固件,将固件放到epstool仓库的根目录。

然后从地址0x00开始烧写固件。

  1. ./esptool.py --chip esp32c3 --port COM23 --baud 921600 write_flash -z 0x0 ESP32_GENERIC_C3-20231005-v1.21.0.bin

开发环境使用

Thonny 启动后提示环境配置,选择默认即可。

在View选项卡中选择需要显示的菜单,帮助开发者开发。

烧写的固件中自带了常用的测试工具,只需要编写几行代码就可以开始编写应用程序了。

Thonny中提供了可以用于输入命令行的Shell,可用于数据不同的Python指令。

在烧写的micropython固件中集成了系统功能函数,可用于查看系统的信息,了解运行的硬件平台的参数和固件的信息。通过输入help('modules')可以查看安装好的库。

使用import os导入系统功能函数,在shell调用相关的函数,比如os.uname即可得到系统的信息。

在Thonny操作界面的右侧可以看到本地文件和开发板上的文件系统,对其进行管理。

开发板上要自动运行程序,至少需要一个boot.py文件,该文件会在开发板上电后自动运行。编写下列函数并上传到开发板中。

  1. import os
  2. import gc
  3. BOARD_NAME = os.uname().machine
  4. gc.collect()
  5. print("开发板:%s" % BOARD_NAME)
  6. print("剩余内存: %0.2fM" % (gc.mem_free()/1024/1024))

输出结果如图

任务2:驱动扩展板上的OLED屏幕

任务二的目标是是使用扩展板上的OLED屏幕显示文字信息。该屏幕的驱动芯片为SSD1306,通讯协议为I2C,在官方提供的固件中提供了I2C的驱动函数,但是没有SSD1306的驱动函数。需要添加额外的驱动库,本文使用官方提供的ssd1306驱动库。

硬件介绍

ESP32C3模组的IO引脚分布以及扩展板的引脚和功能分布如下图。

由图中信息可知IO引脚使用的是GPIO6(SDA)和GPIO7(SCL)。

驱动库上传

在micropython中,第三方的驱动库一般都放在根目录下的lib文件夹中。
驱动库管理

软件编写

确定了OLED屏幕的驱动接口和驱动库,在此基础上就可以编写相应的应用程序了。

首先是显示文字,以下代码实现在OLED屏幕上显示文字

  1. import time
  2. from machine import Pin, SoftI2C
  3. import ssd1306
  4. import math
  5. # ESP8266 Pin assignment
  6. i2c = SoftI2C(scl=Pin(7), sda=Pin(6)) # Adjust the Pin numbers based on your connections
  7. oled_width = 128
  8. oled_height = 64
  9. oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
  10. oled.fill(0) # Clear the screen
  11. oled.text("Hello, Seeder!", 10, 15)
  12. oled.text("/////", 30, 40)
  13. oled.text("(`3`)y", 30, 55)
  14. oled.show() # Show the text

效果如图所示。

接下来是在屏幕上显示图形。

  1. import time
  2. from machine import Pin, SoftI2C
  3. import ssd1306
  4. import math
  5. # ESP32C3 Pin assignment
  6. i2c = SoftI2C(scl=Pin(7), sda=Pin(6)) # Adjust the Pin numbers based on your connections
  7. oled_width = 128
  8. oled_height = 64
  9. oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
  10. center_x = oled_width // 2
  11. center_y = oled_height // 2
  12. square_size = 6 # Size of each square
  13. num_squares = 12 # Number of squares
  14. angle_increment = 2 * math.pi / num_squares
  15. while True:
  16. oled.fill(0) # Clear the screen
  17. for i in range(num_squares):
  18. angle = i * angle_increment
  19. x = int(center_x + (center_x - square_size-30) * math.cos(angle))
  20. y = int(center_y + (center_x - square_size-30) * math.sin(angle))
  21. # Draw all squares
  22. for j in range(num_squares):
  23. angle_j = j * angle_increment
  24. x_j = int(center_x + (center_x - square_size-30) * math.cos(angle_j))
  25. y_j = int(center_y + (center_x - square_size-30) * math.sin(angle_j))
  26. oled.fill_rect(x_j, y_j, square_size, square_size, 1) # Draw the square
  27. oled.fill_rect(x, y, square_size, square_size, 0) # Erase the current square
  28. oled.show()
  29. time.sleep_ms(100) # Pause before next iteration

效果如图所示

任务3:使用蜂鸣器播放音乐

硬件介绍

ESP32C3模组的IO引脚分布以及扩展板的引脚和功能分布如下图。

由图中信息可知控制蜂鸣器的引脚是GPIO5(PWM)

软件编写

蜂鸣器的控制GPIO或者PWM即可实现控制,以下代码实现简单的蜂鸣器提示

  1. import time
  2. from time import sleep
  3. import machine
  4. from machine import Pin, SoftI2C
  5. # Buzzer settings
  6. buzzer_pin = machine.Pin(5, machine.Pin.OUT)
  7. buzzer = machine.PWM(buzzer_pin)
  8. buzzer.freq(1047)
  9. # Buzzer working
  10. while True:
  11. buzzer.duty(10)
  12. time.sleep(1)
  13. buzzer.duty(0)
  14. time.sleep(1)

以下代码实现控制蜂鸣器播放音乐

  1. import machine
  2. import time
  3. # Buzzer settings
  4. buzzer_pin = machine.Pin(5, machine.Pin.OUT)
  5. buzzer = machine.PWM(buzzer_pin)
  6. buzzer.freq(1047)
  7. # Defining frequency of each music note
  8. NOTE_C4 = 262
  9. NOTE_D4 = 294
  10. NOTE_E4 = 330
  11. NOTE_F4 = 349
  12. NOTE_G4 = 392
  13. NOTE_A4 = 440
  14. NOTE_B4 = 494
  15. NOTE_C5 = 523
  16. NOTE_D5 = 587
  17. NOTE_E5 = 659
  18. NOTE_F5 = 698
  19. NOTE_G5 = 784
  20. NOTE_A5 = 880
  21. NOTE_B5 = 988
  22. # Music notes of the song, 0 is a rest/pulse
  23. notes = [
  24. NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
  25. NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
  26. NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
  27. NOTE_A4, NOTE_G4, NOTE_A4, 0,
  28. NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
  29. NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
  30. NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
  31. NOTE_A4, NOTE_G4, NOTE_A4, 0,
  32. NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
  33. NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
  34. NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
  35. NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
  36. NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
  37. NOTE_D5, NOTE_E5, NOTE_A4, 0,
  38. NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
  39. NOTE_C5, NOTE_A4, NOTE_B4, 0,
  40. NOTE_A4, NOTE_A4,
  41. #Repeat of first part
  42. NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
  43. NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
  44. NOTE_A4, NOTE_G4, NOTE_A4, 0,
  45. NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
  46. NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
  47. NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
  48. NOTE_A4, NOTE_G4, NOTE_A4, 0,
  49. NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
  50. NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
  51. NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
  52. NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
  53. NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
  54. NOTE_D5, NOTE_E5, NOTE_A4, 0,
  55. NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
  56. NOTE_C5, NOTE_A4, NOTE_B4, 0,
  57. #End of Repeat
  58. NOTE_E5, 0, 0, NOTE_F5, 0, 0,
  59. NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
  60. NOTE_D5, 0, 0, NOTE_C5, 0, 0,
  61. NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4,
  62. NOTE_E5, 0, 0, NOTE_F5, 0, 0,
  63. NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
  64. NOTE_D5, 0, 0, NOTE_C5, 0, 0,
  65. NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4
  66. ]
  67. # Durations (in ms) of each music note of the song
  68. # Quarter Note is 250 ms when songSpeed = 1.0
  69. durations = [
  70. 125, 125, 250, 125, 125,
  71. 125, 125, 250, 125, 125,
  72. 125, 125, 250, 125, 125,
  73. 125, 125, 375, 125,
  74. 125, 125, 250, 125, 125,
  75. 125, 125, 250, 125, 125,
  76. 125, 125, 250, 125, 125,
  77. 125, 125, 375, 125,
  78. 125, 125, 250, 125, 125,
  79. 125, 125, 250, 125, 125,
  80. 125, 125, 250, 125, 125,
  81. 125, 125, 125, 250, 125,
  82. 125, 125, 250, 125, 125,
  83. 250, 125, 250, 125,
  84. 125, 125, 250, 125, 125,
  85. 125, 125, 375, 375,
  86. 250, 125,
  87. #Rpeat of First Part
  88. 125, 125, 250, 125, 125,
  89. 125, 125, 250, 125, 125,
  90. 125, 125, 375, 125,
  91. 125, 125, 250, 125, 125,
  92. 125, 125, 250, 125, 125,
  93. 125, 125, 250, 125, 125,
  94. 125, 125, 375, 125,
  95. 125, 125, 250, 125, 125,
  96. 125, 125, 250, 125, 125,
  97. 125, 125, 250, 125, 125,
  98. 125, 125, 125, 250, 125,
  99. 125, 125, 250, 125, 125,
  100. 250, 125, 250, 125,
  101. 125, 125, 250, 125, 125,
  102. 125, 125, 375, 375,
  103. #End of Repeat
  104. 250, 125, 375, 250, 125, 375,
  105. 125, 125, 125, 125, 125, 125, 125, 125, 375,
  106. 250, 125, 375, 250, 125, 375,
  107. 125, 125, 125, 125, 125, 500,
  108. 250, 125, 375, 250, 125, 375,
  109. 125, 125, 125, 125, 125, 125, 125, 125, 375,
  110. 250, 125, 375, 250, 125, 375,
  111. 125, 125, 125, 125, 125, 500
  112. ]
  113. def play_song():
  114. total_notes = len(notes)
  115. for i in range(total_notes):
  116. current_note = notes<i>
  117. wait = durations<i>
  118. if current_note != 0:
  119. buzzer.duty(512) # Set duty cycle for sound
  120. buzzer.freq(current_note) # Set frequency of the note
  121. else:
  122. buzzer.duty(0) # Turn off the sound
  123. time.sleep_ms(wait)
  124. buzzer.duty(0) # Turn off the sound
  125. while True:
  126. # Play the song
  127. play_song()

任务4:连接WiFi网络

ESP32C3支持Wifi功能,可以连接互联网,从网上获取信息,比如网络时间信息。

连接网络

参考官方提供的代码,填入自己的wifi名称和密码,即可实现联网。

  1. import network
  2. import urequests
  3. import utime as time
  4. # Network settings
  5. wifi_ssid = "user_wifi"
  6. wifi_password = "user_passwd"
  7. def scan_and_connect():
  8. station = network.WLAN(network.STA_IF)
  9. station.active(True)
  10. print("Scanning for WiFi networks, please wait...")
  11. for ssid, bssid, channel, RSSI, authmode, hidden in station.scan():
  12. print("* {:s}".format(ssid))
  13. print(" - Channel: {}".format(channel))
  14. print(" - RSSI: {}".format(RSSI))
  15. print(" - BSSID: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*bssid))
  16. print()
  17. while not station.isconnected():
  18. print("Connecting...")
  19. station.connect(wifi_ssid, wifi_password)
  20. time.sleep(10)
  21. print("Connected!")
  22. print("My IP Address:", station.ifconfig()[0])
  23. # Execute the functions
  24. scan_and_connect()

串口输出的结果如下:

  1. Scanning for WiFi networks, please wait...
  2. .....
  3. Connected!
  4. My IP Address: 192.168.170.168

获取网络时间并显示

网络时间的获取是通过向网络时间服务器发起请求,在返回的json字符串中提取相关的信息,配合之前实现的ssd1306显示功能,从而实现获取网络时间并显示的功能。

代码如下

  1. from machine import Pin, SoftI2C
  2. import ssd1306
  3. from time import sleep
  4. import time
  5. import network
  6. import urequests
  7. import ujson
  8. # ESP32 Pin assignment
  9. # i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
  10. # ESP32C3 Pin assignment
  11. i2c = SoftI2C(scl=Pin(7), sda=Pin(6)) # Adjust the Pin numbers based on your connections
  12. oled_width = 128
  13. oled_height = 64
  14. oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
  15. station = network.WLAN(network.STA_IF)
  16. station.active(True)
  17. # Network settings
  18. wifi_ssid = "user_wifi"
  19. wifi_password = "user_passwd"
  20. url = "http://worldtimeapi.org/api/timezone/America/New_York"
  21. print("Scanning for WiFi networks, please wait...")
  22. authmodes = ['Open', 'WEP', 'WPA-PSK' 'WPA2-PSK4', 'WPA/WPA2-PSK']
  23. for (ssid, bssid, channel, RSSI, authmode, hidden) in station.scan():
  24. print("* {:s}".format(ssid))
  25. print(" - Channel: {}".format(channel))
  26. print(" - RSSI: {}".format(RSSI))
  27. print(" - BSSID: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*bssid))
  28. print()
  29. # Continually try to connect to WiFi access point
  30. while not station.isconnected():
  31. # Try to connect to WiFi access point
  32. print("Connecting...")
  33. station.connect(wifi_ssid, wifi_password)
  34. time.sleep(10)
  35. # Display connection details
  36. print("Connected!")
  37. print("My IP Address:", station.ifconfig()[0])
  38. while True:
  39. # Perform HTTP GET request on a non-SSL web
  40. response = urequests.get(url)
  41. # Check if the request was successful
  42. if response.status_code == 200:
  43. # Parse the JSON response
  44. data = ujson.loads(response.text)
  45. # Extract the "datetime" field for New York
  46. ny_datetime = data["datetime"]
  47. # Split the date and time components
  48. date_part, time_part = ny_datetime.split("T")
  49. # Get only the first two decimal places of the time
  50. time_part = time_part[:8]
  51. # Get the timezone
  52. timezone = data["timezone"]
  53. # Clear the OLED display
  54. oled.fill(0)
  55. # Display the New York date and time on separate lines
  56. oled.text("New York Date:", 0, 0)
  57. oled.text(date_part, 0, 10)
  58. oled.text("New York Time:", 0, 20)
  59. oled.text(time_part, 0, 30)
  60. oled.text("Timezone:", 0, 40)
  61. oled.text(timezone, 0, 50)
  62. # Update the display
  63. oled.show()
  64. else:
  65. oled.text("Failed to get the time for New York!")
  66. # Update the display
  67. oled.show()

显示效果如图

任务5:使用外部传感器

了解硬件

扩展板上提供了I2C和模拟转换接口,分别对应了ATH20温湿度传感器和模拟关线传感器。

Grove AHT20介绍页面温湿度传感器的接口为I2C。其与扩展板的连接如图所示。

Grove Light Sensor v1.2对其进行了详细的介绍。模块与拓展板的连接如图所示。

添加驱动库

AHT20驱动库地址为:[https://github.com/targetblank/micropython_ahtx0],调用以下指令拉取相关的代码。

  1. git clone https://github.com/targetblank/micropython_ahtx0

将代码仓库中的ahtx0.py复制到设备的lib文件夹中,就可以在代码中使用温湿度传感器了。

软件编写

首先编写读取温湿度传感器的代码,以下代码实现对温湿度传感器数据的读取。

  1. import utime
  2. from machine import Pin, I2C
  3. import ahtx0
  4. i2c = I2C(0, scl=Pin(7), sda=Pin(6), freq=400000)
  5. sensor = ahtx0.AHT10(i2c)
  6. while True:
  7. print("\nTemperature: %0.2f C" % sensor.temperature)
  8. print("Humidity: %0.2f %%" % sensor.relative_humidity)
  9. utime.sleep(1)

通过串口打印出来的数据如下所示:

接下来通过ADC读取光照传感器的模拟信号,得到当前的光照强度。代码如下:

  1. import utime
  2. from machine import Pin, ADC
  3. # 模拟量
  4. sensor_dev = ADC(Pin(2))
  5. sensor_dev.atten(ADC.ATTN_11DB) # 这里配置测量量程为0~3.6V
  6. while True:
  7. sensor_value = 0
  8. for i in range(0,16):
  9. sensor_value = sensor_value + sensor_dev.read()
  10. sensor_value = sensor_value / 16
  11. Vbattf = 2 * sensor_value / 1000.0
  12. print("\nLight value is: %0.2f C" % Vbattf)
  13. utime.sleep(1)

光照强度的读取结果如下图所示:

总结

本次活动提供的ESP32C3是为物联网应用设计的,配合MicroPython可以快速实现物联网应用的快速开发。

点赞 关注
 
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
立即报名 | 2025 瑞萨电子工业以太网技术日即将开启!
3月-4月 深圳、广州、北京、苏州、西安、上海 走进全国6城
2025瑞萨电子工业以太网技术巡回沙龙聚焦工业4.0核心需求,为工程师与企业决策者提供实时通信技术最佳解决方案。
预报从速,好礼等您拿~

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表