ESP32 - OLED

Ce tutoriel vous explique comment utiliser un ESP32 avec un écran OLED I2C 128x64 en utilisant MicroPython. En détail, nous apprendrons :

Écran OLED I2C ESP32 MicroPython

Préparation du matériel

1×Module de développement ESP32 ESP-WROOM-32
1×Câble USB Type-A vers Type-C (pour PC USB-A)
1×Câble USB Type-C vers Type-C (pour PC USB-C)
1×Écran OLED I2C SSD1306 128x64
1×Fils de connexion
1×Recommandé: Carte d'extension à bornier à vis pour ESP32
1×Recommandé: Breakout Expansion Board for ESP32
1×Recommandé: Répartiteur d'alimentation pour ESP32

Ou vous pouvez acheter les kits suivants:

1×Kit de Démarrage DIYables ESP32 (ESP32 inclus)
1×Kit de Capteurs DIYables (30 capteurs/écrans)
1×Kit de Capteurs DIYables (18 capteurs/écrans)
Divulgation : Certains des liens fournis dans cette section sont des liens affiliés Amazon. Nous pouvons recevoir une commission pour tout achat effectué via ces liens, sans coût supplémentaire pour vous. Nous vous remercions de votre soutien.

À propos de l'écran OLED

Les écrans OLED varient en termes de méthodes de communication, de tailles et de couleurs.

  • Protocole de communication : I2C, SPI
  • Tailles : 128 × 64, 128 × 32
  • Couleurs disponibles : blanc, bleu, jaune...
OLED MicroPython sur ESP32

SPI offre un transfert de données plus rapide que l'I2C mais nécessite plus de broches sur l'ESP32. En revanche, l'I2C n'utilise que deux broches et peut se connecter à plusieurs périphériques I2C. Votre choix dépend de savoir si vous privilégiez une utilisation moindre des broches ou une vitesse plus élevée. Pour les écrans OLED basés sur l'I2C, les pilotes courants incluent SSD1306 et SH1106. Ce guide porte spécifiquement sur l'utilisation de l'écran OLED I2C SSD1306 128x64.

Schéma de brochage de l'afficheur OLED I2C

  • Broche GND : Connectez-la à la masse (GND) sur l'ESP32.
  • Broche VCC : Connectez-la à la broche 5 V ou 3,3 V sur l'ESP32 pour l'alimentation.
  • Broche SCL : Il s'agit de la broche d'horloge pour la communication I2C.
  • Broche SDA : Il s'agit de la broche de données pour la communication I2C.
Schéma des broches OLED

※ Note:

Configuration des broches du module OLED

  • Les configurations de broches varient selon le fabricant et le modèle.
  • Suivez les étiquettes sur votre module.

Ce guide utilise le pilote I2C SSD1306. Nous l'avons testé avec un écran OLED DIYables.

Diagramme de câblage

  • Comment connecter l'ESP32 à un écran OLED 128x64 en utilisant breadboard
Schéma de câblage ESP32 MicroPython OLED 128×64

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

Comment connecter l'ESP32 et l'écran OLED

Consultez le tableau ci-dessous pour obtenir des informations sur les autres modèles ESP32.

OLED Module ESP32
VCC 3.3V
GND GND
SDA GPIO21
SCL GPIO22

Code MicroPython ESP32 - Affichage de texte, d'entiers et de nombres à virgule flottante sur OLED

/* * Ce code ESP32 MicroPython a été développé par newbiely.fr * Ce code ESP32 MicroPython 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-micropython/esp32-micropython-oled-128x64 */ from machine import I2C, Pin from DIYables_MicroPython_OLED import OLED_SSD1306_I2C # Initialize I2C i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000) # Adjust ESP32 pins according to your setup # Initialize the OLED display oled = OLED_SSD1306_I2C(128, 64, i2c) # Clear the display oled.clear_display() oled.display() oled.set_text_size(2) # Print a message to the display text = "DIYables" integer_value = 123 float_value = 45.67 oled.set_cursor(0, 0) oled.println(text) oled.set_cursor(0, 25) oled.println(str(integer_value)) # Print integer and move to the next line oled.set_cursor(0, 50) oled.println("{:.2f}".format(float_value)) # Print formatted float and move to the next line oled.display() # Ensure you update the display after writing to it

Étapes rapides

Voici des instructions sur la façon de configurer et d’exécuter votre code MicroPython sur l’ESP32 en utilisant l’IDE Thonny :

  • Assurez-vous que l'IDE Thonny est installé sur votre ordinateur.
  • Confirmez que le firmware MicroPython est chargé sur votre carte ESP32.
  • Si c'est la première fois que vous utilisez un ESP32 avec MicroPython, consultez le guide ESP32 - Premiers pas. pour des instructions étape par étape.
  • Branchez l'afficheur OLED sur l'ESP32 conformément au schéma fourni.
  • Connectez la carte ESP32 à votre ordinateur avec un câble USB.
  • Ouvrez l'IDE Thonny sur votre ordinateur.
  • Dans l'IDE Thonny, allez dans Outils Options.
  • Sous l'onglet Interpréteur, choisissez MicroPython (ESP32) dans le menu déroulant.
  • Assurez-vous que le bon port est sélectionné. L'IDE Thonny le détecte généralement automatiquement, mais vous devrez peut-être le sélectionner manuellement (comme COM3 sur Windows ou /dev/ttyACM0 sur Linux).
  • Accédez à Outils Gérer les paquets dans l'IDE Thonny.
  • Recherchez “DIYables-MicroPython-OLED”, puis trouvez la bibliothèque OLED créée par DIYables.
  • Cliquez sur DIYables-MicroPython-OLED, puis cliquez sur le bouton Install pour installer la bibliothèque OLED.
