Guide pour l’affichage I2C OLED avec Arduino

Guide pour l'affichage I2C OLED avec Arduino

Cet article explique comment utiliser l’écran OLED I2C SSD1306 de 0,96 pouce avec l’Arduino. Nous allons vous montrer quelques fonctionnalités de l’écran OLED, comment le connecter à la carte Arduino et comment écrire du texte, dessiner des formes et afficher des images bitmap. Enfin, nous allons créer un exemple de projet qui affiche les relevés de température et d’humidité.

Guide pour l'affichage I2C OLED avec Arduino

Présentation de l’écran OLED de 0,96 pouce

L’écran à diodes électroluminescentes organiques (OLED) que nous allons utiliser dans ce didacticiel est le modèle SSD1306 : un écran monochrome de 0,96 pouce avec 128 x 64 pixels, comme illustré dans la figure suivante.

Écran OLED SSD1306 I2C

L’écran OLED ne nécessite pas de rétroéclairage, ce qui se traduit par un très beau contraste dans les environnements sombres. De plus, ses pixels ne consomment de l’énergie que lorsqu’ils sont allumés, de sorte que l’écran OLED consomme moins d’énergie par rapport aux autres écrans.

Le modèle que nous utilisons ici n’a que quatre broches et communique avec l’Arduino en utilisant le protocole de communication I2C. Certains modèles sont livrés avec une broche RESET supplémentaire. Il existe également d’autres écrans OLED qui communiquent à l’aide de la communication SPI.

Câblage des broches

Parce que l’écran OLED utilise le protocole de communication I2C, le câblage est très simple. Il vous suffit de vous connecter aux broches Arduino Uno I2C comme indiqué dans le tableau ci-dessous.

Broche Câblage à Arduino Uno
Vin 5V
Terre Terre
SCL A5
SDA A4

Si vous utilisez une autre carte Arduino, assurez-vous de vérifier les bonnes broches I2C :

  • Nano : SDA (A4) ; SCL (A5);
  • MÉGA : SDA (20) ; SCL (21);
  • Léonard : SDA (20) ; SCL (21);
arduino avec schéma d'affichage OLED

Bibliothèques

Pour contrôler l’écran OLED, vous avez besoin du adafruit_SSD1306.h et le adafruit_GFX.h bibliothèques. Suivez les instructions suivantes pour installer ces bibliothèques.

1. Ouvrez votre IDE Arduino et accédez à Sketch > Inclure la bibliothèque > Gérer les bibliothèques. Le gestionnaire de bibliothèque devrait s’ouvrir.

2. Tapez « SSD1306 » dans la zone de recherche et installez la bibliothèque SSD1306 d’Adafruit.

Installation de la bibliothèque OLED SSD1306 ESP8266 ESP32 Arduino

3. Après avoir installé la bibliothèque SSD1306 à partir d’Adafruit, tapez « GFX » dans la zone de recherche et installez la bibliothèque.

Installation de la bibliothèque GFX ESP8266 ESP32 Arduino

4. Après avoir installé les bibliothèques, redémarrez votre IDE Arduino.

Conseils pour écrire du texte à l’aide de ces bibliothèques

Voici quelques fonctions qui vous aideront à gérer la bibliothèque d’affichage OLED pour écrire du texte ou dessiner des graphiques simples.

  • display.clearDisplay() – tous les pixels sont éteints
  • display.drawPixel(x,y, color) – tracer un pixel dans les coordonnées x,y
  • display.setTextSize(n) – définit la taille de la police, prend en charge les tailles de 1 à 8
  • display.setCursor(x,y) – définit les coordonnées pour commencer à écrire du texte
  • display.print(« message ») – imprime les caractères à l’emplacement x,y
  • display.display() – appelez cette méthode pour que les modifications prennent effet

Test de l’écran OLED

Après avoir câblé l’écran OLED à l’Arduino et installé toutes les bibliothèques requises, vous pouvez utiliser un exemple de la bibliothèque pour voir si tout fonctionne correctement.

Dans votre IDE Arduino, accédez à Fichier > Exemples > Adafruit SSD1306 et sélectionnez l’exemple pour l’affichage que vous utilisez.

Test de l'exemple de bibliothèque Adafruit SSD1306

Le code suivant devrait se charger :

/*********
  Complete project details at https://www.raspberryme.com
  
  This is an example for our Monochrome OLEDs based on SSD1306 drivers. Pick one up today in the adafruit shop! ------> http://www.adafruit.com/category/63_98
  This example is for a 128x32 pixel display using I2C to communicate 3 pins are required to interface (two I2C and one reset).
  Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!
  Written by Limor Fried/Ladyada for Adafruit Industries, with contributions from the open source community. BSD license, check license.txt for more information All text above, and the splash screen below must be included in any redistribution. 
*********/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16
static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };

void setup() {
  Serial.begin(115200);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  display.drawPixel(10, 10, WHITE);

  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);
  // display.display() is NOT necessary after every single drawing command,
  // unless that's what you want...rather, you can batch up a bunch of
  // drawing operations and then update the screen all at once by calling
  // display.display(). These examples demonstrate both approaches...

  testdrawline();      // Draw many lines

  testdrawrect();      // Draw rectangles (outlines)

  testfillrect();      // Draw rectangles (filled)

  testdrawcircle();    // Draw circles (outlines)

  testfillcircle();    // Draw circles (filled)

  testdrawroundrect(); // Draw rounded rectangles (outlines)

  testfillroundrect(); // Draw rounded rectangles (filled)

  testdrawtriangle();  // Draw triangles (outlines)

  testfilltriangle();  // Draw triangles (filled)

  testdrawchar();      // Draw characters of the default font

  testdrawstyles();    // Draw 'stylized' characters

  testscrolltext();    // Draw scrolling text

  testdrawbitmap();    // Draw a small bitmap image

  // Invert and restore display, pausing in-between
  display.invertDisplay(true);
  delay(1000);
  display.invertDisplay(false);
  delay(1000);

  testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT); // Animate bitmaps
}

void loop() {
}

