一、目的

        这一节我们学习如何使用我们的ESP32开发板来控制控制DHT11温湿度监测和max7219 LED矩阵屏实时显示。

二、环境

        ESP32 + DHT11温湿度传感器 + 4组max7219 LED矩阵屏 + Thonny IDE + 几根杜邦线

接线方法:

三、代码

max7219驱动如下: max7219.py

"""
MicroPython max7219 cascadable 8x8 LED matrix driver
https://github.com/mcauser/micropython-max7219
MIT License
Copyright (c) 2017 Mike Causer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from micropython import const
import framebuf

_NOOP = const(0)
_DIGIT0 = const(1)
_DECODEMODE = const(9)
_INTENSITY = const(10)
_SCANLIMIT = const(11)
_SHUTDOWN = const(12)
_DISPLAYTEST = const(15)

class Matrix8x8:
    def __init__(self, spi, cs, num):
        """
        Driver for cascading MAX7219 8x8 LED matrices.
        >>> import max7219
        >>> from machine import Pin, SPI
        >>> spi = SPI(1)
        >>> display = max7219.Matrix8x8(spi, Pin('X5'), 4)
        >>> display.text('1234',0,0,1)
        >>> display.show()
        """
        self.spi = spi
        self.cs = cs
        self.cs.init(cs.OUT, True)
        self.buffer = bytearray(8 * num)
        self.num = num
        fb = framebuf.FrameBuffer(self.buffer, 8 * num, 8, framebuf.MONO_HLSB)
        self.framebuf = fb
        # Provide methods for accessing FrameBuffer graphics primitives. This is a workround
        # because inheritance from a native class is currently unsupported.
        # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
        self.fill = fb.fill  # (col)
        self.pixel = fb.pixel # (x, y[, c])
        self.hline = fb.hline  # (x, y, w, col)
        self.vline = fb.vline  # (x, y, h, col)
        self.line = fb.line  # (x1, y1, x2, y2, col)
        self.rect = fb.rect  # (x, y, w, h, col)
        self.fill_rect = fb.fill_rect  # (x, y, w, h, col)
        self.text = fb.text  # (string, x, y, col=1)
        self.scroll = fb.scroll  # (dx, dy)
        self.blit = fb.blit  # (fbuf, x, y[, key])
        self.init()

    def _write(self, command, data):
        self.cs(0)
        for m in range(self.num):
            self.spi.write(bytearray([command, data]))
        self.cs(1)

    def init(self):
        for command, data in (
            (_SHUTDOWN, 0),
            (_DISPLAYTEST, 0),
            (_SCANLIMIT, 7),
            (_DECODEMODE, 0),
            (_SHUTDOWN, 1),
        ):
            self._write(command, data)

    def brightness(self, value):
        if not 0 <= value <= 15:
            raise ValueError("Brightness out of range")
        self._write(_INTENSITY, value)

    def show(self):
        for y in range(8):
            self.cs(0)
            for m in range(self.num):
                self.spi.write(bytearray([_DIGIT0 + y, self.buffer[(y * self.num) + m]]))
            self.cs(1)

 代码如下:

from machine import Pin,SPI
import time,dht
import max7219

# 使用SPI的1;miso用不到,随便给一个IO;Pin12指的是cs; 最后的4指的是矩阵的数目
MAX = max7219.Matrix8x8(SPI(1,1000000,sck=Pin(14),mosi=Pin(13),miso=Pin(23)),Pin(12),8)
MAX.init()

def Hdt11_display():
    dht11 = dht.DHT11(Pin(2))
    dht11.measure()
    temp = dht11.temperature()
    humi = dht11.humidity()
    time.sleep(1)
    print("温度 = %.1f ℃ \t 湿度 = %.1f RH" % (temp,humi))
    
    MAX.fill(0)
    MAX.text("TEMP",0,1,1)
    MAX.show()
    time.sleep(1)
    
    MAX.fill(0)
    MAX.text("%.1f"%temp,0,1,1)
    MAX.show()
    time.sleep(1)
    
    MAX.fill(0)
    MAX.text("HUMI",0,1,1)
    MAX.show()
    time.sleep(1)
    
    MAX.fill(0)
    MAX.text("%.1f"%humi,0,1,1)
    MAX.show()
    time.sleep(1)
    
    
def main():
    MAX.brightness(7) # 亮度范围0-15
    MAX.fill(0)
    MAX.show()
    
    while True:
        Hdt11_display()
    
if __name__ == "__main__":
    main()

四、演示效果

五、购买

某宝链接如下:
https://item.taobao.com/item.htm?spm=a230r.1.14.142.1ccb56ffC1WZT1&id=695433065225&ns=1&abbucket=8#detailhttps://item.taobao.com/item.htm?spm=a230r.1.14.142.1ccb56ffC1WZT1&id=695433065225&ns=1&abbucket=8#detail

 https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-24706531953.9.36e16a4bzWi38D&id=522553143872https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-24706531953.9.36e16a4bzWi38D&id=522553143872

更多推荐