Arduino Mega - OLED 128x32

Ce guide vous montre comment utiliser un Arduino Mega avec un écran OLED 128x32 en utilisant I2C. Vous apprendrez :

Arduino Mega OLED I2C display

Matériel requis

1×Arduino Mega
1×Câble USB 2.0 type A/B
1×Écran OLED I2C SSD1306 128x32
1×Câbles de connexion
1×Recommandé: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommandé: Breadboard Shield for Arduino Mega
1×Recommandé: Enclosure for Arduino Mega

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

Brochage de l'écran OLED I2C

  • Broche GND : connecter à la masse de l'Arduino Mega.
  • Broche VCC : alimentation pour l'écran ; connecter à la broche 5V de l'Arduino Mega.
  • Broche SCL : ligne d'horloge I2C.
  • Broche SDA : ligne de données I2C.
OLED Pinout

※ Note:

La disposition des broches sur un module OLED peut être différente selon le fabricant et le modèle. Vérifiez toujours les étiquettes sur le module OLED et suivez-les. Faites attention !

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

Schéma de câblage

Arduino Mega OLED 128x32 wiring diagram

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

Si vous utilisez un Arduino Mega différent, la disposition des broches sera différente de l'Uno. Consultez le tableau ci-dessous pour des informations sur les autres modèles Arduino Mega.

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

Comment utiliser l'OLED avec Arduino Mega

Installer la bibliothèque OLED SSD1306

  • Trouvez l'icône Libraries sur le côté gauche de l'IDE Arduino.
  • Tapez "SSD1306" dans la boîte de recherche et trouvez la bibliothèque SSD1306 par Adafruit.
  • Cliquez sur Install pour ajouter la bibliothèque.
Arduino Mega OLED library
  • Vous devez installer quelques bibliothèques supplémentaires.
  • Cliquez sur le bouton Install All pour installer toutes les bibliothèques nécessaires.
Arduino Mega Adafruit GFX sensor library

Comment programmer pour OLED

  • Ajouter une bibliothèque.
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>
  • Définir la taille de l'écran OLED 128 par 32.
#define SCREEN_WIDTH 128 // OLED display width in pixels #define SCREEN_HEIGHT 32 // OLED display height in pixels
  • Créer un objet d'affichage OLED SSD1306.
// Instantiate the SSD1306 OLED object for I2C communication (no reset pin) Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  • Dans la fonction setup(), préparer l'écran OLED.
// Initialize OLED display using I2C address 0x3C for a 128x32 display if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); // Output error message if initialization fails while (true); // Stay in an endless loop to stop further processing }
  • Ensuite vous pouvez afficher du texte, des images et dessiner des lignes.

Code Arduino Mega - Afficher du texte sur OLED

/* * Ce code Arduino Mega a été développé par newbiely.fr * Ce code Arduino Mega 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-mega/arduino-mega-oled-128x32-display */ #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'écran OLED :

  • Oled.clearDisplay() : éteint tous les pixels.
  • Oled.drawPixel(x, y, color) : dessine un point à la position x, y.
  • Oled.setTextSize(n) : change la taille du texte ; choisissez de 1 à 8.
  • Oled.setCursor(x, y) : définit où le texte commence.
  • Oled.setTextColor(WHITE) : rend la couleur du texte blanche.
  • Oled.setTextColor(BLACK, WHITE) : rend le texte noir et l'arrière-plan blanc.
  • Oled.println("message") : affiche du texte.
  • Oled.println(number) : affiche un nombre.
  • Oled.println(number, HEX) : affiche un nombre en hexadécimal (base-16).
  • Oled.display() : rafraîchit l'écran pour montrer les changements.
  • Oled.startscrollright(start, stop) : fait défiler le texte de gauche à droite.
  • Oled.startscrollleft(start, stop) : fait défiler le texte de droite à gauche.
  • Oled.startscrolldiagright(start, stop) : fait défiler le texte en diagonale du bas gauche vers le haut droit.
  • Oled.startscrolldiagleft(start, stop) : fait défiler le texte en diagonale du bas droit vers le haut gauche.
  • Oled.stopscroll() : arrête tout défilement de texte.

Code Arduino Mega - Dessiner sur OLED

/* * Ce code Arduino Mega a été développé par newbiely.fr * Ce code Arduino Mega 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-mega/arduino-mega-oled-128x32-display */ #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); }

Code Arduino Mega – Afficher une image

Pour afficher une image sur un écran OLED, transformez d'abord l'image (n'importe quel format) en un tableau bitmap. Vous pouvez utiliser cet outil Convertisseur d'Image vers Bitmap pour la convertir. Regardez l'image ci-dessous pour voir comment changer une image en tableau bitmap. J'ai transformé l'icône Arduino en tableau bitmap.

image to bitmap array

Copiez le nouveau code de tableau et collez-le dans le tableau d'icône Arduino dans le code ci-dessous.

La vidéo ci-dessous montre comment le faire avec un écran OLED 128x64, un Arduino Uno, et le logo Arduino.

Nous pouvons faire de la même façon pour que cela fonctionne avec Arduino Mega et l'OLED 128x32. Le code ci-dessous affiche l'icône DIYables sur l'OLED 128x32.

/* * Ce code Arduino Mega a été développé par newbiely.fr * Ce code Arduino Mega 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-mega/arduino-mega-oled-128x32-display */ #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:

  • L'image doit s'adapter à l'écran ou être plus petite.
  • Si vous voulez utiliser le code avec un OLED 128x32, vous devez redimensionner l'image et changer la largeur et la hauteur dans la fonction oled.drawBitmap().

Comment centrer verticalement et horizontalement du texte/nombre sur OLED

/* * Ce code Arduino Mega a été développé par newbiely.fr * Ce code Arduino Mega 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-mega/arduino-mega-oled-128x32-display */ #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 OLED

Si l'écran OLED est vide, veuillez suivre ces étapes :

  • Assurez-vous que le câblage est correct.
  • Vérifiez que votre OLED I2C utilise un pilote SSD1306.
  • Vérifiez l'adresse I2C de votre OLED en utilisant le code Scanner d'Adresse I2C sur la carte Arduino Mega.
// 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 }

Ce que vous voyez 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 !