void testdrawline() {
  int16_t i;

  display.clearDisplay(); // Clear display buffer

  for(i=0; i<display.width(); i+=4) {
    display.drawLine(0, 0, i, display.height()-1, WHITE);
    display.display(); // Update screen with each newly-drawn line
    delay(1);
  }
  for(i=0; i<display.height(); i+=4) {
    display.drawLine(0, 0, display.width()-1, i, WHITE);
    display.display();
    delay(1);
  }
  delay(250);

  display.clearDisplay();

  for(i=0; i<display.width(); i+=4) {
    display.drawLine(0, display.height()-1, i, 0, WHITE);
    display.display();
    delay(1);
  }
  for(i=display.height()-1; i>=0; i-=4) {
    display.drawLine(0, display.height()-1, display.width()-1, i, WHITE);
    display.display();
    delay(1);
  }
  delay(250);

  display.clearDisplay();

  for(i=display.width()-1; i>=0; i-=4) {
    display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE);
    display.display();
    delay(1);
  }
  for(i=display.height()-1; i>=0; i-=4) {
    display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE);
    display.display();
    delay(1);
  }
  delay(250);

  display.clearDisplay();

  for(i=0; i<display.height(); i+=4) {
    display.drawLine(display.width()-1, 0, 0, i, WHITE);
    display.display();
    delay(1);
  }
  for(i=0; i<display.width(); i+=4) {
    display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE);
    display.display();
    delay(1);
  }

  delay(2000); // Pause for 2 seconds
}

void testdrawrect(void) {
  display.clearDisplay();

  for(int16_t i=0; i<display.height()/2; i+=2) {
    display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE);
    display.display(); // Update screen with each newly-drawn rectangle
    delay(1);
  }

  delay(2000);
}

void testfillrect(void) {
  display.clearDisplay();

  for(int16_t i=0; i<display.height()/2; i+=3) {
    // The INVERSE color is used so rectangles alternate white/black
    display.fillRect(i, i, display.width()-i*2, display.height()-i*2, INVERSE);
    display.display(); // Update screen with each newly-drawn rectangle
    delay(1);
  }

  delay(2000);
}

void testdrawcircle(void) {
  display.clearDisplay();

  for(int16_t i=0; i<max(display.width(),display.height())/2; i+=2) {
    display.drawCircle(display.width()/2, display.height()/2, i, WHITE);
    display.display();
    delay(1);
  }

  delay(2000);
}

void testfillcircle(void) {
  display.clearDisplay();

  for(int16_t i=max(display.width(),display.height())/2; i>0; i-=3) {
    // The INVERSE color is used so circles alternate white/black
    display.fillCircle(display.width() / 2, display.height() / 2, i, INVERSE);
    display.display(); // Update screen with each newly-drawn circle
    delay(1);
  }

  delay(2000);
}

void testdrawroundrect(void) {
  display.clearDisplay();

  for(int16_t i=0; i<display.height()/2-2; i+=2) {
    display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i,
      display.height()/4, WHITE);
    display.display();
    delay(1);
  }

  delay(2000);
}

void testfillroundrect(void) {
  display.clearDisplay();

  for(int16_t i=0; i<display.height()/2-2; i+=2) {
    // The INVERSE color is used so round-rects alternate white/black
    display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i,
      display.height()/4, INVERSE);
    display.display();
    delay(1);
  }

  delay(2000);
}

void testdrawtriangle(void) {
  display.clearDisplay();

  for(int16_t i=0; i<max(display.width(),display.height())/2; i+=5) {
    display.drawTriangle(
      display.width()/2  , display.height()/2-i,
      display.width()/2-i, display.height()/2+i,
      display.width()/2+i, display.height()/2+i, WHITE);
    display.display();
    delay(1);
  }

  delay(2000);
}

void testfilltriangle(void) {
  display.clearDisplay();

  for(int16_t i=max(display.width(),display.height())/2; i>0; i-=5) {
    // The INVERSE color is used so triangles alternate white/black
    display.fillTriangle(
      display.width()/2  , display.height()/2-i,
      display.width()/2-i, display.height()/2+i,
      display.width()/2+i, display.height()/2+i, INVERSE);
    display.display();
    delay(1);
  }

  delay(2000);
}

void testdrawchar(void) {
  display.clearDisplay();

  display.setTextSize(1);      // Normal 1:1 pixel scale
  display.setTextColor(WHITE); // Draw white text
  display.setCursor(0, 0);     // Start at top-left corner
  display.cp437(true);         // Use full 256 char 'Code Page 437' font

  // Not all the characters will fit on the display. This is normal.
  // Library will draw what it can and the rest will be clipped.
  for(int16_t i=0; i<256; i++) {
    if(i == '\n') display.write(' ');
    else          display.write(i);
  }

  display.display();
  delay(2000);
}

void testdrawstyles(void) {
  display.clearDisplay();

  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(F("Hello, world!"));

  display.setTextColor(BLACK, WHITE); // Draw 'inverse' text
  display.println(3.141592);

  display.setTextSize(2);             // Draw 2X-scale text
  display.setTextColor(WHITE);
  display.print(F("0x")); display.println(0xDEADBEEF, HEX);

  display.display();
  delay(2000);
}

