On this post I’ll explaining how to setup a little OLED screen 128X64 on Arduino Leonardo

Tools:

Over here you can see the connection diagram.

Steps:

  1. Install the Arduino IDE from Flathub repo.
  2. Now you have the flatpak Arduino app, below you will see older information related to a bug of the last Arduino Flatpak version, but now it fixed. Please continue with the 3th step.

2. Launch the Arduino IDE:

- As root (not recommended):

$ su

# flatpak run cc.arduino.arduinoide

- As not root:

$ flatpak run cc.arduino.arduinoide

NOTE: if the Arduino IDE reports that it can not read the mount folder at /dec/tty… please take a look on this workaround more detail from Flatpak official documentation.

Check out the permission on that folder created by Arduino IDE:

$ ls -l /dev/ttyACM0

crw-rw----. 1 root dialout 166, 0 Aug 24 20:54 ttyACM0

As you can see above the r/w permission are just allowed to owner and groups, so I granted the permission to “everyone”.

You must be aware about the security’s implication doing this:

# chmod 666 ttyACM1

$ ls -l ttyACM0 crw-rw-rw-. 1 root dialout 166, 0 Aug 24 20:54 ttyACM0

  1. Finally when you have opened the Arduino IDE, copy and paste the code below:

NOTE: you can download the missing library through the Arduino IDE.

#include <Adafruit_SH1106.h>
#include <Adafruit_Sensor.h>
#include "DHT.h"

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET -1
Adafruit_SH1106 display(OLED_RESET);

#define DHTPIN A0     //

// Uncomment the type of sensor in use:
#define DHTTYPE    DHT11     // DHT 11
//#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
}

void loop() {
  delay(5000); // 5 seg delay

  //read temperature and humidity
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
  }

  //clear display
  display.clearDisplay();

  // display temperature
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0,10);
  display.print(t);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(2);
  display.print("C");

  // display humidity
  display.setTextSize(1);
  display.setCursor(0, 35);
  display.print("Humidity: ");
  display.setTextSize(2);
  display.setCursor(0, 45);
  display.print(h);
  display.print(" %");

// print funtion, further detail into Adafruit library
  display.display();
}

Library

Good post (in Spanish): http://www.playbyte.es/electronica/arduino/display-oled-1-3-i2c/