我无法在液晶显示屏上显示温度。因为它不会让我把更新液晶屏并显示温度的部分放在

发布于 2025-01-20 11:28:49 字数 1213 浏览 2 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阪姬 2025-01-27 11:28:49

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

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

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

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

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

If you look at the code you're trying to imitate, you will see this line:

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

In your case, you're doing something similar, only with a different display. You could try:

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

This rounds the value of temp to 2 decimal places, and converts it to a string, before sending that string to the LCD.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文