Arduino UNO R4 - OLED 128x32

Deze tutorial laat u zien hoe u een Arduino UNO R4 gebruikt met een OLED 128x32 I2C display. U leert:

Arduino UNO R4 OLED I2C display

Over OLED Display

I2C OLED Display Pinout

  • GND pin: moet worden aangesloten op de grond van Arduino UNO R4
  • VCC pin: is de voedingsspanning voor het display die we aansluiten op de 5 volt pin van de Arduino UNO R4.
  • SCL pin: is een seriële klokpin voor I2C interface.
  • SDA pin: is een seriële datapin voor I2C interface.
OLED Pinout

※ Notiz:

De rangschikking van pinnen op een OLED module kan verschillen op basis van de fabrikant en het model van de module. Controleer altijd en volg de labels op de OLED module. Wees attent!

Deze handleiding is voor een OLED display dat de SSD1306 I2C driver gebruikt. We hebben het getest met een OLED display van DIYables. Het functioneert perfect zonder problemen.

Bedradingsschema

Arduino UNO R4 OLED 128x32 wiring diagram

This image is created using Fritzing. Click to enlarge image

Zie De beste manier om Arduino Uno R4 en andere componenten van stroom te voorzien.

Als u een ander type Arduino UNO R4 gebruikt, zal de pin-indeling niet hetzelfde zijn als de Uno. Bekijk de onderstaande tabel voor informatie over andere Arduino UNO R4 modellen.

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

Hoe OLED te gebruiken met Arduino UNO R4

SSD1306 OLED bibliotheek installeren

  • Ga naar het Libraries icoon aan de linkerkant van de Arduino IDE.
  • Typ "SSD1306" in het zoekvak en zoek naar de SSD1306 bibliotheek gemaakt door Adafruit.
  • Druk op de Install knop om de bibliotheek toe te voegen.
Arduino UNO R4 OLED library
  • U moet enkele aanvullende bibliotheken installeren.
  • Klik op de Install All knop om alle vereiste bibliotheken te installeren.
Arduino UNO R4 Adafruit GFX sensor library

Hoe te programmeren voor OLED

  • Voeg een bibliotheek toe.
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>
  • Stel de schermgrootte in op OLED 128x32.
#define SCREEN_WIDTH 128 // Defines the width of the OLED display in pixels #define SCREEN_HEIGHT 32 // Defines the height of the OLED display in pixels
  • Maak een SSD1306 OLED object.
// Initialize an Adafruit SSD1306 display object for I2C communication Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  • In de setup() functie, stel het OLED scherm in.
// Start OLED display with specific I2C address (0x3C) for 128x32 resolution if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); // Print error message if initialization fails while (true); // Enter an infinite loop to halt further execution }
  • Dan kunt u tekst, afbeeldingen weergeven en lijnen tekenen.

Arduino UNO R4 Code - Tekst weergeven op OLED

/* * Deze Arduino UNO R4 code is ontwikkeld door newbiely.nl * Deze Arduino UNO R4 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/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() { }

Hier zijn enkele functies die u kunt gebruiken om tekst op het OLED weer te geven:

  • oled.clearDisplay(): schakelt alle pixels uit.
  • oled.drawPixel(x, y, color): tekent een pixel op x, y coördinaten.
  • oled.setTextSize(n): wijzigt tekstgrootte, met keuzes van 1 tot 8.
  • oled.setCursor(x, y): stelt het startpunt voor tekst in.
  • oled.setTextColor(WHITE): maakt de tekstkleur wit.
  • oled.setTextColor(BLACK, WHITE): maakt de tekstkleur zwart en achtergrond wit.
  • oled.println("message"): toont tekst.
  • oled.println(number): toont een cijfer.
  • oled.println(number, HEX): toont een cijfer in hexadecimaal formaat.
  • oled.display(): werkt het display bij met wijzigingen.
  • oled.startscrollright(start, stop): beweegt tekst van links naar rechts.
  • oled.startscrollleft(start, stop): beweegt tekst van rechts naar links.
  • oled.startscrolldiagright(start, stop): beweegt tekst diagonaal van linksonder naar rechtsboven.
  • oled.startscrolldiagleft(start, stop): beweegt tekst diagonaal van rechtsonder naar linksboven.
  • oled.stopscroll(): stopt elke scrollende tekst.

Arduino UNO R4 Code - Tekenen op OLED

/* * Deze Arduino UNO R4 code is ontwikkeld door newbiely.nl * Deze Arduino UNO R4 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/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 – Afbeelding weergeven

Om een afbeelding op een OLED scherm weer te geven, zet eerst de afbeelding (elk formaat) om in een bitmap array. U kunt deze online tool gebruiken om het te converteren. Bekijk de onderstaande afbeelding om te zien hoe u een afbeelding omzet in een bitmap array. Ik heb het Arduino icoon omgezet in een bitmap array.

image to bitmap array

Kopieer de nieuwe array code en werk het bij in de Arduino icoon array in de onderstaande code.

De onderstaande video toont hoe u dit doet met OLED 128x64 display en Arduino Uno en Arduino icoon

We kunnen hetzelfde doen om het te laten werken met Arduino Uno R4 en OLED 128x32. De onderstaande code toont DIYables icoon op OLED 128x32

/* * Deze Arduino UNO R4 code is ontwikkeld door newbiely.nl * Deze Arduino UNO R4 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/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); }

※ Notiz:

  • De afbeeldingsgrootte moet zo klein als of kleiner zijn dan de schermgrootte.
  • Om de gegeven code te gebruiken voor een OLED 128x32, moet u de afbeelding verkleinen en de breedte en hoogte aanpassen in de oled.drawBitmap(); functie.

Hoe tekst/cijfers verticaal en horizontaal te centreren op OLED

/* * Deze Arduino UNO R4 code is ontwikkeld door newbiely.nl * Deze Arduino UNO R4 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/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(); }

OLED Probleemoplossing

Als het OLED scherm niets toont, volg dan deze stappen:

  • Zorg ervoor dat de bedrading correct is uitgevoerd.
  • Bevestig dat uw I2C OLED is uitgerust met een SSD1306 driver.
  • Verifieer het I2C adres van uw OLED met behulp van de volgende I2C Address Scanner code op 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 }

De uitvoer op de Serial Monitor:

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  

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!