Tutorial Arduino #30

Display OLED

Display OLED | Tutorial Arduino ITA #30
Tutorial Arduino #29: Tastierino alfanumerico
Tutorial Arduino #29
Tastierino alfanumerico
Tutorial Arduino #31: Display a matrice 8x8 con il LED driver MAX7219
Tutorial Arduino #31
Display a matrice 8x8 con il LED driver MAX7219

Descrizione

Scopri come far funzionare il tuo display OLED con Arduino.

In questo video tutorial imparerai a visualizzare dati, utilizzare diversi tipi di font, mostrare immagini e creare figure geometriche.

Scarica le risorse

Progetto: Voltmetro

Schema

Componenti

Codice

#include <Adafruit_SSD1306.h> #define OLED_I2C_ADDRESS 0x3C#define OLED_WIDTH 128#define OLED_HEIGHT 64#define INPUT_PIN A0 Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT); void setup() {  if (!oled.begin(SSD1306_SWITCHCAPVCC, OLED_I2C_ADDRESS)) {    while (true);  }  oled.clearDisplay();  showHeader();  oled.display();} void loop() {  float volt = analogRead(INPUT_PIN) * 5.0 / 1024;  showValue(String(volt) + "V");  oled.display();  delay(100);} void showHeader() {  oled.setTextSize(1);  oled.setTextColor(WHITE);  oled.setCursor(44);  oled.print("Voltmetro");  oled.setCursor(1124);  oled.print("A0");  oled.drawLine(01512815, WHITE);} void showValue(String value) {  oled.fillRect(01612848, BLACK);  oled.setTextSize(3);  oled.setCursor(OLED_WIDTH / 2, OLED_HEIGHT / 2 + 8);  printCenteredText(value);} void printCenteredText(String text) {  int16_t x = 0, y = 0;  uint16_t w = 0, h = 0;  int16_t cursorX = oled.getCursorX();  int16_t cursorY = oled.getCursorY();  oled.getTextBounds(text, 00&x, &y, &w, &h);  oled.setCursor(cursorX - x - w / 2, cursorY - y - h / 2);  oled.print(text);}
scarica il file

Progetto: Font

Schema

Componenti

Codice

#include <Adafruit_SSD1306.h>#include <Fonts/FreeMono18pt7b.h>#include <Fonts/FreeSansBold9pt7b.h>#include <Fonts/FreeSerifItalic12pt7b.h> #define OLED_I2C_ADDRESS 0x3C#define OLED_WIDTH 128#define OLED_HEIGHT 64 Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT); void setup() {  if (!oled.begin(SSD1306_SWITCHCAPVCC, OLED_I2C_ADDRESS)) {    while (true);  }  oled.clearDisplay();  showHeader();  oled.display();} void loop() {  showFontMono();  oled.display();  delay(1000);    showFontSans();  oled.display();  delay(1000);    showFontSerif();  oled.display();  delay(1000);} void showFontMono() {  oled.fillRect(01612848, BLACK);  oled.setFont(&FreeMono18pt7b);  oled.setCursor(OLED_WIDTH / 2, OLED_HEIGHT / 2 + 8);  printCenteredText("Mono");} void showFontSans() {  oled.fillRect(01612848, BLACK);  oled.setFont(&FreeSansBold9pt7b);  oled.setCursor(OLED_WIDTH / 2, OLED_HEIGHT / 2 + 8);  printCenteredText("Sans bold");} void showFontSerif() {  oled.fillRect(01612848, BLACK);  oled.setFont(&FreeSerifItalic12pt7b);  oled.setCursor(OLED_WIDTH / 2, OLED_HEIGHT / 2 + 8);  printCenteredText("Serif italic");} void showHeader() {  oled.setFont();  oled.setTextSize(1);  oled.setTextColor(WHITE);  oled.setCursor(OLED_WIDTH / 28);  printCenteredText("Font");} void printCenteredText(String text) {  int16_t x = 0, y = 0;  uint16_t w = 0, h = 0;  int16_t cursorX = oled.getCursorX();  int16_t cursorY = oled.getCursorY();  oled.getTextBounds(text, 00&x, &y, &w, &h);  oled.setCursor(cursorX - x - w / 2, cursorY - y - h / 2);  oled.print(text);}
scarica il file

Progetto: Immagini

Schema

Componenti

Codice

