Arduino – DS18B20 Temperature Sensor

Equipment
  • Arduino
  • DS18B20 temperature sensor
  • 4.7k resistor
  • Breadboard
  • 3 connecting wires
Thinking

We want to be able to use an Arduino to measure temperature. A number of available sensors use one wire to provide data rather than just modify a voltage or resistance, the temperature sensor we will use here is one of those. In order to ‘talk’ to this sensor and extract data from it we need to be able to send it a signal over it’s ‘data’ connection and then translate the data sent back. This can be done from scratch but is complicated, and it’s already been done by others so better for us to ‘stand on the shoulders of giants’ and reuse what we can. In order for code to be shared in this way it is packaged up into a library and stored on the internet, in a similar way to the Examples available with the Arduino IDE.

Doing

Firstly we need to install an appropriate library. We can find and install the library for this in the following way:

  • Run the Arduino IDE
  • Click Sketch->Include Libraries

This displays the libraries that are currently installed. If OneWire is on the list then simply select it, if not then click Manage Libraries …

This displays a dialog box where you can search the available libraries. In the search box enter OneWire and the list below will change. There are a number of options, we will use the one provided by Adafruit as it may be useful for other projects in the future. Click on it in the list and an Install button will appear, click the Install button. The library will be downloaded and installed.

We can now include it in our project, Click Sketch->Include Libraries and scroll down to MAX31850 One Wire and click it.

Having included the library we have also included the example sketches from the library, so let’s make use of that too. Click File->Examples and move the mouse cursor over the MAX31850 OneWire option and then to the right and click DS18x20_Temperature. This will open up a new window with the example code in it. Before we do anything it’s always best to save the sketch so that we can make any changes that we want to. Click File->Save As, change the name if you like to something meaningful to you and then click the Save button.

Plug the Arduino into your computer. Before we compile the code and upload it to the Arduino click the Tools menu option and check that the Board and Port are defined correctly, if not select the correct options for each. Then click the right arrow button to compile and upload the code. You will see the transmit(TX) and receive(RX) lights flash quickly on the Arduino board as the code is transferred. Once this completes the RX light will flash quickly.

Next we need to connect the Arduino to the sensor, but first unplug it from your PC. We load the code first so that the Arduino is in a known state, otherwise there is no telling what was running on there previously and it could affect our sensor.

When looking at the flat side of the sensor the pins are numbered 1 to 3 from left to right. If you have a sensor mounted on a board they may be in a slightly different order but should be labelled, in my example ‘-‘ for the GND connection and ‘S’ for the data or signal pin.

The connections are as follows:

  • Arduino GND -> Sensor Pin 1
  • Arduino 5v -> Sensor Pin 3
  • Arduino Pin 10 -> Sensor Pin 2

Finally connect the resistor between the signal/data and 5v pins.

Now all we need to do is connect the Arduino back to the PC and instead of the RX LED flashing quickly it should flash a couple of times and then wait for about a second before doing the same again, if so then it’s working. This is great but it would be better if we could see some data! Looking back at the example code we can see that at the start in the setup() function there is a call to Serial.begin(9600) and then within the code some calls to Serial.println() with different values. This means that it has initialised the serial monitor and is sending the output to that. To see this Click Tools->Serial Monitor or hold down Ctrl and Shift and then press M.

A window will then appear with something similar to the following:

ROM = 28 FF B8 61 55 16 4 4F
  Chip = DS18B20
  Data = 1 61 1 4B 46 7F FF C 10 57  CRC=57
  Temperature = 22.06 Celsius, 71.71 Fahrenheit
No more addresses.

This is the data being received from the sensor. If the display is not working correctly ensure that the Autoscroll box is checked, and it is set to No line ending and 9600 baud – at the bottom right of the Serial Monitor window.

If you then hold your finger against the sensor the temperature values displayed will increase.

That’s it, you now have a working temperature sensor.