• Worldwide Shipping

  • 24/7 Customer Support

  • FAQ
  • Worldwide Shipping

  • 24/7 Customer Support

  • FAQ

HUM: I2C OLED 128x32

You are a beginner with Croduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by e-radionica where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect the module with Croduino and the basic code. Everything else is left to your imagination.

INTRODUCTION

In this tutorial, we will get acquainted with a very small, but interesting module whose application is found in all projects where it is necessary to display some data from the sensor. It is a 0.91“ OLED display, which makes it easily applicable in all projects because it does not take up much space and gives excellent contrast due to a different work principle than that of LCD displays. Without further ado, let us briefly become acquainted with the way the display works and some basic functions for text printing, although the display can also display some simpler graphical forms.

Module characteristics:
• Voltage: 3.3V or 5 V
• Consumption: 20 mA (depending on the number of turned on pixels)
• Communication: I2C
• Pixel color: White
• Display size: 0.91“
• Module dimensions: 38 mm x 12 mm * 4 mm (without the connector for connecting)

HOW DOES IT WORK?

The display consists of 128*32 individual pixels that are controlled by the SSD1306 controller that lights or extinguishes each pixel depending on how we set it. As we have the pixels that are turned on or off, they provide lighting and the display does not require backlighting like the LCD display, so the consumption of the OLED display is much lower than that of the LCD. For the same reason, the contrast is much better and some much smaller characters can be clearly read. The SSD1306 controller can receive data in 3 ways depending on how we connect the pins that control the communication mode, so we can have 8-bit parallel communication, SPI or I2C communication, and in our case, the I2C communication is selected. For the controller's power supply, we use a 3.3 V voltage and that is the reason why the voltage regulator is installed on the board. The controller can also convert the voltage that it uses for power supply to the voltage (7-9 V) needed to power supply the display itself, i.e. the pixel. Since our module is designed to be powered by a 5-3.3V voltage, in the controller program we have to say that it is necessary to make a higher voltage for the operation of the display.

HOW TO CONNECT IT?

The module is easily connected because it uses I2C communication, which requires only 2 Croduino pins to be connected. To the A4 pin, we connect the SDA pin of the module, and to the A5 Croduino pin, we connect the SCL pin of the module. We only need to connect the power supply of the Croduino to the module, and we are ready to work. As we have indicated under the characteristics, the module can work at 5 V or 3.3 V, so it does not matter what voltage we bring to it because it has a built-in voltage converter. If it is not clear how to connect the device, below you can find the connection scheme in which the DHT11 temperature and humidity sensor is also connected, and from the sensor, we will print the values on the display.

ARDUINO CODE

To use the OLED display with Croduino, you need to install the Adafruit_SSD1306 library to work with an OLED display that contains all of the functions we use in the examples. If you do not know how to install the library, check the tutorial in which everything is explained in detail.
In the first example, we will read the data from the DHT11 sensor and print them on the display (you also need a library for the sensor).

#include <Wire.h> //including the wire library that enables I2C communication  
#include <Adafruit_SSD1306.h> //including the library necessary for work with OLED display
#include "DHT.h" //including the DHT temperature and humidity sensor library 

#define SCREEN_WIDTH 128 //defining the display width, in pixels 
#define SCREEN_HEIGHT 32 //defining the display height, in pixels 
#define DHTPIN 10 // defining the pin to wich the data pin of the DHT sensor is connected  
#define DHTTYPE DHT11 //defining the DHT sensor type (DHT11 or DHT22)

//Our OLED display library constructor, in order to have access to the functions that control the display 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
//DHT sensor constructor
DHT dht(DHTPIN, DHTTYPE);

void setup() {
 Serial.begin(9600);//Serial communication initialization ( speed 9600 baud)
 dht.begin();////DHT sensor library initialization
 //OLED display library initialization
 // SSD1306_SWITCHCAPVCC = generates the voltage for the display(7-9 V) from the 3.3V voltage that it has from the voltage regulator
 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C address for the 128x32 display
 //if the initialization is not successful on the Serial monitor we receive an error message 
 Serial.println(F("SSD1306 allocation failed"));
 for(;;); //an infinite loop, if the initialization is unsuccessful, for the program to stop performing
 }
}
void loop() {
 float temperatura=dht.readTemperature();//to the temperature variable, we save the temperature from the DHT11 sensor
 float vlaga=dht.readHumidity();//to the humidity variable we save the air humidity value from the DHT11 sensor
 display.clearDisplay();//function for deleting everything off the display
 display.setTextSize(1);//function for setting the text size 
 display.setTextColor(WHITE);//function for setting the color of the text (our display can only contain one color) 
 display.setCursor(0,0);//function for setting the cursor (0-128,0-32) on the display where we want to start printing the text 
 display.print("Temp: ");//function for displaying the text we deliver 
 display.print(temperature);//displays temperature 
 display.print(" *C");//displaying the space and *C, in order to know that the temperature is measured in degrees Celsius 
 display.setCursor(0,10);// postavljamo kursor u novi red i ispisujemo podatke za vlagu
 display.print("Humidity: ");
 display.print(humidity);
 display.println(" %");
 display.display();//displaying the full text that we have previously sent to the buffer of the display
 delay(1000);//1 second pause in order for the values not to change constantly 
 }

