Arduino Nano - Sensor de Temperatura e Umidade - OLED

Este tutorial ensina você a ler a temperatura e a umidade de um sensor DHT11/DHT22 e exibi-las em um OLED.

Display OLED com sensor de temperatura e umidade DHT11/DHT22 e Arduino Nano

Hardware Necessário

1×Official Arduino Nano
1×Alternatively, DIYables Nano V3.0 Development Board
1×Cabo USB A para Mini-B
1×Display OLED I2C SSD1306 128x64
1×Display OLED I2C SSD1306 128x32
1×Sensor de Temperatura e Umidade DHT11
1×(Alternativa) DHT11 Sensor
1×Fios Jumper

You can use DHT22 sensor instead of DHT11 sensor.

1×(Recomendado) Placa de Expansão com Bornes de Parafuso para Arduino Nano
1×(Recomendado) Placa de Expansão Breakout para Arduino Nano
1×(Recomendado) Divisor de Alimentação para Arduino Nano

Or you can buy the following kits:

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 e DHT22

Se você não está familiarizado com o display OLED e com o sensor de temperatura e umidade DHT11 e DHT22 (pinagem, funcionalidade, programação ...), os tutoriais a seguir podem ajudar você:

Diagrama de Ligação

Ligação do Arduino Nano com o Módulo DHT11 e o LCD

Diagrama de ligação do Arduino Nano com o sensor DHT11 e o OLED

This image is created using Fritzing. Click to enlarge image

Ligação do Arduino Nano com o Módulo DHT22 e o LCD

Diagrama de ligação do Arduino Nano com o sensor DHT22 e o OLED

This image is created using Fritzing. Click to enlarge image

Veja A melhor forma de alimentar o Arduino Nano e outros componentes.

Código Arduino Nano - Sensor DHT11 - OLED

/* * Este código de Arduino Nano foi desenvolvido por pt.newbiely.com * Este código de Arduino Nano é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.newbiely.com/tutorials/arduino-nano/arduino-nano-temperature-humidity-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels #define DHT_PIN 2 // The Arduino Nano pin connected to DHT11 sensor #define DHT_TYPE DHT11 Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht(DHT_PIN, DHT_TYPE); 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 dht.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 = dht.readHumidity(); // read humidity float tempC = dht.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

  • Clique no ícone Libraries na barra esquerda do Arduino IDE.
  • Procure por “SSD1306” e localize a biblioteca SSD1306 da Adafruit.
  • Pressione o botão Install para instalar a biblioteca.
Biblioteca OLED para Arduino Nano
  • Você será solicitado a instalar algumas outras dependências da biblioteca.
  • Pressione o botão Install All para concluir a instalação de todas as dependências da biblioteca.
Biblioteca Adafruit GFX para Arduino Nano
  • Pesquise por “DHT” e localize a biblioteca Adafruit DHT sensor library.
  • Pressione o botão Install para instalar a biblioteca.
Biblioteca do sensor DHT para Arduino Nano
  • Você será solicitado a instalar algumas outras dependências da biblioteca.
  • Clique no botão Install All para instalar todas as dependências da biblioteca.
Biblioteca Adafruit Unified Sensor para Arduino Nano
  • Copie o código acima e abra-o no Arduino IDE.
  • Clique no botão Upload no Arduino IDE para enviar o código ao Arduino Nano.
  • Coloque o sensor em água quente e fria, ou segure-o na mão.
  • Confira o resultado no OLED e no Serial Monitor.

※ Nota:

O código em questão centraliza automaticamente o texto, tanto na horizontal quanto na vertical, em um display OLED.

Código Arduino Nano - Sensor DHT22 - OLED

/* * Este código de Arduino Nano foi desenvolvido por pt.newbiely.com * Este código de Arduino Nano é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.newbiely.com/tutorials/arduino-nano/arduino-nano-temperature-humidity-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels #define DHT_PIN 2 // The Arduino Nano pin connected to DHT22 sensor #define DHT_TYPE DHT22 Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht(DHT_PIN, DHT_TYPE); 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 dht.begin(); // initialize DHT22 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 = dht.readHumidity(); // read humidity float tempC = dht.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(); }

※ Nota:

O código para DHT11 e DHT22 é o mesmo, exceto por uma linha. A biblioteca usada para ambos também é a mesma.

Vídeo

※ NOSSAS MENSAGENS

  • Fique à vontade para compartilhar o link deste tutorial. Porém, por favor, não use nosso conteúdo em outros sites. Investimos muito esforço e tempo para criar este conteúdo, por favor respeite nosso trabalho!