void testscrolltext(void) {
  display.clearDisplay();

  display.setTextSize(2); // Draw 2X-scale text
  display.setTextColor(WHITE);
  display.setCursor(10, 0);
  display.println(F("scroll"));
  display.display();      // Show initial text
  delay(100);

  // Scroll in various directions, pausing in-between:
  display.startscrollright(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrollleft(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrolldiagright(0x00, 0x07);
  delay(2000);
  display.startscrolldiagleft(0x00, 0x07);
  delay(2000);
  display.stopscroll();
  delay(1000);
}

void testdrawbitmap(void) {
  display.clearDisplay();

  display.drawBitmap(
    (display.width()  - LOGO_WIDTH ) / 2,
    (display.height() - LOGO_HEIGHT) / 2,
    logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
  display.display();
  delay(1000);
}

#define XPOS   0 // Indexes into the 'icons' array in function below
#define YPOS   1
#define DELTAY 2

void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  int8_t f, icons[NUMFLAKES][3];

  // Initialize 'snowflake' positions
  for(f=0; f< NUMFLAKES; f++) {
    icons[f][XPOS]   = random(1 - LOGO_WIDTH, display.width());
    icons[f][YPOS]   = -LOGO_HEIGHT;
    icons[f][DELTAY] = random(1, 6);
    Serial.print(F("x: "));
    Serial.print(icons[f][XPOS], DEC);
    Serial.print(F(" y: "));
    Serial.print(icons[f][YPOS], DEC);
    Serial.print(F(" dy: "));
    Serial.println(icons[f][DELTAY], DEC);
  }

  for(;;) { // Loop forever...
    display.clearDisplay(); // Clear the display buffer

    // Draw each snowflake:
    for(f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);
    }

    display.display(); // Show the display buffer on the screen
    delay(200);        // Pause for 1/10 second

    // Then update coordinates of each flake...
    for(f=0; f< NUMFLAKES; f++) {
      icons[f][YPOS] += icons[f][DELTAY];
      // If snowflake is off the bottom of the screen...
      if (icons[f][YPOS] >= display.height()) {
        // Reinitialize to a random position, just off the top
        icons[f][XPOS]   = random(1 - LOGO_WIDTH, display.width());
        icons[f][YPOS]   = -LOGO_HEIGHT;
        icons[f][DELTAY] = random(1, 6);
      }
    }
  }
}

Afficher le code brut

Si votre OLED n’a pas de broche RESET, vous devez définir la variable OLED_RESET sur -1 comme indiqué ci-dessous :

#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
définir la broche RESET arduino IDE OLED display

Téléchargez le code sur votre carte Arduino. N’oubliez pas de sélectionner la bonne carte et le bon port COM dans le menu Outils.

Vous devriez obtenir une série d’animations différentes dans l’OLED, comme indiqué dans la courte vidéo suivante.

Si votre écran OLED n’affiche rien :

  • Vérifiez que l’écran OLED est correctement câblé à l’Arduino
  • Revérifiez l’adresse I2C de l’écran OLED : avec l’OLED connecté à l’Arduino, télécharger ce code et vérifiez l’adresse I2C dans le moniteur série

Vous devez modifier l’adresse OLED dans la ligne suivante, si nécessaire. Dans notre cas, l’adresse est 0x3C.

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 

Écrire du texte – Écran OLED

La bibliothèque Adafruit pour l’écran OLED est livrée avec plusieurs fonctions pour écrire du texte. Dans cette section, vous apprendrez à écrire et à faire défiler du texte à l’aide des fonctions de la bibliothèque.

« Bonjour le monde! » Écran OLED

L’esquisse suivante affiche Hello, world ! message sur l’écran OLED.

/*********
  Rui Santos
  Complete project details at https://www.raspberryme.com  
*********/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(115200);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  // Display static text
  display.println("Hello, world!");
  display.display(); 
}

void loop() {
  
}

Afficher le code brut

Après avoir téléchargé le code, voici ce que vous obtiendrez dans votre OLED :

ESP32 ESP8266 Affichage Arduino OLED bonjour le monde

Voyons rapidement comment fonctionne le code.

Importation de bibliothèques

Tout d’abord, vous devez importer les bibliothèques nécessaires. La bibliothèque Wire pour utiliser I2C et les bibliothèques Adafruit pour écrire sur l’écran : Adafruit_GFX et Adafruit_SSD1306.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Initialiser l’affichage OLED

Ensuite, vous définissez la largeur et la hauteur de votre OLED. Dans cet exemple, nous utilisons un écran OLED 128×64. Si vous utilisez d’autres tailles, vous pouvez modifier cela dans les variables SCREEN_WIDTH et SCREEN_HEIGHT.

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Ensuite, initialisez un objet d’affichage avec la largeur et la hauteur définies précédemment avec le protocole de communication I2C (&Wire).

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

Le paramètre (-1) signifie que votre écran OLED n’a pas de broche RESET. Si votre écran OLED a une broche RESET, il doit être connecté à un GPIO. Dans ce cas, vous devez passer le numéro GPIO en paramètre.

Dans setup(), initialisez le moniteur série à une vitesse de transmission de 115 200 à des fins de débogage.

Serial.begin(115200);

Initialisez l’affichage OLED avec la méthode begin() comme suit :

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
  Serial.println("SSD1306 allocation failed");
  for(;;); // Don't proceed, loop forever
}

Cet extrait imprime également un message sur le moniteur série, au cas où nous ne pourrions pas nous connecter à l’écran.

Serial.println("SSD1306 allocation failed");

Si vous utilisez un écran OLED différent, vous devrez peut-être modifier l’adresse OLED. Dans notre cas, l’adresse est 0x3C.

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 

Si cette adresse ne fonctionne pas, vous pouvez exécuter une esquisse de scanner I2C pour trouver votre adresse OLED. Tu peux trouver le croquis du scanner I2C ici.

Après avoir initialisé l’affichage, ajoutez un délai de deux secondes, afin que l’OLED ait suffisamment de temps pour s’initialiser avant d’écrire du texte :

delay(2000);

Effacer l’affichage, définir la taille de la police, la couleur et écrire du texte

Après avoir initialisé l’affichage, effacez le tampon d’affichage avec la méthode clearDisplay() :

display.clearDisplay();

Avant d’écrire du texte, vous devez définir la taille du texte, la couleur et l’endroit où le texte sera affiché dans l’OLED.

Définissez la taille de la police à l’aide de la méthode setTextSize() :

display.setTextSize(1);             

Définissez la couleur de la police avec la méthode setTextColor() :

display.setTextColor(WHITE);        

BLANC définit une police blanche et un arrière-plan noir.

Définissez la position où le texte commence à l’aide de la méthode setCursor(x,y). Dans ce cas, nous définissons le texte pour qu’il commence aux coordonnées (0,10).

display.setCursor(0,10);             

Enfin, vous pouvez envoyer le texte à l’écran en utilisant la méthode println(), comme suit :

display.println("Hello, world!");

Ensuite, vous devez appeler la méthode display() pour afficher réellement le texte à l’écran.

display.display();

Texte défilant

La bibliothèque Adafruit OLED fournit des méthodes utiles pour faire défiler facilement du texte.

  • startscrollright(0x00, 0x0F) : fait défiler le texte de gauche à droite
  • startscrollleft(0x00, 0x0F) : fait défiler le texte de droite à gauche
  • startscrolldiagright(0x00, 0x07) : fait défiler le texte du coin inférieur gauche au coin supérieur droit
  • startscrolldiagleft(0x00, 0x07) : fait défiler le texte du coin inférieur droit au coin supérieur gauche

