Project 7.01 Writing Text to the Screen

In this code, you'll learn how to display text and numbers on the OLED 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();
}
////////////////////////////////////////////////////////////////////////
				
			

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Developmental Project 1.00 Vote

In this project, vote using the OLED and buttons.

Developmental Project 2.00 Mind the Dog

The program demonstrates how embedded systems can track time without stopping the program. Instead of using delay(), which pauses execution, it uses the millis() timestamp technique to calculate elapsed time. This allows multiple tasks—timers, display updates, and LED indicators—to run simultaneously.