Arduino - DHT11 - OLED
Neste tutorial, vamos aprender como ler a temperatura e a umidade do módulo DHT11 e exibi-las em um OLED.
Hardware Necessário
Or you can buy the following kits:
| 1 | × | DIYables STEM V3 Starter Kit (Arduino included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Divulgação: Alguns dos links desta seção são links de afiliado da Amazon. Podemos receber uma comissão pelas compras feitas por meio desses links, sem custo adicional para você. Agradecemos o seu apoio.
Buy Note: If you want a bigger OLED display, use the 2.42 inch OLED Display 128x64 .
Sobre o display OLED e o Sensor de Temperatura e Umidade DHT11
Se você ainda não conhece o display OLED e o sensor de temperatura e umidade DHT11 (pinagem, como funciona, como programar ...), aprenda sobre eles nos seguintes tutoriais:
- Tutorial Arduino - OLED
- Tutorial Arduino - DHT11
Diagrama de Ligação

This image is created using Fritzing. Click to enlarge image
Código Arduino - Sensor DHT11 - OLED
/*
* Este código de Arduino foi desenvolvido por pt.newbiely.com
* Este código de Arduino é disponibilizado ao público sem nenhuma restrição.
* Para tutoriais completos e diagramas de ligação, visite:
* https://pt.newbiely.com/tutorials/arduino/arduino-dht11-oled
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define DHT11_PIN 2 // pin connected to DHT11 sensor
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
DHT dht11(DHT11_PIN, DHT11);
String temperature;
String humidity;
void setup() {
Serial.begin(9600);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true)
;
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(3); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display
dht11.begin(); // initialize DHT11 the temperature and humidity sensor
temperature.reserve(10); // to avoid fragmenting memory when using String
humidity.reserve(10); // to avoid fragmenting memory when using String
}
void loop() {
float humi = dht11.readHumidity(); // read humidity
float tempC = dht11.readTemperature(); // read temperature
// check if any reads failed
if (isnan(humi) || isnan(tempC)) {
temperature = "Failed";
humidity = "Failed";
} else {
temperature = String(tempC, 1); // one decimal places
temperature += char(247); // degree character
temperature += "C";
humidity = String(humi, 1); // one decimal places
humidity += "%";
}
Serial.print(tempC); // print to Serial Monitor
Serial.print("°C | " ); // print to Serial Monitor
Serial.print(humi); // print to Serial Monitor
Serial.println("%"); // print to Serial Monitor
oledDisplayCenter(temperature, humidity); // display temperature and humidity on OLED
}
void oledDisplayCenter(String temperature, String humidity) {
int16_t x1;
int16_t y1;
uint16_t width_T;
uint16_t height_T;
uint16_t width_H;
uint16_t height_H;
oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_T, &height_T);
oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_H, &height_H);
// display on horizontal and vertical center
oled.clearDisplay(); // clear display
oled.setCursor((SCREEN_WIDTH - width_T) / 2, SCREEN_HEIGHT / 2 - height_T - 5);
oled.println(temperature); // text to display
oled.setCursor((SCREEN_WIDTH - width_H) / 2, SCREEN_HEIGHT / 2 + 5);
oled.println(humidity); // text to display
oled.display();
}
Passos Rápidos
- Abra o Arduino IDE no seu PC.
- Vá até o ícone Libraries na barra esquerda do Arduino IDE.
- Pesquise “SSD1306” e encontre a biblioteca SSD1306 da Adafruit
- Clique no botão Install para instalar a biblioteca.

- Será solicitado que você instale algumas outras dependências da biblioteca
- Clique no botão Install All para instalar todas as dependências da biblioteca.

- Pesquise “DHT” e encontre a biblioteca DHT sensor library da Adafruit
- Clique no botão Install para instalar a biblioteca.

- Será solicitado que você instale algumas outras dependências da biblioteca
- Clique no botão Install All para instalar todas as dependências da biblioteca.

- Copie o código acima e abra-o no Arduino IDE
- Clique no botão Upload no Arduino IDE para carregar o código no Arduino
- Coloque o sensor em água quente e fria, ou segure o sensor com a mão
- Veja o resultado no OLED e no Serial Monitor
※ Nota:
O código acima centraliza automaticamente o texto na horizontal e na vertical no display OLED