L’esquisse suivante implémente ces méthodes.

/*********
  Rui Santos
  Complete project details at https://www.raspberryme.com  
*********/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(115200);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  // Display static text
  display.println("Scrolling Hello");
  display.display(); 
  delay(100);
 
}

void loop() {
  // Scroll in various directions, pausing in-between:
  display.startscrollright(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrollleft(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrolldiagright(0x00, 0x07);
  delay(2000);
  display.startscrolldiagleft(0x00, 0x07);
  delay(2000);
  display.stopscroll();
  delay(1000);
}

Afficher le code brut

Le texte défile comme indiqué dans la courte vidéo suivante.

Utilisation d’autres polices – Écran OLED

La bibliothèque Adafruit GFX nous permet d’utiliser des polices alternatives en plus des polices intégrées. Il vous permet de choisir entre Serif, Sans et Mono. Chaque police est disponible en gras, en italique et en différentes tailles.

Les tailles sont définies par la police réelle. Ainsi, la méthode setTextSize() ne fonctionne pas avec ces polices. Les polices sont disponibles en tailles 9, 12, 18 et 24 points et contiennent également des caractères 7 bits (codes ASCII) (décrits comme 7b dans le nom de la police).

Vous pouvez choisir parmi la prochaine sélection de polices :

FreeMono12pt7b.h		FreeSansBoldOblique12pt7b.h
FreeMono18pt7b.h		FreeSansBoldOblique18pt7b.h
FreeMono24pt7b.h		FreeSansBoldOblique24pt7b.h
FreeMono9pt7b.h			FreeSansBoldOblique9pt7b.h
FreeMonoBold12pt7b.h		FreeSansOblique12pt7b.h
FreeMonoBold18pt7b.h		FreeSansOblique18pt7b.h
FreeMonoBold24pt7b.h		FreeSansOblique24pt7b.h
FreeMonoBold9pt7b.h		FreeSansOblique9pt7b.h
FreeMonoBoldOblique12pt7b.h	FreeSerif12pt7b.h
FreeMonoBoldOblique18pt7b.h	FreeSerif18pt7b.h
FreeMonoBoldOblique24pt7b.h	FreeSerif24pt7b.h
FreeMonoBoldOblique9pt7b.h	FreeSerif9pt7b.h
FreeMonoOblique12pt7b.h		FreeSerifBold12pt7b.h
FreeMonoOblique18pt7b.h		FreeSerifBold18pt7b.h
FreeMonoOblique24pt7b.h		FreeSerifBold24pt7b.h
FreeMonoOblique9pt7b.h		FreeSerifBold9pt7b.h
FreeSans12pt7b.h		FreeSerifBoldItalic12pt7b.h
FreeSans18pt7b.h		FreeSerifBoldItalic18pt7b.h
FreeSans24pt7b.h		FreeSerifBoldItalic24pt7b.h
FreeSans9pt7b.h			FreeSerifBoldItalic9pt7b.h
FreeSansBold12pt7b.h		FreeSerifItalic12pt7b.h
FreeSansBold18pt7b.h		FreeSerifItalic18pt7b.h
FreeSansBold24pt7b.h		FreeSerifItalic24pt7b.h
FreeSansBold9pt7b.h		FreeSerifItalic9pt7b.h

Les polices qui fonctionnent mieux avec l’écran OLED sont les tailles 9 et 12 points.

Pour utiliser l’une de ces polices, vous devez d’abord l’inclure dans votre croquis, par exemple :

#include <Fonts/FreeSerif12pt7b.h>

Ensuite, il vous suffit d’utiliser la méthode setFont() et de passer en argument, la police spécifiée :

display.setFont(&FreeSerif12pt7b);

Après avoir spécifié la police, toutes les méthodes d’écriture de texte utiliseront cette police. Pour revenir à la police d’origine, il vous suffit d’appeler la méthode setFont() sans arguments :

display.setFont();

Téléchargez le croquis suivant sur votre tableau :

/*********
  Rui Santos
  Complete project details at https://www.raspberryme.com  
*********/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSerif9pt7b.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(115200);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println("SSD1306 allocation failed");
    for(;;);
  }
  delay(2000);

  display.setFont(&FreeSerif9pt7b);
  display.clearDisplay();
  display.setTextSize(1);             
  display.setTextColor(WHITE);        
  display.setCursor(0,20);             
  display.println("Hello, world!");
  display.display();
  delay(2000); 
}
void loop() {
  
}

Afficher le code brut

Maintenant, votre écran imprime le « Hello, world! » message en police FreeSerif.

ESP32 ESP8266 Type de police d'affichage Arduino OLED

Dessiner des formes sur l’écran OLED

La bibliothèque Adafruit OLED fournit des méthodes utiles pour dessiner des pixels, des lignes et des formes. Jetons un coup d’œil à ces méthodes.

Dessiner un pixel

ESP32 ESP8266 Écran Arduino OLED Pixel Dot

Pour dessiner un pixel dans l’affichage OLED, vous pouvez utiliser la méthode drawPixel(x, y, color) qui accepte comme arguments les coordonnées x et y où le pixel apparaît, et la couleur. Par exemple:

display.drawPixel(64, 32, WHITE);

Tracer une ligne

ESP32 ESP8266 Ligne d'affichage Arduino OLED

Utilisez la méthode drawLine(x1, y1, x2, y2, color) pour créer une ligne. Les coordonnées (x1, y1) indiquent le début de la ligne et les coordonnées (x2, y2) indiquent où la ligne se termine. Par exemple:

display.drawLine(0, 0, 127, 20, WHITE);

Dessiner un rectangle

ESP32 ESP8266 Rectangle d'affichage Arduino OLED

Le drawRect(x, y, width, height, color) fournit un moyen facile de dessiner un rectangle. Les coordonnées (x, y) indiquent le coin supérieur gauche du rectangle. Ensuite, vous devez spécifier la largeur, la hauteur et la couleur :

display.drawRect(10, 10, 50, 30, WHITE);

Vous pouvez utiliser fillRect(x, y, width, height, color) pour dessiner un rectangle rempli. Cette méthode accepte les mêmes arguments que drawRect().

ESP32 ESP8266 Écran OLED Arduino Rempli

