ESP8266 - Sensor de Temperatura LM35 OLED
Este tutorial ensina você a usar o ESP8266 para obter a temperatura de um sensor LM35 e mostrá-la em um OLED.
Hardware Necessário
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 OLED e o Sensor de Temperatura LM35
Se você não conhece o OLED e o Sensor de Temperatura LM35 (incluindo pinagem, funcionamento e programação), os tutoriais a seguir podem ajudar:
- Tutorial ESP8266 - OLED
- Tutorial ESP8266 - Sensor de Temperatura LM35
Diagrama de Ligação

This image is created using Fritzing. Click to enlarge image
Para mais informações, veja Pinagem do ESP8266 e como alimentar o ESP8266 e outros componentes.
Código ESP8266 - Sensor de Temperatura LM35 - OLED
/*
* Este código de ESP8266 NodeMCU foi desenvolvido por pt.newbiely.com
* Este código de ESP8266 NodeMCU é disponibilizado ao público sem nenhuma restrição.
* Para tutoriais completos e diagramas de ligação, visite:
* https://pt.newbiely.com/tutorials/esp8266/esp8266-lm35-temperature-sensor-oled
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_WIDTH 128 // OLED display width, in pixels
#define OLED_HEIGHT 64 // OLED display height, in pixels
#define ADC_VREF_mV 5000.0 // in millivolt
#define ADC_RESOLUTION 1024.0
#define PIN_LM35 A0 // pin connected to LM35 temperature sensor
Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
String temperature_str;
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(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display
temperature_str.reserve(10); // to avoid fragmenting memory when using String
}
void loop() {
// get the ADC value from the LM35 temperature sensor
int adcVal = analogRead(PIN_LM35);
// convert the ADC value to voltage in millivolt
float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
// convert the voltage to the temperature in Celsius
float temperature_C = milliVolt / 10;
temperature_str = String(temperature_C, 2); // two decimal places
temperature_str += char(247) + String("C");
Serial.println(temperature_str); // print the temperature in Celsius to Serial Monitor
oled_display_center(temperature_str); // display temperature on OLED
}
void oled_display_center(String text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
// center the display both horizontally and vertically
oled.clearDisplay(); // clear display
oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2);
oled.println(text); // text to display
oled.display();
}
Passos Rápidos
Para começar com o ESP8266 no Arduino IDE, siga estes passos:
- Confira o tutorial como configurar o ambiente para o ESP8266 no Arduino IDE se esta for a sua primeira vez usando o ESP8266.
- Ligue os componentes como mostrado no diagrama.
- Conecte a placa ESP8266 ao seu computador usando um cabo USB.
- Abra o Arduino IDE no seu computador.
- Escolha a placa ESP8266 correta, como (por exemplo, NodeMCU 1.0 (ESP-12E Module)), e a respectiva porta COM.
- Clique no ícone Libraries na barra à esquerda do Arduino IDE.
- Pesquise por “SSD1306” e localize a biblioteca SSD1306 da Adafruit.
- Clique no botão Install para adicionar a biblioteca.

- Você será solicitado a instalar algumas dependências adicionais de bibliotecas.
- Para instalar todas elas, clique no botão Install All.

- 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 ESP8266.
- Coloque o sensor em água quente e fria ou segure-o na mão.
- Veja os resultados no display OLED e no Serial Monitor.
※ Nota:
O código em questão centraliza o texto horizontal e verticalmente em um display OLED.