Connecting and using the DS18B20 temperature sensor with a microcontroller

Introduction

The DS18B20 is a 1-Wire digital temperature sensor from Maxim IC. It reports the temperature in degrees Celsius with 9 to 12-bit precision, with a range from -55 to 125 (+/-0.5). Each temperature sensor has a unique 64-Bit serial number etched into the silicon which allows for a huge number of sensors to be used on one data bus with the probability of 9,223,372,036,854,775,807 to 1 that any two will clash!

The DS18B20 Temperature sensor

Features

  • Unique 1-Wire interface that requires only one port pin for communication.
  • Each device has a unique 64-bit serial code stored in an onboard ROM, as mentioned above.
  • Requires no external components.
  • Can be powered from it’s data line.
  • Power supply range is from 3.0V to 5.5V.
  • Measures temperatures from –55°C to +125°C (–67°F to +257°F) with ±0.5°C accuracy from –10°C to +85°C.
  • Thermometer resolution is user-selectable from 9 to 12 bits.
  • Converts temperature to 12-bit digital word in a maximum time of 750ms.
  • Alarm search command which allows it to identify and addresses devices whose temperature is outside of programmed limits (temperature alarm condition).
  • Applications include thermostatic controls, industrial systems, consumer products, thermometers, or any thermally sensitive system.

What you need to make a working thermometer

To construct an electronic thermometer you will need the following:

  • A microcontroller. (In this instance an Arduino or Atmega328p)
  • A DS18B20 sensor and a 4.7k resistor.
  • Breadboard.
  • Jumper leads.

Libraries

You will need to download and install 2 libraries for this exercise. If you are using the arduino then this should be fairly straight forward and can be acheived by following the menus within the IDE. The two libraries that you will need are:

  • 1-wire bus
  • Dallas Temperature

Build the simple circuit

The DS18B20 circuit diagram

To read the data from DS18B20 in the serial monitor of the Arduino IDE, build the circuit in the above schematic.

Connect the sensor pins to the Arduino in the following order: pin 1 to GND; pin 2 to any digital pin (pin 2 in this case); pin 3 to +5V or +3.3V. Connect the pull-up resistor between the Vcc supply and the data wire.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// First we include the 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
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");

// Start up the library
sensors.begin();
}

