我无法在液晶显示屏上显示温度。因为它不会让我把更新液晶屏并显示温度的部分放在
当我尝试运行代码时,出现此错误。
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()
When I try to run the code it comes up with this error.
Traceback (most recent call last):
File "<stdin>", line 35, in <module>
TypeError: can't convert 'float' object to str implicitly
Here's the code that I'm trying to run.
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看尝试模仿的代码,您将看到这一行:
在您的情况下,您正在做类似的事情,只是显示不同。您可以尝试:
这会将
temp
的值四舍五入到小数点后两位,并将其转换为字符串,然后将该字符串发送到 LCD。If you look at the code you're trying to imitate, you will see this line:
In your case, you're doing something similar, only with a different display. You could try:
This rounds the value of
temp
to 2 decimal places, and converts it to a string, before sending that string to the LCD.