ESP32 - OLED 128×32

Ce tutoriel vous explique comment utiliser un ESP32 et MicroPython avec un écran OLED I2C 128x32. Vous apprendrez :

É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 128x32
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'affichage OLED I2C 128x32

Schéma des broches de l'écran OLED I2C 128x32

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

Diagramme de câblage

  • Comment connecter l'ESP32 et l'OLED 128x32 en utilisant breadboard
ESP32 MicroPython OLED 128x32 schéma de câblage)

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

Comment connecter l'ESP32 et un écran OLED 128x32

Ci-dessous se trouve le tableau de câblage entre le module OLED 128x32 et l'ESP32.

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

Code MicroPython pour ESP32 - Afficher du texte, des nombres entiers et des 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-128x32 */ 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) # Initialize the OLED display oled = OLED_SSD1306_I2C(128, 32, 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.678 oled.set_cursor(0, 0) oled.println(text) oled.set_cursor(0, 19) oled.println(str(integer_value)) # Print integer and move to the next line oled.set_cursor(50, 19) oled.println("{:.3f}".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

Veuillez suivre ces instructions étape par étape :

  • Assurez-vous que l'IDE Thonny est installé sur votre ordinateur.
  • Assurez-vous que le micrologiciel MicroPython est installé sur votre carte ESP32.
  • Si c'est la première fois que vous utilisez un ESP32 avec MicroPython, reportez-vous au tutoriel ESP32 - Premiers pas. pour des instructions détaillées.
  • Connectez l'écran OLED à l'ESP32 selon le schéma fourni.
  • Connectez la carte ESP32 à votre ordinateur à l'aide d'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 port correct est sélectionné. L'IDE Thonny le détecte généralement automatiquement, mais vous devrez peut-être le sélectionner manuellement (comme COM12 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 ci-dessus et collez-le dans l'éditeur de Thonny IDE.
  • Enregistrez le script sur votre carte ESP32 en procédant comme suit:
    • Cliquez sur le bouton Enregistrer, ou utilisez les touches Ctrl+S.
    • Dans la boîte de dialogue d'enregistrement, vous verrez deux sections : Cet ordinateur et l'appareil MicroPython. Sélectionnez l'appareil MicroPython
    • Enregistrez le fichier sous main.py
  • Cliquez sur le bouton vert Exécuter (ou appuyez sur F5) pour exécuter le script. Le script sera exécuté.
  • Vérifiez l'affichage OLED. Il ressemble à ce qui suit :
ESP32 MicroPython OLED affichage de texte, nombre entier et nombre à virgule flottante

Code MicroPython pour ESP32 - Dessiner 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-128x32 */ 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) # Initialize the OLED display oled = OLED_SSD1306_I2C(128, 32, 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) # Draw a circle oled.draw_circle(64, 16, 15, 1) #oled.fill_circle(64, 16, 15, 1) # Draw a triangle #oled.draw_triangle(80, 31, 128, 31, 104, 0, 1) oled.fill_triangle(80, 31, 128, 31, 104, 0, 1) oled.display()

Lorsque vous exécutez le code ci-dessus, un rectangle, un cercle et un triangle apparaîtront sur l'écran OLED, comme démontré ci-dessous.

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

ESP32 MicroPython Code – Afficher une image sur un écran OLED

