首页 > 其他分享 >【ESP32】DIY一个电子测光仪

【ESP32】DIY一个电子测光仪

时间:2024-11-13 21:15:05浏览次数:3  
标签:group text ESP32 value display displayio DIY key 测光

这里写目录标题


0 前言

开发板:ESP32-S3-5691
开发环境:circuitpython+thony


1 开箱


2 过程

2.1 下载固件

使用circuitpython的方式开发,需要先安装circuitpython

我手里的开发板是ESP32-S3 5691 找到对应型号的开发板 找到对应的固件,烧录进去
(这里要注意选择本开发板的固件,不要选错了,我第一次就烧录错了,只有按键和LED可以使用,LCD不能使用,问了陈工,陈工说固件烧录错了,哈哈哈)

在这里插入图片描述
开发板资料网址:https://learn.adafruit.com/esp32-s3-reverse-tft-feather
固件网址:https://circuitpython.org/board/adafruit_feather_esp32s3_reverse_tft/

进入固件的网址之后,下载固件,下载UF2文件,如图所示,我已经下载好了
在这里插入图片描述

2.2 烧录固件

  • 用数据线将核心板链接电脑后双击 RST 按钮
  • 电脑将弹出 FTHRS3BOOT 驱动器
  • 将下载好的固件拖进去即可完成 circuitpython的安装

安装好的样子

在这里插入图片描述

2.3 编程环境 Thonny

推荐使用编程环境Thonny

安装传送门:Thonny, Python IDE for beginner
安装网址:https://thonny.org/

小伙伴们可以自行安装

2.4 点灯大师

安装好编程环境之后,我们就开始写我们的第一个代码,我们作为点灯大师最熟悉的还是点亮一个LED。

复制例程

import board
import digitalio
import time

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)

将例程粘贴到Thonny中,点击绿色的运行按钮,即可看到现象。

在这里插入图片描述

现象是对的:背面的红色LED在闪烁,1S亮灭一次
点亮LED之后,信心大增,开始点亮LCD屏幕吧!!!

2.5 TFT屏幕

开发板板载一块TFT屏幕,ST7789,此屏幕需要依赖库文件,我们先下载它的库文件
下载地址:https://github.com/adafruit/Adafruit_CircuitPython_ST7789

方法:https://learn.adafruit.com/esp32-s3-reverse-tft-feather/displayio-example
https://github.com/adafruit/Adafruit_CircuitPython_Display_Text

先在github下载这两个文件夹,从中选取所需移植的文件

  • adafruit_st7789.py
  • adafruit_display_text

将下载好的这两个文件,复制粘贴到开发板的lib文件夹下即可

ST7789屏幕的例程代码:https://github.com/adafruit/Adafruit_CircuitPython_ST7789

import board
import displayio
# Starting in CircuitPython 9.x fourwire will be a seperate internal library
# rather than a component of the displayio library
try:
    from fourwire import FourWire
except ImportError:
    from displayio import FourWire

from adafruit_st7789 import ST7789

displayio.release_displays()

spi = board.SPI()
while not spi.try_lock():
    pass
spi.configure(baudrate=24000000) # Configure SPI for 24MHz
spi.unlock()
tft_cs = board.D5
tft_dc = board.D6

display_bus = FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D9)

display = ST7789(display_bus, width=240, height=240, rowstart=80)

# Make the display context
splash = displayio.Group()
display.root_group = splash

