Introducing the 0.96 inch OLED display
The OLED display is the one in the following figure:
RELATED CONTENT: Like ESP8266? Check out Home Automation Using ESP8266
It is a very small display, the screen has 25mm x 14mm (0.98in x 0.55in). It is made of 128 by 64 individual OLED pixels and no backlight is required. That OLED display is monochrome (white color), but there are other models with several colors.
This display uses I2C communication. This means that it communicates with the Arduino using just 2 pins.
Pin wiring
Wiring the OLED display to your Arduino is pretty straightforward:
Pin | Wiring to Arduino Uno |
Vin | 5V |
GND | GND |
SCL | A5 |
SDA | A4 |
If you’re using other Arduino board rather than the uno, chek out what are their SCL and SDA pins.
- Nano: SDA (A4); SCL(A5);
- MEGA: SDA (20); SCL(21);
- Leonardo: SDA (20); SCL(21);
Libraries
To control the OLED display you’ll need the “adafruit_GFX.h” library and the “adafruit_SSD1306.h” library.
Installing the adafruit_GFX library
- Click here to download the adafruit GFX library. You should have a .zip folder in your Downloads folder
- Unzip the .zip folder and you should get Adafruit-GFX-Library-master folder
- Rename your folder from
Adafruit-GFX-Library-masterto Adafruit_GFX_Library(you really need ro replace those “-” by “_”) - Move the Adafruit_GFX_Library folder to your Arduino IDE installation libraries folder
- Finally, re-open your Arduino IDE
Installing the adafruit_SSD1306 library
- Click here to download the adafruit_SSD1306 library. You should have a .zip folder in your Downloads folder
- Unzip the .zip folder and you should get Adafruit-GFX-Library-master folder
- Rename your folder from
Adafruit_SSD1306-masterto Adafruit_SSD1306 - Move the Adafruit_SSD1306 folder to your Arduino IDE installation libraries folder
- Finally, re-open your Arduino IDE
Tips about writing text using these libraries
Here’s some functions that will help you handle the OLED display library to write text or draw simple graphics.
DOWNLOAD FREE PDF: Arduino eBook with 18+ Projects
- display.clearDisplay() – all pixels are off
- display.drawPixel(x,y, color) – plot a pixel in the x,y coordinates
- display.setTextSize(n) – set the font size, supports sizes from 1 to 8
- display.setCursor(x,y) – set the coordinates to start writing text
- display.print(“message”) – print the characters at location x,y
Example: display the temperature and humidity in the OLED display
In this example you will display the temperature and humidity in the OLED display. The aim of this project is to get familiar with the OLED display.
The temperature and humidity will be measured using the DHT11 temperature and humidity sensor. If you’re not familiar with the DHT11 sensor I recommend that you check out the following post:
- Complete Guide for DHT11/DHT22 Humidity and Temperature Sensor With Arduino
- ESP8266 DHT11/DHT22 Temperature and Humidity Web Server with Arduino IDE
Parts required
For this example you’ll need the following components:
- 1x 0.96 inch OLED display (view on eBay)
- 1x Arduino (view on eBay)
- 1x DHT11 temperature and humidity sensor (view on eBay)
- 1x Breadboard
- 1x 10k Ohm resistor
- Jumper wires
Schematics
Assemble all the parts as in the schematics below.
Code
Make sure you’ve installed the necessary libraries to control the OLED display. You also need to install the DHT library.
Installing the DHT sensor library
- Click here to download the DHT-sensor-library. You should have a .zip folder in your Downloads folder
- Unzip the .zip folder and you should get DHT-sensor-library-master folder
- Rename your folder from
DHT-sensor-library-master toDHT_sensor_library (you really need ro replace those “-” by “_”) - Move the DHT_sensor_library folder to your Arduino IDE installation libraries folder
- Finally, re-open your Arduino IDE
Then, you can upload the following code.
/* * Random Nerd Tutorials - Rui Santos * Complete Project Details http://randomnerdtutorials.com */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define DHTPIN 2 // what pin we're connected to #define DHTTYPE DHT11 // DHT 11 #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); // Initialize DHT sensor for normal 16mhz Arduino DHT dht(DHTPIN, DHTTYPE); void setup() { Wire.begin(); dht.begin(); // initialize dht display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// initialize with the I2C addr 0x3C (for the 128x32)(initializing the display) Serial.begin(9600); } void displayTempHumid(){ delay(2000); // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius float t = dht.readTemperature(); // Read temperature as Fahrenheit float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { display.clearDisplay(); // clearing the display display.setTextColor(WHITE); //setting the color display.setTextSize(1); //set the font size display.setCursor(5,0); //set the cursor coordinates display.print("Failed to read from DHT sensor!"); return; } display.clearDisplay(); display.setTextColor(WHITE); display.setTextSize(1); display.setCursor(0,0); display.print("Humidity: "); display.print(h); display.print(" %\t"); display.setCursor(0,10); display.print("Temperature: "); display.print(t); display.print(" C"); display.setCursor(0,20); display.print("Temperature: "); display.print(f); display.print(" F"); } void loop() { displayTempHumid(); display.display(); }
Demonstration
Here’s the OLED display in action after uploading the code.
Wrapping up
I hope you’ve found this guide useful. If you like this post probably you might like my next ones, so make sure you subscribe the RNT blog.
Are you planning to use the OLED display in your next projects? Let me know in the comments section below.
SIMPLE CODE:
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
// OLED display TWI address
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(-1);
// SUA LOI http://cyaninfinite.com/tutorials/interfacing-0-96-oled-display-with-arduino-uno/
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
void setup() {
// initialize and clear display
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.display();
// display a pixel in each corner of the screen
display.drawPixel(0, 0, WHITE);
display.drawPixel(127, 0, WHITE);
display.drawPixel(0, 63, WHITE);
display.drawPixel(127, 63, WHITE);
// display a line of text
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(27,30);
display.print("Hello, world!");
// update display with all of the above graphics
display.display();
}
void loop() {
// put your main code here, to run repeatedly:
}
No comments:
Post a Comment