Arduino UNO R4 - OLED 128x32

Ce tutoriel vous montrera comment utiliser un Arduino UNO R4 avec un écran OLED I2C 128x32. Vous apprendrez :

Écran OLED I2C Arduino UNO R4

À propos de l'écran OLED

Pinout d'affichage OLED I2C

  • Broche GND : doit être connectée à la masse de l'Arduino UNO R4
  • Broche VCC : est l'alimentation du module d'affichage à laquelle nous connectons la broche 5 V de l'Arduino UNO R4.
  • Broche SCL : est la ligne d'horloge série de l'interface I2C.
  • Broche SDA : est la ligne de données série de l'interface I2C.
Schéma des broches OLED

※ Note:

L'arrangement des broches sur un module OLED peut différer selon le fabricant et le modèle du module. Vérifiez toujours et suivez les étiquettes sur le module OLED. Soyez attentif !

Ce guide concerne un écran OLED qui utilise le pilote I2C SSD1306. Nous l'avons testé avec un écran OLED de DIYables. Il fonctionne parfaitement sans aucun problème.

Diagramme de câblage

Schéma de câblage Arduino UNO R4 OLED 128x32

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

Voir Comment alimenter l'Arduino UNO R4..

Si vous utilisez un autre type d'Arduino UNO R4, la disposition des broches ne sera pas la même que celle de l'UNO. Consultez le tableau ci-dessous pour obtenir des informations sur les autres modèles Arduino UNO R4.

128x32 OLED Module Arduino UNO R4
Vin 5V
GND GND
SDA A4
SCL A5

Comment utiliser l'OLED avec Arduino UNO R4

Installer la bibliothèque OLED SSD1306

  • Accédez à l'icône Libraries sur le côté gauche de l'IDE Arduino.
  • Tapez "SSD1306" dans la zone de recherche et recherchez la bibliothèque SSD1306 créée par Adafruit.
  • Appuyez sur le bouton Install pour ajouter la bibliothèque.
Bibliothèque OLED pour Arduino UNO R4
  • Vous devrez installer certaines bibliothèques supplémentaires.
  • Cliquez sur le bouton Tout installer pour installer toutes les bibliothèques requises.
Bibliothèque de capteurs Adafruit GFX pour Arduino UNO R4

Comment programmer pour un écran OLED

  • Inclure une bibliothèque.
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>
  • Définir la taille de l'écran sur OLED 123x32.
#define SCREEN_WIDTH 128 // Définit la largeur de l'affichage OLED en pixels #define SCREEN_HEIGHT 32 // Définit la hauteur de l'affichage OLED en pixels
  • Créer un élément OLED SSD1306.
// Initialiser un objet d'affichage Adafruit SSD1306 pour la communication I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  • Dans la fonction setup(), configurez l'écran OLED.
// Démarrer l'écran OLED avec l'adresse I2C spécifique (0x3C) pour une résolution de 128x32 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); // Afficher le message d'erreur si l'initialisation échoue while (true); // Entrer dans une boucle infinie pour arrêter l'exécution }
  • Ensuite, vous pouvez afficher du texte, des images et tracer des lignes.

Code Arduino UNO R4 - Afficher du texte sur OLED

/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 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/arduino-uno-r4/arduino-uno-r4-oled-128x32 */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // declare an SSD1306 display object connected to I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x32 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(1); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display oled.println("Hello World!"); // text to display oled.display(); // show on OLED } void loop() { }

Voici quelques fonctions que vous pouvez utiliser pour afficher du texte sur l'OLED :

  • oled.clearDisplay(): éteint tous les pixels.
  • oled.drawPixel(x, y, color): dessine un pixel aux coordonnées x, y.
  • oled.setTextSize(n): modifie la taille du texte, avec des valeurs possibles de 1 à 8.
  • oled.setCursor(x, y): définit le point de départ du texte.
  • oled.setTextColor(WHITE): met le texte en blanc.
  • oled.setTextColor(BLACK, WHITE): met le texte en noir et l'arrière-plan en blanc.
  • oled.println("message"): affiche le texte.
  • oled.println(number): affiche un nombre.
  • oled.println(number, HEX): affiche un nombre en format hexadécimal.
  • oled.display(): met à jour l'affichage avec les modifications.
  • oled.startscrollright(start, stop): déplace le texte de gauche à droite.
  • oled.startscrollleft(start, stop): déplace le texte de droite à gauche.
  • oled.startscrolldiagright(start, stop): déplace le texte en diagonale du coin inférieur gauche vers le coin supérieur droit.
  • oled.startscrolldiagleft(start, stop): déplace le texte en diagonale du coin inférieur droit vers le coin supérieur gauche.
  • oled.stopscroll(): arrête tout défilement du texte.