color_bitmap = displayio.Bitmap(240, 240, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0xFF0000

bg_sprite = displayio.TileGrid(color_bitmap,
                               pixel_shader=color_palette,
                               x=0, y=0)
splash.append(bg_sprite)

while True:
    pass

CV到code.py
运行,LCD没有反应……
寻找解决方法:https://learn.adafruit.com/esp32-s3-reverse-tft-feather/displayio-example
在这一页里其实介绍了怎么使用LCD,最开始没有仔细看在这里插入图片描述

DisplayIO Example
Your board comes with a lovely TFT display built right in. You can use the display with CircuitPython and the displayio module. This module allows you to easily write Python code that lets you create graphics.
CircuitPython Usage
To use with CircuitPython, you need to first install the adafruit_display_text library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folders:

  • /adafruit_display_text
  • /adafruit_bitmap_font

翻译一下就是

DisplayIO 示例
您的主板内置了一个可爱的 TFT 显示屏。您可以将显示器与 CircuitPython 和 displayio 模块一起使用。此模块允许您轻松编写用于创建图形的 Python 代码。
CircuitPython 用法
要与 CircuitPython 一起使用,您需要首先将 adafruit_display_text 库及其依赖项安装到 CIRCUITPY 驱动器上的 lib 文件夹中。然后,您需要使用示例脚本更新 code.py。
值得庆幸的是,我们可以一次性完成此操作。在下面的示例中,单击下面的 Download Project Bundle 按钮,以 zip 文件的形式下载必要的库和 code.py 文件。解压缩 zip 文件的内容,并将整个 lib 文件夹和 code.py 文件复制到 CIRCUITPY 驱动器
您的 CIRCUITPY/lib 文件夹应包含以下文件夹:

  • /adafruit_display_text
  • /adafruit_bitmap_font

那我们现在需要按照官方给出的步骤操作即可。

在这里插入图片描述
下载这个zip文件,解压,将里面的文件复制粘贴到开发板的lib文件夹下面

\Adafruit_Feather_ESP32-S2_Reverse_TFT\display\CircuitPython 9.x 文件夹下的文件有两个

  • lib
  • code.py

整个 lib 文件夹和 code.py 文件复制到 CIRCUITPY 驱动器
然后就发现,lib文件夹下多了几个后缀为.mpy的文件
在这里插入图片描述
在这里插入图片描述
现在,我们再运行一下程序试试,能否点亮TFT屏幕

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This test will initialize the display using displayio and draw a solid green
background, a smaller purple rectangle, and some yellow text.
"""
import board
import terminalio
import displayio
from adafruit_display_text import label

# First set some parameters used for shapes and text
BORDER = 20
FONTSCALE = 2
BACKGROUND_COLOR = 0x00FF00  # Bright Green
FOREGROUND_COLOR = 0xAA0088  # Purple
TEXT_COLOR = 0xFFFF00

display = board.DISPLAY

# Make the display context
splash = displayio.Group()
display.root_group = splash

color_bitmap = displayio.Bitmap(display.width, display.height, 1)
color_palette = displayio.Palette(1)
color_palette[0] = BACKGROUND_COLOR

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(
    display.width - BORDER * 2, display.height - BORDER * 2, 1
)
inner_palette = displayio.Palette(1)
inner_palette[0] = FOREGROUND_COLOR
inner_sprite = displayio.TileGrid(
    inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER
)
splash.append(inner_sprite)

# Draw a label
text = "Hello World!"
text_area = label.Label(terminalio.FONT, text=text, color=TEXT_COLOR)
text_width = text_area.bounding_box[2] * FONTSCALE
text_group = displayio.Group(
    scale=FONTSCALE,
    x=display.width // 2 - text_width // 2,
    y=display.height // 2,
)
text_group.append(text_area)  # Subgroup for text scaling
splash.append(text_group)

while True:
    pass

在这里插入图片描述

终于点亮了这块屏幕

2.6 BH1750传感器

BH1750 数字光传感器模块的优点以及信号的获取

  • 硬件接线,本传感器是I2C协议的,开发板后面的丝印有显示SCL、SDA、3V3、GND,先把硬件接线完成

  • 源码:https://github.com/adafruit/Adafruit_CircuitPython_ST7789

  • 在github里下载库文件adafruit_st7789.py,然后放到lib文件夹下,复制源码

import time
import board
import adafruit_bh1750

i2c = board.I2C()
sensor = adafruit_bh1750.BH1750(i2c)

while True:
    print("%.2f Lux"%sensor.lux)
    time.sleep(1)

运行代码,效果如下:
在这里插入图片描述
在这里插入图片描述
可以读取到传感器的数据

参考大佬的代码:
https://forum.eepw.com.cn/thread/387489/1/#2
https://share.eepw.com.cn/share/download/id/394263

import time
import board
import terminalio
import displayio
import adafruit_bh1750
import math
from digitalio import DigitalInOut, Direction, Pull
from adafruit_display_text import label
from adafruit_bh1750 import Resolution

#keypad
KEY_NULL = 0
KEY_D0   = 1
KEY_D1   = 2
KEY_D2   = 4

key0 = DigitalInOut(board.BOOT0)
key1 = DigitalInOut(board.D1)
key2 = DigitalInOut(board.D2)

def keyInit():
    key0.direction = Direction.INPUT
    key0.pull = Pull.UP
    key1.direction = Direction.INPUT
    key1.pull = Pull.DOWN
    key2.direction = Direction.INPUT
    key2.pull = Pull.DOWN

def keyDetect():
    key_value = KEY_NULL
    if key0.value == 0:
        key_value +=KEY_D0
    if key1.value:
        key_value +=KEY_D1
    if key2.value:
        key_value +=KEY_D2
    return key_value

def isKeyPressed():
    key_value = keyDetect()
    if key_value:
        time.sleep(0.01) 
        if key_value == keyDetect():
            return key_value
    return 0

# display
BORDER = 20
TEXT_HIGH = 26
FONTSCALE = 2
BACKGROUND_COLOR = 0x00FF00  # Bright Green
FOREGROUND_COLOR = 0xAA0088  # Purple
TEXT_COLOR = 0xFFFF00        # Yellow
TEXT_COLOR_RED = 0xFF0000    # Red
TEXT_COLOR_WHITE = 0xFFFFFF  # White


# BH1750
i2c = board.I2C()
sensor = adafruit_bh1750.BH1750(i2c)

AV = [
    '1  ',
    '1.4',
    '2  ',
    '2.8',
    '4  ',
    '5.6',
    '8  ',
    '11 ',
    '16 ',
    '22 ',
    '32 '
]
TV = [
    '1     ',
    '1/2   ',
    '1/4   ',
    '1/8   ',
    '1/15  ',
    '1/30  ',
    '1/60  ',
    '1/125 ',
    '1/250 ',
    '1/500 ',
    '1/1000'
]
SV = [
    '100 ',
    '200 ',
    '400 ',
    '800 ',
    '1600'
]

def showCameraInfo(svIdx : int, avIdx : int):
    lux = sensor.lux
    ev = math.floor(2 + math.log(lux / 10) / math.log(2))

    display = board.DISPLAY

    splash = displayio.Group()
    display.root_group = splash

    text_Line0 = "LUX: " + ("%0.2f" % lux)
    text_Line0_area = label.Label(terminalio.FONT, text=text_Line0, color=TEXT_COLOR)
    text_Line0_group = displayio.Group(
        scale=FONTSCALE, x=0, y=TEXT_HIGH  // 2
    )
    text_Line0_group.append(text_Line0_area)
    splash.append(text_Line0_group)

    text_Line1 = "EV: " + str(ev)
    text_Line1_area = label.Label(terminalio.FONT, text=text_Line1, color=TEXT_COLOR)
    text_Line1_group = displayio.Group(
        scale=FONTSCALE, x=0, y=TEXT_HIGH * 2 - TEXT_HIGH  // 2
    )
    text_Line1_group.append(text_Line1_area)
    splash.append(text_Line1_group)

    text_Line2 = "F: " + AV[avIdx]
    text_Line2_area = label.Label(terminalio.FONT, text=text_Line2, color=TEXT_COLOR)
    text_Line2_group = displayio.Group(
        scale=FONTSCALE, x=0, y=TEXT_HIGH * 3 - TEXT_HIGH  // 2
    )
    text_Line2_group.append(text_Line2_area)
    splash.append(text_Line2_group)

    text_Line3 = "ISO: " + SV[svIdx]
    text_Line3_area = label.Label(terminalio.FONT, text=text_Line3, color=TEXT_COLOR)
    text_Line3_group = displayio.Group(
        scale=FONTSCALE, x=0, y=TEXT_HIGH * 4 - TEXT_HIGH  // 2
    )
    text_Line3_group.append(text_Line3_area)
    splash.append(text_Line3_group)

    text_Line4 = "S: " + TV[ev + svIdx - avIdx]
    text_Line4_area = label.Label(terminalio.FONT, text=text_Line4, color=TEXT_COLOR)
    text_Line4_group = displayio.Group(
        scale=FONTSCALE, x=0, y=TEXT_HIGH * 5 - TEXT_HIGH  // 2
    )
    text_Line4_group.append(text_Line4_area)
    splash.append(text_Line4_group)

def cameraInfoHdl(old_key_value : int):
    key_value = old_key_value

    svIdx = 0
    avIdx = 0
    showCameraInfo(svIdx, avIdx)
    while True:
        key_value=isKeyPressed()
        if old_key_value != key_value:
            old_key_value = key_value

            if key_value == KEY_D0:
                break

            elif key_value == KEY_D1:
                svIdx += 1
                if len(SV) <= svIdx :
                    svIdx = 0
                showCameraInfo(svIdx, avIdx)

            elif key_value == KEY_D2:
                avIdx +=1
                if avIdx >= len(AV):
                    avIdx = 0
                showCameraInfo(svIdx, avIdx)

def showLux(mode : int):
    display = board.DISPLAY

    splash = displayio.Group()
    display.root_group = splash

    if mode == 0:
        text_mode = "Mode High"
    elif mode == 1:
        text_mode = "Mode Low"
    elif mode == 2:
        text_mode = "Mode Middle"
    text_mode_area = label.Label(terminalio.FONT, text=text_mode, color=TEXT_COLOR)
    text_mode_group = displayio.Group(
        scale=FONTSCALE, x=0, y=display.height // 4
    )
    text_mode_group.append(text_mode_area)
    splash.append(text_mode_group)

    text_lux = ("%0.2f" % sensor.lux) + "Lux"
    text_lux_area = label.Label(terminalio.FONT, text=text_lux, color=TEXT_COLOR)
    text_lux_group = displayio.Group(
        scale=FONTSCALE, x=0, y=display.height // 2
    )
    text_lux_group.append(text_lux_area)
    splash.append(text_lux_group)

def luxHdl(old_key_value : int):
    key_value = old_key_value
    i = 0
    showLux(i)

    while True:
        key_value=isKeyPressed()
        if old_key_value != key_value:
            old_key_value = key_value

            if key_value == KEY_D0:
                break

            elif key_value == KEY_D1:
                i += 1
                if i == 1:
                    sensor.set_mode(Resolution.LOW)
                elif i == 2:
                    sensor.set_mode(Resolution.MID)
                elif i == 3:
                    sensor.set_mode(Resolution.HIGH)
                    i = 0
            showLux(i)

def showHelloEEPW():
    display = board.DISPLAY

    splash = displayio.Group()
    display.root_group = splash
    color_palette = displayio.Palette(1)

    """
    color_bitmap = displayio.Bitmap(display.width, display.height, 1)
    color_palette[0] = BACKGROUND_COLOR
    
    bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
    splash.append(bg_sprite)
    """
    
    """
    inner_bitmap = displayio.Bitmap(
        display.width, display.height - BORDER * 2, 1
    )
    inner_palette = displayio.Palette(1)
    inner_palette[0] = FOREGROUND_COLOR
    inner_sprite = displayio.TileGrid(
        inner_bitmap, pixel_shader=inner_palette, x=0, y=BORDER
    )
    splash.append(inner_sprite)
    """
    
    text = "EEPW & DigiKey"
    text_area = label.Label(terminalio.FONT, text=text, color=TEXT_COLOR)
    text_width = text_area.bounding_box[2] * FONTSCALE
    text_group = displayio.Group(
        scale=FONTSCALE, x=display.width // 2 - text_width // 2, y=display.height // 2
    )
    text_group.append(text_area)
    splash.append(text_group)

def showEEPWHdl(old_key_value : int):
    key_value = old_key_value

    showHelloEEPW()
    while True:
        key_value=isKeyPressed()
        if old_key_value != key_value:
            old_key_value = key_value

            if key_value == KEY_D0:
                break


def showFunctionMenu(index: int):
    display = board.DISPLAY

    splash = displayio.Group()
    display.root_group = splash

    text_line0 = "EEPW & DigiKey"
    if index == 0:
        text_line0_area = label.Label(terminalio.FONT, text=text_line0, background_color=TEXT_COLOR_WHITE, color=TEXT_COLOR_RED)
    else:
        text_line0_area = label.Label(terminalio.FONT, text=text_line0, color=TEXT_COLOR)

    text_line0_group = displayio.Group(
        scale=FONTSCALE, x=0, y=TEXT_HIGH  // 2
    )
    text_line0_group.append(text_line0_area)
    splash.append(text_line0_group)
    
    text_line1 = "Lux Control"
    if index == 1:
        text_line1_area = label.Label(terminalio.FONT, text=text_line1, background_color=TEXT_COLOR_WHITE, color=TEXT_COLOR_RED)
    else:
        text_line1_area = label.Label(terminalio.FONT, text=text_line1, color=TEXT_COLOR)

    text_line1_group = displayio.Group(
        scale=FONTSCALE, x=0, y=TEXT_HIGH * 2 - TEXT_HIGH  // 2
    )
    text_line1_group.append(text_line1_area)
    splash.append(text_line1_group)

    text_line2 = "ISO"
    if index == 2:
        text_line2_area = label.Label(terminalio.FONT, text=text_line2, background_color=TEXT_COLOR_WHITE, color=TEXT_COLOR_RED)
    else:
        text_line2_area = label.Label(terminalio.FONT, text=text_line2, color=TEXT_COLOR)

    text_line2_group = displayio.Group(
        scale=FONTSCALE, x=0, y=TEXT_HIGH * 3 - TEXT_HIGH  // 2
    )
    text_line2_group.append(text_line2_area)
    splash.append(text_line2_group)

# Main function
key_value = 0
old_key_value = key_value
i = 0
keyInit()
showFunctionMenu(i)
while True:
    # 按键检测
    key_value = isKeyPressed()
    if old_key_value != key_value:
        old_key_value = key_value

        if key_value == KEY_D0:
            if i == 0:
                showEEPWHdl(key_value)
            elif i == 1:
                luxHdl(key_value)
            elif i == 2:
                cameraInfoHdl(key_value)

        elif key_value == KEY_D1:
            if i == 0:
                i = 2
            else:
                i -= 1

        elif key_value == KEY_D2:
            if i == 2:
                i = 0
            else:
                i += 1

        showFunctionMenu(i)

成果展示

上电后,按D1和D2切换选择功能项,选中的功能项会高亮显示,按D0是确定按键,进入当前的功能。
在这里插入图片描述

进入亮度检测模式后,按D1键切换亮度检测精度,D2键刷新亮度,D0键返回上一级

在这里插入图片描述

进入logo界面,按D0键返回上一级

在这里插入图片描述

进入ISO界面,按D2切换光圈大小,D1键切换ISO,D0键退出

在这里插入图片描述

标签:group,text,ESP32,value,display,displayio,DIY,key,测光
From: https://blog.csdn.net/weixin_63135906/article/details/143729060

相关文章

  • ESP32串口通信
    基于ArduinoIDE开发环境编写的ESP32程序示例:通过串口与电脑通信,按下boot按键开始以1Hz的频率发送学号,再次按下则停止发送//引入ESP32的相关库#include<Arduino.h>//定义学号,这里假设学号是123456,你需要替换成你自己的真实学号constchar*studentID="123456";......
  • 基于ESP32的桌面小屏幕实战[3]:硬件设计之主控模块、墨水屏和TP、USB转串口、蜂鸣器、
    1.主控模块主控用的是ESP32-S。在立创商城搜索它,找到ESP32-S,复制编号。回到嘉立创EDA,用编号搜原件。把原件放置在原理图中。按照之前的外设接口说明接线。注意,给引脚命名的时候,要单击鼠标右键,点击属性,在名称处编辑。打开这个芯片的数据手册,找到外围设计原理图。参......
  • ESP32开发__搭建VSCode开发环境试编译项目
    目录1.概述2.安装相关必要插件3.VSCode及相关扩展件安装3.1.VSCode3.2.ESP-IDFVisualStudioCodeExtension3.3.ConfigureESP-IDF4.Demo试运行4.1.打开工程4.2.连接设备并配置端口4.3.配置工程4.3.1. 设置“目标”芯片4.3.2.menuconfig配置工程选......
  • ESP32-S3模组上跑通esp32-camera(9)
    接前一篇文章:ESP32-S3模组上跑通esp32-camera(8) 本文内容参考:esp32-camera入门(基于ESP-IDF)_esp32camera-CSDN博客OV5640手册解读-CSDN博客ESP32_CAMCameraWebServer例程源码解析笔记(一)_voidstartcameraserver();-CSDN博客特此致谢! 一、OV5640初始化1.配置接线和......
  • ESP32-S3模组上跑通esp32-camera(10)
    接前一篇文章:ESP32-S3模组上跑通esp32-camera(9) 本文内容参考:esp32-camera入门(基于ESP-IDF)_esp32camera-CSDN博客OV5640手册解读-CSDN博客ESP32_CAMCameraWebServer例程源码解析笔记(一)_voidstartcameraserver();-CSDN博客特此致谢! 一、OV5640初始化1.配置接线和......
  • esp32实现简单的kv数据库
    我来帮你优化代码,使用SPIFFS(SPIFlashFileSystem)来实现数据持久化存储。#include<ESP8266WebServer.h>#include<ESP8266WiFi.h>#include<FS.h>#include<ArduinoJson.h>//WiFi设置constchar*ssid="你的WiFi名称";constchar*password=&quo......
  • ESP32学习笔记2(GPIO的数字输入输出功能)
    1.普通5mm直径LED参数测定实验以上为普通5mm直径LED,手册建议持续工作电流为20mA以内。以下,采用学生电源(带控压限流功能)通过限流电阻170欧给各色LED供电,通过缓慢加压测流和观察LED亮度的方法,确定电流、压降与亮度关系,实测该批次LED颜色与压降大致如下:颜色1mA状态与压降......
  • 使用 vscode 简单配置 ESP32 连接 Wi-Fi 每日定时发送 HTTP 和 HTTPS 请求
    最新博客文章链接文字更新时间:2024/11/07由于学校校园网,如果长时间不重新登陆的话,网速会下降,所以想弄个能定时发送HTTP请求的东西。由于不想给路由器刷系统,也麻烦。就开始考虑使用局域网内的服务器,不过由于服务器没有Wi-Fi模块,也不想搞USB无线wifi网卡,就想着干脆用单......
  • 基于ESP32的桌面小屏幕实战[2]:硬件设计之充电管理
    1.硬件基础知识1.1原理图设计、PCB设计、PCB(电路板)、PCBA(电路板+元器件)分别长什么样?1.2高低电平一般可以理解为输出电压=VCC就是高电平,输出电压=GND(一般是0V)就是低电平,分别用1和0来表示,这个是理想值。但实际上它也有一个范围,比如你的单片机供电压(VCC)=5V,那么datasheet里会......
  • 使用platformio平台Arduino开发ESP32-C2
    使用platformio平台Arduino开发ESP32-C2有两种方法,推荐方法二。方法一:安装vscode后安装platformio插件(参考:YourGatewaytoEmbeddedSoftwareDevelopmentExcellence·PlatformIO安装时,需要可靠的网络链接。使用platformio创建一个esp32-c3项目(platformio平台默认......