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
Question:
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()
Answers:
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.
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()
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.