La bibliothèque fournit également des méthodes pour afficher des rectangles aux coins arrondis : drawRoundRect() et fillRoundRect(). Ces méthodes acceptent les mêmes arguments que les méthodes précédentes plus le rayon du coin. Par exemple:

display.drawRoundRect(10, 10, 30, 50, 2, WHITE);
ESP32 ESP8266 Écran OLED Arduino Rectangle Rond

Ou un rectangle rond rempli :

display.fillRoundRect(10, 10, 30, 50, 2, WHITE);
ESP32 ESP8266 Affichage Arduino OLED Rectangle Rond Rempli

Dessiner un cercle

Cercle d'affichage ESP32 ESP8266 Arduino OLED

Pour dessiner un cercle, utilisez la méthode drawCircle(x, y, radius, color). Les coordonnées (x,y) indiquent le centre du cercle. Vous devez également passer le rayon en argument. Par exemple:

display.drawCircle(64, 32, 10, WHITE);

De la même manière, pour construire un cercle plein, utilisez la méthode fillCircle() avec les mêmes arguments :

display.fillCircle(64, 32, 10, WHITE);
ESP32 ESP8266 Cercle d'affichage Arduino OLED rempli

Dessiner un triangle

Triangle d'affichage OLED ESP32 ESP8266 Arduino

Utilisez la méthode drawTriangle(x1, y1, x2, y2, x3, y3, color) pour construire un triangle. Cette méthode accepte comme arguments les coordonnées de chaque coin et la couleur.

display.drawTriangle(10, 10, 55, 20, 5, 40, WHITE);

Utilisez la méthode fillTriangle() pour dessiner un triangle rempli.

display.fillTriangle(10, 10, 55, 20, 5, 40, WHITE);
ESP32 ESP8266 Triangle d'affichage Arduino OLED rempli

Inverser

La bibliothèque fournit une méthode supplémentaire que vous pouvez utiliser avec des formes ou du texte : la méthode invertDisplay(). Passez true en argument pour inverser les couleurs de l’écran ou false pour revenir aux couleurs d’origine.

Si vous appelez la commande suivante après avoir défini le triangle :

display.invertDisplay(true);

Vous obtiendrez un triangle inversé comme suit :

ESP32 ESP8266 Arduino OLED Affichage Triangle Fond rempli

Code – Dessiner des formes

Téléchargez le croquis suivant qui implémente chaque extrait de code que nous avons couvert précédemment et passe en revue toutes les formes.

/*********
  Rui Santos
  Complete project details at https://www.raspberryme.com  
*********/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(115200);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  display.drawPixel(64, 32, WHITE);
  display.display();
  delay(3000);

  // Draw line
  display.clearDisplay();
  display.drawLine(0, 0, 127, 20, WHITE);
  display.display();
  delay(3000);
  
  // Draw rectangle
  display.clearDisplay();
  display.drawRect(30, 10, 50, 30, WHITE);
  display.display();
  delay(3000);
  // Fill rectangle
  display.fillRect(30, 10, 50, 30, WHITE);
  display.display();
  delay(3000);

  // Draw round rectangle
  display.clearDisplay();
  display.drawRoundRect(10, 10, 30, 50, 2, WHITE);
  display.display();
  delay(3000);
  // Fill round rectangle
  display.clearDisplay();
  display.fillRoundRect(10, 10, 30, 50, 2, WHITE);
  display.display();
  delay(3000);
  
  // Draw circle
  display.clearDisplay();
  display.drawCircle(64, 32, 10, WHITE);
  display.display();
  delay(3000);
  // Fill circle
  display.fillCircle(64, 32, 10, WHITE);
  display.display();
  delay(3000);
  
  // Draw triangle
  display.clearDisplay();
  display.drawTriangle(10, 10, 55, 20, 5, 40, WHITE);
  display.display();
  delay(3000);
  // Fill triangle
  display.fillTriangle(10, 10, 55, 20, 5, 40, WHITE);
  display.display();
  delay(3000);

  // Invert and restore display, pausing in-between
  display.invertDisplay(true);
  delay(3000);
  display.invertDisplay(false);
  delay(3000);
}

void loop() {
  
}

Afficher le code brut

Afficher des images bitmap dans l’OLED

Vous pouvez afficher des images monochromes 128×64 bitmap sur l’écran OLED.

Tout d’abord, utilisez un programme d’imagerie pour redimensionner une photo ou une image et l’enregistrer en tant que bitmap monochrome. Si vous êtes sur un PC Windows, vous pouvez utiliser Paint.

Afficher les images bitmap OLED convertir l'image

Ensuite, utilisez un convertisseur Image vers tableau C pour convertir l’image en tableau. j’ai utilisé Convertisseur d’images LCD.

Exécutez le programme et démarrez avec une nouvelle image. Accédez à Image > Importer et sélectionnez l’image bitmap que vous avez créée précédemment.

Afficher les images bitmap OLED

Allez dans Options > Conversion et dans l’onglet Préparer, sélectionnez les options suivantes :

  • Type : monochrome, tramage de seuil
  • Sens de numérisation principal : de haut en bas
  • Direction de balayage de ligne : vers l’avant
Afficher les images bitmap OLED convertir l'image

Accédez à l’onglet Image et sélectionnez les options suivantes :

  • Fractionner en lignes
  • Taille de bloc : 8 bits
  • Ordre des octets : Little-Endian
Afficher les images bitmap OLED convertir l'exportation d'image

Ensuite, cliquez sur OK. Enfin, dans le menu principal, allez dans Fichier > Convertir. Un nouveau fichier avec l’extension .c doit être enregistré. Ce fichier contient le tableau C pour l’image. Ouvrez ce fichier avec un éditeur de texte et copiez le tableau.

Dans notre cas, voici le tableau que nous obtenons :

