ESP32 - Écran OLED SSD1309 128x64 | Tutoriel OLED I2C de 2,42 pouces

Un écran OLED (diode électroluminescente organique) offre des pixels auto-émissifs qui produisent des noirs profonds, un contraste élevé et de larges angles de vision — ce qui en fait une excellente amélioration par rapport aux LCD traditionnels. Le SSD1309 est le circuit intégré de pilotage couramment trouvé sur des modules OLED I2C 128×64 de 2,42 pouces (parfois étiquetés 2,4 pouces).

OLED SSD1309 de 2,42 pouces pour ESP32

Dans ce guide étape par étape, vous apprendrez comment connecter et programmer le SSD1309 OLED 128×64 avec une carte ESP32 en utilisant la bibliothèque DIYables_OLED_SSD1309. Plus précisément, nous aborderons :

À propos de l'écran OLED SSD1309 de 2,42 pouces

Le SSD1309 est un circuit intégré pilote OLED CMOS à une seule puce conçu pour des panneaux à matrice de points 128×64. Il est compatible au niveau des registres avec le SSD1306, largement utilisé, de sorte que de nombreux exemples de code existants peuvent être réutilisés avec peu de modifications. Les principales différences matérielles sont les suivantes :

  • Pas de pompe à charge intégrée — le SSD1309 nécessite une alimentation VCC externe, bien que pratiquement toutes les cartes breakout (y compris les modules de 2,42 pouces et 2,4 pouces) soient livrées avec un convertisseur boost intégré, ce qui vous est transparent.
  • Tolérance de tension plus élevée — le SSD1309 accepte jusqu'à 16 V VCC, alors que le SSD1306 est limité à environ 4,2 V.

Le module OLED de 2,42 pouces (2,4 pouces) utilise généralement le pilote SSD1309 et offre une résolution de 128×64 pixels avec une interface I2C. La couleur du panneau (blanc, bleu, jaune, vert ou zone double) est déterminée par le matériau OLED physique et n'est pas contrôlable par logiciel.

Ce tutoriel communique avec l'affichage via le I2C bus, qui ne nécessite que deux fils de signalisation (SDA et SCL) et peut partager le bus avec d'autres périphériques I2C.

Schéma des broches de l'OLED SSD1309 (Module I2C)

Le module OLED I2C SSD1309 typique de 2,42 pouces expose quatre broches :

  • Broche GND : doit être connectée à la masse de l'ESP32
  • Broche VCC : doit être connectée à la broche 3,3 V ou 5 V de l'ESP32 (vérifiez les spécifications de votre module)
  • Broche SCL : est une broche d'horloge I2C. Connectez-la à la GPIO 22 (SCL par défaut)
  • Broche SDA : est une broche de données I2C. Connectez-la à la GPIO 21 (SDA par défaut)
Disposition des broches OLED SSD1309

※ Note:

L'ordre des broches peut varier d'un fabricant à l'autre. Vérifiez toujours les étiquettes imprimées sur votre module OLED.

Diagramme de câblage

  • Comment connecter l'ESP32 à l'écran OLED SSD1309 128x64 en utilisant une breadboard
Schéma de câblage ESP32 SSD1309 OLED 128x64

Cette image a été créée avec Fritzing. Cliquez pour agrandir l'image.

Si vous ne savez pas comment alimenter l'ESP32 et d'autres composants, consultez les instructions dans le tutoriel suivant : Comment alimenter l'ESP32..

Comment connecter l'ESP32 et l'écran OLED SSD1309.

Cette image a été créée avec Fritzing. Cliquez pour agrandir l'image.

Comment programmer l'ESP32 pour l'écran OLED SSD1309

Installer la bibliothèque DIYables_OLED_SSD1309

La bibliothèque DIYables_OLED_SSD1309 est spécialement conçue pour les écrans SSD1309 et étend Adafruit_GFX pour un support graphique plus riche.

  • Ouvrez l'IDE Arduino
  • Accédez à l'icône Bibliothèques dans la barre latérale gauche
  • Recherchez DIYables_OLED_SSD1309
  • Cliquez sur Installer
  • Une fenêtre de dépendances apparaîtra — cliquez sur Tout installer pour installer également la Adafruit GFX Library (nécessaire pour le dessin des formes et les polices)
Bibliothèque OLED SSD1309 pour ESP32

Étapes de programmation

  1. Incluez les bibliothèques requises
#include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h>
  1. Définir les dimensions de l'écran
#define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64
  1. Déclarez l'objet d'affichage
