In the video below you will see three experiments done on the OLED, an explanation of which follows thereafter.
Â
First, let us look at progress bars. Copy the code below and upload it to your NodeMCU. Here a global progress variable is being used to track progress and draw the progress bar.
//Include the necessary libraries
#include <Wire.h>
#include <SH1106Wire.h>
Â
//Declare the display
//I2C address is 0x3c
SH1106Wire display(0x3c, D2, D1);
Â
//declare a progress variable
int progress = 0;
Â
void setup()
{
 // Initialize the display
 display.init();
Â
 //if you want to invert the display
 display.flipScreenVertically();
}
Â
void loop()
{
 //clear the display
 display.clear();
Â
 //set the font face and font size
 display.setFont(ArialMT_Plain_16);
Â
 //center align the text
 display.setTextAlignment(TEXT_ALIGN_CENTER);
Â
 //draw the text on the screen
 display.drawString(64, 28, "Progress");
Â
 display.drawProgressBar(17, 15, 95, 10, progress);
 progress = (progress > 100 ? 0 : progress + 1);
Â
 //write the buffer to the display
 display.display();
Â
 delay(10);
}
Â
Â
In the code above you see the method display.drawProgressBar() being used. It takes 5 arguments. The first two arguments are the x and y coordinates of the progress bar. The next two arguments are the width and height of the progress bar. And the final argument is the progress in percentage. It takes a number between 1 and 100 and draws the progress bar accordingly.
Â
Next, let us look at drawing geometric shapes on the OLED display.
Â
//Include the necessary libraries
#include <Wire.h>
#include <SH1106Wire.h>
Â
//Declare the display
//I2C address is 0x3c
SH1106Wire display(0x3c, D2, D1);
Â
void setup()
{
 // Initialize the display
 display.init();
Â
 //if you want to invert the display
 display.flipScreenVertically();
Â
 //clear the display
 display.clear();
Â
 //draw a line
 display.drawLine(0, 0, 60, 35);
Â
 //draw a rectangle both filled and hollow
 display.fillRect(20, 40, 25, 15);
 display.drawRect(0, 25, 15, 25);
Â
 //draw a circle both filled and hollow and quadrant
 display.fillCircle(60, 15, 10);
 display.drawCircle(90, 15, 15);
 display.drawCircleQuads(80, 35, 20, 0x8);
Â
 //write the buffer to the display
 display.display();
}
Â
void loop(){}
Â
Â
In the code above you can notice the various functions that are being used for the geometric shapes.
Â
display.drawLine(x1, y1, x2, y2) - the first two parameters are the x and y coordinates of the starting point of the line and next two parameters are the x and y coordinates of the terminating point of the line
Â
display.drawRect(x, y, w, h) - here the first two parameters are the x and y coordinates of the top left vertex of the rectangle. The next two parameters are the width and height of the rectangle in pixels. display fillRect() method also uses the same parameters list.
Â
display.drawCircle(x0, y0, r) - here the first two parameters are the x and y coordinates of the circle. The last parameter is the radius of the circle in pixels. The display.fillCircle() method also uses the same parameters list.
Â
display.drawCircleQuads(x0, y0, r, q) - here the first three parameters are the same as the drawCircle() method. The last parameter q is the quads bit mask which specifies the quadrants to be drawn.
Â
Let us look into displaying animations on the OLED display now. For this, you need an array of images which when played in succession will appear as a video or an animation. One sample animation is included in the header file âanim1.hâ. Download it from the link given below and place it in the same directory as your main file so that it can be included.
Â
Get the 'anim1.h' file here
Â
//Include the necessary libraries
#include <Wire.h>
#include <SH1106Wire.h>
#include <anim1.h>
Â
//Declare the display
//I2C address is 0x3c
SH1106Wire display(0x3c, D2, D1);
Â
void setup()
{
 // Initialize the display
 display.init();
Â
 //if you want to invert the display
 display.flipScreenVertically();
}
Â
void loop()
{
 for (int i = 0; i < num_frames; i++)
 {
  //clear the display
  display.clear();
Â
  //draw the image on the screen
  display.drawXbm(0, 0, width_video, height_video, anim1[i]);
Â
  //write the buffer to the display
  display.display();
Â
  //wait to maintain fps of video
  delay(30);
 }
}
Here the anim.h header file contains the anim1 variable which is an array of images which are displayed sequentially. The variable num_frames is also present in this header file and defines the number of frames present in the video, once the sequence is completed it starts from the beginning.Â