Controlling a ESP8266 HW-364B OLED board with Micropython
One of my projects in (kind of) progress at the moment is a display (upstairs) which shows whether the washing machine (downstairs in the cellar) has finished its cycle.
The basic idea is that Home Assistant monitors the power draw of the washing machine, detects when that drops back down to zero, waits a short while for the machine to unlock itself, and then updates a display upstairs.
Browsing around on AliExpress I came across a combined ESP8266 and OLED display board that would seem to do the job, at least for the prototyping phase. It's a standard ESP866 with the OLED display mounted on the same board - there's no reason why I couldn't have picked up the two things separately, but this ended up cheaper.
There's a few Arduino-based examples floating around the web, but not a lot for Micropython. Getting an ESP board up and running is no problem, but the OLED display was a bit more of an issue. This is how I solved it, YMMV.
Assumptions
- You're programming the board using Thonny
- The board is flashed with the right Micropython build
Adding the OLED library
- You'll need the SSD1306 library installed on the board: in Thonny, select
Tools
->Manage packages
, search forSSD1306
and clickInstall
- This will copy the library into the
lib
folder on the device. If you're anything like me, you'll forget that this step took place and won't include thelib
folder when you commit the project to a Git repo.
The code
This is about the most basic "Hello, world" example possible:
import ssd1306
from machine import Pin, I2C
i2c = I2C(sda=Pin(14), scl=Pin(12))
display = ssd1306.SSD1306_I2C(128, 64, i2c)
display.text('Hello, World!', 0, 0, 1)
display.show()
If you run into problems, it's probably because the board is built connecting the display to a different set of GPIO outputs. A lot of the examples out there on the web assume that SDA
is on pin 4 and SCL
is on pin 5 - that might work, but didn't for my board. Playing around with these pin assignments turned out to be the answer to the problem.