Indice
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.
#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(4, 4);
oled.print("Voltmetro");
oled.setCursor(112, 4);
oled.print("A0");
oled.drawLine(0, 15, 128, 15, WHITE);
}
void showValue(String value) {
oled.fillRect(0, 16, 128, 48, 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, 0, 0, &x, &y, &w, &h);
oled.setCursor(cursorX - x - w / 2, cursorY - y - h / 2);
oled.print(text);
}
#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(0, 16, 128, 48, BLACK);
oled.setFont(&FreeMono18pt7b);
oled.setCursor(OLED_WIDTH / 2, OLED_HEIGHT / 2 + 8);
printCenteredText("Mono");
}
void showFontSans() {
oled.fillRect(0, 16, 128, 48, BLACK);
oled.setFont(&FreeSansBold9pt7b);
oled.setCursor(OLED_WIDTH / 2, OLED_HEIGHT / 2 + 8);
printCenteredText("Sans bold");
}
void showFontSerif() {
oled.fillRect(0, 16, 128, 48, 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 / 2, 8);
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, 0, 0, &x, &y, &w, &h);
oled.setCursor(cursorX - x - w / 2, cursorY - y - h / 2);
oled.print(text);
}
#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(0, 16, logo, 128, 48, 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 / 2, 8);
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, 0, 0, &x, &y, &w, &h);
oled.setCursor(cursorX - x - w / 2, cursorY - y - h / 2);
oled.print(text);
}
// 'logo', 128x48px
const unsigned char logo [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x03, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x03, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xc6, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xcc, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x01, 0x98, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xff, 0x37, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0x80, 0x67, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xcc, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0x98, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xc0, 0x30, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf9, 0x80, 0xe6, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0x3f, 0xce, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc6, 0x60, 0x18, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xc0, 0x70, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf9, 0x9f, 0xe0, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0x30, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe4, 0x60, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8c, 0xf8, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9c, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xe0, 0x01, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xe0, 0x01, 0xff, 0xff, 0xff, 0xff, 0xc3, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0x3f, 0x00, 0x70, 0x04, 0xfe, 0xc3, 0xe0, 0xe0, 0x39, 0xf9, 0x80, 0x60, 0x1f, 0xff,
0xff, 0xff, 0x3f, 0x00, 0x60, 0x04, 0xfc, 0x41, 0xc4, 0x80, 0x19, 0xf3, 0x00, 0x20, 0x07, 0xff,
0xff, 0xff, 0x3f, 0x3f, 0xe7, 0xfc, 0xfc, 0x48, 0xcc, 0x8f, 0x89, 0xe3, 0x1f, 0xe7, 0xe7, 0xff,
0xff, 0xff, 0x3f, 0x3f, 0xe7, 0xfc, 0xfc, 0x4c, 0x0c, 0x9f, 0xc9, 0xc7, 0x1f, 0xe7, 0xe7, 0xff,
0xff, 0xff, 0x3f, 0x01, 0xe7, 0xfc, 0x00, 0x4e, 0x1c, 0x9f, 0xc8, 0x0f, 0x00, 0xe7, 0xe7, 0xff,
0xff, 0xff, 0x3f, 0x00, 0xe7, 0xfc, 0x00, 0x4f, 0x3c, 0x80, 0x08, 0x1f, 0x00, 0xe0, 0x07, 0xff,
0xff, 0xff, 0x3f, 0x3f, 0xe7, 0xfc, 0xfc, 0x4f, 0xfc, 0x80, 0x09, 0x8f, 0x1f, 0xe0, 0x0f, 0xff,
0xff, 0xff, 0x3f, 0x3f, 0xe7, 0xfc, 0xfc, 0x4f, 0xfc, 0x80, 0x09, 0xc7, 0x1f, 0xe7, 0x1f, 0xff,
0xff, 0xff, 0x3f, 0x00, 0x60, 0x04, 0xfc, 0x4f, 0xfc, 0x9f, 0xc9, 0xe3, 0x00, 0x27, 0x8f, 0xff,
0xff, 0xff, 0x3f, 0x00, 0x60, 0x04, 0xfc, 0x4f, 0xfc, 0x9f, 0xc9, 0xf1, 0x00, 0x27, 0xc7, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
#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(0, 0, 128, 16, WHITE);
oled.setTextColor(BLACK);
} else {
oled.drawLine(0, 16, 128, 16, WHITE);
oled.setTextColor(WHITE);
}
oled.setCursor(OLED_WIDTH / 2, 8);
printCenteredText(text);
oled.display();
delay(400);
}
void showRect() {
showHeader("Rettangoli", true);
oled.drawRect(31, 22, 60, 30, WHITE);
oled.display();
delay(400);
oled.fillRect(38, 29, 60, 30, WHITE);
oled.display();
delay(400);
}
void showRoundRect() {
showHeader("Bordi arrotondati", false);
for (int r = 0; r < 15; r++) {
oled.fillRect(0, 17, 128, 48, BLACK);
oled.drawRoundRect(31, 22, 60, 30, r, WHITE);
oled.display();
}
for (int r = 0; r < 15; r++) {
oled.fillRect(0, 17, 128, 48, BLACK);
oled.drawRoundRect(31, 22, 60, 30, 15, WHITE);
oled.fillRoundRect(38, 29, 60, 30, r, WHITE);
oled.display();
}
delay(200);
}
void showCircle() {
showHeader("Cerchi", true);
oled.drawCircle(54, 39, 15, WHITE);
oled.display();
delay(400);
oled.fillCircle(74, 45, 15, WHITE);
oled.display();
delay(400);
}
void showLine() {
showHeader("Linee", false);
oled.drawLine(20, 30, 35, 50, WHITE);
oled.display();
delay(250);
oled.drawLine(40, 37, 105, 28, WHITE);
oled.display();
delay(250);
oled.drawLine(80, 45, 110, 56, WHITE);
oled.display();
delay(250);
}
void showTriangle() {
showHeader("Triangoli", true);
oled.drawTriangle(25, 54, 55, 20, 85, 48, WHITE);
oled.display();
delay(400);
oled.fillTriangle(45, 58, 75, 24, 105, 52, WHITE);
oled.display();
delay(400);
}
void showPixel() {
showHeader("Pixel", false);
for (int i = 0; i < 50; i++) {
oled.drawPixel(random(0, 128), random(20, 64), 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, 0, 0, &x, &y, &w, &h);
oled.setCursor(cursorX - x - w / 2, cursorY - y - h / 2);
oled.print(text);
}