Code Arduino UNO R4 - Dessiner sur OLED

/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 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/arduino-uno-r4/arduino-uno-r4-oled-128x32 */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // declare an SSD1306 display object connected to I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x32 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.setCursor(0, 0); } void loop() { // draw rectangle oled.clearDisplay(); oled.drawRect(0, 15, 60, 40, WHITE); oled.display(); delay(2000); // fill rectangle oled.clearDisplay(); oled.fillRect(0, 15, 60, 40, WHITE); oled.display(); delay(2000); // draw the round rectangle oled.clearDisplay(); oled.drawRoundRect(0, 15, 60, 40, 8, WHITE); oled.display(); delay(2000); // fill the round rectangle oled.clearDisplay(); oled.fillRoundRect(0, 15, 60, 40, 8, WHITE); oled.display(); delay(2000); // draw circle oled.clearDisplay(); oled.drawCircle(20, 35, 20, WHITE); oled.display(); delay(2000); // fill circle oled.clearDisplay(); oled.fillCircle(20, 35, 20, WHITE); oled.display(); delay(2000); // draw triangle oled.clearDisplay(); oled.drawTriangle(30, 15, 0, 60, 60, 60, WHITE); oled.display(); delay(2000); // fill triangle oled.clearDisplay(); oled.fillTriangle(30, 15, 0, 60, 60, 60, WHITE); oled.display(); delay(2000); }

Arduino UNO R4 Code – Afficher l'image

Pour afficher une image sur un écran OLED, commencez par convertir l'image, quel que soit son format, en un tableau bitmap. Vous pouvez utiliser cet outil en ligne (https://javl.github.io/image2cpp/) pour le convertir. Regardez l'image ci-dessous pour voir comment transformer une image en un tableau bitmap. J'ai converti l'icône Arduino en un tableau bitmap.

image en tableau bitmap

Copiez le code du nouveau tableau et mettez-le à jour dans le tableau d'icônes Arduino dans le code ci-dessous.

La vidéo ci-dessous montre comment le faire avec l'écran OLED 128x64 et Arduino Uno et l'icône Arduino.

Nous pouvons faire de même pour le faire fonctionner avec l'Arduino Uno R4 et l'OLED 128x32. Le code ci-dessous affiche l'icône DIYables sur l'OLED 128x32.

/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 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/arduino-uno-r4/arduino-uno-r4-oled-128x32 */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // declare an SSD1306 display object connected to I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // bitmap of DIYable-icon image int bitmap_width = 72; // MUST match to bitmap image size int bitmap_height = 32; // MUST match to bitmap image size const unsigned char bitmap_DIYables [] PROGMEM = { // '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 }; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x32 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing } void loop() { oled.clearDisplay(); // display bitmap to the center int x = (SCREEN_WIDTH - bitmap_width) / 2; int y = (SCREEN_HEIGHT - bitmap_height) / 2; oled.drawBitmap(x, y, bitmap_DIYables, bitmap_width, bitmap_height, WHITE); oled.display(); delay(2000); }

※ Note:

  • La taille de l'image doit être aussi petite que possible ou inférieure à la taille de l'écran.
  • Pour utiliser le code donné pour un OLED 128x32, vous devez redimensionner l'image et ajuster la largeur et la hauteur dans la fonction oled.drawBitmap();.

Comment centrer verticalement et horizontalement le texte et le nombre sur un écran OLED

/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 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/arduino-uno-r4/arduino-uno-r4-oled-128x32 */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // // create SSD1306 display object connected to I2C void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x32 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(2); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display } void loop() { // display string String text = "DIYables"; oledDisplayCenter(text); delay(2000); // display number int number = 21; String str = String(number); oledDisplayCenter(str); delay(2000); } void oledDisplayCenter(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Dépannage des écrans OLED

Si l'écran OLED n'affiche rien, veuillez suivre ces étapes :

  • Assurez-vous que le câblage est correctement réalisé.
  • Confirmez que votre OLED I2C est équipé d'un pilote SSD1306.
  • Vérifiez l'adresse I2C de votre OLED en utilisant le code suivant du scanner d'adresses I2C sur Arduino UNO R4.
// I2C address scanner program #include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); Serial.println("I2C Scanner"); } void loop() { byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; 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); // wait 5 seconds for next scan }

La sortie sur le moniteur série :

COM6
Send
Scanning... I2C device found at address 0x3C ! done Scanning... I2C device found at address 0x3C ! done
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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