Arduino - Mostrar la temperatura del sensor LM35 en OLED

En este tutorial, vamos a aprender cómo leer la temperatura del sensor LM35 y mostrarla en una pantalla OLED.

Sensor de temperatura LM35 para Arduino OLED W

Acerca de OLED y del sensor de temperatura LM35

Si no conoces OLED y el Sensor de Temperatura LM35 (disposición de pines, cómo funciona, cómo programar ...), aprende sobre ellos en los siguientes tutoriales:

Diagrama de Cableado

Diagrama de cableado de Arduino, LM35 sensor de temperatura y OLED

This image is created using Fritzing. Click to enlarge image

Código de Arduino - Sensor de temperatura LM35 - OLED

/* * Este código de Arduino fue desarrollado por es.newbiely.com * Este código de Arduino se proporciona al público sin ninguna restricción. * Para tutoriales completos y diagramas de cableado, visite: * https://es.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(); }

Pasos R\u00e1pidos

  • Abre Arduino IDE en tu PC.
  • Navega al icono Bibliotecas en la barra izquierda del Arduino IDE.
  • Busca “SSD1306”, luego localiza la biblioteca SSD1306 de Adafruit
  • Haz clic en el botón Instalar para instalar la biblioteca.
Biblioteca OLED para Arduino
  • Se le pedirá que instale algunas dependencias de otras bibliotecas.
  • Haga clic en el botón Instalar todo para instalar todas las dependencias de la biblioteca.
Biblioteca de sensores de Arduino Adafruit GFX
  • Haz clic en el botón Subir en Arduino IDE para cargar el código en Arduino
  • Coloca el sensor en agua caliente y en agua fría, o sostén el sensor con la mano
  • Ve el resultado en la pantalla OLED y en el Monitor Serial

※ Nota:

El código de la sección Acerca de alinea automáticamente el texto horizontal y verticalmente en la pantalla OLED.

Video Tutorial

Estamos considerando crear tutoriales en video. Si considera que los tutoriales en video son importantes, suscríbase a nuestro canal de YouTube para motivarnos a crear los videos.

※ NUESTROS MENSAJES

  • No dude en compartir el enlace de este tutorial. Sin embargo, por favor no use nuestro contenido en otros sitios web. Hemos invertido mucho esfuerzo y tiempo en crear el contenido, ¡por favor respete nuestro trabajo!