static const uint8_t image_data_Saraarray[1024] = {
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x14, 0x9e, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x36, 0x3f, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x6d, 0xff, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0xfb, 0xff, 0x80, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x03, 0xd7, 0xff, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x07, 0xef, 0xff, 0x80, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x0f, 0xdf, 0xff, 0x90, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x0f, 0xbf, 0xff, 0xd0, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x1d, 0x7f, 0xff, 0xd0, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x01, 0x1b, 0xff, 0xff, 0xc0, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x02, 0xa7, 0xff, 0xff, 0xc0, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0xff, 0x80, 0x00, 0x0b, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x03, 0xff, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x07, 0xff, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0f, 0x07, 0xff, 0xf8, 0xf8, 0x03, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0e, 0x01, 0xff, 0xc0, 0x38, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x1c, 0x46, 0xff, 0xb1, 0x18, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0x97, 0xff, 0xc0, 0x7a, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfe, 0x01, 0x3f, 0xff, 0xff, 0xff, 0xfe, 0x01, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfe, 0x01, 0xbf, 0xff, 0xff, 0xff, 0xfe, 0x81, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xbf, 0xff, 0xff, 0xff, 0xfc, 0x81, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xff, 0xff, 0xfe, 0xff, 0xfd, 0x83, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0xbf, 0xff, 0xfe, 0xff, 0xfd, 0x01, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xfb, 0x03, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x3f, 0xff, 0xdc, 0xff, 0xfa, 0x03, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xd8, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xf8, 0x03, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xd0, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x90, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xf8, 0x02, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xb0, 0x00, 0x0f, 0xf5, 0xff, 0xd7, 0xf8, 0x01, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xb0, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x5f, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xa0, 0x00, 0x0f, 0xfb, 0xff, 0xff, 0xf0, 0x00, 0x3f, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x0f, 0xfd, 0xff, 0xdf, 0xf0, 0x00, 0x3f, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x07, 0xff, 0xff, 0xbf, 0xf0, 0x00, 0x0f, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x07, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x87, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x03, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x43, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x60, 0x00, 0x01, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x73, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xfe, 0xe0, 0x00, 0x00, 0xff, 0xff, 0xff, 0x80, 0x00, 0x7b, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xfd, 0xe0, 0x00, 0x00, 0x7f, 0xff, 0xfe, 0x00, 0x00, 0x33, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xfd, 0xe0, 0x00, 0x00, 0x3f, 0xff, 0xf8, 0x00, 0x00, 0x27, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x0f, 0xff, 0xf0, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x60, 0x00, 0x00, 0x67, 0xff, 0xe0, 0x00, 0x00, 0x1b, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xfd, 0x40, 0x00, 0x00, 0xf3, 0xff, 0xc4, 0x00, 0x00, 0x0b, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xfe, 0x80, 0x00, 0x00, 0xfc, 0xff, 0x8c, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x7f, 0x3c, 0x3c, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x7c, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xfc, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff
};

Afficher le code brut

Copiez votre tableau dans l’esquisse. Ensuite, pour afficher le tableau, utilisez la méthode drawBitmap() qui accepte les arguments suivants (x, y, tableau image, largeur image, hauteur image, rotation). Les coordonnées (x, y) définissent où l’image commence à s’afficher.

Copiez le code ci-dessous pour afficher votre image bitmap dans l’OLED.

/*********
  Rui Santos
  Complete project details at https://www.raspberryme.com  
*********/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

static const unsigned char PROGMEM image_data_Saraarray[] = {
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x14, 0x9e, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x36, 0x3f, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x6d, 0xff, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0xfb, 0xff, 0x80, 0x1f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x03, 0xd7, 0xff, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x07, 0xef, 0xff, 0x80, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x0f, 0xdf, 0xff, 0x90, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x0f, 0xbf, 0xff, 0xd0, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x1d, 0x7f, 0xff, 0xd0, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x01, 0x1b, 0xff, 0xff, 0xc0, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x02, 0xa7, 0xff, 0xff, 0xc0, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0xff, 0x80, 0x00, 0x0b, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x03, 0xff, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x07, 0xff, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0f, 0x07, 0xff, 0xf8, 0xf8, 0x03, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0e, 0x01, 0xff, 0xc0, 0x38, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x1c, 0x46, 0xff, 0xb1, 0x18, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0x97, 0xff, 0xc0, 0x7a, 0x07, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfe, 0x01, 0x3f, 0xff, 0xff, 0xff, 0xfe, 0x01, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfe, 0x01, 0xbf, 0xff, 0xff, 0xff, 0xfe, 0x81, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xbf, 0xff, 0xff, 0xff, 0xfc, 0x81, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xff, 0xff, 0xfe, 0xff, 0xfd, 0x83, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0xbf, 0xff, 0xfe, 0xff, 0xfd, 0x01, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xfb, 0x03, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x3f, 0xff, 0xdc, 0xff, 0xfa, 0x03, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xd8, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xf8, 0x03, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xd0, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x90, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xf8, 0x02, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xb0, 0x00, 0x0f, 0xf5, 0xff, 0xd7, 0xf8, 0x01, 0xff, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xb0, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x5f, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xa0, 0x00, 0x0f, 0xfb, 0xff, 0xff, 0xf0, 0x00, 0x3f, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x0f, 0xfd, 0xff, 0xdf, 0xf0, 0x00, 0x3f, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x07, 0xff, 0xff, 0xbf, 0xf0, 0x00, 0x0f, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x07, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x87, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x03, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x43, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x60, 0x00, 0x01, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x73, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xfe, 0xe0, 0x00, 0x00, 0xff, 0xff, 0xff, 0x80, 0x00, 0x7b, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xfd, 0xe0, 0x00, 0x00, 0x7f, 0xff, 0xfe, 0x00, 0x00, 0x33, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xfd, 0xe0, 0x00, 0x00, 0x3f, 0xff, 0xf8, 0x00, 0x00, 0x27, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x0f, 0xff, 0xf0, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x60, 0x00, 0x00, 0x67, 0xff, 0xe0, 0x00, 0x00, 0x1b, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xfd, 0x40, 0x00, 0x00, 0xf3, 0xff, 0xc4, 0x00, 0x00, 0x0b, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xfe, 0x80, 0x00, 0x00, 0xfc, 0xff, 0x8c, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x7f, 0x3c, 0x3c, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x7c, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 
    0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xfc, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff
};
 
void setup() {
  Serial.begin(115200);
 
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000); // Pause for 2 seconds
 
  // Clear the buffer.
  display.clearDisplay();
  
  // Draw bitmap on the screen
  display.drawBitmap(0, 0, image_data_Saraarray, 128, 64, 1);
  display.display();
}
 
void loop() {
  
}

Afficher le code brut

Après avoir téléchargé le code, voici ce que nous obtenons à l’écran.

Image d'affichage OLED ESP32 ESP8266 Arduino

