Skip to content

How to use a 0.96 inch OLED with a Micro:bit?

How to use a 0.96 inch OLED with a Micro:bit

To use a 0.96 inch OLED with a Micro:bit, you need to connect the display via I2C or SPI, then write code in MicroPython or MakeCode to control it. The most common approach is I2C, which only uses two wires (SDA and SCL) plus power and ground. For a 0.96 inch 128x64 spi i2c oled display, the resolution is 128x64 pixels, and it uses the SSD1306 driver chip. This driver is widely supported, so you can get text and graphics running in under 10 minutes.

The Micro:bit V2 has a 32-bit ARM Cortex-M4 processor running at 64 MHz, with 256 KB flash and 128 KB RAM. The OLED display typically draws 20 mA during operation, which is fine for the Micro:bit’s 3.3V logic and 5V USB power. The I2C address is usually 0x3C or 0x3D, depending on the module’s resistor configuration. You can check this by scanning the I2C bus with a simple script. The display’s controller supports both I2C (up to 400 kHz) and SPI (up to 10 MHz), but I2C is simpler for beginners because it uses fewer pins.

Hardware Wiring for I2C
The Micro:bit has dedicated I2C pins on the edge connector: pin 19 (SCL) and pin 20 (SDA). On the breakout board, these are often labeled as P19 and P20. For the OLED, connect VCC to 3.3V (pin 1 on the Micro:bit), GND to GND (pin 2), SCL to pin 19, and SDA to pin 20. Some OLED modules have a built-in voltage regulator, so they can accept 5V on VCC, but the Micro:bit’s 3.3V output is safer. If the module uses SPI, you need four pins: CS (chip select), DC (data/command), MOSI (master out slave in), and SCK (clock). The Micro:bit’s SPI pins are P15 (MOSI), P14 (MISO, not used for OLED), P13 (SCK), and you can use P16 for CS and P12 for DC. However, SPI wiring is more complex, so I2C is recommended for most projects.

Software Setup in MicroPython
MicroPython on the Micro:bit comes with the machine module, which includes I2C support. You need to install the ssd1306 driver library. This is a standard library in the MicroPython ecosystem, but you may need to copy it to your Micro:bit via the Mu editor or a file manager. The driver handles pixel drawing, text rendering, and buffer management. Here is a minimal example to display “Hello World”:

```python
from microbit import i2c
import ssd1306

oled = ssd1306.SSD1306_I2C(128, 64, i2c)
oled.text('Hello World', 0, 0)
oled.show()
```

This code initializes the display with a 128x64 pixel buffer, writes text at the top-left corner, and sends the buffer to the OLED. The buffer is 1024 bytes (128x64/8), which fits comfortably in the Micro:bit’s RAM. You can also draw shapes like lines, rectangles, and circles using the line(), rect(), and ellipse() methods. The display updates at about 30 frames per second with simple graphics, but complex images may drop to 10 FPS.

Using MakeCode for Beginners
If you prefer block-based coding, MakeCode has an extension for OLED displays. Search for “OLED” in the extensions menu and install the “OLED” package by Microsoft. This gives you blocks like “initialize OLED with width 128 height 64”, “show string”, “show number”, and “clear display”. You can also use the “draw pixel” block for custom graphics. MakeCode compiles to C++ and runs on the Micro:bit’s native firmware, so performance is similar to MicroPython. The extension uses I2C by default, so no extra wiring is needed.

Displaying Data and Graphics
The 128x64 resolution is enough for 8 lines of text (each 8 pixels high) with 16 characters per line (using a 5x7 font). You can display sensor readings, like temperature from the Micro:bit’s internal sensor (which measures 0-50°C with ±2°C accuracy), or accelerometer data (16-bit values for X, Y, Z axes). For example, to show the temperature every second:

```python
from microbit import i2c, temperature, sleep
import ssd1306

oled = ssd1306.SSD1306_I2C(128, 64, i2c)
while True:
oled.fill(0)
oled.text('Temp: ' + str(temperature()) + ' C', 0, 0)
oled.show()
sleep(1000)
```

You can also draw bar graphs or line charts. The SSD1306 supports horizontal and vertical scrolling, which is useful for long text. The display’s contrast is adjustable via the contrast() method, with values from 0 to 255. Default is 128, but you may need to increase it to 200 for bright environments. The OLED’s viewing angle is >160 degrees, and it has a fast response time of under 10 microseconds, so it works well for animations.

Power Consumption and Battery Life
The Micro:bit draws about 30 mA when idle, and the OLED adds 20 mA when active. With a 2000 mAh battery pack, you can run the display for about 40 hours continuously. If you use the display’s sleep mode (via the poweroff() method), current drops to under 10 µA, which extends battery life significantly. The SSD1306 has a built-in charge pump for the OLED pixels, so no external components are needed. The display operates at 3.3V, but the Micro:bit’s 3.3V regulator can supply up to 300 mA, so power is not an issue.