#define OLED_RESET -1 // Reset pin not used on most I2C modules #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  1. Initialiser l'OLED dans setup()
if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); // halt }
  1. Afficher le contenu
display.clearDisplay(); display.setTextSize(2); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 20); display.println(F("Hello ESP32")); display.display(); // push buffer to screen

Code ESP32 — Bonjour le monde sur l'écran OLED SSD1309

Le point de départ le plus simple : imprimer quelques lignes de texte à des tailles différentes.

/* * Ce code ESP32 a été développé par newbiely.fr * Ce code ESP32 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Hello World * Prints text in two sizes on the 2.42 inch 128x64 I2C OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.setTextSize(1); // 6x8 pixels per character display.setTextColor(SSD1309_PIXEL_ON); // turn pixels on display.setCursor(0, 0); display.println(F("Hello, World!")); display.println(); display.setTextSize(2); // 12x16 pixels per character display.println(F("DIYables")); display.setTextSize(1); display.println(); display.println(F("SSD1309 OLED 128x64")); display.display(); // push buffer to screen } void loop() { }

Code ESP32 — Affichage du texte sur l'OLED SSD1309

L'exemple suivant illustre davantage de fonctionnalités liées au texte — plusieurs tailles, le formatage des nombres et la macro F() pour économiser de la RAM.

/* * Ce code ESP32 a été développé par newbiely.fr * Ce code ESP32 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Display Text * Displays text in various sizes and formats on the 2.42" SSD1309 OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); // Various text sizes display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 0); display.println(F("Size 1 - DIYables")); display.setTextSize(2); display.println(F("Size 2")); display.setTextSize(3); display.println(F("Sz3")); display.display(); delay(3000); // Number formatting display.clearDisplay(); display.setTextSize(1); display.setCursor(0, 0); int value = 42; float pi = 3.14159; display.print(F("Integer: ")); display.println(value); display.print(F("Float: ")); display.println(pi, 2); // 2 decimal places display.print(F("Hex: 0x")); display.println(value, HEX); display.print(F("Binary: 0b")); display.println(value, BIN); display.display(); } void loop() { }

Référence des fonctions d'affichage utiles

Ci-dessous figure une liste de référence rapide des fonctions les plus fréquemment utilisées lors de l'utilisation de l'OLED SSD1309 via la bibliothèque DIYables :

  • oled.clearDisplay() — effacer le tampon d'affichage (tous les pixels éteints).
  • oled.display() — transférer le tampon vers l'OLED afin que les modifications soient visibles.
  • oled.drawPixel(x, y, color) — définir ou effacer un pixel individuel.
  • oled.setTextSize(n) — agrandir la police par le facteur *n* (1 = 6×8, 2 = 12×16, …, jusqu’à 8).
  • oled.setCursor(x, y) — déplacer le curseur de texte vers les coordonnées en pixels *(x, y)*.
  • oled.setTextColor(SSD1309_PIXEL_ON) — couleur du texte uniquement (l'arrière-plan est transparent).
  • oled.setTextColor(SSD1309_PIXEL_OFF, SSD1309_PIXEL_ON) — texte avec une couleur d'arrière-plan explicite.
  • oled.println("message") — afficher une chaîne et passer à la ligne suivante.
  • oled.println(number) — afficher un entier en décimal.
  • oled.println(number, HEX) — afficher un entier en hexadécimal.
  • oled.startscrollright(start, stop) — début du défilement matériel vers la droite entre la page *start* et la page *stop*.
  • oled.startscrollleft(start, stop) — début du défilement matériel vers la gauche.
  • oled.startscrolldiagright(start, stop) — début du défilement matériel en diagonale vers la droite.
  • oled.startscrolldiagleft(start, stop) — début du défilement matériel en diagonale vers la gauche.
  • oled.stopscroll() — arrêter tout défilement matériel actif.
  • oled.setContrast(value) — Ajuster la luminosité de l'affichage (0–255).
  • oled.dim(true/false) — assombrir rapidement l'affichage au minimum ou restaurer le contraste précédent.
  • oled.invertDisplay(true/false) — inversion des couleurs au niveau matériel (pixels allumés ↔ pixels éteints).

Comment centrer verticalement et horizontalement le texte sur l'écran OLED SSD1309

Voir Comment centrer verticalement et horizontalement sur un écran OLED

Code ESP32 — Dessiner des formes sur l'OLED SSD1309

Parce que la bibliothèque DIYables_OLED_SSD1309 étend Adafruit_GFX, vous obtenez un ensemble complet de primitives de dessin de formes : pixels, lignes, rectangles, rectangles remplis, cercles, cercles remplis, triangles, triangles remplis et rectangles arrondis. Le sketch ci-dessous parcourt tous ces éléments avec des démonstrations animées.

/* * Ce code ESP32 a été développé par newbiely.fr * Ce code ESP32 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Draw Shapes * Demonstrates drawing pixels, lines, rectangles, circles, and triangles on the 2.42" OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.display(); } void loop() { // Draw pixels display.clearDisplay(); for (int i = 0; i < 20; i++) { display.drawPixel(random(SCREEN_WIDTH), random(SCREEN_HEIGHT), SSD1309_PIXEL_ON); } display.display(); delay(2000); // Draw lines display.clearDisplay(); for (int y = 0; y < SCREEN_HEIGHT; y += 8) { display.drawLine(0, 0, SCREEN_WIDTH - 1, y, SSD1309_PIXEL_ON); } display.display(); delay(2000); // Draw rectangles display.clearDisplay(); display.drawRect(10, 10, 40, 30, SSD1309_PIXEL_ON); // outline display.fillRect(60, 10, 40, 30, SSD1309_PIXEL_ON); // filled display.drawRoundRect(10, 45, 40, 15, 5, SSD1309_PIXEL_ON); // rounded corners display.display(); delay(2000); // Draw circles display.clearDisplay(); display.drawCircle(32, 32, 20, SSD1309_PIXEL_ON); // outline display.fillCircle(96, 32, 20, SSD1309_PIXEL_ON); // filled display.display(); delay(2000); // Draw triangles display.clearDisplay(); display.drawTriangle(20, 10, 10, 50, 30, 50, SSD1309_PIXEL_ON); // outline display.fillTriangle(90, 10, 70, 50, 110, 50, SSD1309_PIXEL_ON); // filled display.display(); delay(2000); }

Code ESP32 — Défilement matériel sur l'écran OLED SSD1309

Le SSD1309 dispose d'un moteur de défilement intégré qui déplace le contenu de l'affichage sans charge du processeur. La bibliothèque DIYables expose quatre directions de défilement : droite, gauche, diagonale droite et diagonale gauche. Chacune prend un paramètre de page de départ et de page d'arrêt (les pages sont des bandes horizontales de 8 pixels de hauteur numérotées de 0 à 7 sur un écran de 64 pixels de hauteur).

※ Note:

Appelez toujours display() pour afficher votre contenu sur l'OLED avant de démarrer le défilement. Évitez d'afficher du nouveau contenu pendant que le défilement est actif — appelez stopscroll() d'abord.

/* * Ce code ESP32 a été développé par newbiely.fr * Ce code ESP32 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Scroll Text * Demonstrates all four hardware scroll directions on the 2.42" OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.setTextSize(2); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(10, 24); display.println(F("DIYables")); display.display(); delay(2000); } void loop() { // Scroll right across all pages display.startscrollright(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); // Scroll left display.startscrollleft(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); // Diagonal scroll right display.startscrolldiagright(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); // Diagonal scroll left display.startscrolldiagleft(0x00, 0x07); delay(3000); display.stopscroll(); delay(500); }

Code ESP32 — Afficher une image bitmap sur l'écran OLED SSD1309

Pour afficher une bitmap sur l'écran OLED SSD1309, vous devez d'abord convertir votre image en un tableau d'octets en C. Utilisez l'outil en ligne gratuit image2cpp online tool pour cette conversion :

  1. Téléchargez votre fichier image (PNG, JPG, BMP, etc.).
  2. Réglez la taille du canevas à 128 × 64 (ou moins).
  3. Choisissez le code Arduino comme format de sortie.
  4. Copiez le tableau généré dans votre croquis.
image en tableau bitmap

L'exemple ci-dessous alterne entre une icône cœur 16×16 et un logo DIYables en pleine largeur.

/* * Ce code ESP32 a été développé par newbiely.fr * Ce code ESP32 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Display Bitmap Image * Shows a 16x16 heart icon and 128x64 DIYables logo on the 2.42" OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // 16x16 heart icon bitmap const unsigned char heart_icon[] PROGMEM = { 0x00, 0x00, 0x0c, 0x30, 0x1e, 0x78, 0x3f, 0xfc, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xfe, 0x3f, 0xfc, 0x1f, 0xf8, 0x0f, 0xf0, 0x03, 0xc0, 0x00, 0x00 }; // 128x64 DIYables logo bitmap const unsigned char diyables_logo[] PROGMEM = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x07, 0xff, 0xc1, 0xff, 0xe0, 0x7f, 0xf0, 0x1f, 0xfc, 0x03, 0x00, 0x7f, 0xff, 0xff, 0xfc, 0x00, 0x1f, 0xff, 0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xfc, 0x03, 0x00, 0x00, 0xff, 0xe0, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc0, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc0, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc0, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x80, 0x7f, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x7f, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xfe, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xf0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xe0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x07, 0xc0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x0f, 0x80, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x3e, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x7c, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0xf0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0xe0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.display(); } void loop() { // Display small heart icon centered display.clearDisplay(); display.drawBitmap((SCREEN_WIDTH - 16) / 2, (SCREEN_HEIGHT - 16) / 2, heart_icon, 16, 16, SSD1309_PIXEL_ON); display.display(); delay(3000); // Display full-screen DIYables logo display.clearDisplay(); display.drawBitmap(0, 0, diyables_logo, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1309_PIXEL_ON); display.display(); delay(3000); // Invert display display.invertDisplay(true); delay(2000); display.invertDisplay(false); delay(1000); }

※ Note:

  • Les dimensions du bitmap ne doivent pas dépasser la résolution de l'écran (128 × 64 pour le module de 2,42 pouces).

Code ESP32 — Contraste et réduction de la luminosité sur l'OLED SSD1309

Le SSD1309 prend en charge 256 niveaux de contraste (0 à 255). La bibliothèque DIYables fournit setContrast() pour un contrôle précis et dim() pour basculer rapidement entre la luminosité minimale et le niveau configuré précédemment.

/* * Ce code ESP32 a été développé par newbiely.fr * Ce code ESP32 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – Contrast & Dim * Sweeps contrast from 0→255→0 then toggles dim mode on the 2.42" OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } // Draw a test pattern so the contrast changes are visible display.clearDisplay(); display.fillRect(0, 0, 64, 32, SSD1309_PIXEL_ON); display.fillRect(64, 32, 64, 32, SSD1309_PIXEL_ON); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_INVERSE); display.setCursor(16, 28); display.println(F("Contrast Demo")); display.display(); delay(2000); } void loop() { // Ramp up for (int c = 0; c <= 255; c += 5) { display.setContrast((uint8_t)c); delay(30); } delay(1000); // Ramp down for (int c = 255; c >= 0; c -= 5) { display.setContrast((uint8_t)c); delay(30); } delay(1000); // Quick dim toggle display.dim(true); // minimum brightness delay(2000); display.dim(false); // restore delay(2000); }

Code ESP32 — Polices externes personnalisées sur l'écran OLED SSD1309

La bibliothèque Adafruit GFX est livrée avec des dizaines de polices de caractères FreeFont évolutives (Serif, Sans, Mono — chacune en Regular, Bold, Italic, et quatre tailles). Vous pouvez activer l'une d'entre elles sur l'affichage SSD1309 en incluant l'en-tête correspondant et en appelant setFont().

※ Note:

  • Lorsqu'une police externe est active, la coordonnée Y du curseur fait référence à la ligne de base du texte, et non au coin supérieur gauche. Cela diffère de la police intégrée 5×7.
  • Les polices externes sont stockées dans la mémoire flash (PROGMEM). L'ESP32 dispose d'une grande mémoire flash, alors n'hésitez pas à utiliser plusieurs fichiers de police dans votre projet.
/* * Ce code ESP32 a été développé par newbiely.fr * Ce code ESP32 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/esp32/esp32-ssd1309-oled-display */ /* * DIYables OLED SSD1309 – External Fonts * Cycles through three FreeFont typefaces on the 2.42" SSD1309 OLED with ESP32. */ #include <Wire.h> #include <Adafruit_GFX.h> #include <DIYables_OLED_SSD1309.h> #include <Fonts/FreeSerif9pt7b.h> #include <Fonts/FreeSansBold12pt7b.h> #include <Fonts/FreeMono9pt7b.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C DIYables_OLED_SSD1309 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(115200); if (!display.begin(SSD1309_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1309 allocation failed")); for (;;); } display.clearDisplay(); display.display(); } void loop() { // ── Built-in 5×7 font ── display.clearDisplay(); display.setFont(NULL); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 0); display.println(F("Built-in 5x7 font")); display.println(); display.setTextSize(2); display.println(F("DIYables")); display.display(); delay(3000); // ── FreeSerif 9pt ── display.clearDisplay(); display.setFont(&FreeSerif9pt7b); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 14); // Y = baseline display.println(F("FreeSerif 9pt")); display.setCursor(0, 38); display.println(F("DIYables OLED")); display.setCursor(0, 58); display.println(F("Hello World!")); display.display(); delay(3000); // ── FreeSansBold 12pt ── display.clearDisplay(); display.setFont(&FreeSansBold12pt7b); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 20); display.println(F("SansBold")); display.setCursor(0, 52); display.println(F("DIYables")); display.display(); delay(3000); // ── FreeMono 9pt ── display.clearDisplay(); display.setFont(&FreeMono9pt7b); display.setTextSize(1); display.setTextColor(SSD1309_PIXEL_ON); display.setCursor(0, 14); display.println(F("FreeMono 9pt")); display.setCursor(0, 34); display.println(F("0123456789")); display.setCursor(0, 54); display.println(F("!@#$%^&*()")); display.display(); delay(3000); }