Afficher la température et l’humidité sur l’écran OLED avec Arduino

Dans cette section, nous allons construire un projet qui affiche les relevés de température et d’humidité sur l’écran OLED. Nous obtiendrons la température et l’humidité en utilisant le Capteur de température et d’humidité DHT11. Si vous n’êtes pas familier avec le capteur DHT11, lisez l’article suivant :

Pièces nécessaires

Pour réaliser ce projet, vous avez besoin des composants suivants :

Vous pouvez utiliser les liens précédents ou accéder directement à MakerAdvisor.com/tools pour trouver toutes les pièces pour vos projets au meilleur prix !

1679890399 771 Guide pour laffichage I2C OLED avec Arduino

Schématique

Assemblez le circuit en suivant le schéma suivant.

Arduino avec schéma d'affichage DHT11 DHT22 et OLED

Remarque : si vous utilisez un module avec un capteur DHT, il est normalement livré avec seulement trois broches. Les broches doivent être étiquetées afin que vous sachiez comment les câbler. De plus, bon nombre de ces modules sont déjà équipés d’une résistance de rappel interne, vous n’avez donc pas besoin d’en ajouter une au circuit.

Installation des bibliothèques

Avant de continuer, assurez-vous d’avoir installé les bibliothèques « adafruit_GFX.h » et « adafruit_SSD1306.h » pour contrôler l’affichage OLED.

Pour ce projet, vous avez également besoin de deux bibliothèques supplémentaires pour lire à partir du capteur DHT : le DHT bibliothèque et la Adafruit_Sensor bibliothèque. Suivez les étapes suivantes pour installer ces bibliothèques

1. Ouvrez votre IDE Arduino et accédez à Sketch > Inclure la bibliothèque > Gérer les bibliothèques. Le gestionnaire de bibliothèque devrait s’ouvrir.

2. Recherchez « DHT » dans la zone de recherche et installez la bibliothèque DHT d’Adafruit.

Installation de la bibliothèque Adafruit DHT dans l'IDE Arduino

3. Après avoir installé la bibliothèque DHT d’Adafruit, saisissez « Adafruit Unified Sensor » dans la zone de recherche. Faites défiler vers le bas pour trouver la bibliothèque et installez-la.

Installation de la bibliothèque de pilotes Adafruit Unified Sensor dans Arduino IDE

4. Redémarrez votre IDE Arduino.

Code

Après avoir installé toutes les bibliothèques nécessaires, vous pouvez télécharger le code suivant.

/*********
  Rui Santos
  Complete project details at https://www.raspberryme.com  
*********/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define DHTPIN 2     // Digital pin connected to the DHT sensor

// Uncomment the type of sensor in use:
#define DHTTYPE    DHT11     // DHT 11
//#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);

  dht.begin();

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
}

void loop() {
  delay(5000);

  //read temperature and humidity
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  if (isnan(h) || isnan
    Serial.println("Failed to read from DHT sensor!");
  }

  //clear display
  display.clearDisplay();

  // display temperature
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0,10);
  display.print
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(2);
  display.print("C");
  
  // display humidity
  display.setTextSize(1);
  display.setCursor(0, 35);
  display.print("Humidity: ");
  display.setTextSize(2);
  display.setCursor(0, 45);
  display.print(h);
  display.print(" %"); 
  
  display.display(); 
}

Afficher le code brut

Comment fonctionne le code

Lisez cette section si vous voulez savoir comment fonctionne le code. Sinon, vous pouvez passer à la section « Démonstration ».

Importation de bibliothèques

Le code commence par inclure les bibliothèques nécessaires. Le Wire, Adafruit_GFX et Adafruit_SSD1306 sont utilisés pour s’interfacer avec l’écran OLED. Les bibliothèques Adafruit_Sensor et DHT sont utilisées pour s’interfacer avec les capteurs DHT22 ou DHT11.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

Créer un objet d’affichage

Ensuite, définissez les dimensions de votre écran OLED. Dans ce cas, nous utilisons un écran de 128 × 64 pixels.

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Ensuite, initialisez un objet d’affichage avec la largeur et la hauteur définies précédemment avec le protocole de communication I2C (&Wire).

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

Le paramètre (-1) signifie que votre écran OLED n’a pas de broche RESET. Si votre écran OLED a une broche RESET, il doit être connecté à un GPIO. Dans ce cas, vous devez passer le numéro GPIO en paramètre.

Créer un objet DHT

Ensuite, définissez le type de capteur DHT que vous utilisez. Si vous utilisez un DHT11, vous n’avez rien à changer dans le code. Si vous utilisez un autre capteur, décommentez simplement le capteur que vous utilisez et commentez les autres.

#define DHTTYPE    DHT11     // DHT 11
//#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)

Initialisez un objet capteur DHT avec la broche et le type définis précédemment.

DHT dht(DHTPIN, DHTTYPE);

installation()

Dans setup(), initialisez le moniteur série à des fins de débogage.

Serial.begin(115200);

Initialisez le capteur DHT :

dht.begin();

Ensuite, initialisez l’écran OLED.

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
  Serial.println(F("SSD1306 allocation failed"));
  for(;;);
}

Dans ce cas, l’adresse de l’écran OLED que nous utilisons est 0x3C. Si cette adresse ne fonctionne pas, vous pouvez exécuter une esquisse de scanner I2C pour trouver votre adresse OLED. Tu peux trouver le croquis du scanner I2C ici.

Ajoutez un délai pour donner le temps à l’affichage de s’initialiser, d’effacer l’affichage et de définir la couleur du texte sur blanc :

delay(2000);
display.clearDisplay();
display.setTextColor(WHITE)

Dans la boucle () est l’endroit où nous lisons le capteur et affichons la température et l’humidité sur l’écran.

Obtenez des lectures de température et d’humidité de DHT

La température et l’humidité sont enregistrées respectivement sur les variables t et h. Lire la température et l’humidité est aussi simple que d’utiliser les méthodes readTemperature() et readHumidity() sur l’objet dht.

float t = dht.readTemperature();
float h = dht.readHumidity();

Si nous ne sommes pas en mesure d’obtenir les lectures, affichez un message d’erreur :

if (isnan(h) || isnan
  Serial.println("Failed to read from DHT sensor!");
}

