Help with a Waveshare 1.8inch LCD Display Module
I am using this LCD to display readings from a BME860 sensor. I need help with a few issues:
1. To display the degree sign I tried to use chr(248). It printed a different character. What is the right special character number. (For now, I did a work-around using a lower case "o" as the degree sgn.)
2. I would like to display graphic files, e.g. a thermometer. What is the code for this?
3. Can you make the font size smaller?
Thanks,
smaller?
1. To display the degree sign I tried to use chr(248). It printed a different character. What is the right special character number. (For now, I did a work-around using a lower case "o" as the degree sgn.)
2. I would like to display graphic files, e.g. a thermometer. What is the code for this?
3. Can you make the font size smaller?
Thanks,
Code: Select all
# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details: https://RandomNerdTutorials.com/raspberry-pi-pico-bme680-micropython/
# Adding waveShare 1.8" LCD
# Modified S. Stein 9/7/25 R3
from machine import Pin, I2C, SPI,PWM
from time import sleep
import time
import utime as time
from bme680R1 import *
import os
# Test 10 July 2021 Mod 8/27/25
import framebuf
import utime, time
# Source: Electrocredible.com, Language: MicroPython
from machine import ADC
adc = machine.ADC(4)
BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9
1 # New driver received from WaveShare on 10th July 2021 for 1.8" LCD
class LCD_1inch8(framebuf.FrameBuffer):
def __init__(self):
self.width = 160
self.height = 128
self.cs = Pin(CS,Pin.OUT)
self.rst = Pin(RST,Pin.OUT)
self.cs(1)
self.spi = SPI(1)
self.spi = SPI(1,1000_000)
self.spi = SPI(1,10000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
self.dc = Pin(DC,Pin.OUT)
self.dc(1)
self.buffer = bytearray(self.height * self.width * 2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
self.init_display()
self.WHITE = 0xFFFF
self.BLACK = 0x0000
self.GREEN = 0x001F
self.BLUE = 0xF800
self.RED = 0x07E0
def write_cmd(self, cmd):
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
def write_data(self, buf):
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(bytearray([buf]))
self.cs(1)
def init_display(self):
"""Initialize dispaly"""
self.rst(1)
self.rst(0)
self.rst(1)
self.write_cmd(0x36);
self.write_data(0x70);
self.write_cmd(0x3A);
self.write_data(0x05);
#ST7735R Frame Rate
self.write_cmd(0xB1);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_cmd(0xB2);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_cmd(0xB3);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_cmd(0xB4); #Column inversion
self.write_data(0x07);
#ST7735R Power Sequence
self.write_cmd(0xC0);
self.write_data(0xA2);
self.write_data(0x02);
self.write_data(0x84);
self.write_cmd(0xC1);
self.write_data(0xC5);
self.write_cmd(0xC2);
self.write_data(0x0A);
self.write_data(0x00);
self.write_cmd(0xC3);
self.write_data(0x8A);
self.write_data(0x2A);
self.write_cmd(0xC4);
self.write_data(0x8A);
self.write_data(0xEE);
self.write_cmd(0xC5); #VCOM
self.write_data(0x0E);
#ST7735R Gamma Sequence
self.write_cmd(0xe0);
self.write_data(0x0f);
self.write_data(0x1a);
self.write_data(0x0f);
self.write_data(0x18);
self.write_data(0x2f);
self.write_data(0x28);
self.write_data(0x20);
self.write_data(0x22);
self.write_data(0x1f);
self.write_data(0x1b);
self.write_data(0x23);
self.write_data(0x37);
self.write_data(0x00);
self.write_data(0x07);
self.write_data(0x02);
self.write_data(0x10);
self.write_cmd(0xe1);
self.write_data(0x0f);
self.write_data(0x1b);
self.write_data(0x0f);
self.write_data(0x17);
self.write_data(0x33);
self.write_data(0x2c);
self.write_data(0x29);
self.write_data(0x2e);
self.write_data(0x30);
self.write_data(0x30);
self.write_data(0x39);
self.write_data(0x3f);
self.write_data(0x00);
self.write_data(0x07);
self.write_data(0x03);
self.write_data(0x10);
self.write_cmd(0xF0); #Enable test command
self.write_data(0x01);
self.write_cmd(0xF6); #Disable ram power save mode
self.write_data(0x00);
#sleep out
self.write_cmd(0x11);
#DEV_Delay_ms(120);
#Turn on the LCD display
self.write_cmd(0x29);
def show(self):
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0x01)
self.write_data(0x00)
self.write_data(0xA0)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x02)
self.write_data(0x00)
self.write_data(0x82)
self.write_cmd(0x2C)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(self.buffer)
self.cs(1)
# ==============End of driver setup==============================
def colour(R,G,B):
# Get RED value
rp = int(R*31/255) # range 0 to 31
if rp < 0: rp = 0
r = rp *8
# Get Green value - more complicated!
gp = int(G*63/255) # range 0 - 63
if gp < 0: gp = 0
g = 0
if gp & 1: g = g + 8192
if gp & 2: g = g + 16384
if gp & 4: g = g + 32768
if gp & 8: g = g + 1
if gp & 16: g = g + 2
if gp & 32: g = g + 4
# Get BLUE value
bp =int(B*31/255) # range 0 - 31
if bp < 0: bp = 0
b = bp *256
colour = r+g+b
return colour
if __name__=='__main__':
pwm = PWM(Pin(BL))
pwm.freq(1000)
pwm.duty_u16(32768)#max 65535
LCD = LCD_1inch8()
#color BRG
LCD.fill(0x4)
LCD.show() # All pixels lit dark green
LCD.pixel(0,0,0xFFFF) # Left Top - OK
LCD.pixel(0,152,0xFFFF) # Left Bottom - OK
LCD.pixel(190,0,0xFFFF) # Right Top - OK
LCD.pixel(190,152,0xFFFF) # Right Bottom - OK
# RPi Pico - Pin assignment
i2c = I2C(id=0, scl=Pin(21), sda=Pin(20))
bme = BME680_I2C(i2c=i2c)
if 'BME680 log.txt' in os.listdir():
print('Header Row exists; pass on to next step!')
else:
file = open ("BME680 log.txt", "a")
print(type(file))
results1 = "Date" +'\t' +'\t' + "Time" + '\t' + "Temp." + chr(176) +'F' + '\t' + '\t' + "Hum. %" + '\t' + "Press. in Hg" + '\t' + "Gas KOhms" + "\n"
file.write(results1)
file.close()
pass
while True:
try:
year, month, day, hour, mins, secs, weekday, yearday = time.localtime()
today = "Date:" + "{}-{:02d}-{:02d}".format(month, day,year)
timenow = "Time:" +"{}:".format((hour-1)%12+1) + "%02d:%02d"% (mins,secs) +" " + chr(65+15*(hour//12))+ "M"
#temp = str(round(bme.temperature, 2)) + ' C'
temp = (bme.temperature) * (9/5) + 32
temp_F = "Temp:" + str(round(temp, 2)) + chr(248)+ ' F'
hum = "Humidity:" +str(round(bme.humidity, 2)) + '%'
humfile = str(round(bme.humidity, 2))
pres = (bme.pressure) * 0.02952998
presfile = str(round(pres, 2))
pres = "Press.: " +str(round(pres, 2)) + ' in. Hg'
gas = "VOCs:" +str(round(bme.gas/1000, 2)) + ' KOhms'
gasfile = str(round(bme.gas/1000, 2))
print('Temperature:', temp_F)
print('Humidity:', hum)
print('Pressure:', pres)
print('Gas:', gas)
print(today, timenow)
print('----------')
LCD.fill(0x4)
LCD.show()
LCD.text("Raspberry Pi Pico",00,00,LCD.WHITE) # Left edge - OK
LCD.text(today,00,20,LCD.BLUE)
LCD.text(timenow,00,40,LCD.BLACK)
LCD.text(temp_F, 00,60,colour(255,255,0))
LCD.text(hum, 00,80,colour(0,255,0))
LCD.text(pres, 00,100,colour(255,128,0))
LCD.text(gas, 00,118,colour(255, 0, 255))
#LCD.show()
#utime.sleep(5)
#LCD.fill(0x0)
#LCD.show()
file = open ("BME680 log.txt", "a")
print(type(file))
results = today + '\t' + timenow + '\t' + str(round(temp, 2)) + '\t' + humfile + '\t' + presfile + '\t' + " " + gasfile + "\n"
file.write(results)
file.close()
LCD.show()
utime.sleep(1)
LCD.fill(0xFFFF)
except OSError as e:
print('Failed to read sensor.')
sleep(30)
Re: Help with a Waveshare 1.8inch LCD Display Module
- From the extended ASCII table https://www.ascii-code.com/ , I think you are looking at CHR(176)
- Ask on a Waveshare support forum
- Ask on a Waveshare support forum
Code: Select all
temp=22
temp_F = "Temp:" + str(round(temp, 2)) + chr(176)+ ' F'
print(temp_F)
>>> %Run -c $EDITOR_CONTENT
Temp:22° F
>>> Re: Help with a Waveshare 1.8inch LCD Display Module
No. According to the docs, the glyph size is 8x8. IMHO less than 8 pixel in size is barely readable.3. Can you make the font size smaller?
See micropython, module 'framebuf'
- drawing_text.png
- drawing_text.png (27.65 KiB) Viewed 2358 times
To display the degree sign
Of course you are free to create your own font with different glyph size. Which is a considerable effort, can be reduced when only limited number of characters need to be displayed.
A sample here, add following to your LCD class:
Code: Select all
class LCD_1inch8(framebuf.FrameBuffer):
... all your existing code
def glyph(self, c, x, y, color=1):
"""display a glyph for a char "c" at given pixel
position x, y. X, y are topleft corner of glyph drawn.
For simplicity, the glyph are defined in 'ascii art'
and evaluated at runtime.
More efficient to generate byte data offline from
ascii definitions.
"""
glyphs = {
'°': [ " ** ",
" * * ",
" ** ",
" ",
" ",
" ",
" ",
" ", ],
'↑': [ " * ", # just for fun
" * * ",
" * * * ",
" * ",
" * ",
" * ",
" * ",
" * ", ],
}
if not c in glyphs:
print( f"glyphs does not contain '{c}'")
return
glyph = glyphs[c]
for row in range(0,8):
for col in range(0,8):
if glyph[row][col] == '*':
self.pixel(x+col, y+row, color)
Code: Select all
temp = 23.456
text = f"temp={temp:5.2f}"
cursor_pixel = 0
display.text(text, cursor_pixel, 0, 1)
cursor_pixel += len(text)*8
display.glyph('°', cursor_pixel, 0, 1)
cursor_pixel += 8
display.text("C", cursor_pixel, 0, 1)
cursor_pixel += 8
display.show()
- degree.jpg
- degree.jpg (188.75 KiB) Viewed 2358 times
Re: Help with a Waveshare 1.8inch LCD Display Module
I tried to run the glyph coding, but it doesn't run: NameError: name 'display' isn't defined
Here is the code. Please assit in making this work; this is good solution to the problem Thanks
Here is the code. Please assit in making this work; this is good solution to the problem Thanks
Code: Select all
# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details: https://RandomNerdTutorials.com/raspberry-pi-pico-bme680-micropython/
# Adding waveShare 1.8" LCD
# Modified S. Stein 9/7/25 R4
from machine import Pin, I2C, SPI,PWM
from time import sleep
import time
import utime as time
from bme680R1 import *
import os
# Test 10 July 2021 Mod 8/27/25
import framebuf
import utime, time
# Source: Electrocredible.com, Language: MicroPython
from machine import ADC
adc = machine.ADC(4)
BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9
1 # New driver received from WaveShare on 10th July 2021 for 1.8" LCD
class LCD_1inch8(framebuf.FrameBuffer):
def __init__(self):
self.width = 160
self.height = 128
self.cs = Pin(CS,Pin.OUT)
self.rst = Pin(RST,Pin.OUT)
self.cs(1)
self.spi = SPI(1)
self.spi = SPI(1,1000_000)
self.spi = SPI(1,10000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
self.dc = Pin(DC,Pin.OUT)
self.dc(1)
self.buffer = bytearray(self.height * self.width * 2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
self.init_display()
self.WHITE = 0xFFFF
self.BLACK = 0x0000
self.GREEN = 0x001F
self.BLUE = 0xF800
self.RED = 0x07E0
def write_cmd(self, cmd):
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
def write_data(self, buf):
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(bytearray([buf]))
self.cs(1)
def init_display(self):
"""Initialize dispaly"""
self.rst(1)
self.rst(0)
self.rst(1)
self.write_cmd(0x36);
self.write_data(0x70);
self.write_cmd(0x3A);
self.write_data(0x05);
#ST7735R Frame Rate
self.write_cmd(0xB1);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_cmd(0xB2);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_cmd(0xB3);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_cmd(0xB4); #Column inversion
self.write_data(0x07);
#ST7735R Power Sequence
self.write_cmd(0xC0);
self.write_data(0xA2);
self.write_data(0x02);
self.write_data(0x84);
self.write_cmd(0xC1);
self.write_data(0xC5);
self.write_cmd(0xC2);
self.write_data(0x0A);
self.write_data(0x00);
self.write_cmd(0xC3);
self.write_data(0x8A);
self.write_data(0x2A);
self.write_cmd(0xC4);
self.write_data(0x8A);
self.write_data(0xEE);
self.write_cmd(0xC5); #VCOM
self.write_data(0x0E);
#ST7735R Gamma Sequence
self.write_cmd(0xe0);
self.write_data(0x0f);
self.write_data(0x1a);
self.write_data(0x0f);
self.write_data(0x18);
self.write_data(0x2f);
self.write_data(0x28);
self.write_data(0x20);
self.write_data(0x22);
self.write_data(0x1f);
self.write_data(0x1b);
self.write_data(0x23);
self.write_data(0x37);
self.write_data(0x00);
self.write_data(0x07);
self.write_data(0x02);
self.write_data(0x10);
self.write_cmd(0xe1);
self.write_data(0x0f);
self.write_data(0x1b);
self.write_data(0x0f);
self.write_data(0x17);
self.write_data(0x33);
self.write_data(0x2c);
self.write_data(0x29);
self.write_data(0x2e);
self.write_data(0x30);
self.write_data(0x30);
self.write_data(0x39);
self.write_data(0x3f);
self.write_data(0x00);
self.write_data(0x07);
self.write_data(0x03);
self.write_data(0x10);
self.write_cmd(0xF0); #Enable test command
self.write_data(0x01);
self.write_cmd(0xF6); #Disable ram power save mode
self.write_data(0x00);
#sleep out
self.write_cmd(0x11);
#DEV_Delay_ms(120);
#Turn on the LCD display
self.write_cmd(0x29);
def show(self):
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0x01)
self.write_data(0x00)
self.write_data(0xA0)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x02)
self.write_data(0x00)
self.write_data(0x82)
self.write_cmd(0x2C)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(self.buffer)
self.cs(1)
# ==============End of driver setup==============================
def colour(R,G,B):
# Get RED value
rp = int(R*31/255) # range 0 to 31
if rp < 0: rp = 0
r = rp *8
# Get Green value - more complicated!
gp = int(G*63/255) # range 0 - 63
if gp < 0: gp = 0
g = 0
if gp & 1: g = g + 8192
if gp & 2: g = g + 16384
if gp & 4: g = g + 32768
if gp & 8: g = g + 1
if gp & 16: g = g + 2
if gp & 32: g = g + 4
# Get BLUE value
bp =int(B*31/255) # range 0 - 31
if bp < 0: bp = 0
b = bp *256
colour = r+g+b
return colour
if __name__=='__main__':
pwm = PWM(Pin(BL))
pwm.freq(1000)
pwm.duty_u16(32768)#max 65535
LCD = LCD_1inch8()
#color BRG
LCD.fill(0x4)
LCD.show() # All pixels lit dark green
LCD.pixel(0,0,0xFFFF) # Left Top - OK
LCD.pixel(0,152,0xFFFF) # Left Bottom - OK
LCD.pixel(190,0,0xFFFF) # Right Top - OK
LCD.pixel(190,152,0xFFFF) # Right Bottom - OK
# RPi Pico - Pin assignment
i2c = I2C(id=0, scl=Pin(21), sda=Pin(20))
bme = BME680_I2C(i2c=i2c)
if 'BME680 log.txt' in os.listdir():
print('Header Row exists; pass on to next step!')
else:
file = open ("BME680 log.txt", "a")
print(type(file))
results1 = "Date" +'\t' +'\t' + "Time" + '\t' + "Temp." + chr(176) +'F' + '\t' + '\t' + "Hum. %" + '\t' + "Press. in Hg" + '\t' + "Gas KOhms" + "\n"
file.write(results1)
file.close()
pass
def glyph(self, c, x, y, color=1):
"""display a glyph for a char "c" at given pixel
position x, y. X, y are topleft corner of glyph drawn.
For simplicity, the glyph are defined in 'ascii art'
and evaluated at runtime.
More efficient to generate byte data offline from
ascii definitions.
"""
glyphs = {
'°': [ " ** ",
" * * ",
" ** ",
" ",
" ",
" ",
" ",
" ", ],
'↑': [ " * ", # just for fun
" * * ",
" * * * ",
" * ",
" * ",
" * ",
" * ",
" * ", ],
}
if not c in glyphs:
print( f"glyphs does not contain '{c}'")
return
glyph = glyphs[c]
for row in range(0,8):
for col in range(0,8):
if glyph[row][col] == '*':
self.pixel(x+col, y+row, color)
temp = 23.456
text = f"temp={temp:5.2f}"
cursor_pixel = 0
display.text(text, cursor_pixel, 0, 1)
cursor_pixel += len(text)*8
display.glyph('°', cursor_pixel, 0, 1)
cursor_pixel += 8
display.text("C", cursor_pixel, 0, 1)
cursor_pixel += 8
display.show()
LCD.show()
Re: Help with a Waveshare 1.8inch LCD Display Module
- the glyph-method needs to be a class method inside LCD_1inch8. See https://docs.python.org/3/tutorial/classes.html
- in your code, an instance of LCD_1inch8 is created in line 226 LCD = LCD_1inch8() . So change the name 'display' to 'LCD' in lines 292 to 308
- there is a strange char '1' in line 28.
- in your code, an instance of LCD_1inch8 is created in line 226 LCD = LCD_1inch8() . So change the name 'display' to 'LCD' in lines 292 to 308
- there is a strange char '1' in line 28.
Re: Help with a Waveshare 1.8inch LCD Display Module
Thanks for the instructions to modify the code. I made the changes and got the following error:
File "<stdin>", line 296, in <module>
AttributeError: 'LCD_1inch8' object has no attribute 'glyph'
Can you determine how to recode this? Thanks
Here is my new code:
File "<stdin>", line 296, in <module>
AttributeError: 'LCD_1inch8' object has no attribute 'glyph'
Can you determine how to recode this? Thanks
Here is my new code:
Code: Select all
# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details: https://RandomNerdTutorials.com/raspberry-pi-pico-bme680-micropython/
# Adding waveShare 1.8" LCD
# Modified S. Stein 9/7/25 R4
from machine import Pin, I2C, SPI,PWM
from time import sleep
import time
import utime as time
from bme680R1 import *
import os
# Test 10 July 2021 Mod 8/27/25
import framebuf
import utime, time
# Source: Electrocredible.com, Language: MicroPython
from machine import ADC
adc = machine.ADC(4)
BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9
# New driver received from WaveShare on 10th July 2021 for 1.8" LCD
class LCD_1inch8(framebuf.FrameBuffer):
def __init__(self):
self.width = 160
self.height = 128
self.cs = Pin(CS,Pin.OUT)
self.rst = Pin(RST,Pin.OUT)
self.cs(1)
self.spi = SPI(1)
self.spi = SPI(1,1000_000)
self.spi = SPI(1,10000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
self.dc = Pin(DC,Pin.OUT)
self.dc(1)
self.buffer = bytearray(self.height * self.width * 2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
self.init_display()
self.WHITE = 0xFFFF
self.BLACK = 0x0000
self.GREEN = 0x001F
self.BLUE = 0xF800
self.RED = 0x07E0
def write_cmd(self, cmd):
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
def write_data(self, buf):
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(bytearray([buf]))
self.cs(1)
def init_display(self):
"""Initialize dispaly"""
self.rst(1)
self.rst(0)
self.rst(1)
self.write_cmd(0x36);
self.write_data(0x70);
self.write_cmd(0x3A);
self.write_data(0x05);
#ST7735R Frame Rate
self.write_cmd(0xB1);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_cmd(0xB2);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_cmd(0xB3);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_cmd(0xB4); #Column inversion
self.write_data(0x07);
#ST7735R Power Sequence
self.write_cmd(0xC0);
self.write_data(0xA2);
self.write_data(0x02);
self.write_data(0x84);
self.write_cmd(0xC1);
self.write_data(0xC5);
self.write_cmd(0xC2);
self.write_data(0x0A);
self.write_data(0x00);
self.write_cmd(0xC3);
self.write_data(0x8A);
self.write_data(0x2A);
self.write_cmd(0xC4);
self.write_data(0x8A);
self.write_data(0xEE);
self.write_cmd(0xC5); #VCOM
self.write_data(0x0E);
#ST7735R Gamma Sequence
self.write_cmd(0xe0);
self.write_data(0x0f);
self.write_data(0x1a);
self.write_data(0x0f);
self.write_data(0x18);
self.write_data(0x2f);
self.write_data(0x28);
self.write_data(0x20);
self.write_data(0x22);
self.write_data(0x1f);
self.write_data(0x1b);
self.write_data(0x23);
self.write_data(0x37);
self.write_data(0x00);
self.write_data(0x07);
self.write_data(0x02);
self.write_data(0x10);
self.write_cmd(0xe1);
self.write_data(0x0f);
self.write_data(0x1b);
self.write_data(0x0f);
self.write_data(0x17);
self.write_data(0x33);
self.write_data(0x2c);
self.write_data(0x29);
self.write_data(0x2e);
self.write_data(0x30);
self.write_data(0x30);
self.write_data(0x39);
self.write_data(0x3f);
self.write_data(0x00);
self.write_data(0x07);
self.write_data(0x03);
self.write_data(0x10);
self.write_cmd(0xF0); #Enable test command
self.write_data(0x01);
self.write_cmd(0xF6); #Disable ram power save mode
self.write_data(0x00);
#sleep out
self.write_cmd(0x11);
#DEV_Delay_ms(120);
#Turn on the LCD display
self.write_cmd(0x29);
def show(self):
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0x01)
self.write_data(0x00)
self.write_data(0xA0)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x02)
self.write_data(0x00)
self.write_data(0x82)
self.write_cmd(0x2C)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(self.buffer)
self.cs(1)
# ==============End of driver setup==============================
def colour(R,G,B):
# Get RED value
rp = int(R*31/255) # range 0 to 31
if rp < 0: rp = 0
r = rp *8
# Get Green value - more complicated!
gp = int(G*63/255) # range 0 - 63
if gp < 0: gp = 0
g = 0
if gp & 1: g = g + 8192
if gp & 2: g = g + 16384
if gp & 4: g = g + 32768
if gp & 8: g = g + 1
if gp & 16: g = g + 2
if gp & 32: g = g + 4
# Get BLUE value
bp =int(B*31/255) # range 0 - 31
if bp < 0: bp = 0
b = bp *256
colour = r+g+b
return colour
if __name__=='__main__':
pwm = PWM(Pin(BL))
pwm.freq(1000)
pwm.duty_u16(32768)#max 65535
LCD = LCD_1inch8()
#color BRG
LCD.fill(0x4)
LCD.show() # All pixels lit dark green
LCD.pixel(0,0,0xFFFF) # Left Top - OK
LCD.pixel(0,152,0xFFFF) # Left Bottom - OK
LCD.pixel(190,0,0xFFFF) # Right Top - OK
LCD.pixel(190,152,0xFFFF) # Right Bottom - OK
# RPi Pico - Pin assignment
i2c = I2C(id=0, scl=Pin(21), sda=Pin(20))
bme = BME680_I2C(i2c=i2c)
if 'BME680 log.txt' in os.listdir():
print('Header Row exists; pass on to next step!')
else:
file = open ("BME680 log.txt", "a")
print(type(file))
results1 = "Date" +'\t' +'\t' + "Time" + '\t' + "Temp." + chr(176) +'F' + '\t' + '\t' + "Hum. %" + '\t' + "Press. in Hg" + '\t' + "Gas KOhms" + "\n"
file.write(results1)
file.close()
pass
def glyph(self, c, x, y, color=1):
"""display a glyph for a char "c" at given pixel
position x, y. X, y are topleft corner of glyph drawn.
For simplicity, the glyph are defined in 'ascii art'
and evaluated at runtime.
More efficient to generate byte data offline from
ascii definitions.
"""
glyphs = {
'°': [ " ** ",
" * * ",
" ** ",
" ",
" ",
" ",
" ",
" ", ],
'↑': [ " * ", # just for fun
" * * ",
" * * * ",
" * ",
" * ",
" * ",
" * ",
" * ", ],
}
if not c in glyphs:
print( f"glyphs does not contain '{c}'")
return
glyph = glyphs[c]
for row in range(0,8):
for col in range(0,8):
if glyph[row][col] == '*':
self.pixel(x+col, y+row, color)
temp = 23.456
text = f"temp={temp:5.2f}"
cursor_pixel = 0
LCD.text(text, cursor_pixel, 0, 1)
cursor_pixel += len(text)*8
LCD.glyph('°', cursor_pixel, 0, 1)
cursor_pixel += 8
LCD.text("C", cursor_pixel, 0, 1)
cursor_pixel += 8
LCD.show()
Re: Help with a Waveshare 1.8inch LCD Display Module
This is what @ghp meant by placing the method inside the LCD_1inch8 class, also include the LCD class color definition for the text foreground color when calling the glyph method as per the example.
Code: Select all
# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details: https://RandomNerdTutorials.com/raspberry-pi-pico-bme680-micropython/
# Adding waveShare 1.8" LCD
# Modified S. Stein 9/7/25 R4
from machine import Pin, I2C, SPI,PWM
from time import sleep
import time
import utime as time
from bme680R1 import *
import os
# Test 10 July 2021 Mod 8/27/25
import framebuf
import utime, time
# Source: Electrocredible.com, Language: MicroPython
from machine import ADC
adc = machine.ADC(4)
BL = 13
DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9
# New driver received from WaveShare on 10th July 2021 for 1.8" LCD
class LCD_1inch8(framebuf.FrameBuffer):
def __init__(self):
self.width = 160
self.height = 128
self.cs = Pin(CS,Pin.OUT)
self.rst = Pin(RST,Pin.OUT)
self.cs(1)
self.spi = SPI(1)
self.spi = SPI(1,1000_000)
self.spi = SPI(1,10000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
self.dc = Pin(DC,Pin.OUT)
self.dc(1)
self.buffer = bytearray(self.height * self.width * 2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
self.init_display()
self.WHITE = 0xFFFF
self.BLACK = 0x0000
self.GREEN = 0x001F
self.BLUE = 0xF800
self.RED = 0x07E0
def glyph(self, c, x, y, color=1):
"""display a glyph for a char "c" at given pixel
position x, y. X, y are topleft corner of glyph drawn.
For simplicity, the glyph are defined in 'ascii art'
and evaluated at runtime.
More efficient to generate byte data offline from
ascii definitions.
"""
glyphs = {
'°': [ " ** ",
" * * ",
" ** ",
" ",
" ",
" ",
" ",
" ", ],
'↑': [ " * ", # just for fun
" * * ",
" * * * ",
" * ",
" * ",
" * ",
" * ",
" * ", ],
}
if not c in glyphs:
print( f"glyphs does not contain '{c}'")
return
glyph = glyphs[c]
for row in range(0,8):
for col in range(0,8):
if glyph[row][col] == '*':
self.pixel(x+col, y+row, color)
def write_cmd(self, cmd):
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
def write_data(self, buf):
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(bytearray([buf]))
self.cs(1)
def init_display(self):
"""Initialize dispaly"""
self.rst(1)
self.rst(0)
self.rst(1)
self.write_cmd(0x36);
self.write_data(0x70);
self.write_cmd(0x3A);
self.write_data(0x05);
#ST7735R Frame Rate
self.write_cmd(0xB1);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_cmd(0xB2);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_cmd(0xB3);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_data(0x01);
self.write_data(0x2C);
self.write_data(0x2D);
self.write_cmd(0xB4); #Column inversion
self.write_data(0x07);
#ST7735R Power Sequence
self.write_cmd(0xC0);
self.write_data(0xA2);
self.write_data(0x02);
self.write_data(0x84);
self.write_cmd(0xC1);
self.write_data(0xC5);
self.write_cmd(0xC2);
self.write_data(0x0A);
self.write_data(0x00);
self.write_cmd(0xC3);
self.write_data(0x8A);
self.write_data(0x2A);
self.write_cmd(0xC4);
self.write_data(0x8A);
self.write_data(0xEE);
self.write_cmd(0xC5); #VCOM
self.write_data(0x0E);
#ST7735R Gamma Sequence
self.write_cmd(0xe0);
self.write_data(0x0f);
self.write_data(0x1a);
self.write_data(0x0f);
self.write_data(0x18);
self.write_data(0x2f);
self.write_data(0x28);
self.write_data(0x20);
self.write_data(0x22);
self.write_data(0x1f);
self.write_data(0x1b);
self.write_data(0x23);
self.write_data(0x37);
self.write_data(0x00);
self.write_data(0x07);
self.write_data(0x02);
self.write_data(0x10);
self.write_cmd(0xe1);
self.write_data(0x0f);
self.write_data(0x1b);
self.write_data(0x0f);
self.write_data(0x17);
self.write_data(0x33);
self.write_data(0x2c);
self.write_data(0x29);
self.write_data(0x2e);
self.write_data(0x30);
self.write_data(0x30);
self.write_data(0x39);
self.write_data(0x3f);
self.write_data(0x00);
self.write_data(0x07);
self.write_data(0x03);
self.write_data(0x10);
self.write_cmd(0xF0); #Enable test command
self.write_data(0x01);
self.write_cmd(0xF6); #Disable ram power save mode
self.write_data(0x00);
#sleep out
self.write_cmd(0x11);
#DEV_Delay_ms(120);
#Turn on the LCD display
self.write_cmd(0x29);
def show(self):
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0x01)
self.write_data(0x00)
self.write_data(0xA0)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x02)
self.write_data(0x00)
self.write_data(0x82)
self.write_cmd(0x2C)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(self.buffer)
self.cs(1)
# ==============End of driver setup==============================
def colour(R,G,B):
# Get RED value
rp = int(R*31/255) # range 0 to 31
if rp < 0: rp = 0
r = rp *8
# Get Green value - more complicated!
gp = int(G*63/255) # range 0 - 63
if gp < 0: gp = 0
g = 0
if gp & 1: g = g + 8192
if gp & 2: g = g + 16384
if gp & 4: g = g + 32768
if gp & 8: g = g + 1
if gp & 16: g = g + 2
if gp & 32: g = g + 4
# Get BLUE value
bp =int(B*31/255) # range 0 - 31
if bp < 0: bp = 0
b = bp *256
colour = r+g+b
return colour
if __name__=='__main__':
pwm = PWM(Pin(BL))
pwm.freq(1000)
pwm.duty_u16(32768)#max 65535
LCD = LCD_1inch8()
#color BRG
LCD.fill(0x4)
LCD.show() # All pixels lit dark green
LCD.pixel(0,0,0xFFFF) # Left Top - OK
LCD.pixel(0,152,0xFFFF) # Left Bottom - OK
LCD.pixel(190,0,0xFFFF) # Right Top - OK
LCD.pixel(190,152,0xFFFF) # Right Bottom - OK
# RPi Pico - Pin assignment
i2c = I2C(id=0, scl=Pin(21), sda=Pin(20))
bme = BME680_I2C(i2c=i2c)
if 'BME680 log.txt' in os.listdir():
print('Header Row exists; pass on to next step!')
else:
file = open ("BME680 log.txt", "a")
print(type(file))
results1 = "Date" +'\t' +'\t' + "Time" + '\t' + "Temp." + chr(176) +'F' + '\t' + '\t' + "Hum. %" + '\t' + "Press. in Hg" + '\t' + "Gas KOhms" + "\n"
file.write(results1)
file.close()
pass
temp = 23.456
text = f"temp={temp:5.2f}"
cursor_pixel = 0
LCD.text(text, cursor_pixel, 0, LCD.WHITE)
cursor_pixel += len(text)*8
LCD.glyph('°', cursor_pixel, 0, LCD.WHITE)
cursor_pixel += 8
LCD.text("C", cursor_pixel, 0, LCD.WHITE)
cursor_pixel += 8
LCD.show()
Re: Help with a Waveshare 1.8inch LCD Display Module
Jeff_T and ghp,
Thanks for your time and effotrs. I got this working.
I researched how to generate byte data offline from ascii definitions, but didn't find anything that seemed appropriate.
Can you explain how to do this? (I am relatively new to programming and that is why I need more help than I would like to ask for.)
Thanks for your time and effotrs. I got this working.
I researched how to generate byte data offline from ascii definitions, but didn't find anything that seemed appropriate.
Can you explain how to do this? (I am relatively new to programming and that is why I need more help than I would like to ask for.)
Re: Help with a Waveshare 1.8inch LCD Display Module
What is it you are trying to do, your original topic has been solved you may need to start a new thread with your new question and a few more details.I researched how to generate byte data offline from ascii definitions
Jump to
- Community
- General discussion
- Announcements
- Other languages
- Deutsch
- Español
- Français
- Italiano
- Nederlands
- 日本語
- Polski
- Português
- Русский
- Türkçe
- User groups and events
- Raspberry Pi Official Magazine
- Using the Raspberry Pi
- Beginners
- Troubleshooting
- Advanced users
- Assistive technology and accessibility
- Education
- Picademy
- Teaching and learning resources
- Staffroom, classroom and projects
- Astro Pi
- Mathematica
- High Altitude Balloon
- Weather station
- Programming
- C/C++
- Java
- Python
- Scratch
- Other programming languages
- Windows 10 for IoT
- Wolfram Language
- Bare metal, Assembly language
- Graphics programming
- OpenGLES
- OpenVG
- OpenMAX
- General programming discussion
- Projects
- Networking and servers
- Automation, sensing and robotics
- Graphics, sound and multimedia
- Other projects
- Media centres
- Gaming
- AIY Projects
- Hardware and peripherals
- Camera board
- Compute Module
- Official Display
- HATs and other add-ons
- Device Tree
- Interfacing (DSI, CSI, I2C, etc.)
- Keyboard computers (400, 500, 500+)
- Raspberry Pi Pico
- General
- SDK
- MicroPython
- Other RP2040 boards
- Zephyr
- Rust
- AI Accelerator
- AI Camera - IMX500
- Hailo
- Software
- Raspberry Pi OS
- Raspberry Pi Connect
- Raspberry Pi Desktop for PC and Mac
- Beta testing
- Other
- Android
- Debian
- FreeBSD
- Gentoo
- Linux Kernel
- NetBSD
- openSUSE
- Plan 9
- Puppy
- Arch
- Pidora / Fedora
- RISCOS
- Ubuntu
- Ye Olde Pi Shoppe
- For sale
- Wanted
- Off topic
- Off topic discussion