|
| 1 | +.. _ssd1306: |
| 2 | + |
| 3 | +Using a SSD1306 OLED display |
| 4 | +============================ |
| 5 | + |
| 6 | +The SSD1306 OLED display uses either a SPI or I2C interface and comes in a variety of |
| 7 | +sizes (128x64, 128x32, 72x40, 64x48) and colours (white, yellow, blue, yellow + blue). |
| 8 | + |
| 9 | +Hardware SPI interface:: |
| 10 | + |
| 11 | + from machine import Pin, SPI |
| 12 | + import ssd1306 |
| 13 | + |
| 14 | + hspi = SPI(1) # sck=14 (scl), mosi=13 (sda), miso=12 (unused) |
| 15 | + |
| 16 | + dc = Pin(4) # data/command |
| 17 | + rst = Pin(5) # reset |
| 18 | + cs = Pin(15) # chip select, some modules do not have a pin for this |
| 19 | + |
| 20 | + display = ssd1306.SSD1306_SPI(128, 64, hspi, dc, rst, cs) |
| 21 | + |
| 22 | +Software SPI interface:: |
| 23 | + |
| 24 | + from machine import Pin, SoftSPI |
| 25 | + import ssd1306 |
| 26 | + |
| 27 | + spi = SoftSPI(baudrate=500000, polarity=1, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12)) |
| 28 | + |
| 29 | + dc = Pin(4) # data/command |
| 30 | + rst = Pin(5) # reset |
| 31 | + cs = Pin(15) # chip select, some modules do not have a pin for this |
| 32 | + |
| 33 | + display = ssd1306.SSD1306_SPI(128, 64, spi, dc, rst, cs) |
| 34 | + |
| 35 | +I2C interface:: |
| 36 | + |
| 37 | + from machine import Pin, I2C |
| 38 | + import ssd1306 |
| 39 | + |
| 40 | + # using default address 0x3C |
| 41 | + i2c = I2C(sda=Pin(4), scl=Pin(5)) |
| 42 | + display = ssd1306.SSD1306_I2C(128, 64, i2c) |
| 43 | + |
| 44 | +Print Hello World on the first line:: |
| 45 | + |
| 46 | + display.text('Hello, World!', 0, 0, 1) |
| 47 | + display.show() |
| 48 | + |
| 49 | +Basic functions:: |
| 50 | + |
| 51 | + display.poweroff() # power off the display, pixels persist in memory |
| 52 | + display.poweron() # power on the display, pixels redrawn |
| 53 | + display.contrast(0) # dim |
| 54 | + display.contrast(255) # bright |
| 55 | + display.invert(1) # display inverted |
| 56 | + display.invert(0) # display normal |
| 57 | + display.rotate(True) # rotate 180 degrees |
| 58 | + display.rotate(False) # rotate 0 degrees |
| 59 | + display.show() # write the contents of the FrameBuffer to display memory |
| 60 | + |
| 61 | +Subclassing FrameBuffer provides support for graphics primitives:: |
| 62 | + |
| 63 | + display.fill(0) # fill entire screen with colour=0 |
| 64 | + display.pixel(0, 10) # get pixel at x=0, y=10 |
| 65 | + display.pixel(0, 10, 1) # set pixel at x=0, y=10 to colour=1 |
| 66 | + display.hline(0, 8, 4, 1) # draw horizontal line x=0, y=8, width=4, colour=1 |
| 67 | + display.vline(0, 8, 4, 1) # draw vertical line x=0, y=8, height=4, colour=1 |
| 68 | + display.line(0, 0, 127, 63, 1) # draw a line from 0,0 to 127,63 |
| 69 | + display.rect(10, 10, 107, 43, 1) # draw a rectangle outline 10,10 to 107,43, colour=1 |
| 70 | + display.fill_rect(10, 10, 107, 43, 1) # draw a solid rectangle 10,10 to 107,43, colour=1 |
| 71 | + display.text('Hello World', 0, 0, 1) # draw some text at x=0, y=0, colour=1 |
| 72 | + display.scroll(20, 0) # scroll 20 pixels to the right |
| 73 | + |
| 74 | + # draw another FrameBuffer on top of the current one at the given coordinates |
| 75 | + import framebuf |
| 76 | + fbuf = framebuf.FrameBuffer(bytearray(8 * 8 * 1), 8, 8, framebuf.MONO_VLSB) |
| 77 | + fbuf.line(0, 0, 7, 7, 1) |
| 78 | + display.blit(fbuf, 10, 10, 0) # draw on top at x=10, y=10, key=0 |
| 79 | + display.show() |
| 80 | + |
| 81 | +Draw the MicroPython logo and print some text:: |
| 82 | + |
| 83 | + display.fill(0) |
| 84 | + display.fill_rect(0, 0, 32, 32, 1) |
| 85 | + display.fill_rect(2, 2, 28, 28, 0) |
| 86 | + display.vline(9, 8, 22, 1) |
| 87 | + display.vline(16, 2, 22, 1) |
| 88 | + display.vline(23, 8, 22, 1) |
| 89 | + display.fill_rect(26, 24, 2, 4, 1) |
| 90 | + display.text('MicroPython', 40, 0, 1) |
| 91 | + display.text('SSD1306', 40, 12, 1) |
| 92 | + display.text('OLED 128x64', 40, 24, 1) |
| 93 | + display.show() |
0 commit comments