Si vous obtenez ce message d’erreur, lisez notre guide de dépannage : comment réparer « Échec de la lecture du capteur DHT ».

Afficher les lectures du capteur sur l’écran OLED

Les lignes suivantes affichent la température sur l’écran OLED.

  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0,10);
  display.print
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(2);
  display.print("C");

Nous utilisons la méthode setTextSize() pour définir la taille de la police, les ensembles setCursor() où le texte doit commencer à être affiché et la méthode print() est utilisée pour écrire quelque chose sur l’affichage.

Pour imprimer la température et l’humidité, il vous suffit de passer leurs variables à la méthode print() comme suit :

display.print



L'étiquette « Température » ​​est affichée en taille 1 et la lecture réelle est affichée en taille 2.

Pour afficher le symbole º, nous utilisons le Page de code 437 Police de caractère. Pour cela, vous devez définir le cp437 sur true comme suit :

display.cp437(true);

Ensuite, utilisez la méthode write() pour afficher le caractère que vous avez choisi. Le symbole º correspond au caractère 167.

display.write(167);

Une approche similaire est utilisée pour afficher l'humidité :

display.setTextSize(1);
display.setCursor(0, 35);
display.print("Humidity: ");
display.setTextSize(2);
display.setCursor(0, 45);
display.print(h);
display.print(" %"); 

N'oubliez pas que vous devez appeler display.display() à la fin, afin que vous puissiez réellement afficher quelque chose sur l'OLED.

display.display(); 

Manifestation

Après avoir câblé le circuit et téléchargé le code, l'écran OLED affiche les relevés de température et d'humidité. Les lectures du capteur sont mises à jour toutes les cinq secondes.

Affichage température et humidité DHT22 DHT11 sur écran OLED

Dépannage

Si votre capteur DHT ne parvient pas à obtenir les lectures ou si vous obtenez le message "Échec de la lecture du capteur DHT", lisez notre Guide de dépannage DHT pour vous aider à résoudre ce problème.

Si vous obtenez l'erreur « SSD1306 allocation failed » ou si l'OLED n'affiche rien à l'écran, il peut s'agir de l'un des problèmes suivants :

Mauvaise adresse I2C

L'adresse I2C de l'écran OLED que nous utilisons est 0x3C. Cependant, le vôtre peut être différent. Assurez-vous donc de vérifier votre adresse I2C d'affichage à l'aide d'un Croquis du scanner I2C.

SDA et SCL ne sont pas correctement connectés

Assurez-vous que les broches SDA et SCL de l'écran OLED sont correctement câblées.

Conclusion

L'écran OLED offre un moyen simple et peu coûteux d'afficher du texte ou des graphiques à l'aide d'un Arduino. Nous espérons que vous avez trouvé ce guide et l'exemple de projet utiles.

Si vous aimez Arduino, assurez-vous de consulter toutes nos ressources Arduino :

Ne manquez pas nos prochains tutoriels et projets ! Assurez-vous de vous abonner au blog RNT.

Merci d'avoir lu

Découvrez l'histoire de Raspberry Pi dans cette vidéo :

YouTube video

  • OLED I2C Display Module RUIZHI 3 pièces I2C Display 128 x 64 Pixel 0.96 Pouce,1306 Caractères de Couleur Blanche, Display Compatible avec Arduino
    【Haute résolution】 : la taille de l'écran OLED est de 0,96 pouce, le point de pixel est de 128*64, le type d'écran est OLED, présentant des images et des textes clairs et détaillés. 【Module d'affichage OLED】 : le module OLED est un écran monochrome blanc, utilisant la communication I2C, seuls 2 fils sont nécessaires pour contrôler l'OLED. 【Communication I2C 】: simplifie la connexion avec le contrôleur principal et réduit l'occupation des broches. Le pilote interne 1306 assure la compatibilité et la stabilité. 【4 broches】 : Le module OLED est composé de VCC, GND, SCL, SDA, où VCC et GND sont l'alimentation positive et négative respectivement, SCL et SDA sont les lignes d'horloge et de données pour la communication I2C. 【Note】 : Le module OLED ne nécessite pas de haute tension, directement connecté au 3.3V peut fonctionner, mais n'est pas compatible avec l'interface 5V.
  • ARCELI Module OLED 0,96 Pouce 12864 128x64 Bleu Blanc Pilote I2C Série Auto-Lumineux Panneau d'affichage pour Arduino Raspberry PI
    UCTRONICS 0,96 pouces Module OLED permettant d'afficher des informations graphiques et textuelles directement sur vos projets de micro-contrôleurs. Il prend en charge de nombreuses puces: Arduino UNO et Mega, Raspberry Pi, 51 MCU, STIM 32, etc. Résolution: 128 x 64, Angle de vue:> 160 °, Tension prise en charge: 3,3V-5V CC, Consommation électrique: 0,04W en fonctionnement normal, plein écran allumé 0,08W Communication: interface I2C / IIC, ne nécessite que deux ports d'E / S Pas besoin de rétro-éclairage, l'écran peut être auto-lumineux. Il présente un contraste très élevé, des points lumineux et nets, même des polices minuscules bien lisibles Aucune police incorporée dans le contrôleur OLED, l'utilisateur peut créer les polices via le logiciel de génération de polices.
  • OLED Display I2C Module RUIZHI 2 pièces I2C Display 128 x 64 Pixel 1.3 Pouce,1306 Caractères de Couleur Blanche, Display Compatible avec Arduino
    Haute résolution : la taille de l'écran OLED est de 1,3 pouce, le point de pixel est de 128*64, le type d'écran est OLED, présentant des images et des textes clairs et détaillés. Module d'affichage OLED : le module OLED est un écran blanc bicolore, utilisant la communication I2C, seuls 2 fils sont nécessaires pour contrôler l'OLED. Communication I2C : simplifie la connexion avec le contrôleur principal et réduit l'occupation des broches. Le pilote interne 1306 assure la compatibilité et la stabilité. 4 broches : Le module OLED est composé de VCC, GND, SCL, SDA, où VCC et GND sont l'alimentation positive et négative respectivement, SCL et SDA sont les lignes d'horloge et de données pour la communication I2C. Note : Le module OLED ne nécessite pas de haute tension, directement connecté au 3.3V peut fonctionner, mais n'est pas compatible avec l'interface 5V.