1529|0

221

帖子

2

TA的资源

纯净的硅(初级)

楼主
 

【得捷电子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调库做一些简单的应用还是很舒服的

点赞 关注(1)
 
 

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

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

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

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

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

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