The second example shows us how to draw various images on the display, of course, if they are not too large and too complicated. To draw on the display, you need to download the Adafruit_GFX library that contains various drawing features (rectangles, circles, ellipses, lines) as well as the drawing function for bitmaps. In order to get a bitmap from the image, we need to save the picture in png and jpg format. Using the online converter, we get a bitmap that we copy into the Croduino code and we can draw our shape.

#include <Wire.h> //including the wire library that enables I2C communication 
#include <Adafruit_SSD1306.h> //including the library necessary for work with OLED display
#include <Adafruit_GFX.h> //including the Adafruit GFX library that contains all functions for drawing different shapes 

#define SCREEN_WIDTH 128 //defining the display width, in pixels 
#define SCREEN_HEIGHT 32 //defining the display height, in pixels 
//Our OLED display library constructor, in order to have access to the functions that control the display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);

#define LOGO_HEIGHT 32//height of the image we want to display (in pixels)
#define LOGO_WIDTH 32//width of the image we want to display (in pixels)
//bitmap of the image we display
static const uint8_t logo_bmp[] PROGMEM = {
 B00000000, B00000000, B00000000, B00000000, 
 B00000000, B00000001, B10000000, B00000000, 
 B00000000, B00001111, B11100000, B00000000, 
 B00000000, B00111111, B11111100, B00000000, 
 B00000000, B11111111, B11111110, B00000000, 
 B00000001, B11111111, B11111111, B10000000, 
 B00000111, B11111110, B01111111, B11100000, 
 B00011111, B11100000, B00000111, B11111000, 
 B01111111, B11001111, B11111001, B11111110, 
 B01111111, B00111111, B11111100, B11111110, 
 B01111110, B01111111, B11111111, B01111110, 
 B01111110, B11111111, B11111111, B10111110, 
 B01111100, B11110000, B00011111, B10011110, 
 B01111101, B11011101, B01000001, B11011110, 
 B01111101, B10111111, B01011111, B11011110, 
 B01111000, B10100011, B11011111, B11011110, 
 B01111111, B10111111, B11000011, B11011110, 
 B01111111, B10011101, B01011111, B11011110, 
 B01111111, B11001011, B01000001, B11011110, 
 B01111100, B11110000, B00011111, B10011110, 
 B01111110, B11111111, B11111111, B10111110, 
 B01111110, B01111111, B11111111, B01111110, 
 B01111111, B00111111, B11111110, B01111110, 
 B01111111, B11001111, B11111001, B11111110, 
 B00111111, B11100000, B00000111, B11111100, 
 B00001111, B11111110, B01111111, B11110000, 
 B00000011, B11111111, B11111111, B11000000, 
 B00000000, B11111111, B11111111, B00000000, 
 B00000000, B00111111, B11111100, B00000000, 
 B00000000, B00001111, B11110000, B00000000, 
 B00000000, B00000011, B11000000, B00000000, 
 B00000000, B00000000, B00000000, B00000000, 
};
void setup() {
 Serial.begin(9600);//Serial communication initialization (speed 9600 baud)
 //OLED display library initialization 
 // SSD1306_SWITCHCAPVCC = generates the voltage for the display(7-9 V) from the 3.3V voltage that it has from the voltage regulator
 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { //  0x3C address for the 128x32 display
 //if the initialization is not successful on the Serial monitor we receive an error message 
 Serial.println(F("SSD1306 allocation failed"));
 for(;;); //an infinite loop, if the initialization is unsuccessful, for the program to stop performing
 }
}

//Adafruit logo that the library forwards to the display is displayed, because we have not saved anything to the buffer  
 display.display();
 delay(2000); //2 seconds pause
 //function for deleting everything off the display
 display.clearDisplay();
 // Turn one pixel on (x,y, color)
 display.drawPixel(10, 20, WHITE);
 display.display();//function for drawing what is saved to the buffer
 delay(2000);//2 seconds pause
 
 dIscrtaj_sliku(); //Drawing our bitmap image by calling the function Iscrtaj_sliku(the image is shown for 1 second because there is a 1 second pause in the function)

 // Invert the image on the display, make a 1 s pause and return the image to its initial state, and then stop the program again for 1 second 
 display.invertDisplay(true);
 delay(1000);
 display.invertDisplay(false);
 delay(1000);
}
//the image will remain displayed because we have drawn it last, and we have not sent anything new for drawing, there is no code in the loop 
void loop() {
}

// function for drawing the bitmap image 
void Iscrtaj_sliku(void) {
 display.clearDisplay();// clear the display
 //calling the function for drawing, drawBitmap, using the following parameters (x coordinate for the beginning of drawing,
 // y coordinate for the beginning of drawing, a variable to which the bitmap of the image is saved, image width, image height)
 display.drawBitmap(
 (display.width() - LOGO_WIDTH ) / 2,
 (display.height() - LOGO_HEIGHT) / 2,
 logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
 display.display();//displaying the image 
 delay(1000);//1 second pause
}

 

Leave a Reply