Arduino - Exibir a Temperatura do Sensor LM35 no OLED
Neste tutorial, vamos aprender como ler a temperatura do sensor LM35 e exibi-la 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 OLED e o Sensor de Temperatura LM35
Se você ainda não conhece o OLED e o Sensor de Temperatura LM35 (pinagem, como funciona, como programar ...), aprenda sobre eles nos seguintes tutoriais:
- Tutorial Arduino - OLED
- Tutorial Arduino - Sensor de Temperatura LM35
Diagrama de Ligação

This image is created using Fritzing. Click to enlarge image
Código Arduino - Sensor de Temperatura LM35 - 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-display-temperature-from-lm35-sensor-on-oled
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_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(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
String tempString;
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
tempString.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 tempC = milliVolt / 10;
tempString = String(tempC, 2); // two decimal places
tempString += char(247) + String("C");
Serial.println(tempString); // print the temperature in Celsius to Serial Monitor
oledDisplayCenter(tempString); // display temperature on OLED
}
void oledDisplayCenter(String text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
// display on horizontal and vertical center
oled.clearDisplay(); // clear display
oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
oled.println(text); // 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.

- 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