IoT Bytes

Bits and Bytes of IoT

DS18B20 Temperature Sensor with Arduino

Pradeep Singh | 18th Nov 2017


In this article, you will find, how to create a Temperature sensor that can act as the basis for some other automation, for example, a temperature controlled Fan etc. So let’s get started and create the Light Sensor.

1. What do you need:

Before starting with this article, you should make sure that you have the following parts ready –

  • Arduino Uno
  • 4.7K Ohm Resistor
  • DS18B20 Temperature Sensor
  • Breadboard
  • Male to Male Jumper Wires

2. The Circuit:

Connect the Arduino Uno with the DS18B20 Temperature Sensor as shown in the following diagram –


This is how my setup looks like with a breadboard –


3. The Code / Sketch:

Once you are ready with your circuit, you can open your Arduino IDE and use the following sketch to read the temperature readings from the LDR. This sketch is based on the Code sample available at –

//Include libraries
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

void setup(void)
 Serial.begin(9600); //Begin serial communication
 Serial.println("Arduino Digital Temperature // Serial Monitor Version"); //Print a message

void loop(void)
 // Send the command to get temperatures
 Serial.print("Temperature is: ");
 Serial.println(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
 //Update value every 1 sec.

4. The Result:

After pushing the sketch to the Arduino Uno, you can open the Serial Monitor (Tools –> Serial Monitor). In the Serial Monitor window, you should see the values coming from your DS18B20 Temperature Sensor.

Try touching the sensor with your fingers and see the changing values on the Serial Monitor.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: