HUM: I2C OLED 128X32

You are a beginner with Dasduino. 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 soldered 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 Dasduino 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 Dasduino pins to be connected. To the A4 pin, we connect the SDA pin of the module, and to the A5 Dasduino pin, we connect the SCL pin of the module. We only need to connect the power supply of the Dasduino 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 Dasduino, 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 Dasduino code and we can draw our shape.