0-GUIDE Arduino guide Project Tech

Guide for I2C OLED Display with Arduino

Guide for I2C OLED Display with Arduino

This text exhibits the best way to use the SSD1306 zero.96 inch I2C OLED show with the Arduino. We’ll present you some options of the OLED show, the way to wire it to the Arduino board, and learn how to write textual content to the show. Lastly, we’ll construct a venture instance that shows temperature and humidity readings.

Introducing the zero.96 inch OLED show

The natural light-emitting diode (OLED) show that we’ll use on this tutorial is the SSD1306 mannequin: a monocolor, zero.96-inch show with 128×64 pixels as proven within the following determine.

The OLED show doesn’t require backlight, which leads to a really good distinction in darkish environments. Moreover, its pixels eat power solely when they’re on, so the OLED show consumes much less energy compared with different shows.

The mannequin we’re utilizing right here has solely 4 pins and communicates with the Arduino utilizing I2C communication protocol. There are fashions that include an additional RESET pin. There are additionally different OLED shows that talk utilizing SPI communication.

Pin wiring

As a result of the OLED show makes use of I2C communication protocol, wiring could be very easy. You simply want to hook up with the Arduino Uno I2C pins as proven within the desk under.

Pin Wiring to Arduino Uno
Vin 5V
GND GND
SCL A5
SDA A4

In case you’re utilizing a special Arduino board, ensure you examine the right I2C pins:

  • Nano: SDA (A4); SCL(A5);
  • MEGA: SDA (20); SCL(21);
  • Leonardo: SDA (20); SCL(21);

Libraries

To regulate the OLED show you want the “adafruit_GFX.h” and the “adafruit_SSD1306.h” libraries. Comply with the subsequent directions to put in these libraries.

Putting in the adafruit_GFX library

  1. Click on right here to obtain the adafruit GFX library. You need to have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and it is best to get Adafruit-GFX-Library-master folder
  3. Rename your folder from Adafruit-GFX-Library-master to Adafruit_GFX_Library (you actually need ro exchange these “-” by “_”)
  4. Transfer the Adafruit_GFX_Library folder to your Arduino IDE set up libraries folder

Putting in the adafruit_SSD1306 library

  1. Click on right here to obtain the adafruit_SSD1306 library. You need to have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you must get Adafruit-GFX-Library-master folder
  3. Rename your folder from Adafruit_SSD1306-master to Adafruit_SSD1306
  4. Transfer the Adafruit_SSD1306 folder to your Arduino IDE set up libraries folder
  5. Lastly, re-open your Arduino IDE

Ideas for writing textual content utilizing these libraries

Right here’s some features that may show you how to deal with the OLED show library to write down textual content or draw easy graphics.

  • show.clearDisplay() – all pixels are off
  • show.drawPixel(x,y, shade) – plot a pixel within the x,y coordinates
  • show.setTextSize(n) – set the font measurement, helps sizes from 1 to eight
  • show.setCursor(x,y) – set the coordinates to start out writing textual content
  • show.print(“message”) – print the characters at location x,y

Show Temperature and Humidity within the OLED Show with Arduino

On this part we’ll construct a challenge that shows temperature and humidity readings on the OLED show. We’ll get temperature and humidity utilizing the DHT11 temperature and humidity sensor. If you’re not conversant in the DHT11 sensor, learn the next article:

Elements required

To finish this venture you want the next elements:

You should use the previous hyperlinks or go on to MakerAdvisor.com/instruments to seek out all of the elements in your tasks at the perfect worth!

Schematic

Assemble the circuit by following the subsequent schematic diagram.

display-dht11-oled_bb

Notice: for those who’re utilizing a module with a DHT sensor, it usually comes with solely three pins. The pins must be labeled in order that you understand how to wire them. Moreover, many of those modules already include an inner pull up resistor, so that you don’t want so as to add one to the circuit.

Code

Earlier than continuing, ensure you have put in the“adafruit_GFX.h” and the “adafruit_SSD1306.h” libraries to regulate the OLED show. For this undertaking you additionally want two libraries to learn from the DHT sensor: the DHT library and the Adafruit_Sensor library.

Putting in the DHT library

To learn from the DHT sensor, you need to have the DHT library put in. When you don’t have the DHT library put in, comply with the directions under:

  1. Click on right here to obtain the DHT-sensor-library. You must have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you must get DHT-sensor-library-master folder
  3. Rename your folder from DHT-sensor-library-master to DHT
  4. Transfer the DHT folder to your Arduino IDE set up libraries folder