#include <Adafruit_SSD1306.h>#include "images.h" #define OLED_I2C_ADDRESS 0x3C#define OLED_WIDTH 128#define OLED_HEIGHT 64 Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT); bool inverted = false; void setup() {  if (!oled.begin(SSD1306_SWITCHCAPVCC, OLED_I2C_ADDRESS)) {    while (true);  }  oled.clearDisplay();  showHeader();  oled.drawBitmap(016, logo, 12848, WHITE);  oled.display();} void loop() {  oled.invertDisplay(inverted);     oled.display();   inverted = !inverted;  delay(1000);} void showHeader() {  oled.setTextSize(1);  oled.setTextColor(WHITE);  oled.setCursor(OLED_WIDTH / 28);  printCenteredText("Tutorial di Arduino");} void printCenteredText(String text) {  int16_t x = 0, y = 0;  uint16_t w = 0, h = 0;  int16_t cursorX = oled.getCursorX();  int16_t cursorY = oled.getCursorY();  oled.getTextBounds(text, 00&x, &y, &w, &h);  oled.setCursor(cursorX - x - w / 2, cursorY - y - h / 2);  oled.print(text);}
// 'logo', 128x48pxconst unsigned char logo [] PROGMEM = {  0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xfe0x030xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xf00x000xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xe00x000x3f0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xc00x000x1f0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0x800x000x0f0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0x000x000x0f0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xfe0x000x030x070xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xfe0x000x030x070xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xfc0x000xc60x030xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xfc0x000xcc0x030xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xfc0x010x980x030xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xfc0xff0x370x030xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xfd0x800x670x030xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0x010xcc0x030xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xfe0x7f0x980x030xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xfc0xc00x300x030xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xf90x800xe60x030xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xe30x3f0xce0x070xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xc60x600x180x070xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xcc0xc00x700x0f0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xf90x9f0xe00x1f0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xf30x300x000x3f0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xe40x600x000x7f0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0x8c0xf80x010xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0x9c0xff0xff0xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff  0xff0xe00x010xff0xff0xff0xff0xc70xf80xff0xff0xff0xff0xff0xff0xff  0xff0xe00x010xff0xff0xff0xff0xc30xf00xff0xff0xff0xff0xff0xff0xff  0xff0xff0x3f0x000x700x040xfe0xc30xe00xe00x390xf90x800x600x1f0xff  0xff0xff0x3f0x000x600x040xfc0x410xc40x800x190xf30x000x200x070xff  0xff0xff0x3f0x3f0xe70xfc0xfc0x480xcc0x8f0x890xe30x1f0xe70xe70xff  0xff0xff0x3f0x3f0xe70xfc0xfc0x4c0x0c0x9f0xc90xc70x1f0xe70xe70xff  0xff0xff0x3f0x010xe70xfc0x000x4e0x1c0x9f0xc80x0f0x000xe70xe70xff  0xff0xff0x3f0x000xe70xfc0x000x4f0x3c0x800x080x1f0x000xe00x070xff  0xff0xff0x3f0x3f0xe70xfc0xfc0x4f0xfc0x800x090x8f0x1f0xe00x0f0xff  0xff0xff0x3f0x3f0xe70xfc0xfc0x4f0xfc0x800x090xc70x1f0xe70x1f0xff  0xff0xff0x3f0x000x600x040xfc0x4f0xfc0x9f0xc90xe30x000x270x8f0xff  0xff0xff0x3f0x000x600x040xfc0x4f0xfc0x9f0xc90xf10x000x270xc70xff  0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff  0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff0xff};
scarica il file

Progetto: Figure geometriche

Schema

Componenti

Codice

#include <Adafruit_SSD1306.h> #define OLED_I2C_ADDRESS 0x3C#define OLED_WIDTH 128#define OLED_HEIGHT 64 Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT); void setup() {  if (!oled.begin(SSD1306_SWITCHCAPVCC, OLED_I2C_ADDRESS)) {    while (true);  }  oled.clearDisplay();  oled.display();  randomSeed(analogRead(2));} void loop() {  showRect();  showRoundRect();  showCircle();  showLine();  showTriangle();  showPixel();} void showHeader(String text, bool filled) {  oled.clearDisplay();  oled.setTextSize(1);  if (filled) {    oled.fillRect(0012816, WHITE);    oled.setTextColor(BLACK);      } else {    oled.drawLine(01612816, WHITE);    oled.setTextColor(WHITE);  }  oled.setCursor(OLED_WIDTH / 28);  printCenteredText(text);  oled.display();  delay(400);} void showRect() {  showHeader("Rettangoli"true);    oled.drawRect(31226030, WHITE);  oled.display();  delay(400);    oled.fillRect(38296030, WHITE);  oled.display();  delay(400);} void showRoundRect() {  showHeader("Bordi arrotondati"false);    for (int r = 0; r < 15; r++) {    oled.fillRect(01712848, BLACK);    oled.drawRoundRect(31226030, r, WHITE);    oled.display();  }    for (int r = 0; r < 15; r++) {    oled.fillRect(01712848, BLACK);    oled.drawRoundRect(3122603015, WHITE);    oled.fillRoundRect(38296030, r, WHITE);    oled.display();  }    delay(200);} void showCircle() {  showHeader("Cerchi"true);    oled.drawCircle(543915, WHITE);  oled.display();  delay(400);    oled.fillCircle(744515, WHITE);  oled.display();  delay(400);} void showLine() {  showHeader("Linee"false);    oled.drawLine(20303550, WHITE);  oled.display();  delay(250);   oled.drawLine(403710528, WHITE);  oled.display();  delay(250);   oled.drawLine(804511056, WHITE);  oled.display();  delay(250);} void showTriangle() {  showHeader("Triangoli"true);   oled.drawTriangle(255455208548, WHITE);  oled.display();  delay(400);    oled.fillTriangle(4558752410552, WHITE);  oled.display();  delay(400);} void showPixel() {  showHeader("Pixel"false);   for (int i = 0; i < 50; i++) {    oled.drawPixel(random(0128), random(2064), WHITE);    oled.display();  }    delay(200);} void printCenteredText(String text) {  int16_t x = 0, y = 0;  uint16_t w = 0, h = 0;  int16_t cursorX = oled.getCursorX();  int16_t cursorY = oled.getCursorY();  oled.getTextBounds(text, 00&x, &y, &w, &h);  oled.setCursor(cursorX - x - w / 2, cursorY - y - h / 2);  oled.print(text);}
scarica il file

Arduino Software Hero

Vuoi imparare a programmare Arduino, ma non sai da dove cominciare?

Iscriviti al corso online

Libri consigliati

In qualità di Affiliato Amazon riceviamo un guadagno dagli acquisti idonei


Tutorial consigliati

E-commerce di elettronica

Sul nostro negozio online trovi tutti i componenti usati nei tutorial.
Inoltre hai un'ampia scelta di prodotti tra:

  • Arduino
  • Raspberry Pi
  • Sensori
  • Robotica
  • Componenti elettronici
  • Accessori

Compra online, ricevi comodamente a casa

Visita il negozio