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

Project 1.00 Blink

In this project, you’ll learn how to blink an LED!

Project 1.01 Blink x2

In this project, you’ll learn how to blink more than one LED!

Bri Wagner

Education Solution Specialist

Bri Wagner brings a bachelor’s degree in Elementary Education and nine years of classroom teaching experience to her role at 1st Maker Space. A former Teacher of the Year Award recipient, Bri is passionate about creating engaging, hands-on learning experiences that inspire student success. She understands the impact that innovative instruction and meaningful classroom resources can have on student achievement and is dedicated to partnering with educators to bring high-quality STEM solutions into classrooms. Bri is committed to empowering teachers and helping students develop the creativity, confidence, and problem-solving skills they need to thrive.