🌡 Connecting ds18b20 to Arduino (temperature sensor)

The DS18B20 temperature sensor is a digital temperature sensor. This means that you can measure the temperature using a very simple circuit. It communicates with a common bus, which means that you can connect multiple devices and measure their values using only one Arduino digital output.
The sensor has only three contacts, as you can see in the following figure. The DS18B20 is also available in a waterproof version.

Here are some of the main features of the DS18B20 temperature sensor:

  • Communication over a single-wire communication bus
  • Operating temperature: -55ÂșC to 125ÂșC
  • Accuracy +/- 0.5 ÂșC (between a range from -10ÂșC to 85ÂșC)
Required materials:
  • Arduino (Uno, Nano, Mega, ...)
  • DS18B20 sensor
  • Breadboard
  • Wires
  • 4,7 kOhm resistor

Scheme
3-wire connection required. Here is the scheme you must follow:

The black part in the diagram is a temperature sensor.

You will need to install the OneWire and DallasTemperature libraries.

  • You must have .zip archives of libraries.
  • Add via “sketch” - “libraries” - ”add zip library”

Download the following code to the Arduino board.

#include <OneWire.h>
#include <DallasTemperature.h>
// ds18b20 is connected to second digital pin on Arduino
#define ONE_WIRE_BUS 2
// Setting up a oneWire instance to communicate with any devices
OneWire
OneWire oneWire(ONE_WIRE_BUS);
//transmitting onewire link to temperature sensor
DallasTemperature sensors(&oneWire);
void setup(void)
{
 Serial.begin(9600);
 // Sensor begin
 sensors.begin();
}


void loop(void){
 // Call sensor.requestTemperatures () to issue global temperature and queries to all devices on the bus.
 sensors.requestTemperatures();
 Serial.print("Celsius temperature: ");
 //You can have more than one chip on the same bus. 0 refers to the first IC on the wire
 Serial.print(sensors.getTempCByIndex(0));
 Serial.print(" - Fahrenheit temperature: ");
 Serial.println(sensors.getTempFByIndex(0));
 delay(1000);
}
Finally, you should open the Arduino IDE serial monitor at 9600 baud, and you will see the temperature displayed in both Celsius and Fahrenheit:
Share this article with a friend who also loves electronics.
And write in the comments if there are difficulties.

Leave a comment

Name .
.
Message .

Please note, comments must be approved before they are published