Project 7.01 Writing Text to the Screen
In this project you’ll learn how to write text to the OLED, change its size, and about printing variables.
Project Code:
////////////////////////////////////////////////////////////////////////
// 7.01 - Writing Text to The Screen
#include <Adafruit_SSD1306.h>
#include <splash.h>
byte screenWidth = 128;
byte screenHeight = 64;
byte screenAddress = 0x3C;
byte numberToPrint = 123;
Adafruit_SSD1306 display(screenWidth, screenHeight, &Wire);
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, screenAddress);
display.setTextColor(SSD1306_WHITE);
}
void loop() {
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(3);
display.println("Hello!");
display.print(numberToPrint);
display.display();
}
////////////////////////////////////////////////////////////////////////
*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.
First, we have to include the correct libraries, declare our standard variables, and include the constructor.
#include <Adafruit_SSD1306.h> #include <splash.h> byte screenWidth = 128; byte screenHeight = 64; byte screenAddress = 0x3C; byte numberToPrint = 123; Adafruit_SSD1306 display(screenWidth, screenHeight, &Wire);
In the setup() function, we need to set the OLED up. The only two things that you absolutely need to do are use the begin and setTextColor functions.
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, screenAddress);
display.setTextColor(SSD1306_WHITE);
}
In the loop() function, the first thing we want to do is clear any text that is currently on the screen and reset the cursor to (0,0).
void loop() {
display.clearDisplay();
display.setCursor(0,0);
Next, we need to set the text size that we would like. Good options are 1-3, but you can go bigger.
display.setTextSize(3);
Then we can print some text! First, we print “Hello!” and then we print a variable. You can use the print and println functions to print all of the same things that you can print using the Serial functions.
display.println("Hello!");
display.print(numberToPrint);
display.display();
}
////////////////////////////////////////////////////////////////////////

