oled-i2c.ino

Teensy 4.1 · File : oled-i2c.ino · Language : Arduino · Use case : Afficher tension et uptime sur un écran OLED I2C SSD1306

A 128x64 I2C OLED shows the voltage read on A0 and the uptime. The framebuffer managed by Adafruit_SSD1306 enables full graphics (text, curves, sprites) over a single two-wire bus.

Hardware

A Teensy 4.1 (NXP i.MX RT1062, Cortex-M7 @ 600 MHz, 3.3 V logic, Ethernet and extra flash storage) powered over USB. 1 SSD1306 I2C OLED module (address 0x3C or 0x3D).

Wiring

SDA (pin 18 on Teensy 3.x, 18/19 on 4.x), SCL (19/16), VCC 3.3 V, GND. The module accepts 3.3 V logic.

How it works

Wire drives I2C at 400 kHz; the 128x64 framebuffer is built in RAM then transferred in one block by display(). The 12-bit ADC gives the A0 voltage (max 3.3 V).

Running it

Teensyduino in the Arduino IDE, « Teensy 4.1 » board, Upload then the Teensy Loader button. 3.3 V pins; the 4.1 adds Ethernet, USB host and extra pins.

Variants

Draw a live curve of the readings (drawPixel in a loop), or show a touch sensor level as a bar.

oled-i2c.ino

// OLED 0,96" SSD1306 (I2C 0x3C) avec la bibliotheque Adafruit_SSD1306.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define ADRESSE 0x3C
#define RAZ -1 // pas de broche reset sur les modules I2C

Adafruit_SSD1306 ecran(128, 64, &Wire, RAZ);

void setup() {
  Serial.begin(9600);
  Wire.setClock(400000); // les Teensy montent a 400 kHz sans souci
  if (!ecran.begin(SSD1306_SWITCHCAPVCC, ADRESSE)) {
    Serial.println(F("OLED absent (verifier 0x3C/0x3D)"));
    for (;;);
  }
  ecran.clearDisplay();
  ecran.setTextColor(SSD1306_WHITE);
  ecran.setTextSize(1);
  ecran.setCursor(0, 0);
  ecran.println(F("Teensy + OLED"));
  ecran.display();
}

void loop() {
  static unsigned long t0 = 0;
  if (millis() - t0 < 500) return;
  t0 = millis();

  analogReadResolution(12);
  float v = analogRead(A0) * 3.3 / 4095.0;

  ecran.clearDisplay();
  ecran.setCursor(0, 0);
  ecran.print(F("A0 : ")); ecran.print(v, 2); ecran.println(F(" V"));
  ecran.print(F("Up : ")); ecran.print(millis() / 1000); ecran.println(F(" s"));
  ecran.display(); // transfert du framebuffer vers l'ecran
}

← Back to the board