In the world of IoT and embedded systems, the ESP32 microcontroller stands out due to its versatility and powerful features. In this article, we will walk through the process of building a temperature sensor using the DS18B20 sensor and displaying the temperature readings on an I2C OLED 128x32 display. This project is perfect for those looking to get started with IoT projects or wanting to monitor environmental conditions in real-time.
Components Required
- ESP32 Development Board: The brain of our project, known for its Wi-Fi and Bluetooth capabilities.
- DS18B20 Temperature Sensor: A digital temperature sensor with a unique 1-Wire interface.
- 128x32 I2C OLED Display: A small, energy-efficient display to show the temperature readings.
- 4.7kΩ Resistor: Used as a pull-up resistor for the DS18B20 data line.
- Jumper Wires: For connecting components.
- Breadboard: To prototype the circuit.
Circuit Diagram
Here’s how you can connect the components:
-
DS18B20:
- VDD to 3.3V on ESP32
- GND to GND on ESP32
- Data to GPIO 4 on ESP32 (with a 4.7kΩ resistor connected between Data and VDD)
-
OLED Display:
- VCC to 3.3V on ESP32
- GND to GND on ESP32
- SCL to GPIO 22 on ESP32
- SDA to GPIO 21 on ESP32
Setting Up the Development Environment
- Arduino IDE: Ensure you have the latest version of the Arduino IDE installed.
- ESP32 Board Support: Install the ESP32 board support in the Arduino IDE by going to
File > Preferences
and adding the URLhttps://dl.espressif.com/dl/package_esp32_index.json
in the Additional Board Manager URLs field. Then, go toTools > Board > Board Manager
and search for “ESP32” to install it. - Libraries: Install the following libraries via the Arduino Library Manager:
OneWire
for communicating with the DS18B20 sensor.DallasTemperature
for handling temperature readings.Adafruit_GFX
andAdafruit_SSD1306
for controlling the OLED display.
Writing the Code
Below is a sample code to read temperature from the DS18B20 sensor and display it on the OLED:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define ONE_WIRE_BUS 4
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
sensors.begin();
if(!display.begin(SSD1306_I2C_ADDRESS, OLED_RESET)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
}
void loop() {
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
display.clearDisplay();
display.setCursor(0, 0);
display.print("Temp: ");
display.print(temperatureC);
display.println(" C");
display.display();
delay(1000);
}
Explanation
- OneWire and DallasTemperature Libraries: These libraries are used to interface with the DS18B20 sensor. The
OneWire
library handles the communication protocol, whileDallasTemperature
simplifies temperature reading. - Adafruit Libraries: These libraries manage the OLED display, allowing us to draw text and graphics.
- Setup Function: Initializes the sensor and display. It also checks if the display is connected properly.
- Loop Function: Continuously reads the temperature from the DS18B20 sensor and updates the OLED display every second.
Conclusion
This project demonstrates how to integrate a temperature sensor with an ESP32 and display the readings on an OLED screen. It’s a simple yet effective way to monitor temperature in various environments. The ESP32’s capabilities make it an excellent choice for IoT applications, and the DS18B20 sensor provides accurate temperature readings with minimal setup.
By following this guide, you can expand your project by adding more sensors or integrating with cloud services for remote monitoring. Happy tinkering!