Tutorial: How to use 0.96 OLED - a small and cute display

Specifications

Description: 0.96 inch monochrome OLED display
Resolution: 128×64 pixels
Dimension: WxHxT 27.3 mm x 27.8 mm x 4.3 mm
Protocol: I2C
Operating Voltage: 3V to 5V

Common variations of this OLED module:

  1. Different monochrome color: white, blue, yellow
  2. Different resolution: 128×64 and 128×32 pixels
  3. Different communication protocol: I2C and SPI
  4. Different driver IC: SSD1306 and SSH1106
  5. Some has additional reset pin.

Circuit Diagram

links of libraries: https://github.com/adafruit/Adafruit_SSD1306 https://github.com/adafruit/Adafruit-GFX-Librar

Video Demonstration

Call To Action

If you have any question regarding this tutorial, please do not hesitate to write it in the comment box below and I will be happy to answer it.

If you like this video, please kindly consider supporting me by Subscribing to TechToTinker Youtube channel. Click this to SUBSCRIBE.

Thank you and have a good day.

Source Code

  1// Load the libraries for the DHT sensor
  2#include "Wire.h"
  3#include "DHT.h"
  4#include "Adafruit_Sensor.h"
  5
  6// Load the libraries for the OLED display 
  7#include "Adafruit_GFX.h"
  8#include "Adafruit_SSD1306.h"
  9
 10// OLED display size in pixels
 11#define SCREEN_WIDTH 128 
 12#define SCREEN_HEIGHT 64 
 13
 14// DHT11 data pin assignment
 15#define DHTPIN A0
 16
 17// Define the type of sensor to be use
 18#define DHTTYPE    DHT11     // DHT 11
 19//#define DHTTYPE    DHT22     // DHT 22 (AM2302)
 20//#define DHTTYPE    DHT21     // DHT 21 (AM2301)
 21
 22// Create the "oled" object
 23#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
 24Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 25
 26// Create the "dht" object
 27DHT dht(DHTPIN, DHTTYPE);
 28
 29// Declare global variables here 
 30float temperature;
 31float humidity;
 32
 33void setup() {
 34  // Initialized the Serial
 35  Serial.begin(115200);
 36
 37  // Initialized the DHT11
 38  dht.begin();
 39
 40  // Initialized the OLED
 41  if(!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
 42    Serial.println(F("SSD1306 failed"));
 43    for(;;);
 44  }
 45
 46  oled.clearDisplay();
 47  oled.setTextColor(WHITE);
 48}
 49
 50void loop() {
 51  // Get the sensor readings
 52  getSensorValue();
 53
 54  // and display it to the oled
 55  updateDisplay();
 56
 57  // Delay for some time
 58  delay(3000);
 59}
 60
 61void getSensorValue() {
 62  // Get the temperature and humidity sensor readings
 63  temperature = dht.readTemperature();
 64  humidity = dht.readHumidity();
 65  if (isnan(humidity) || isnan(temperature)) {
 66    Serial.println("DHT sensor failed");
 67  }  
 68}
 69
 70void updateDisplay() {
 71  // Clear the display
 72  oled.clearDisplay();
 73
 74  // Display the current temperature
 75  oled.setTextSize(1);
 76  oled.setCursor(0,0);
 77  oled.print("Temperature: ");
 78  oled.setTextSize(2);
 79  oled.setCursor(0,10);
 80  oled.print(temperature);
 81  oled.print(" ");
 82  oled.setTextSize(1);
 83
 84  // Enable the Code Page 437
 85  // https://en.wikipedia.org/wiki/Code_page_437
 86  oled.cp437(true);
 87
 88  // Then print the chosen characters
 89  // which in this case is the degrees symbol
 90  // https://www.ascii-codes.com/
 91  oled.write(248);
 92  oled.setTextSize(2);
 93  oled.print("C");
 94  
 95  // Display the current humidity
 96  oled.setTextSize(1);
 97  oled.setCursor(0, 35);
 98  oled.print("Humidity: ");
 99  oled.setTextSize(2);
100  oled.setCursor(0, 45);
101  oled.print(humidity);
102  oled.print(" %"); 
103
104  // Update and show the display
105  oled.display();   
106}


Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.