test code for oled 0.9 display using arduino nano
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
}
void loop() {
display.clearDisplay();
display.setCursor(0,0);
display.println("Hello World!");
display.display();
delay(1000);
}
setup()
function and sets some display settings such as text size and color. In the loop()
function, it clears the display, writes "Hello World!" to it, and displays it. It then waits for 1 second before repeating the process.#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define SCREEN_WIDTH 64
#define SCREEN_HEIGHT 48
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.println("Hello, world!");
display.display();
}
SCREEN_WIDTH
and SCREEN_HEIGHT
are defined as 64 and 48 respectively for a 0.9 inch OLED display. In the setup()
function, the display is initialized and the text color is set to white. In the loop()
function, the display is cleared and some text is printed. The display is then updated with display.display()
.