Raspberry Pi Pico - OLED

Ce tutoriel explique comment utiliser un Raspberry Pi Pico avec un écran OLED I2C 128x64. Nous aborderons :

Écran OLED I2C Raspberry Pi Pico

Préparation du matériel

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternativement:
1×Câble Micro USB
1×Écran OLED I2C SSD1306 128x64
1×Fils de connexion
1×Recommandé: Carte d'extension à bornier à vis pour Raspberry Pi Pico

Ou vous pouvez acheter les kits suivants:

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 existent sous différents types, chacun diffère par les protocoles de communication, leurs tailles et les options de couleur.

  • Protocoles de communication : I2C, SPI
  • Tailles : 128x64, 128x32
  • Couleurs disponibles : blanc, bleu, jaune et d'autres
OLED Raspberry Pi Pico

SPI et I2C pour les écrans OLED

  • SPI: Plus rapide mais nécessite plus de broches sur le Raspberry Pi Pico.
  • I2C: Utilise moins de broches et permet la connexion à plusieurs péripheriques.

Choisissez le SPI pour un transfert de données plus rapide ou l'I2C pour moins de broches. Ce guide utilise l'affichage OLED SSD1306 I2C 128×64.

Schéma des broches de l'affichage OLED I2C

  • Broche GND : Connectez-la à la masse du Raspberry Pi Pico.
  • Broche VCC : Connectez-la à 5 V ou à 3,3 V sur le Raspberry Pi Pico.
  • Broche SCL : Broche d'horloge pour l'interface I2C.
  • Broche SDA : Broche de données pour l'interface I2C.
Schéma des broches OLED

※ Note:

Ce guide concerne l'utilisation d'un écran OLED avec le pilote I2C SSD1306. Nous l'avons testé avec un écran OLED de DIYables, et cela a parfaitement fonctionné.

Diagramme de câblage

  • Comment connecter le Raspberry Pi Pico à un écran OLED 128x64
Schéma de câblage OLED 128x64 pour Raspberry Pi Pico

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

Consultez le tableau ci-dessous pour obtenir des détails sur d'autres modèles de Raspberry Pi Pico.

OLED Module Raspberry Pi Pico
VCC 3.3V
GND GND
SDA GP0
SCL GP1

Code pour Raspberry Pi Pico - Afficher du texte, des nombres entiers et des nombres à virgule flottante sur l'écran OLED

/* * Ce code Raspberry Pi Pico a été développé par newbiely.fr * Ce code Raspberry Pi Pico 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/raspberry-pico/raspberry-pi-pico-oled-128x64 */ from machine import I2C, Pin from DIYables_MicroPython_OLED import OLED_SSD1306_I2C # Initialize I2C i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000) # Adjust Raspberry Pi Pico 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

Veuillez suivre ces instructions étape par étape :

  • Assurez-vous que l'IDE Thonny est installé sur votre ordinateur.
  • Assurez-vous que le firmware MicroPython est installé sur votre Raspberry Pi Pico.
  • Si c'est la première fois que vous utilisez un Raspberry Pi Pico, reportez-vous au tutoriel Raspberry Pi Pico - Premiers pas. pour des instructions détaillées.
  • Connectez l'écran OLED au Raspberry Pi Pico selon le schéma fourni.
  • Connectez le Raspberry Pi Pico à votre ordinateur à l'aide d'un câble USB.
  • Lancez l'IDE Thonny sur votre ordinateur.
  • Dans l'IDE Thonny, sélectionnez l'interpréteur MicroPython (Raspberry Pi Pico) en vous rendant dans Outils Options.
  • Dans l'onglet Interpréteur, sélectionnez MicroPython (Raspberry Pi Pico) dans le menu déroulant.
  • Assurez-vous que le bon port est sélectionné. L'IDE Thonny devrait détecter automatiquement le port, mais vous devrez peut-être le sélectionner manuellement (par exemple, COM3 sous Windows ou /dev/ttyACM0 sous 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 pour Raspberry Pi Pico
  • Copiez le code ci-dessus et collez-le dans l’éditeur de l’IDE Thonny.
  • Enregistrez le script sur votre Raspberry Pi Pico 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 Raspberry Pi Pico. Sélectionnez Raspberry Pi Pico.
    • 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é.
  • Regardez la sortie sur l’écran OLED. Elle ressemble à ceci :
Affichage OLED du Raspberry Pi Pico : texte, entier et nombre à virgule flottante

Comment centrer automatiquement le texte et les chiffres verticalement et horizontalement sur un écran OLED

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

/* * Ce code Raspberry Pi Pico a été développé par newbiely.fr * Ce code Raspberry Pi Pico 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/raspberry-pico/raspberry-pi-pico-oled-128x64 */ from machine import I2C, Pin from DIYables_MicroPython_OLED import OLED_SSD1306_I2C import utime # Initialize I2C i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000) # Adjust Raspberry Pi Pico 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, le texte sera centré à la fois verticalement et horizontalement sur l'écran OLED.

Raspberry Pi Pico OLED alignement vertical et horizontal

Code pour Raspberry Pi Pico - Dessiner sur OLED

/* * Ce code Raspberry Pi Pico a été développé par newbiely.fr * Ce code Raspberry Pi Pico 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/raspberry-pico/raspberry-pi-pico-oled-128x64 */ from machine import I2C, Pin from DIYables_MicroPython_OLED import OLED_SSD1306_I2C # Initialize I2C i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000) # Adjust Raspberry Pi Pico 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()

En exécutant le code ci-dessus, vous verrez un rectangle, un cercle et un triangle affichés sur l'écran OLED comme indiqué ci-dessous.

Raspberry Pi Pico dessine un rectangle, un cercle et un triangle sur un écran OLED.

Code Raspberry Pi Pico – Afficher une image sur OLED

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

/* * Ce code Raspberry Pi Pico a été développé par newbiely.fr * Ce code Raspberry Pi Pico 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/raspberry-pico/raspberry-pi-pico-oled-128x64 */ from machine import I2C, Pin from DIYables_MicroPython_OLED import OLED_SSD1306_I2C import utime # Initialize I2C i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000) # Adjust Raspberry Pi Pico 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 ci-dessous.

Raspberry Pi Pico affiche une image sur un écran OLED.

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

  • Convertissez l'image (dans n'importe quel format) en un tableau de bits. Vous pouvez utiliser cet outil en ligne pour la conversion. Consultez l'image ci-dessous pour apprendre comment convertir une image en un tableau de bits. J'ai converti l'icône Raspberry Pi Pico en un tableau de bits.
image en tableau bitmap
  • Mettez à jour le tableau bitmap converti dans le code Raspberry Pi Pico avec le nouveau code du tableau.
  • Modifiez la largeur et la hauteur de l'image dans le code Raspberry Pi Pico pour les faire correspondre aux dimensions de l'image.

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

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 le Raspberry Pi Pico.
from machine import I2C, Pin import utime i2c = I2C(0, sda=Pin(0), scl=Pin(1)) 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 (Raspberry Pi Pico) • Board CDC @ COM29 ≡

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