DS18B20 Temperature Sensor Module with Arduino

Get detailed temperature readings with the Arduino

Written By: Marcus Schappi

Dash icon
Difficulty
Easy
Steps icon
Steps
6
In this guide, we will use a DS18B20 temperature sensor module with the Arduino. This temperature sensor is able to get readings like 23.50 degree celsius rather than just 23 degree celsius. Complete this guide to get started with using this very useful component, where it could be used for building your own weather station, gardening assistance and more!

Step 1 DS18B20 Temperature Module

Before we begin, let's take a closer look at the DS18B20 Digital Temperature Sensor Module! It has three pins: GND: This pin is labelled as '-' on the module, it is also called 'GND'. What is 'GND'? In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called ground or GND. Out: The DS18B20 temperature sensor uses the 1-Wire protocol, and only requires a single data line. This is the data out pin where the temperature data will be output. Later, we will connect this to a GPIO pin on the micro:bit. + : This is connected to 3.3V on the micro:bit. Voltage is the difference in potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to.

Step 2 Connect + to 5V

First, connect a red jumper wire from + on the module to 5V on the Little Bird Uno R3.

Step 3 Connect OUT to Digital Pin 3

Next, connect a jumper wire from OUT on the module to Digital Pin 3 on the Little Bird Uno R3.

Step 4 Connect GND to GND

Finally, connect a black jumper wire from - on the module to GND on the Little Bird Uno R3.

Step 5 Install DallasTemperature Library

A quick way to get temperature readings from the DS18B20 is to use an existing library! In this guide, we will use the DallasTemperature library. Navigate to Tools > Manage Libraries. Type 'dallastemperature' into the search field. Click on the 'Install' button next to DallasTemperature by Miles Burton ... Once complete, it should state 'INSTALLED'. Click on the 'Close' button to return to the Arduino IDE.

Step 6 Code for DS18B20 Module with Arduino

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into digital pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

void setup(void)
{
  sensors.begin();  // Start up the library
  Serial.begin(9600);
}

void loop(void)
{
  // Send the command to get temperatures
  sensors.requestTemperatures();

  //print the temperature in Celsius
  Serial.print("Temperature: ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.print((char)176);//shows degrees character
  Serial.print("C  |  ");

  //print the temperature in Fahrenheit
  Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
  Serial.print((char)176);//shows degrees character
  Serial.println("F");

  delay(1000);
}
Copy and paste this sketch into the Arduino IDE. Upload the code to the Little Bird Uno R3. Navigate to Tools > Serial Monitor. You should see temperature readings being printed onto the serial monitor!