Dépannage OLED SSD1309 avec ESP32

Si rien n'apparaît sur l'écran OLED SSD1309 de 2,42 pouces après avoir téléversé votre sketch, suivez ces vérifications :

  • Vérifier le câblage — confirmer que SDA, SCL, VCC et GND sont connectés aux broches ESP32 correctes.
  • Confirmer le contrôleur — cette bibliothèque est conçue pour le SSD1309. Si votre module utilise un contrôleur différent (par exemple SH1106), il ne répondra pas correctement.
  • Vérifier l'adresse I2C — la plupart des modules SSD1309 par défaut utilisent 0x3C, mais certains utilisent 0x3D. Exécutez le sketch de scanner I2C ci-dessous pour détecter l'adresse réelle:
// I2C address scanner — prints every detected device to the Serial Monitor #include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); Serial.println("I2C Scanner"); } void loop() { byte error, address; int nDevices = 0; Serial.println("Scanning..."); for (address = 1; address < 127; address++) { Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address < 16) Serial.print("0"); Serial.print(address, HEX); Serial.println(" !"); nDevices++; } else if (error == 4) { Serial.print("Unknown error at address 0x"); if (address < 16) Serial.print("0"); Serial.println(address, HEX); } } if (nDevices == 0) Serial.println("No I2C devices found"); else Serial.println("done"); delay(5000); }