Putting in the Adafruit_Sensor library

To make use of the DHT temperature and humidity sensor, you additionally want to put in the Adafruit_Sensor library. Comply with the subsequent steps to put in the library in your Arduino IDE:

  1. Click on right here to obtain the Adafruit_Sensor library. You must have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and it is best to get Adafruit_Sensor-master folder
  3. Rename your folder from Adafruit_Sensor-master to Adafruit_Sensor
  4. Transfer the Adafruit_Sensor folder to your Arduino IDE set up libraries folder
  5. Lastly, re-open your Arduino IDE

After putting in all the required libraries, you’ll be able to add the next code.

How the Code Works

Learn this part if you wish to find out how the code works. In any other case, you possibly can skip to the “Demonstration” part.

Embrace libraries

You’ll want to embrace the required libraries to put in writing to the OLED show: the Adafruit_GFX and the Adafruit_SSD1306. You additionally want to incorporate the Wire library for I2C communication.

#embrace
#embrace
#embrace

Embrace the DHT library to learn from the DHT11 sensor.

#embrace

Outline pins and objects

Outline the pin the DHT11 is related to (digital pin 2) and outline the DHT sensor sort (DHT11).

#outline DHTPIN 2 
#outline DHTTYPE DHT11 // DHT 11

Regardless that our OLED show doesn’t have a RESET pin, we have to initialize it to make the library work:

#outline OLED_RESET four
Adafruit_SSD1306 show(OLED_RESET);

Create a DHT object referred to as dht with the settings you’ve particular earlier (pin and sort).

DHT dht(DHTPIN, DHTTYPE);

setup()

Within the setup(), initialize I2C communication:

Wire.start();

Initialize the DHT sensor:

dht.start(); // initialize dht

And eventually, initialize the OLED show:

show.start(SSD1306_SWITCHCAPVCC, 0x3C);

displayTempHumid() perform

Then, we create a displayTempHumid() perform that reads temperature and humidity and shows the outcomes on the OLED show. This perform is then referred to as within the loop().

The features begins by studying humidity, temperature in Celsius and temperature in Fahrenheit. These readings are saved on the h, t, and f variables.

float h = dht.readHumidity();
// Learn temperature as Celsius
float t = dht.readTemperature();
// Learn temperature as Fahrenheit
float f = dht.readTemperature(true)

To study extra about find out how to use the DHT11/DHT22 temperature and humidity sensor with the Arduino learn our DHT11/DHT22 information.

There’s an if assertion that checks if the readings fail.

if (isnan(h) || isnan(t) || isnan(f))
show.clearDisplay(); // clearing the show
show.setTextColor(WHITE); //setting the colour
show.setTextSize(1); //set the font measurement
show.setCursor(5,zero); //set the cursor coordinates
show.print(“Did not learn from DHT sensor!”);
return;

If the sensor fails to ship readings, we show a message “Did not learn from DHT sensor” on the OLED show. For that, we have to do the next procedures:

Clear the show:

show.clearDisplay();

Set the textual content shade, measurement, and the coordinates to show the textual content:

show.setTextColor(WHITE); //setting the colour
show.setTextSize(1); //set the font measurement
show.setCursor(5,zero); //set the cursor coordinates

Show the message:

show.print(“Did not learn from DHT sensor!”);

If we’ve got legitimate sensor readings, show these readings on the OLED.

show.clearDisplay();
show.setTextColor(WHITE);
show.setTextSize(1);
show.setCursor(zero,zero);
show.print(“Humidity: “);
show.print(h);
show.print(” %t”);
show.setCursor(zero,10);
show.print(“Temperature: “);
show.print(t);
show.print(” C”);
show.setCursor(zero,20);
show.print(“Temperature: “);
show.print(f);
show.print(” F”);

Within the loop() we name the displayTempHumid() perform to get temperature and humidity readings on the OLED show.

displayTempHumid();

Lastly, we use the show() technique to replace the show.

show.show();

Demonstration

After wiring the circuit and importing the code, the OLED show exhibits humidity and temperature (each in Celsius and Fahrenheit) readings. The sensor readings are up to date on the show each two seconds.

demonstration

Wrapping Up

The OLED show offers a simple and cheap approach to show textual content or graphics utilizing an Arduino. We hope you’ve discovered this information and the challenge instance helpful.

For those who like Arduino, be sure to examine all our Arduino assets:

Don’t miss our subsequent tutorials and tasks! Ensure you subscribe the RNT weblog.

Thanks for studying

January 29, 2019


Really helpful Assets