Bibliothèque OLED MicroPython pour ESP32
  • Copiez le code MicroPython fourni et collez-le dans l'éditeur de Thonny.
  • Enregistrez le code sur votre ESP32 comme suit:
    • Cliquez sur le bouton Enregistrer ou appuyez sur Ctrl+S.
    • Dans la boîte de dialogue d'enregistrement, choisissez l'appareil MicroPython.
    • Nommez le fichier main.py.
  • Cliquez sur le bouton vert Exécuter (ou appuyez sur F5) pour exécuter le script.
  • Regardez le résultat sur l'écran OLED. Il apparaît comme ci-dessous:
ESP32 MicroPython OLED affichage de texte, nombre entier et nombre à virgule flottante

Comment centrer automatiquement verticalement et horizontalement le texte ou le numéro sur l'écran OLED

Le code MicroPython ci-dessous centre automatiquement le texte à la fois verticalement et horizontalement sur l'écran OLED.

/* * Ce code ESP32 MicroPython a été développé par newbiely.fr * Ce code ESP32 MicroPython 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-micropython/esp32-micropython-oled-128x64 */ from machine import I2C, Pin from DIYables_MicroPython_OLED import OLED_SSD1306_I2C import utime # Initialize I2C i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000) # Adjust ESP32 pins according to your setup # Initialize the OLED display oled = OLED_SSD1306_I2C(128, 64, i2c) # Clear the display oled.clear_display() oled.display() def oled_display_center(oled, text): # Get the text bounds (width and height) of the string x1, y1, width, height = oled.get_text_bounds(text, 0, 0) # Set cursor to the calculated centered position cursor_x = (oled.WIDTH - width) // 2 cursor_y = (oled.HEIGHT - height) // 2 oled.set_cursor(cursor_x, cursor_y) # Print the text on the display oled.println(text) # Refresh the display to show the text oled.display() oled.set_text_size(2) oled_display_center(oled, "DIYables")

Après avoir exécuté le code, vous verrez un carré, un cercle et un triangle sur l'écran OLED.

ESP32 MicroPython OLED centrage vertical et horizontal

Code MicroPython ESP32 – Afficher une image sur l'OLED

Le code ci-dessous trace une image sur l'écran LCD. L'image est l'icône DIYables.

/* * Ce code ESP32 MicroPython a été développé par newbiely.fr * Ce code ESP32 MicroPython 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-micropython/esp32-micropython-oled-128x64 */ from machine import I2C, Pin from DIYables_MicroPython_OLED import OLED_SSD1306_I2C import utime # Initialize I2C (replace with your correct pins for I2C) i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000) # Adjust ESP32 pins according to your setup # Create the SSD1306 display object oled = OLED_SSD1306_I2C(128, 64, i2c) # Clear the display buffer oled.clear_display() # 16x16 heart bitmap in RGB565 format heart_bitmap = [ # 'DIYables Icon', 128x57px 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xfe, 0x00, 0x03, 0xf1, 0xf8, 0x00, 0x3e, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xc0, 0x03, 0xf1, 0xf8, 0x00, 0x7e, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf0, 0x03, 0xf0, 0xfc, 0x00, 0x7c, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf8, 0x03, 0xf0, 0xfc, 0x00, 0xfc, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xfc, 0x03, 0xf0, 0x7e, 0x00, 0xf8, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x01, 0xfe, 0x03, 0xf0, 0x3f, 0x01, 0xf8, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x7f, 0x03, 0xf0, 0x3f, 0x03, 0xf0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x3f, 0x03, 0xf0, 0x1f, 0x83, 0xe0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x3f, 0x83, 0xf0, 0x1f, 0x87, 0xe0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x0f, 0xc7, 0xc0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x07, 0xef, 0xc0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x07, 0xff, 0x80, 0xf0, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x03, 0xff, 0x00, 0xfe, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x03, 0xff, 0x00, 0xff, 0xc0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x01, 0xfe, 0x00, 0xff, 0xe0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x00, 0xfe, 0x00, 0xff, 0xf8, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x1f, 0x83, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xfc, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x3f, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0x7f, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x00, 0xff, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xf8, 0x03, 0xfe, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xfc, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf8, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xe0, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0x80, 0x03, 0xf0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xf8, 0x00, 0x03, 0xe0, 0x00, 0xfc, 0x00, 0xff, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xc7, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xc7, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xc7, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xc7, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0x9f, 0xc6, 0x7f, 0x1f, 0xdf, 0xf9, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0x03, 0xc0, 0x0f, 0x1e, 0x03, 0xe0, 0x3f, 0xff, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0x03, 0xc0, 0x07, 0x1c, 0x01, 0xc0, 0x3f, 0xff, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xfe, 0x00, 0xfd, 0xf1, 0xc3, 0xc7, 0x18, 0xf8, 0x8f, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xfe, 0x00, 0xff, 0xf1, 0xc7, 0xe3, 0x18, 0xf8, 0x8f, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xfe, 0x01, 0xc7, 0xe3, 0x18, 0x00, 0xc0, 0xff, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xfc, 0x01, 0xc7, 0xe3, 0x18, 0x00, 0xe0, 0x3f, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xff, 0xf8, 0x00, 0xf8, 0xf1, 0xc7, 0xe3, 0x18, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0x83, 0xff, 0xff, 0xff, 0xf0, 0x00, 0xf8, 0xf1, 0xc3, 0xc7, 0x18, 0xff, 0xff, 0x1f, 0xff, 0xff, 0x03, 0xff, 0xff, 0xff, 0xe0, 0x00, 0xf8, 0x61, 0xc0, 0x07, 0x1c, 0x01, 0xc0, 0x3f, 0xff, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xc0, 0x00, 0xfc, 0x01, 0xc0, 0x0f, 0x1e, 0x01, 0x80, 0x3f, 0xff, 0xfc, 0x03, 0xff, 0xff, 0xff, 0x80, 0x00, 0xfe, 0x19, 0xc4, 0x3f, 0x1f, 0x87, 0xe0, 0xff, 0xff, 0xf8, 0x03, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x03, 0xff, 0xff, 0xfc, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x03, 0xff, 0xff, 0xf0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x03, 0xff, 0xff, 0xc0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ] # Draw the bitmap on the display oled.draw_bitmap(0, 0, heart_bitmap, 128, 57, 1) # Update the display with the new image oled.display() utime.sleep(3) #oled.invert_display(True)