Sortie attendue du moniteur série lorsque le SSD1309 est détecté :

COM6
Send
Scanning... I2C device found at address 0x3C ! done
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Assurez-vous que display() est appelé — le SSD1309 utilise un tampon d'affichage. Les fonctions de dessin ne modifient que ce tampon dans la RAM; rien n'apparaît à l'écran tant que vous n'appelez pas oled.display().
  • Vérifiez l'alimentation — le module de 2,42 pouces consomme plus de courant que les petits OLED. Assurez-vous que votre source d'alimentation peut délivrer un courant suffisant (typiquement 20–40 mA à pleine luminosité). Si vous utilisez une alimentation USB, cela ne pose généralement pas de problème avec l'ESP32.
  • Broches I2C personnalisées (optionnel) — Si vous devez utiliser des broches I2C non par défaut, initialisez Wire avec Wire.begin(SDA_PIN, SCL_PIN) avant d'appeler display.begin().

Conclusion

Ce guide a couvert l'ensemble des éléments essentiels pour l'utilisation de l'écran OLED SSD1309 de 2,42 pouces (128×64) avec l'ESP32, y compris :

  • Connexions matérielles et câblage I2C
  • Affichage de texte et de nombres de plusieurs tailles
  • Dessin de formes (rectangles, cercles, triangles)
  • Affichage d'images bitmap
  • Défilement matériel dans les quatre directions
  • Contrôle du contraste et de la luminosité
  • Polices externes personnalisées

La bibliothèque DIYables_OLED_SSD1309 simplifie la programmation du SSD1309 en fournissant des fonctions de haut niveau pour le texte, les graphismes et le contrôle de l'affichage, tout en tirant parti d'Adafruit GFX pour des capacités de dessin étendues.

Pour plus de projets et de tutoriels ESP32, visitez esp32io.com

※ NOS MESSAGES

  • N'hésitez pas à partager le lien de ce tutoriel. Cependant, veuillez ne pas utiliser notre contenu sur d'autres sites web. Nous avons investi beaucoup d'efforts et de temps pour créer ce contenu, veuillez respecter notre travail !