【得捷电子Follow me第1期】第二贴:在OLED屏上显示温湿度
[复制链接]
本帖最后由 walker2048 于 2023-6-25 16:05 编辑
我们先尝试用官方demo代码点亮OLED屏
复制examples/ssd1306_simpletest.py文件的内容,并将I2C引脚指定为SCL(GP9),SDA(GP8)
# Basic example of clearing and drawing pixels on a SSD1306 OLED display.
# This example and library is meant to work with Adafruit CircuitPython API.
# Author: Tony DiCola
# License: Public Domain
# Import all board pins.
import board
import busio
import displayio
import terminalio
import adafruit_displayio_ssd1306
from adafruit_display_text import label
displayio.release_displays()
# Create the I2C interface.
i2c = busio.I2C(scl=board.GP9, sda=board.GP8)
display_bus = displayio.I2CDisplay (i2c, device_address = 0x3C) # The address of my Board
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=64, rotation=180)
splash = displayio.Group() # no max_size needed
display.show(splash)
color_bitmap = displayio.Bitmap(128, 64, 1) # Full screen white
color_palette = displayio.Palette(1)
color_palette[0] = 0xFFFFFF # White
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)
# Draw a smaller inner rectangle
inner_bitmap = displayio.Bitmap(126, 62, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0x000000 # Black
inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=1, y=1)
splash.append(inner_sprite)
# Draw a label
text = "Hello, eeWorld"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00, x=20, y=15)
splash.append(text_area)
text = "This is text"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00, x=30, y=30)
splash.append(text_area)
while True:
pass
把代码复制到code.py里,可以看到屏幕顺利点亮了。
获取温湿度传感器数据
从下载的源码包里可以找到sht31的示范代码examples/sht31d_simpletest.py
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
import board
import adafruit_sht31d
# Create sensor object, communicating over the board's default I2C bus
i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
sensor = adafruit_sht31d.SHT31D(i2c)
loopcount = 0
while True:
print("\nTemperature: %0.1f C" % sensor.temperature)
print("Humidity: %0.1f %%" % sensor.relative_humidity)
loopcount += 1
time.sleep(2)
# every 10 passes turn on the heater for 1 second
if loopcount == 10:
loopcount = 0
sensor.heater = True
print("Sensor Heater status =", sensor.heater)
time.sleep(1)
sensor.heater = False
print("Sensor Heater status =", sensor.heater)
根据示范代码里的内容,调整一下I2C引脚(这里和SSD1306屏幕使用同一个I2C外设),以及输出的函数,并将所需要用到的库复制到板子上的lib目录,将代码修改成以下内容,即可看到屏幕上显示了温湿度数据。
# Basic example of clearing and drawing pixels on a SSD1306 OLED display.
# This example and library is meant to work with Adafruit CircuitPython API.
# Author: Tony DiCola
# License: Public Domain
# Import all board pins.
import board
import busio
import displayio
import terminalio
import adafruit_displayio_ssd1306
from adafruit_display_text import label
import time
import adafruit_sht31d
displayio.release_displays()
# Create the I2C interface.
i2c = busio.I2C(scl=board.GP9, sda=board.GP8)
display_bus = displayio.I2CDisplay (i2c, device_address = 0x3C) # The address of my Board
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=64, rotation=180)
splash = displayio.Group() # no max_size needed
display.show(splash)
color_bitmap = displayio.Bitmap(128, 64, 1) # Full screen white
color_palette = displayio.Palette(1)
color_palette[0] = 0xFFFFFF # White
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)
# Draw a smaller inner rectangle
inner_bitmap = displayio.Bitmap(126, 62, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0x000000 # Black
inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=1, y=1)
splash.append(inner_sprite)
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
sensor = adafruit_sht31d.SHT31D(i2c)
while True:
# Draw a label
text = "Temperature: %0.1f C" % sensor.temperature
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00, x=6, y=20)
splash.append(text_area)
text = "Humidity: %0.1f %%" % sensor.relative_humidity
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00, x=6, y=35)
splash.append(text_area)
time.sleep(2)
是不是非常简单,CircuitPython调库做一些简单的应用还是很舒服的
|