Common Issues and Fixes
If the display shows nothing, check the I2C address. Use this script to scan:

```python
from microbit import i2c
for addr in range(0x00, 0x80):
try:
i2c.read(addr, 1)
print('Found device at', hex(addr))
except:
pass
```

If no device is found, verify the wiring: VCC to 3.3V, GND to GND, SCL to pin 19, SDA to pin 20. Some modules have pull-up resistors for I2C, but the Micro:bit has internal pull-ups on pins 19 and 20, so external resistors are not needed. If the display is dim, increase contrast or check the power supply. The OLED can be damaged by reverse polarity, so double-check connections before powering on.

Advanced Techniques
You can use the OLED with the Micro:bit’s radio module to display data from another Micro:bit. For example, create a wireless sensor network where one Micro:bit sends temperature readings, and another displays them on the OLED. The radio module operates at 2.4 GHz with a range of about 20 meters indoors. The OLED’s buffer can be updated in real-time, so you can show scrolling text or animated icons. The framebuf module in MicroPython allows you to create custom fonts or bitmaps. For instance, you can load a 128x64 monochrome image as a byte array and display it with blit().

Performance Data
The Micro:bit’s I2C bus runs at 100 kHz by default, but you can increase it to 400 kHz by setting the frequency in the i2c.init() method. At 400 kHz, the display updates in about 2 ms for a full frame, compared to 8 ms at 100 kHz. SPI is faster, with a theoretical maximum of 10 MHz, but the Micro:bit’s SPI clock is limited to 1 MHz in MicroPython. In practice, SPI updates a full frame in about 1 ms. However, I2C is simpler and sufficient for most applications. The OLED’s pixel refresh rate is 100 Hz, so you can run animations at 10-20 FPS without flicker.

Comparison with Other Displays
Compared to a 16x2 LCD (which uses 32 characters), the OLED offers 128x64 pixels, so you can show graphics, icons, and multiple font sizes. The OLED’s contrast ratio is 2000:1, while a typical LCD is 100:1. The OLED also has a wider operating temperature range (-40°C to 85°C) than LCDs, which often fail below 0°C. The 0.96 inch OLED is about 0.1 inches thick, making it easy to integrate into wearable projects. The only downside is that OLEDs consume more power per pixel when displaying bright content, but for typical text and simple graphics, the difference is negligible.

Real-World Applications
You can use the OLED with a Micro:bit to build a digital clock, a step counter (using the accelerometer), a weather station (with an external sensor like the BME280), or a game like Pong. The display’s small size makes it ideal for handheld gadgets. For example, a pedometer can show step count and calories burned, updating every second. The Micro:bit’s built-in magnetometer can be used as a compass, and the OLED can display a rotating arrow. With the radio module, you can create a multiplayer game where each player’s score is shown on their own OLED.

Code Optimization Tips
To reduce memory usage, update only changed parts of the buffer instead of redrawing the entire screen. Use the blit() method to copy small images from a precomputed buffer. For text, use the text() method with a bytearray font to avoid string operations. The Micro:bit’s flash memory is limited, so store large bitmaps in the flash module if needed. The OLED driver uses a 1024-byte buffer, which is about 0.8% of the Micro:bit’s RAM, so you have plenty of space for other data.

Hardware Variants
Some 0.96 inch OLED modules have a reset pin (RST) that you can connect to a GPIO pin for hardware reset. Others have a 4-pin I2C interface or a 7-pin SPI interface. Check the datasheet for your specific module. The SSD1306 supports both 128x64 and 128x32 resolutions, but the 128x64 version is more common. The display’s pixel color is white, blue, or yellow, depending on the model. Blue is the most common, but white offers better contrast in bright light. The module’s operating voltage is 3.3V to 5V, but the logic level is 3.3V, so direct connection to the Micro:bit is safe.

Testing and Debugging
If the display shows garbled characters, the I2C address might be wrong, or the baud rate is too high. Start with the default 100 kHz. If the display is blank, try a hardware reset by disconnecting and reconnecting power. The Micro:bit’s I2C pins are shared with the edge connector, so make sure no other devices are connected to those pins. You can use a multimeter to measure voltage on the OLED’s VCC pin (should be 3.3V) and GND pin (0V). The SCL and SDA pins should show 3.3V when idle, with pulses during communication.

Community Resources
The Micro:bit community has many tutorials for OLED displays. Search for “Micro:bit OLED SSD1306” on GitHub or the Micro:bit forum. The official Micro:bit Python editor (Mu) includes a built-in library for SSD1306, but you can also use the ssd1306.py file from the MicroPython repository. For MakeCode, the OLED extension is maintained by the Pimoroni team, and it includes blocks for drawing shapes, text, and images. The display’s datasheet is available from the manufacturer, and it includes timing diagrams and register maps for advanced users.