from machine import Pin, SPI
import ssd1306
import time
import framebuf
# Configuration for a 128x64 SPI OLED display
OLED_WIDTH = 128
OLED_HEIGHT = 64
# Pin assignments (adjust GPIO numbers as needed for your specific wiring)
# HSPI used here for hardware SPI
hspi = SPI(1, baudrate=500000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23))
dc = Pin(16) # Data/Command pin
rst = Pin(17) # Reset pin
cs = Pin(5) # Chip Select pin
# Initialize the SSD1306_SPI object
try:
display = ssd1306.SSD1306_SPI(OLED_WIDTH, OLED_HEIGHT, hspi, dc, rst, cs)
except Exception as e:
print(f"OLED initialization error: {e}")
# Consider stopping execution or printing an error to the OLED if possible
# Clear the display buffer
display.fill(0)
# Display a "Hello, World!" message
display.text('Hello, World!', 0, 0, 1)
display.text('ESP32 & Python', 0, 10, 1)
display.text('7-Pin SPI Test', 0, 20, 1)
# Draw a rectangle for demonstration
display.rect(0, 35, 128, 28, 1)
display.hline(0, 45, 128, 1)
# Update the display with the buffer contents
display.show()
# Keep the program running to keep the display on
while True:
pass