In the previous article, I learned how to write C language to control Pico in the Arduino IDE development environment, including the initial setting method and the steps of compiling and uploading the program. After that, I will continue with the implementation of MicroPython. I will come back to learn more about Arduino IDE and Pico development in the future. The usage of the environment. The LCD display function is very important in the development of the microcontroller. This article will implement the date, time and temperature sensing value of the development board to be displayed on the LCD1602 screen. At the same time, the function of calling other programs is also used here. The functions of LCD1602 are controlled through function calls, such as display, moving cursor, etc., to make writing programs easier.
There is a temperature sensor on the Pi Pico development board, which is connected to the ADC (Analog-to-digital converter, or A/D or A to D) special pin for analog-to-digital conversion. On the specification drawing, you can see Pin 31 -35 Mark ADC0, ADC1, AGND, ADC2 and ADC_VREF (or ADC3, used to detect hardware voltage) to connect to ADC externally. The temperature sensor on the Pico development board has no physical pins, but the value can be obtained by accessing ADC4.
The access method of ADC is different from that of general GPIO. The reason is that GPIO only displays two states of high potential and low potential, while ADC will obtain different voltage values. Pico's ADC uses 12 bits to calculate the value, which means that the detected value can range from 0 to 4095. But MicroPython expands the ADC value to the 16-bit range, and the effective value ranges from 0 to 65535. The voltage of the Pico runs at 3.3V, which means that when the ADC pin is at 3.3V, it will return a value of 65535. If there is no voltage, it will return a value of 0. When the voltage applied to the pin is between 0 and 3.3V, a value between the two will be obtained.
According to the sensor specifications on the Pico development board, when the temperature is 27 degrees Celsius, it will provide a voltage of 0.706V. For every 1 degree Celsius increase, the voltage will drop by 1.721 millivolts, or 0.001721 volts. This value can be used to convert the temperature obtained by the ADC voltage at that time.
If LCD1602 communicates with I2C, it needs an external module PCF8574. If you don't know the I2C number used by this interface, you can execute the following program to get it, but this value is a decimal. If it is used in the program, it needs to be converted into hexadecimal.
Taking LCD1602 as an example, the obtained value is 39, which is converted into hexadecimal to 27. This value will be used in the later program.
LCD1602 liquid crystal display x1
PCF8574 LCD connection module x1
Breadboard x1
N wires
utime.localtime(secs): Get the date and time of the system.
If secs is not filled in or blank, use RTC to get the current date and time. The time provided here is the operating system OS or RTOS. MicroPython only uses the OS API to query the date/time. As a result, 8 groups of date numbers are returned, which are year, month, day, hour, minute, second, working day, and year day. Obtained by means of an array. As in the following example, the date and time value obtained by time = utime.localtime() will be placed in the time array. Time[0] can be used to get the year, time[1] can be used to get the month, and so on to time[7] to get the year and day. The functions required by the program include:
There is a temperature sensor on the Pi Pico development board, which is connected to the ADC (Analog-to-digital converter, or A/D or A to D) special pin for analog-to-digital conversion. On the specification drawing, you can see Pin 31 -35 Mark ADC0, ADC1, AGND, ADC2 and ADC_VREF (or ADC3, used to detect hardware voltage) to connect to ADC externally. The temperature sensor on the Pico development board has no physical pins, but the value can be obtained by accessing ADC4.
The access method of ADC is different from that of general GPIO. The reason is that GPIO only displays two states of high potential and low potential, while ADC will obtain different voltage values. Pico's ADC uses 12 bits to calculate the value, which means that the detected value can range from 0 to 4095. But MicroPython expands the ADC value to the 16-bit range, and the effective value ranges from 0 to 65535. The voltage of the Pico runs at 3.3V, which means that when the ADC pin is at 3.3V, it will return a value of 65535. If there is no voltage, it will return a value of 0. When the voltage applied to the pin is between 0 and 3.3V, a value between the two will be obtained.
According to the sensor specifications on the Pico development board, when the temperature is 27 degrees Celsius, it will provide a voltage of 0.706V. For every 1 degree Celsius increase, the voltage will drop by 1.721 millivolts, or 0.001721 volts. This value can be used to convert the temperature obtained by the ADC voltage at that time.
from machine import ADC temper_sensor = ADC(4) temper_volts = temper_sensor.read_u16()Convert the voltage value of temper_volts to the value corresponding to 12 bits.
temper_volts = temper_volts * 3.3 / 65535Subtract the obtained voltage from the voltage at 27 degrees Celsius, and divide the voltage difference obtained by the voltage change value per 1 degree temperature to calculate the temperature on the Pico development board at that time.
celsius_degrees = 27 - (temper_volts - 0.706) / 0.001721To convert to Fahrenheit temperature, the following formula can be applied:
fahrenheit_degrees = celsius_degrees * 9 / 5 + 32
If LCD1602 communicates with I2C, it needs an external module PCF8574. If you don't know the I2C number used by this interface, you can execute the following program to get it, but this value is a decimal. If it is used in the program, it needs to be converted into hexadecimal.
import machine sda=machine.Pin(0) scl=machine.Pin(1) i2c=machine.I2C(0,sda=sda, scl=scl, freq=400000) print(i2c.scan())
Taking LCD1602 as an example, the obtained value is 39, which is converted into hexadecimal to 27. This value will be used in the later program.
[Material]
Raspberry Pi Pico x1LCD1602 liquid crystal display x1
PCF8574 LCD connection module x1
Breadboard x1
N wires
[Wiring diagram]
LCD1602 needs 5V power supply, while Pico provides 3.3V, LCD1602 can display, but the font and background brightness is not enough; but there is an alternative solution, which is to connect to Pin40's VBUS. According to the description of "pico-datasheet", VBUS is micro -USB input voltage, connected to Pin 1 of the micro-USB Port, usually 5V, if not connected to USB, the voltage is 0V.Pi Pico | LCD1602 I2C |
---|---|
Pin 1(GP0) | SDA |
Pin 2(GP1) | SCL |
Pin 38(GND) | GND |
Pin 40(VBUS) | VCC |
[Code]
LCD1602 library, I refer to the function of Github developer Tyler Peppy. If you want to execute the following program, you need to download the lcd_api.py and pico_i2c_lcd.py programs of RPI-PICO-I2C-LCD . Put the three files in the same directory, which can be a computer directory or a Pico virtual disk. Pay special attention to the capitalization of the file name to be exactly the same as that written in the following program, otherwise an error will occur when calling the program. It is recommended to use all lowercase letters to avoid confusion. The localtime() function of utime is used in the program, and the description is as follows:utime.localtime(secs): Get the date and time of the system.
If secs is not filled in or blank, use RTC to get the current date and time. The time provided here is the operating system OS or RTOS. MicroPython only uses the OS API to query the date/time. As a result, 8 groups of date numbers are returned, which are year, month, day, hour, minute, second, working day, and year day. Obtained by means of an array. As in the following example, the date and time value obtained by time = utime.localtime() will be placed in the time array. Time[0] can be used to get the year, time[1] can be used to get the month, and so on to time[7] to get the year and day. The functions required by the program include:
import machine import utime from machine import ADC from machine import I2C from lcd_api import LcdApi from pico_i2c_lcd import I2cLcd I2C_ADDR = 0x27 #LCD1602 I2C address I2C_NUM_ROWS = 2 #LCD1602 column number I2C_NUM_COLS = 16 #LCD1602 row number i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000) #è¨å®šI2C lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS) utime.sleep(2) to_volts = 3.3 / 65535 temper_sensor = ADC(4) # to obtain the voltage value of the temperature sensor from the ADC(4) while True: lcd.clear() time = utime.localtime() # Display date and time lcd.putstr("{month:>02d}/{day:>02d} {HH:>02d}:{MM:>02d}:{SS:>02d}".format( month=time[1], day=time[2], HH=time[3], MM=time[4], SS=time[5])) temper_volts = temper_sensor.read_u16() * to_volts #Get the voltage of the current temperature celsius_degrees = 27 - (temper_volts - 0.706) / 0.001721 # calculation Celsius # fahrenheit_degrees = celsius_degrees * 9 / 5 + 32 # calculates Fahrenheit lcd.move_to(0,1) #The cursor jumps to the first row of the second column lcd.putstr('TEMP:') lcd.putstr(str(round(celsius_degrees,3))) lcd.putstr(' oC') # Use lowercase O instead of the degree symbol utime.sleep(3) # Pause for 3 seconds