【问题标题】:I cant display a temp on a lcd display. Because it wont let me put the part where it updates the lcd and displays it the temp我无法在液晶显示器上显示温度。因为它不会让我把它更新液晶显示器的部分放在它上面并显示它的温度
【发布时间】:2022-08-17 04:27:50
【问题描述】:

当我尝试运行代码时,会出现此错误。

Traceback (most recent call last):
  File \"<stdin>\", line 35, in <module>
TypeError: can\'t convert \'float\' object to str implicitly

这是我要运行的代码。

import framebuf
import os
import time
import lcd
import machine
import utime
 
BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9
 
 #lcd prep
if __name__==\'__main__\':
    # Setup the LCD display
    pwm = PWM(Pin(BL))
    pwm.freq(1000)
    pwm.duty_u16(32768)#max 65535

    lcd_display = lcd.LCD_2inch()
#temp 
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)

while True:
    reading = sensor_temp.read_u16() * conversion_factor 
    temp = 27 - (reading - 0.706)/0.001721
    print(temp)
    utime.sleep(2)
    
lcd_display.fill(lcd_display.black)
lcd_display.text(temp, 0, 0, lcd_display.white) #heres where there error sends me to
lcd_display.show()
  • 看来您必须先将temp 转换为字符串,然后再将其传递给lcd_display.text

标签: python micropython raspberry-pi-pico


【解决方案1】:

如果您查看您尝试模仿的代码,您将看到这一行:

    oled.text(str(round(temperature,2)),50,8)

在您的情况下,您正在做类似的事情,只是使用不同的显示。你可以试试:

lcd_display.text(str(round(temp,2)), 0, 0, lcd_display.white) 

这会将temp 的值四舍五入到小数点后两位,并将其转换为字符串,然后将该字符串发送到 LCD。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多