Project 7.03 Drawing Shapes with the OLED

We can do more than just text on the OLED. In this project, you'll learn how to draw shapes too!

We can do more than just text on the OLED. In this project you’ll learn how to draw shapes too!

Project Code:

				
					////////////////////////////////////////////////////
// 7.03 Drawing Shapes with the OLED

#include <Adafruit_SSD1306.h>

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

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

void loop() {
  display.clearDisplay();
  display.drawPixel(63, 32, WHITE);
  display.display();
  delay(1000);

  display.clearDisplay();
  display.drawLine(0, 0, 63, 63, WHITE);
  display.display();
  delay(1000);

  display.clearDisplay();
  display.drawCircle(63, 31, 12, WHITE);
  display.display();
  delay(1000);

  display.clearDisplay();
  display.fillCircle(63, 32, 12, WHITE);
  display.display();
  delay(1000);

  display.clearDisplay();
  display.drawTriangle(63, 0, 96, 63, 32, 63, WHITE);
  display.display();
  delay(1000);

  display.clearDisplay();
  display.fillTriangle(63, 0, 96, 63, 32, 63, WHITE);
  display.display();
  delay(1000);

  display.clearDisplay();
  display.drawRect(10, 10, 107, 43, WHITE);
  display.display();
  delay(1000);

  display.clearDisplay();
  display.fillRect(10, 10, 107, 43, WHITE);
  display.display();
  delay(1000);
}
////////////////////////////////////////////////////

				
			

*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 include the library needed for the OLED

				
					#include <Adafruit_SSD1306.h>
				
			

Then we make some standard variables for the OLED to define screen width, height, and the I2C address.

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

After that we use the constructor to make our display object.

				
					Adafruit_SSD1306 display(screenWidth, screenHeight, &Wire);
				
			

Now we can set the OLED up in the setup() function.

				
					void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, screenAddress);
  display.setTextColor(SSD1306_WHITE); 
}
				
			

Next, we can use specific functions to draw different shapes on the OLED! Don’t forget that (0,0) for the OLED is actually in the top left of the screen.

The first shape we will explore is actually just a point. We can draw points on the OLED with the function drawPixel. The first parameter is the X position, the second parameter is the Y position, and the last parameter is the color. All of these functions last parameter will be “WHITE” since this OLED can only display white.

				
					void loop() {
  display.clearDisplay();
  display.drawPixel(63, 32, WHITE);
  display.display();
  delay(1000);
				
			

The next shape is a line. We can use the drawLine function. These parameters are the starting x position, starting y position, ending x position, ending y position, and the color.

				
					display.clearDisplay();
display.drawLine(0, 0, 63, 63, WHITE);
display.display();
delay(1000);
				
			

The next shape is a circle. We can use the drawCircle function. These parameters are the x position, y position, circle radius, and color.

				
					display.clearDisplay();
display.drawCircle(63, 31, 12, WHITE);
display.display();
delay(1000);
				
			

The next shape is also a circle. We can use the fillCircle function to draw a circle that is filled in. These parameters are the x position, y position, circle radius, and color.

				
					display.clearDisplay();
display.fillCircle(63, 32, 12, WHITE);
display.display();
delay(1000);
				
			

The next shape is a triangle. We can use the drawTriangle function. This function takes a set of three points and draws lines connecting them. The parameters are x0, y0, x1, y1, x2, y2, and color.

				
					display.clearDisplay();
display.drawTriangle(63, 0, 96, 63, 32, 63, WHITE);
display.display();
delay(1000);
				
			

The next shape is also a triangle. We can use the fillTriangle function. This function takes a set of three points, draws lines connecting them and fills in that area. The parameters are x0, y0, x1, y1, x2, y2, and color.

				
					display.clearDisplay();
display.fillTriangle(63, 0, 96, 63, 32, 63, WHITE);
display.display();
delay(1000);
				
			

The next shape is a rectangle. We can use the drawRect function to draw a rectangle. This function takes the top left starting x position, top left starting y position, width of the rectangle, height of the rectangle and color.

				
					display.clearDisplay();
display.drawRect(10, 10, 107, 43, WHITE);
display.display();
delay(1000);
				
			

The next shape is also a rectangle. We can use the fillRect function to draw and fill in a rectangle. This function takes the top left starting x position, top left starting y position, width of the rectangle, height of the rectangle and color.

				
					display.clearDisplay();
display.drawCircle(63, 31, 12, WHITE);
display.display();
delay(1000);
				
			

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.