Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssd1306: Add I2C protocol example for SSD1306 display. #852

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# MicroPython SSD1306 display via I2C protocol example
#
# This example demonstrates how to configure SSD1306 OLED display
# via I2C protocol.
#
#
# To run this example:
# 1. Connect your SSD1306 display with the microcontroller.
# (In this example an ESP32 is used with pins: SCL = Pin 22, SDA = Pin 21.
# Note that IC pin numbering may not map with the board's pin numbers.
# Please refer the boards datasheet.)
#
# 2. Make sure `ssd1306` is installed via: mpremote mip install ssd1306
# (Alternatively, you can copy ssd1306.py to your board.)
#
# 3. Run the example via: mpremote run example_ssd1306_i2c.py
# (You can also copy this file to the board and run it.)
#
# 4. Observe the output on your SSD1306 OLED display.
#
# MIT license; Copyright (c) 2024 Tharuka Pavith.

import ssd1306
from machine import Pin, SoftI2C
from time import sleep_ms, localtime
from micropython import const

# I2C is deprecated. Therefore, use SoftI2C
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))

# SSD1306 OLED display dimensions
WIDTH = const(128)
HEIGHT = const(64)

disp = ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c) # Create SSD1306 display object

disp.invert(False) # Toggle this True/False to invert pixels
disp.contrast(20) # Set contrast

while True:
yy, mm, dd, hr, mn, sec, *ext = localtime() # Unpack the localtime tuple
disp.text("MicroPython", 5, 4) # Set text starting from x=5, y=4 coordinates
disp.text(f"Time :{hr}:{mn}:{sec}", 5, 20) # Set time text
disp.text(f"Date :{dd}/{mm}/{yy}", 5, 40) # Set date text
disp.show() # Update the display

sleep_ms(1000)

disp.fill(0) # Make all pixels low (depends on inverted or not)
Loading