En exécutant le code ci-dessus, vous verrez l'image affichée sur l'écran OLED comme indiqué ci-dessous.

ESP32 MicroPython affiche une image sur OLED

Pour afficher une autre image sur un écran OLED, vous pouvez suivre les étapes ci-dessous :

  • Changez l'image (dans n'importe quel format) en un tableau bitmap. Vous pouvez utiliser cet outil en ligne outil en ligne pour la conversion. Regardez l'image ci-dessous pour apprendre comment convertir une image en un tableau bitmap. J'ai converti l'icône ESP32 en un tableau bitmap.
image vers tableau bitmap
  • Mettre à jour le tableau bitmap converti dans le code MicroPython ESP32 avec le nouveau code pour le tableau.
  • Modifier la largeur et la hauteur de l'image dans le code MicroPython ESP32 pour qu'elles correspondent aux dimensions de l'image.

Veuillez noter que la taille de l'image doit être égale ou inférieure à celle de l'écran.

Code MicroPython ESP32 : Dessiner des formes sur un écran OLED

/* * Ce code ESP32 MicroPython a été développé par newbiely.fr * Ce code ESP32 MicroPython 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-micropython/esp32-micropython-oled-128x64 */ from machine import I2C, Pin from DIYables_MicroPython_OLED import OLED_SSD1306_I2C # Initialize I2C i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000) # Adjust ESP32 pins according to your setup # Initialize the OLED display oled = OLED_SSD1306_I2C(128, 64, i2c) # Clear the display oled.clear_display() oled.display() # Draw a rectangle oled.draw_rect(0, 0, 40, 25, 1) #oled.fill_rect(0, 0, 40, 25, 1) oled.display() # Draw a circle #oled.draw_circle(64, 32, 20, 1) oled.fill_circle(64, 32, 20, 1) oled.display() # Draw a triangle oled.draw_triangle(80, 62, 128, 62, 104, 32, 1) #oled.fill_triangle(80, 62, 128, 62, 104, 32, 1) oled.display()

Après avoir exécuté le code, vous verrez un rectangle, un cercle et un triangle sur l'écran OLED.

ESP32 MicroPython dessine un rectangle, un cercle et un triangle sur OLED

Dépannage OLED

Si rien n'apparaît sur l'écran OLED, veuillez suivre ces étapes :

  • Vérifiez que le câblage est correct.
  • Assurez-vous que votre OLED I2C dispose d'un pilote SSD1306.
  • Trouvez l'adresse I2C de votre OLED en utilisant ce code de scanner d'adresses I2C sur l'ESP32.
from machine import I2C, Pin import utime i2c = I2C(1, scl=Pin(22), sda=Pin(21)) print('Scanning I2C bus...') devices = i2c.scan() if len(devices) == 0: print("No I2C devices found") else: print('I2C devices found:',len(devices)) for device in devices: print("Decimal address: ",device," | Hex address: ",hex(device)) utime.sleep(2)

Sortie dans le Shell en bas de Thonny :

Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Scanning... I2C device found at address 0x3C ! done Scanning... I2C device found at address 0x3C ! done
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

※ 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 !