Le code ci-dessous dessine 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-128x32 */ 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) # Create the SSD1306 display object oled = OLED_SSD1306_I2C(128, 32, i2c) # Clear the display buffer oled.clear_display() utime.sleep(2) # 16x16 heart bitmap in RGB565 format heart_bitmap = [ # 'DIYables Icon', 72x32 0x00, 0x0f, 0xff, 0xff, 0x8f, 0xf8, 0x07, 0x38, 0x07, 0x00, 0x0f, 0xff, 0xff, 0x8f, 0xfe, 0x07, 0x1c, 0x0e, 0x00, 0x0f, 0xff, 0xff, 0x8f, 0xff, 0x07, 0x1c, 0x1c, 0x00, 0x0f, 0xff, 0xff, 0x8e, 0x07, 0x87, 0x0e, 0x1c, 0x00, 0x0f, 0xff, 0xff, 0x8e, 0x03, 0xc7, 0x0f, 0x38, 0x00, 0x0f, 0xff, 0xff, 0x8e, 0x01, 0xc7, 0x07, 0x38, 0x00, 0x0f, 0xff, 0xff, 0x8e, 0x01, 0xc7, 0x03, 0xf0, 0xf0, 0x0f, 0xff, 0xff, 0x8e, 0x01, 0xc7, 0x03, 0xe0, 0xfc, 0x0f, 0xff, 0xff, 0x8e, 0x01, 0xc7, 0x01, 0xe0, 0xfe, 0x0f, 0xff, 0xff, 0x8e, 0x03, 0xc7, 0x01, 0xc0, 0xff, 0x8f, 0xff, 0xff, 0x8e, 0x03, 0x87, 0x01, 0xc0, 0xff, 0x8f, 0xff, 0xff, 0x8e, 0x0f, 0x87, 0x01, 0xc0, 0xff, 0xcf, 0xff, 0xff, 0x8f, 0xff, 0x07, 0x01, 0xc0, 0xff, 0xef, 0xff, 0xff, 0x8f, 0xfc, 0x07, 0x01, 0xc0, 0xff, 0xef, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xfc, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xfc, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xfc, 0xfc, 0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0x0e, 0x0c, 0x0c, 0xc3, 0x07, 0xff, 0xef, 0xff, 0xfe, 0x0f, 0xec, 0xec, 0x99, 0x7f, 0xff, 0xef, 0xff, 0xfe, 0x0f, 0x04, 0xe4, 0x81, 0x0f, 0xff, 0xcf, 0xff, 0xfc, 0x0e, 0x32, 0xe4, 0x9f, 0xc7, 0xff, 0x8f, 0xff, 0xf8, 0x0e, 0x32, 0x4c, 0x9b, 0x67, 0xff, 0x0f, 0xff, 0xf0, 0x0e, 0x04, 0x0c, 0xc3, 0x0f, 0xfe, 0x0f, 0xff, 0xe0, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x0f, 0xff, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x0f, 0xfc, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff ] # Draw the bitmap on the display oled.draw_bitmap(0, 0, heart_bitmap, 72, 32, 1) # Update the display with the new image oled.display() utime.sleep(3) #oled.invert_display(True)

Lorsque vous exécutez le code ci-dessus, l'image apparaîtra sur l'écran OLED, comme indiqué ci-dessous.

ESP32 MicroPython afficher une image sur l'écran OLED

Pour afficher une image différente sur l'écran OLED, suivez ces étapes :

  • Convertissez l'image (dans n'importe quel format) en un tableau bitmap.
  • Vous pouvez utiliser cet outil en ligne pour la conversion (https://javl.github.io/image2cpp/).
  • Reportez-vous à l'image ci-dessous pour vous guider sur la façon de convertir une image en un tableau bitmap.
  • Dans cet exemple, j'ai converti l'icône ESP32 en un tableau bitmap.
image en tableau de bits
  • Remplacez le tableau bitmap existant dans votre code MicroPython pour ESP32 par le tableau nouvellement converti.
  • Ajustez la largeur et la hauteur de l'image dans votre code MicroPython pour ESP32 afin de correspondre aux dimensions de la nouvelle image.

Note : Assurez-vous que la taille de l'image est égale ou inférieure à celle de l'écran OLED.

Comment centrer automatiquement verticalement et horizontalement le texte/numéro sur 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-128x32 */ 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) # Initialize the OLED display oled = OLED_SSD1306_I2C(128, 32, 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, le texte sera centré à la fois verticalement et horizontalement sur l'écran OLED.

Alignement vertical et horizontal centré pour l'OLED sur ESP32 MicroPython

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