|
| 1 | +# MicroPython SSD1306 display via I2C protocol example |
| 2 | +# |
| 3 | +# This example demonstrates how to configure SSD1306 OLED display |
| 4 | +# via I2C protocol. |
| 5 | +# |
| 6 | +# |
| 7 | +# To run this example: |
| 8 | +# 1. Connect your SSD1306 display with the microcontroller. |
| 9 | +# (In this example an ESP32 is used with pins: SCL = Pin 22, SDA = Pin 21. |
| 10 | +# Note that IC pin numbering may not map with the board's pin numbers. |
| 11 | +# Please refer the boards datasheet.) |
| 12 | +# |
| 13 | +# 2. Make sure `ssd1306` is installed via: mpremote mip install ssd1306 |
| 14 | +# (Alternatively, you can copy ssd1306.py to your board.) |
| 15 | +# |
| 16 | +# 3. Run the example via: mpremote run example_ssd1306_i2c.py |
| 17 | +# (You can also copy this file to the board and run it.) |
| 18 | +# |
| 19 | +# 4. Observe the output on your SSD1306 OLED display. |
| 20 | +# |
| 21 | +# MIT license; Copyright (c) 2024 Tharuka Pavith. |
| 22 | + |
| 23 | +import ssd1306 |
| 24 | +from machine import Pin, SoftI2C |
| 25 | +from time import sleep_ms, localtime |
| 26 | + |
| 27 | +# I2C is deprecated. Therefore, use SoftI2C |
| 28 | +i2c = SoftI2C(scl=Pin(22), sda=Pin(21)) |
| 29 | + |
| 30 | +# SSD1306 OLED display dimensions |
| 31 | +WIDTH = const(128) |
| 32 | +HEIGHT = const(64) |
| 33 | + |
| 34 | +disp = ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c) # Create SSD1306 display object |
| 35 | + |
| 36 | +disp.invert(False) # Toggle this True/False to invert pixels |
| 37 | +disp.contrast(20) # Set contrast |
| 38 | + |
| 39 | +while True: |
| 40 | + yy, mm, dd, hr, mn, sec, *ext = localtime() # Unpack the localtime tuple |
| 41 | + disp.text("MicroPython", 5, 4) # Set text starting from x=5, y=4 coordinates |
| 42 | + disp.text(f"Time :{hr}:{mn}:{sec}", 5, 20) # Set time text |
| 43 | + disp.text(f"Date :{dd}/{mm}/{yy}", 5, 40) # Set date text |
| 44 | + disp.show() # Update the display |
| 45 | + |
| 46 | + sleep_ms(1000) |
| 47 | + |
| 48 | + disp.fill(0) # Make all pixels low (depends on inverted or not) |
0 commit comments