HUM: OLED 128X64

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 an interesting module that finds its application in all the projects where it is necessary to show some data from the sensor. It is about an OLED 0.96“ display, whose size makes it easily applicable in every project because it does not take up too much space and it provides an excellent contrast due to the different work principle than that of the LCD display.
Module characteristics:
• Operating voltage: 3.3V or 5 V
• Consumption: 30 mA (depends on the number of turned on pixels)
• Communication: I2C or SPI
• Pixel color: White
• Display size: 0.96“
HOW DOES IT WORK?
The display consists of 128 * 64 individual pixels that are controlled by the SSD1306 controller that turns each pixel on or off, depending on how we set it. As we have the pixels that turn 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 you can clearly read even smaller characters from the display, despite the display’s small dimensions. The SSD1306 controller can receive data in 2 ways: via SPI or I2C communication, depending on how we choose. On the back of the display, it is indicated which pins we need to connect briefly in order to have SPI or I2C communication. For the controller’s power supply, we use a 3.3V voltage and that is why a voltage regulator is implemented on the board. The controller can also generate the voltage it uses for power supply (7-9V) to the voltage necessary for power supplying the display itself, i.e. pixels. Since our module is calibrated to a 5-3.3V power supply, we have to make the controller produce a higher voltage for the display to work, in the program.
HOW TO CONNECT IT?
The module is connected easily because it uses I2C or SPI communication. If we use I2C communication, to the A4 pin we connect the SDA (D1) module pin, and to the pin A5 on the Dasduino, we connect the SCL (D0) module pin. If we use SPI communication, we need 5 communication conductors, and below, it is written which Dasduino pin is connected to which pin on the display. As indicated in the characteristics, the module can operate at 5 V or 3.3 V, so it does not matter which voltage we deliver to it, because it has a built-in voltage converter.
SPI komunikacija
Display >>>>>>>>>>>> Dasduino
GND >>>>>>>>>>>> GND
VCC >>>>>>>>>>>> 5 V or 3.3 V
D0 >>>>>>>>>>>> D10
D1 >>>>>>>>>>>> D9
RES >>>>>>>>>>>> D13
DC >>>>>>>>>>>>D11
CS >>>>>>>>>>>> D12
ARDUINO CODE
To use the OLED display with Dasduino, you need to install the Adafruit_SSD1306 library for 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 use SPI communication and display the characters sent from the Serial Monitor on the display.
#include <SPI.h> //including the SPI library that allows SPI communication
#include <Adafruit_SSD1306.h> //including the library for work with an OLED display
#define SCREEN_WIDTH 128 //defining the display width, in pixels
#define SCREEN_HEIGHT 64 //defining the display height, in pixels
//declaring the pins to which we connect the oled display for SPI communication (standard Croduino pins for SPI communication)
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
//Our OLED display library constructor, in order to have access to the functions that control the display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
//the string to which we save data from the Serial monitor and then we display it
String
buf;
void
setup
() {
Serial.begin(9600);
//starting serial communication(9600 baud)
//Initialization of the OLED display library
// SSD1306_SWITCHCAPVCC = generates the display voltage(7-9 V) from 3.3V voltage that it has from the voltage regulator
if
(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
//0x3C address for the 128x64 display
//if the initialization was not successful, on the Serial monitoru we receive an error message
Serial.println(F(
"SSD1306 allocation failed"
));
for
(;;);
//infinite loop if the initialization was not successful, for the program to stop the execution
}
}
void
loop
() {
//checking if there is data on the Serial communication, and if there is, we proceed with the execution of the code
if
(Serial.available()){
buf=Serial.readString();
//the data we receive from the serial communication is saved to buf variable
//Displaying what we have received on the Serial monitor
Serial.println(
"This is received"
);
Serial.println(buf);
display.clearDisplay();
//clear the display
display.setTextSize(1);
//set the text size to 1
display.setTextColor(WHITE);
//set the text color
display.setCursor(0,0);
//set the cursor to the beginning of the first row
display.println(buf);
//send the received string to the display
display.display();
//on the display, we print everything we have sent
}
}
The second example uses I2C communication and in it, we will read the temperature from the DHT sensor and display it.