1. Required Components
- 1 x Arduino board (e.g., Arduino Uno)
- 1 x DS18B20 temperature sensor
- 1 x 4.7kΩ pull-up resistor
- Breadboard and jumper wires
- USB cable to connect Arduino to PC
2. DS18B20 Pinout
Follow these connections to interface the DS18B20 sensor with your Arduino:
- GND: Connect to Arduino GND
- VDD: Connect to 5V (or 3.3V) on Arduino
- DQ: (Data pin) Connect to digital pin 2 of Arduino
Note: Place a 4.7kΩ resistor between the DQ and VDD pins for proper operation.
3. Code Explanation
The code utilizes the OneWire
and DallasTemperature
libraries to manage communication with the DS18B20 sensor. Here's an overview of the main functions:
OneWire()
: Handles the OneWire communication protocol used by the DS18B20.sensors.requestTemperatures()
: Requests the sensor to measure the current temperature.sensors.getTempCByIndex(0)
: Retrieves the temperature in Celsius from the first (and only) sensor in this setup.
Installing Required Libraries
Before uploading the code, install the required libraries:
- In the Arduino IDE, navigate to Sketch > Include Library > Manage Libraries....
- Search for OneWire and click "Install."
- Search for DallasTemperature and click "Install."
4. Arduino Code
Use the following code to read the temperature data from the DS18B20 sensor:
// Include necessary libraries
#include
#include
// Pin to which the DS18B20 data line is connected
#define ONE_WIRE_BUS 2
// Initialize OneWire instance and pass it to DallasTemperature library
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize the temperature sensor library
sensors.begin();
}
void loop() {
// Request temperature data from the sensor
sensors.requestTemperatures();
// Get temperature in Celsius
float temperatureC = sensors.getTempCByIndex(0);
// Print the temperature to the serial monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
// Wait for 1 second before the next reading
delay(1000);
}
5. Upload and Test
Follow these steps to upload the code and test your setup:
- Connect your Arduino to your PC using a USB cable.
- Open the Arduino IDE and paste the code above into a new sketch.
- Install the required libraries if you haven’t already.
- Select the correct board and port under Tools in the IDE.
- Click the upload button to program your Arduino.
- Open the Serial Monitor (set baud rate to
9600
) to see real-time temperature readings.
6. Additional Tips
- Multiple Sensors: The DS18B20 supports multiple sensors on a single data line. Use their unique addresses to read data individually.
- Accuracy: The DS18B20 has a default resolution of 12 bits, accurate to ±0.5°C. Adjust resolution settings if necessary for your project.
- Expand the Project: Display temperature readings on an LCD, send data wirelessly, or log it to an SD card for more advanced applications.
7. Conclusion
This experiment showed how to interface the DS18B20 sensor with an Arduino to measure and display temperature data. This simple setup is highly versatile and can be integrated into larger projects such as weather stations, home automation systems, or industrial temperature monitoring.