Ds18b20 thermometer and its connection to arduino

DS18B20 is a digital temperature sensor. The sensor is very easy to use. First of all, it's digital, and secondly, he has only one contact, from which we receive a useful signal. I.e, you can connect a huge number of these sensors to one Arduino at the same time. Pins will be more than enough. Little of, you can even connect multiple sensors to one pin on the Arduino! But first things first.

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Arduino temperature sensor DS18B20

DS18B20 has various form factors. So the choice, which one to use, is up to you. Three options available: 8-Pin SO (150 mils), 8-Pin µSOP, и 3-Pin TO-92. Surfing on eBay or Aliexpress shows, that the Chinese offer the TO-92 version in a waterproof case. I.e, you can safely dip such a miracle into the water, use in the rain, etc.. and so on. These sensors are manufactured with three output pins (black - GND, red - Vdd and white - Data).

Various form factors of DS18B20 sensors are shown in the figure below.

Arduino temperature sensor DS18B20

Model DS18B20 in a waterproof case:

Waterproof temperature sensor DS18B20

DS18B20 is convenient to use. You can power it through the data contact (in this case, you use only two out of three contacts to connect!). The sensor operates in the voltage range from 3.0 In to 5.5 B and measures temperature from -55°C to +125°C (-67°F to +257°F) with an accuracy of ±0.5°C (-10°C to +85°C).

Another cool feature: you can connect up to 127 sensors! and read temperature readings from each separately. Not quite clear, What kind of project would need something like this?, but you can connect two sensors and control the temperature in the refrigerator and freezer. In doing so, you will leave a bunch of pins free on the Arduino ... In general, nice feature.

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

What You Need to Control Temperature with Arduino and DS18B20

Software

  • Naturally, you need an Arduino IDE;
  • OneWire library, which makes it much easier to work with Arduino and DS18B20 sensor;
  • Sketch ...

You can download the Arduino IDE from the official Arduino website..

The OneWire Library can be downloaded from the OneWire Project Page (it is advisable to download the latest version of the library).

Equipment

  • At least one DS18B20 digital temperature sensor;
  • Arduino controller (in this example, Arduino Uno is used);
  • 3 connector;
  • Circuit board (Breadboard);
  • USB cable for connecting Arduino to a personal computer.

A USB cable is needed to program our Arduino. After, how do you “upload” the sketch to the board, can be connected to a separate power supply.

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Connecting DS18B20 to Arduino

The sensor is connected elementarily.

GND pin from DS18B20 connects to GND on Arduino.

Vdd pin from DS18B20 connects to +5V on Arduino.

Data pin from DS18B20 connects to any digital pin on Arduino. In this example, the pin is used 2.

The only thing, what needs to be added from the external additional strapping is a pull-up resistor on 4.7 KOm.

The wiring diagram for DS18B20 to Arduino is shown below (in the sketch, which will be given below, check the lines 10 and 65. They have pins, to which you connected the signal contact from the sensor and the power mode!):

Connecting DS18B20 to Arduino

The figure below is a photograph of our simple circuit "in life".

Connecting DS18B20 to Arduino 2

Parasitic and normal food

There is an alternative connection option - the so-called "stray" connection. In this case, we will not connect the +5V pin to the Vdd pin on the DS18B20 sensor.. Instead, we will connect the Vdd pin from the DS18B20 sensor to GND.. The advantages of such a connection are obvious: we only need two connectors!

The disadvantage of this connection is the limitation of the number of simultaneously connected sensors.. Connecting cables must be as short as possible!

All in all, with a “stray” connection, you need to be more careful and it’s better not to use it anyway. The results (temperature values) may be the most unexpected.

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Sketch for Arduino and DS18B20 sensor

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Install the OneWire Library

After you have downloaded the archive with the library, it needs to be imported. To do this, in the Arduino IDE, select "Sketch" - "Import Library" - "Add Library" and select the archive, which you downloaded. If you're having trouble, with library installation, read the instructions for installing libraries in Arduino.

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Uploading the sketch to the Arduino

Sketch, which is shown below, available in the OneWire library, in the examples category. Go to "File" - "Examples" - "OneWire" and select the example "DS18x20_Temperature". The program code is shown below.

This example uses the OneWire Library, for, to collect data from all connected DS28B20 temperature sensors (how to connect multiple sensors is described at the end of the article) and display them in the Arduino IDE serial monitor window.

In the serial monitor window, you will see something like this:

ROM = 28 88 84 82 5 0 0 6A

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Chip = DS18B20

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Data = 1 56 1 4B 46 7F FF A 10 D1 CRC=D1

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Temperature = 21.37 Celsius, 70.47 Fahrenheit

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

No more addresses.

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

ROM = 28 88 84 82 5 0 0 6A

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Chip = DS18B20

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Data = 1 56 1 4B 46 7F FF A 10 D1 CRC=D1

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Temperature = 21.37 Celsius, 70.47 Fahrenheit

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

No more addresses.

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

