Arduino Nano - Sensor de Temperatura LM35 OLED
Este tutorial ensina você a usar o Arduino Nano para ler a temperatura de um sensor LM35 e depois exibi-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 está familiarizado com o OLED e o Sensor de Temperatura LM35 (incluindo pinagem, funcionalidade, programação etc.), os tutoriais a seguir podem ajudar:
- Tutorial Arduino Nano - OLED
Diagrama de Ligação

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 de Temperatura LM35 - 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-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 // Arduio Nano 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
- Clique no ícone Libraries na barra esquerda do Arduino IDE.
- Pesquise por “SSD1306” e localize a biblioteca SSD1306 da Adafruit.
- Em seguida, pressione o botão Install para concluir a instalação.

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

- Copie o código 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 o texto, tanto na horizontal quanto na vertical, em um display OLED.