Project 7.00 Using the OLED

The OLED is like a mini-TV screen. In fact, many TVs now-a-days are OLEDs. In this project you’ll learn how to setup and use the OLED properly.

Project Code:

//////////////////////////////////////////////////////////////
// 7.00 - Using The OLED

#include <Adafruit_SSD1306.h>
#include <splash.h>

byte screenWidth = 128; 
byte screenHeight = 64;  
byte screenAddress = 0x3C;  

Adafruit_SSD1306 display(screenWidth, screenHeight, &Wire);
void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, screenAddress);
  display.clearDisplay();  
  display.display(); 

  display.setCursor(0, 0);
  display.setTextSize(1);  
  display.setTextColor(SSD1306_WHITE);
  display.println("Hello World!");
  display.display();  
}

void loop() {
}
//////////////////////////////////////////////////////////////

*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.

Before we do anything, we need to install the OLED Library. You can do this by going to Tools -> Manage Libraries, and type in “Adafruit_SSD1306”. It looks like this:

Click install. If it asks you to install dependencies go ahead and do so.

For reference, you can include it into a project by going to Sketch -> Include Library and click “Adafruit_SSD1306”. This does not need to be done in this sketch as it is already included.

The library reference where you can find all of the functions avaliable can be found here:

https://adafruit.github.io/Adafruit_SSD1306/html/class_adafruit___s_s_d1306.html

The first thing that we need to do is to define the length and width of our OLED in pixels.

byte screenWidth = 128; 
byte screenHeight = 64;  

Next, we have to define the I2C address of our OLED. I2C is a communication protocol that uses unique device addresses to communicate data. You do not need to understand anything about I2C other than the address for the OLED is 0x3C.

byte screenAddress = 0x3C;  

What does 0x3C mean? Well, that number starts with “0x” because it is in hexadecimal. Hexadecimal is a base 16 numbering system that uses letters to represent numbers 10 – 15. These are letters A – F. Our day-to-day numbers are base 10 and are referred to as decimal numbers. 0x3C is 60 in decimal. You could write the address as 60 but I2C addresses are generally done in hex.

byte screenAddress = 0x3C;   

Next, is the constructor for the class. The constructor contains a name, the screen’s width, the screen’s height, and a reference to the Wire object. That last one is a bit complicated, so we won’t worry about it. Just know that you need the “&Wire” as the last parameter. To break that down into a table:

Parameters Description
display This is the name of the class we are creating. This is just like the “pixels” in the Neopixel projects.
screenWidth This is the screen’s width in pixels (128)
screenHeight This is the screen’s height in pixels (64)
&Wire This is a reference to the Wire class. Don’t worry about understanding this right now.
Adafruit_SSD1306 display(screenWidth, screenHeight, &Wire);

A few things need to be done in the setup() function. First, we have to initialize the OLED with the begin function. The “SSD1306_SWITCHCAPVCC” that is passed is just telling the OLED to create its own IO voltage. It’s basically creating a higher voltage than the MC Trainer can supply to support itself. We also have to pass the I2C address to it.

display.begin(SSD1306_SWITCHCAPVCC, screenAddress);

Next, the screen is cleared with the clearDisplay function.

display.clearDisplay();  

Once loaded with data, the OLED needs to be told to display it with the display function. This works exactly the same as the show function with the Neopixels. In this case we’ve loaded the clear screen data.

display.display(); 

Now we set the cursor to coordinates (0,0). This (0,0) is different from a normal graph. The (0,0) for the OLED starts in the top left corner.

display.setCursor(0, 0);

Next, we will set the size of the text to be displayed.

display.setTextSize(1);

Lastly for setup we have to tell the OLED what color the text is with the setTextColor function. This OLED can only display white.

display.setTextColor(SSD1306_WHITE);

We are now ready to print some text! This works exactly the same as the Serial functions.

display.println("Hello World!");

Once the data is loaded, we have to display it with the display function.

display.display();  

There is nothing in the loop() function in this project.

void loop() {
}
//////////////////////////////////////////////////////////////
Previous
Previous

Project 7.01 Writing Text to the Screen

Next
Next

Project 6.01 Changing Color with the Potentiometer