ROM = 28 88 84 82 5 0 0 6A

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Chip = DS18B20

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Data = 1 56 1 4B 46 7F FF A 10 D1 CRC=D1

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Temperature = 21.37 Celsius, 70.47 Fahrenheit

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

No more addresses.

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Normal or parasitic nutrition?

DS18B20 can work in normal or in the so-called "stray" mode. In normal mode, you use 3 connector, in the "parasitic" mode - in its only 2.

You need to set the correct mode in the sketch, To take reliable readings from the sensor:

  • For "stray" mode in the line 65 must be specified: ds.write(0x44, 1Stepper motor driver (suitable UNL
  • For normal mode in line 65 specified: ds.write(0x44);

Make Sure, that you have entered the correct pins!

In line 10, where it says “OneWire ds(2);” pin is set, to which the data contact from the sensor is connected.

Pin used in this example 2, but the default pin values ​​in the OneWire example are set to 10. You can also use it.

#include <OneWire.h>
// example of using the OneWire DS18S20 library, DS18B20, DS1822
OneWire ds(2); // and pine 10 (need resistor 4.7 КОм)
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
delay(250);
return;
}
Serial.print("ROM =");
for( i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(addr[i], HEX);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println();
// первый байт определяет чип
switch (addr[0]) {
case 0x10:
Serial.println(" Chip = DS18S20"); // или более старый DS1820
type_s = 1 0 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44); // start the transformation, используя ds.write(0x44,1) с "паразитным" питанием
delay(1000); // 750 may be enough, and maybe not enough
// we could use ds.depower() here, но reset позаботится об этом
present = ds.reset();
ds.select(addr);
ds.write(0xBE);
Serial.print(" Data = ");
Serial.print(present,  0; i < 9; i++) { // We need 9 байт
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println();
// convert data to actual temperature
// since the result is 16 bit integer, it must be stored in
// переменной с типом данных "int16_t", which is always equal 16 bitam,
// даже если мы проводим компиляцию на 32-х битном процессоре
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3Wiring diagram: // permission 9 бит по умолчанию
if (data[7] == 0x10) {
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// for small values, small bits are not defined, давайте их обнулим
if (cfg == 0x00) raw = raw & ~7; // permission 9 page, 93.75 мс
else if (cfg == 0x20) raw = raw & ~3; // permission 10 page, 187.5 мс
else if (cfg == 0x40) raw = raw & ~1; // permission 11 page, 375 ms
//// default resolution is 12 page, conversion time - 750 мс
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
Serial.print(fahrenheit);
Serial.println(" Fahrenheit");
}

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

How to connect multiple DS18B20 sensors to Arduino?

You can connect multiple DS18B20 digital temperature sensors in parallel. At the same time, the OneWire library will allow you to read data from all sensors at the same time.

Two methods for connecting sensors are described below..

For a large number of sensors (more 10), Resistors with lower resistance should be used (eg., 1.6 kOhm or even less).

Besides, if you connect more than 10 sensors, problems may arise (errors when taking readings). Therefore, it is recommended to install an additional resistor with a resistance of 100 ... 120 Ohm between the data pin on the Arduino and the data pin on each sensor!

The result of the previous sketch with two sensors connected might look something like this:

ROM = 28 88 84 82 5 0 0 6A

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Chip = DS18B20

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Data = 1 51 1 4B 46 7F FF F 10 FE CRC=FE

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Temperature = 21.06 Celsius, 69.91 Fahrenheit

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

ROM = 28 DA CA 27 5 0 0 49

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Chip = DS18B20

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Data = 1 4E 1 4B 46 7F FF 2 10 D9 CRC=D9

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Temperature = 20.87 Celsius, 69.57 Fahrenheit

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

No more addresses.

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Choosing the right sensor

It would be nice to know, from which sensor you receive data, when you use several sensors in parallel. How to do it?

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Serial number

Since the sensors are digital, each one has an individual serial number, which can be used to identify a particular sensor. Everything seems to be simple. But ... we need to pre-determine these serial numbers, before using them to identify the sensor, right?

You could see in the examples above, that the sketch gives us data in the form of a 64-bit serial number - the value “ROM”. For example:

28 88 84 82 5 0 0 6And or 28 DA CA 27 5 0 0 49 in the example above.

Do not forget, if you use a large number of sensors at the same time (10 or more), need to add resistors 100 … 120 Ohm between the data pins from the DS18B20 sensor and the data pin on the Arduino (for each sensor!).

чтение карты if ( ! mfrc522.PICC_ReadCardSerial()) return;

Normal diet

The diagram below shows a parallel connection of several sensors using three pins..

Normal diet

Parasitic power mode

In "stray" mode, the Vdd contact remains virtually unused. The sensor is powered via the data contact.

Parasitic power mode

Leave your comments, questions and share your personal experience below. In the discussion, new ideas and projects are often born!

Link to source: http://arduino-diy.com/arduino-tsifrovoy-datchik-temperatury-DS18B20