void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print(" Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperature readings
Serial.println("DONE");
Serial.print("Temperature is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
// You can have more than one DS18B20 on the same bus.
// 0 refers to the first IC on the wire
delay(1000);
}

Place the above code into the IDE and upload via the arduino uploader. If everything is okay you should see the temperature being measured and showed in the serial monitor of the IDE.

The difference between TCP and UDP

What is the difference between TCP and UDP?

Both TCP and UDP are protocols used for sending bits of data — known as packets — over the Internet. They both build on top of the Internet protocol. In other words, whether you are sending a packet via TCP or UDP, that packet is sent to an IP address. These packets are treated similarly, as they are forwarded from your computer to intermediary routers and on to the destination.

TCP and UDP are not the only protocols that work on top of IP. However, they are the most widely used. The widely used term “TCP/IP” refers to TCP over IP. UDP over IP could just as well be referred to as “UDP/IP”, although this is not a common term.

TCP

TCP stands for Transmission Control Protocol. It is the most commonly used protocol on the Internet.

When you load a web page, your computer sends TCP packets to the web server’s address, asking it to send the web page to you. The web server responds by sending a stream of TCP packets, which your web browser stitches together to form the web page and display it to you. When you click a link, sign in, post a comment, or do anything else, your web browser sends TCP packets to the server and the server sends TCP packets back. TCP is not just one way communication — the remote system sends packets back to acknowledge it is received your packets.

TCP guarantees the recipient will receive the packets in order by numbering them. The recipient sends messages back to the sender saying it received the messages. If the sender does not get a correct response, it will resend the packets to ensure the recipient received them. Packets are also checked for errors. TCP is all about this reliability — packets sent with TCP are tracked so no data is lost or corrupted in transit. This is why file downloads do not become corrupted even if there are network hiccups. Of course, if the recipient is completely offline, your computer will give up and you will see an error message saying it can not communicate with the remote host.

UDP

UDP stands for User Datagram Protocol — a datagram is the same thing as a packet of information. The UDP protocol works similarly to TCP, but it throws all the error-checking stuff out. All the back-and-forth communication and deliverability guarantees slow things down.

When using UDP, packets are just sent to the recipient. The sender will not wait to make sure the recipient received the packet — it will just continue sending the next packets. If you are the recipient and you miss some UDP packets, too bad — you can not ask for those packets again. There is no guarantee you are getting all the packets and there is no way to ask for a packet again if you miss it, but losing all this overhead means the computers can communicate more quickly.

UDP is used when speed is desirable and error correction is not necessary. For example, UDP is frequently used for live broadcasts and online games.

avrdude

Avrdude is a library that allows you to perform functions and upload files to the AVR series of microcontrollers. It is open source software and is freely available to download. With an interface it makes it easy to upload files and interrogate microcontrollers from the command line. It is the software that the Arduino IDE uses in the background.

Example of erasing a chip completely: (Warning this will erase the bootloader too!)

avrdude -p m328p - c usbtiny -e

Will result in output similar to the below, confirming that your chip indeed has been erased.

1
2
3
4
5
6
7
8
9
10
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: erasing chip

avrdude: safemode: Fuses OK (E:FD, H:DE, L:FF)

avrdude done. Thank you.

Programming fuses with avrdude and USBTiny

Fuse programming on the ATMega328 is very straightforward with the USBTiny. Connect the USBTiny to the AVR with the ICSP (In Circuit Serial Programming) connections. You need to ensure that you have avrdude installed on your computer. Once connected the following commands apply.

avrdude -c usbtiny -p ATmega328p

the above line reads the fuse settings on an ATmega328p

avrdude -p m328p -c usbtiny -U efuse:w:0xff:m -U hfuse:w:0xd9:m -U lfuse:w:0xe2:m

the above line writes the fuse bits on an ATmega328p

The fuse settings for standard 16MHz ext clock are E:FD H:DE L:FF

The fuse settings for standard 8MHz int clock are E:FF H:D9 L:E2

The fuse settings for standard 1MHz int clock are E:FF H:D9 L:62

AVR HVPP (High Voltage Parallel Programmer)

This post is a work in progress. All information contained within is incomplete.

High Voltage Parallel Programmer

Occasionally the fusebits get mis-programmed and render an AVR chip virtually useless. If this happens it can seem like the chip has died and is unrecoverable. Using A HVPP you can reset the chip and teh fue bits and bring back an AVR chip to life. I have successfully performed this operation though have lost my notes. All that I have so far is the connection map. I will aim to update this in future in case anyone needs any help. Ultimately all that is missing is the program used and the basic circuit of allowing 12V to hit the programming pin on the chip. (Hence the name High Voltage.)

Connection Map

// Arduino pins Target pins
// ———— ———————–
12 6, // 14 (PB0) (data bit 0)
13 7, // 15 (PB1) (data bit 1)
14 8, // 16 (PB2) (data bit 2)
15 9, // 17 (PB3) (data bit 3)
16 10, // 18 (PB4) (data bit 4)
17 11, // 19 (PB5) (data bit 5)
18 12, // 23 (PC0) (data bit 6)
19 13 // 24 (PC1) (data bit 7)

23 RDY = A0, // 3 (PD1) (low means busy)
24 OE = A1, // 4 (PD2) (low means output enabled)
25 WR = A2, // 5 (PD3) (low means write)
26 BS1 = A3, // 6 (PD4)
27 XTAL1 = A4, // 9 (XTAL1)
28 XA0 = A5, // 11 (PD5)
4 XA1 = 2, // 12 (PD6)
5 PAGEL = 3, // 13 (PD7)
6 BS2 = 4, // 25 (PC2)
11 VCC = 5, // 7 and 20 (VCC and AVCC)

AVR Introduction

Introduction

The AVR family of chips are a set of microprocessors manufactured by Atmel. They are probably most commonly known by their iplementation on Arduino boards.

Programming methods

There are several ways to program the AVR chips.

Using the Arduino

This is easily done and the most simple way to program your AVR. The chip is inserted on to an Arduino board which is connected to your computer via a usb cable. Using free downloadable Arduino software the chip can have your firmware uploaded easily using the Arduino’s built in FTDI interface. The one downside to this is that a section of the AVR must have a bootloader hosted on it. This takes up space on the chip, but is usually not a problem.

Using an external programmer

There are several external programmer options available. An example is the USBTiny programmer which can be picked up extremely cheaply from the likes of Ebay. This will allow you to program your AVR without the need of an arduino. i.e. You can program the chip directly using minimal components, for example on a breadboard or embedded in a circuit, as long as the programming pins are exposed. This method does not require a bootloader, therefore you have access to the full range of the AVR’s memory.

Using Atmel ICE

This is Atmels grand version of an external programmer. It allows you to do everything you should ever need to do with the AVR. Not only does it allow you to program the chip, but it allows you to set fusebits and also perform onchip debugging. It also fully integrates with Atmel Studio which is free software from Atmel.

^