-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ssd1306: Add I2C protocol example for SSD1306 display.
Signed-off-by: tharuka-pavith <[email protected]>
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
micropython/drivers/display/ssd1306/examples/example_ssd1306_i2c.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |