In the previous article, a seven-segment display was connected to the pins of Pi Pico, and the high and low potentials of different GPIOs were set to make the seven-segment display display numbers. This article will use the TM1637 module with I2C. The module connection only requires four pins, which makes it much easier to control four seven-segment displays. Let's take a look at how to use MicroPython to implement the display of the temperature detected by the DS18B20 and display the date and time.
To copy tm1637.py or other library programs to Pi Pico, my method is to open a new file in Thonny, open the library program file, copy and paste it to the blank file opened by Thonny, and save it To Pi Pico memory.
[Material]
- Raspberry Pi Pico x1
- TM1637 Four-digit seven-segment LED display x1
- DS18B20 temperature sensor x1
- Resistance 4.7K x1
- Wires x n
[Wiring diagram]
Connecting the CLK and DIO pins to the Pico can support any two GPIO pins of I2C SCL/SDA, and you can connect the GND pin to any of the 8 GND pins of the Pico.Pi Pico接腳 | TM1637 4位數7段LED顯示器 | DS18B20 |
---|---|---|
Pin 38(GND) | GND | GND |
Pin 36(3V3) | VCC | VDD |
Pin 31(GPIO26)SDA | CLK | - |
Pin 32(GPIO27)SCL | DIO | - |
Pin 34(GPIO28) | - | DQ |
[Code]
Github:mcauser/micropython-tm1637 has many functions. I only use the display string, the date and time with a colon, and the temperature function. If you are interested, you can take a look at the sample program on Github. For example, you can set the display brightness , Let the display text scroll and other functions, have the opportunity to use other functions again. The function of calling convert_temp() function in the program can start the temperature reading. According to the specification, it must wait at least 750ms before reading the value. The functions required by the program include:import sys import utime import onewire import ds18x20 import tm1637 from machine import Pin from utime import sleep Display = tm1637.TM1637(clk=Pin(26), dio=Pin(27)) #Set tm1637 Pin ds_pin = machine.Pin(28) ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin)) #Set DS18B20 Pin roms = ds_sensor.scan() # scan devices on the bus while(True): # Get date and time time = utime.localtime() year = str("{:<4d}".format(time[0])) month = str("{:2d}".format(time[1])) day = str("{:2d}".format(time[2])) hour = int("{:2d}".format(time[3])) minute = int("{:2d}".format(time[4])) second = int("{:2d}".format(time[5])) ss = time[5] ds_sensor.convert_temp() #Start temperature reading if (ss > 25 and ss ≤ 30) or (ss > 55 and ss ≤ 60): #Display month and day for 10 seconds every minute Display.show(month+day) elif (ss > 35 and ss ≤ 40) or (ss > 5 and ss ≤ 10): #Display hours, minutes and 10 seconds every minute Display.numbers(hour,minute) #Display hours: minutes, this function will display colons: elif (ss > 45 and ss ≤ 48) or (ss > 15 and ss ≤ 18): #Display temperature for 6 seconds every minute for rom in roms: Display.temperature(int(ds_sensor.read_temp(rom))) else: Display.numbers(minute,second) #Display minutes: seconds, this function will display colons:
[Result]
In the process of implementation, I saved tm1347.py in the same directory as the above program on my computer. When I executed the program, "ImportError: no module named'tm1637'" always appeared. I found many ways to try To be resolved, but they were unable to succeed. Only when the above program and tm1347.py were saved to Pi Pico's memory, the problem was solved. So far, I still don't know the cause? If you have a friend who knows, you can also leave a message to me and tell me where is the mistake?To copy tm1637.py or other library programs to Pi Pico, my method is to open a new file in Thonny, open the library program file, copy and paste it to the blank file opened by Thonny, and save it To Pi Pico memory.
[Reference]
- Github: mcauser/micropython-tm1637
- Github: micropython/drivers/onewire
- MicroPython: DS18B20 Temperature Sensor with ESP32 and ESP8266
- MicroPython: Controlling 1-wire devices