Temperature sensor ds. About temperature sensors DS18B20. Interaction with the control system

The temperature sensor in Arduino is one of the most common types of sensors. There are many options available to the Arduino thermometer project developer. different options, differing in the principle of operation, accuracy, design. The DS18B20 digital sensor is one of the most popular temperature sensors, often used in a waterproof housing to measure the temperature of water or other liquids. In this article you will find a description of the ds18b20 sensor in Russian, together we will consider the features of connecting to an arduino, the principle of operation of the sensor, a description of libraries and sketches.

DS18B20 is a digital temperature sensor with many useful features. In fact, the DS18B20 is a whole microcontroller that can store the measurement value, signal the temperature beyond the set limits (we can set and change the limits), change the measurement accuracy, the way of interacting with the controller, and much more. All this in a very small package, which is also available in a waterproof version.

The DS18B20 temperature sensor has a variety of housing types. You can choose from three - 8-Pin SO (150 mils), 8-Pin µSOP, and 3-Pin TO-92. The latter is the most common and is made in a special waterproof housing, so that it can be safely used under water. Each sensor has 3 pins. For the TO-92 case, you need to look at the color of the wires: black - ground, red - power and white / yellow / blue - signal. In online stores, you can buy a ready-made DS18B20 module.

Where to buy a sensor

Naturally, the DS18B20 is the cheapest to buy on Aliexpress, although it is also sold in any specialized Russian online stores with arduino. Here are some links for examples:

Sensor memory consists of two types: operational and non-volatile - SRAM and EEPROM. The latter contains the configuration registers and the TH, TL registers, which can be used as general-purpose registers if they are not used to indicate the range of allowable temperature values.

The main task of the DS18B20 is to determine the temperature and convert the result into digital form. We can independently set the required resolution by setting the number of bits of precision - 9, 10, 11 and 12. In these cases, the resolutions will be respectively equal to 0.5C, 0.25C, 0.125C and 0.0625C.

The received temperature measurements are stored in the SRAM of the sensor. Bytes 1 and 2 store the received temperature value, 3 and 4 store the measurement limits, 5 and 6 are reserved, 7 and 8 are used for high-precision temperature detection, the last 9 bytes store the noise-resistant CRC code.

Connecting DS18B20 to Arduino

DS18B20 is a digital sensor. Digital sensors transmit the value of the measured temperature in the form of a certain binary code, which is fed to the digital or analog pins of the arduino and then decoded. The codes can be very different, ds18b20 works on the 1-Wire data protocol. We will not go into the details of this digital protocol, we will only indicate necessary minimum to understand the principles of interaction.

The exchange of information in 1-Wire occurs due to the following operations:

  • Initialization - definition of the sequence of signals from which the measurement and other operations begin. The master device sends a reset pulse, after which the sensor must give a presence pulse, indicating that it is ready to perform the operation.
  • Data writing - a data byte is being transferred to the sensor.
  • Reading data - a byte is received from the sensor.

To work with the sensor, we need software:

  • Arduino IDE
  • OneWire library, if you use multiple sensors on the bus, you can use the DallasTemperature library. It will run on top of OneWire.

From the equipment you will need:

  • One or more DS18B20 sensors;
  • Arduino microcontroller;
  • connectors;
  • 4.7 kOhm resistor (if one sensor is connected, a resistor with a value of 4 to 10K will go);
  • Circuit board;
  • USB cable for connecting to a computer.

The sensor is connected to the Arduino UNO board simply: GND from the temperature sensor is connected to Arduino GND, Vdd is connected to 5V, Data is connected to any digital pin.

The simplest connection diagram for the DS18B20 digital sensor is shown in the figure.

The algorithm for obtaining information about the temperature in the sketch consists of the following steps:

  • Determining the address of the sensor, checking its connection.
  • A command is sent to the sensor with the requirement to read the temperature and put the measured value into the register. The procedure takes longer than the rest, it takes about 750 ms.
  • A command is given to read information from the register and send the received value to the "port monitor",
  • If required, it will be converted to Celsius/Fahrenheit.

Simple sketch example for DS18B20

The simplest sketch for working with a digital sensor is as follows. (in the sketch we use the OneWire library, which we will talk about in more detail a little later).

#include /* * Description of interaction with digital sensor ds18b20 * Connecting ds18b20 to arduino via pin 8 */ OneWire ds(8); // Create a OneWire object for the 1-Wire bus, which will be used to work with the sensor void setup()( Serial.begin(9600); ) void loop()( // Determine the temperature from the DS18b20 sensor byte data; // Location for the temperature value ds.reset(); // Start the interaction by resetting all previous commands and parameters ds.write(0xCC); // Command the DS18b20 sensor to skip address lookup. In our case, only one device ds.write(0x44) ; // We give the DS18b20 sensor a command to measure the temperature. We do not receive the temperature value itself yet - the sensor will put it in internal memory delay(1000); // The microcircuit measures the temperature, and we are waiting. ds.reset(); // Now we are preparing to receive value of the measured temperature ds.write(0xCC); ds.write(0xBE); // Please send us the value of the registers with the temperature value // Get and read the response data = ds.read(); // Read the low byte of the temperature value data = ds.read(); // And now the older one // Forming the final value ie: // - first "glue" the value, // - then multiply it by a factor corresponding to the resolution (for 12 bits, the default is 0.0625) float temperature = ((data<< 8) | data) * 0.0625; // Выводим полученное значение температуры в монитор порта Serial.println(temperature); }

Sketch for working with the ds18b20 sensor without delay

You can slightly complicate the program for ds18b20 to get rid of the slowdown in the sketch.

#include OneWire ds(8); // OneWire object int temperature = 0; // Global variable for storing the temperature value from the DS18B20 sensor long lastUpdateTime = 0; // Variable to store the time of the last reading from the sensor const int TEMP_UPDATE_TIME = 1000; // Determine the frequency of checks void setup()( Serial.begin(9600); ) void loop()( detectTemperature(); // Determine the temperature from the DS18b20 sensor Serial.println(temperature); // Print the received temperature value // T (because the temperature variable is of type int, the fractional part will simply be discarded) int detectTemperature()( byte data; ds.reset(); ds.write(0xCC); ds.write(0x44); if (millis() - lastUpdateTime > TEMP_UPDATE_TIME) ( lastUpdateTime = millis(); ds.reset(); ds.write(0xCC); ds.write(0xBE); data = ds.read(); data = ds.read(); // Generate value temperature = (data<< 8) + data; temperature = temperature >> 4; } }

DallasTemperature Library and DS18b20

In our sketches, we can use the DallasTemperature library, which simplifies some aspects of working with the ds18b20 sensor over 1-Wire. Sketch example:

#include // Arduino pin number with sensor connected #define PIN_DS18B20 8 // Create object OneWire OneWire oneWire(PIN_DS18B20); // Create a DallasTemperature object for working with sensors, passing it a reference to an object for working with 1-Wire. DallasTemperature dallasSensors(&oneWire); // Special object for storing device address DeviceAddress sensorAddress; void loop(void)( // Request for temperature sensor measurements Serial.print("Measuring temperature..."); dallasSensors.requestTemperatures(); // Asking ds18b20 to collect data Serial.println("Done"); // Request to get the stored temperature value printTemperature(sensorAddress); // Delay so that something can be parsed on the screen delay(1000); ) // Auxiliary function for printing the temperature value for the device void printTemperature(DeviceAddress deviceAddress)( float tempC = dallasSensors.getTempC(deviceAddress); Serial.print("Temp C: "); Serial.println(tempC); ) // Helper function to display sensor address ds18b20 void printAddress(DeviceAddress deviceAddress)( for (uint8_t i = 0; i< 8; i++) { if (deviceAddress[i] < 16) Serial.print("0"); Serial.print(deviceAddress[i], HEX); } }

OneWire library for working with DS18B20

DS18B20 uses the 1-Wire protocol to exchange information with arduino, for which an excellent library has already been written. You can and should use it so as not to implement all the functions manually. . To install the library, download the archive, unpack it into the library folder of your Arduino directory. The library is included using the #include command

All DS18B20 sensors are connected in parallel, one resistor is enough for all of them. Using the OneWire library, you can simultaneously read all data from all sensors. If the number of connected sensors is more than 10, you need to select a resistor with a resistance of not more than 1.6 kOhm. Also, for a more accurate temperature measurement, you need to put an additional 100 ... 120 Ohm resistor between the data output on the Arduino board and data on each sensor. You can find out from which sensor a particular value was obtained using a unique 64-bit serial code that will be issued as a result of the program execution.

To connect temperature sensors in normal mode, you need to use the circuit shown in the figure.

conclusions

The Dallas DS18B20 chip is very interesting device. Temperature sensors and thermometers based on it have characteristics acceptable for most tasks, advanced functionality, and are relatively inexpensive. The DS18B20 has gained particular popularity as a waterproof device for measuring the temperature of liquids.

For additional features, you have to pay with the relative complexity of working with the sensor. To connect the DS18B20, we will definitely need a resistor with a rating of about 5K. To work with the sensor in Arduino sketches, you need to install an additional library and get certain skills to work with it - everything is not entirely trivial there. However, you can buy a ready-made module, and for a sketch, in most cases, the simple examples given in this article will suffice.

In the process of studying microcontrollers, sooner or later it becomes necessary to measure such a meteorological parameter environment like her temperature. The modern global market for electronic components offers a wide range of temperature sensors. The main differences between them are in the range of measured temperature, supply voltage, application area, overall dimensions, temperature conversion methods, interface for interaction with the user control system. It so happened historically that at the moment one of the most popular temperature sensors is the sensor DS18B20 Dallas Semiconductor Corp. The following story is about him.

DS18B20– digital temperature sensor with programmable conversion resolution.

Distinctive features:

1) Using the 1-Wire interface data bus to interact with the control system;
2) The presence of a unique 64-bit serial identification code located in the internal ROM-memory and intended for multipoint systems where it is necessary to address a specific sensor;
3) The supply voltage is 3-5.5V, which allows it to be used not only in 5-volt systems, but also in 3.3 (most microcontrollers);
4) The range of the measured temperature is -55…+125 о С;
5) Accuracy of ± 0.5 ° C, although this is true only for the range -10 ... + 85 ° C;
6) The resolution of the conversion is determined by the user and is 9…12 bits;
7) Has internal registers of triggers of the upper and lower thresholds with the generation of an alarm signal for systems using thermostatic logic of operation;
8) These sensors are software compatible with DS1822 and are widely used in industrial thermostatic controllers, industrial systems, consumer electronics and other temperature sensitive systems.

Description and principle of operation of the device:

In my article, I will describe an example of working with a sensor made in the TO-92 package.

It looks like this:

Inside this contraption is arranged very simply, take a look for yourself:

Let's take a closer look at this block diagram.

However, powering in this way introduces some restrictions on the time parameters of the sensor. Holding the data line for a while will discharge the capacitor, which will lead to a de-energization of the INTERNAL Vdd line, and, accordingly, the sensor as a whole. Therefore, during unused time, the DQ line must be held high. One important remark should be noted. When converting temperature and copying data from Scratchpad to EEPROM (to one of the registers), the current consumed by the INTERNAL Vdd line can reach 1.5 mA, which is unbearable for the internal capacitor, and there will be a large voltage drop on the pull-up resistor, which will unacceptably affect the operation of the device in in general. To do this, it is necessary to organize the DQ lines in a powerful pull-up scheme, implemented according to the following scheme:

After issuing the command ConvertT or CopyScratchpad it is necessary to turn on a powerful pull-up of the DQ line with a MOSFET transistor no later than 10 μs (max), according to the sensor developers, and then wait for the conversion time (Tconv) or data transfer time (Twr = 10 ms), and at this time no action is taken when the powerful pull-up is on on the DQ line should not be!

Little needs to be said about standard power, because everything is simple here, and even a MOSFET is not needed at all:

The "64-BIT ROM AND 1-Wire PORT" subsystem contains a unique 64-bit serial identification code located in the non-volatile ROM memory, and this node also contains an interface for interaction with the 1-Wire control system. The “Memory Control Logic” subsystem carries out data transfers between the 1-Wire interface subsystem and the Scratchpad type memory, which, in turn, has access to the temperature sensor registers, the registers for setting the upper and lower alarm thresholds, the configuration register and the generator register 8- bit checksum to protect the system from incorrect data.

When powered on, the sensor defaults to 12-bit conversion resolution and immediately enters the low power mode. To initiate the conversion, the master must send the command ConvertT . After converting the temperature into a digital code, this code is stored in the Scratchpad memory as a two-byte word, and the sensor switches back to power-saving mode.

Temperature conversion.

Now let's figure out how the temperature is converted in the sensor. In fact, the ADC is located inside the temperature sensor itself, and the output data located in the temperature register is transferred to Scratchpad memory. The temperature data has the following format:

The S flag is a sign flag, used to indicate the sign of a number (S=0 is the number contained in bits 10-0 is positive, and S=1 if the number contained in the same bits is negative, i.e. in this case temperature is represented in two's complement code (two's complement code)).

When set to 12-bit conversion resolution, all 12 bits (bit 11-bit 0) are enabled and contain valid data. When set to 11 bits, the contents of bit 0 should be ignored, when set to 10 bits, bits 0 and 1 should not be taken into account, and so on.

The alarm signal is a function of the thermostat.

For this, 2 8-bit registers, Th and Tl, are provided. Th contains the value of the upper temperature threshold, and Tl, respectively, the lower one. If the temperature is above Th or below Tl, an alarm flag is set. This alarm flag is detected by the master by issuing the command Alarm Search to the DQ line. The alarm flag is updated after each temperature conversion operation. Incidentally, only bits 11 to 4 of the temperature register are used in comparison to the Th or Tl register, which means that the thermostat function only works for integer temperatures. The registers are physically EEPROM memory, so they retain their values ​​when the power is turned off. The registers themselves are similar to the temperature register, only they are 8-bit, the S flag has exactly the same meaning as in the previous case:

This code, as noted earlier, is necessary to identify each device on the line in multipoint temperature measurement systems.

The format of this memory is:

The lower 8 bits are reserved for the family designation, and contain the value 0x28. The next 48 bits contain the unique serial number of the device. The most significant byte contains the value of the CRC checksum calculated for the lower 56 bits of the ROM memory.

Organization of memory.

The sensor's memory consists of a Scratchpad memory space and an EEPROM for storing configuration data and high and low alarm register values.

When the power is turned off, data bytes 2, 3 and 4 retain their value in the EEPROM. Well, when turned on, the value in them remains unchanged. Bytes 0 and 1 contain the value of the converted temperature, bytes 5, 6, 7 are reserved for internal use and cannot be accessed by the user for his needs.

The 8th byte contains the value generated by the built-in CRC generation logic for bytes 0 to 7, which minimizes the possibility of erroneous temperature readings in the end.

It should be noted that if the thermostat function is not used, then the Th and Tl registers can be used as general purpose memory - you can store any information in them.

Data is written to bytes 2, 3 and 4 starting with the least significant bit of byte 2 using the instruction Write Scratchpad. To check the integrity of the recorded data, you can read them, for which you need to send the command to the sensor Read Scratchpad, after which the master should receive data starting from the least significant bit of byte 0.

To store the data of the high, low registers of the thermostat, as well as the configuration register in EEPROM-memory, the master device must send the command to the sensor Copy Scratchpad.

As noted earlier, data already written to the EEPROM is retained when the power is turned off. But when the power is turned on, the values ​​from the corresponding EEPROM cells are automatically loaded into the corresponding scratchpad memory registers. Convenient, isn't it? :)

In addition, data written to EEPROM can be overwritten to scratchpad memory at any time. This is necessary, for example, when you change the configuration during operation, and then you need to switch to the “normal mode”, i.e. return the operation configuration that was before the change in the contents of the scratchpad memory registers. Actually, for this, the master device must send the command to the sensor Recall E2 .

In the configuration register, only 2 bits can be defined by the user: R0 and R1. These bits determine the temperature conversion resolution, and are set to 1 by default, which is the initial setting for 12-bit conversion resolution.

All possible configurations of these bits and the corresponding permissions are shown in the table below. It should be noted that the larger the conversion resolution, the longer the conversion time, for example, for 12-bit resolution, the conversion time is 750ms (max.).

Interaction with the control system.

DS18B20, as noted earlier, uses a 1-Wire interface data bus to communicate with a slave device. Therefore, to connect it, the control system must provide an output with an open drain or with a Hi-Z line state.

The internal configuration of the sensor interface is shown below:

In the inactive state (idle state), the DQ line is pulled up by a resistor to the "+" power supply. Thus, between transactions (data transfers), this line must always be held in this state. If, for any reason, transactions must be suspended, the DQ line must be held high if this transfer is to be resumed further. In the process of stopping the transaction, we can keep the DQ line in a high logical level for as long as we like, starting from 1 µs. But, if the data bus is held low for longer than 480 µs, a complete reset of all sensors present on this bus will occur.

The sequence of operations for the exchange.

Each time the control system accesses the sensor, the following sequence of actions must be observed:

1) Initialization;
2) ROM command (followed by the necessary data exchange);
3) Sensor function command (followed by required communication).

If there is no step when accessing the sensor, the sensor will not respond. The exception is the commands SearchROM [ F0 h] And alarmSearch [ ECH] , after their execution, the master must return to the first step of the control sequence.

So. All transactions start with initialization. This operation is followed by the generation of a reset pulse by the master, upon which the slaves (in this case, the sensor(s)) transmit a presence pulse to the master, which let it know that the sensors are connected and ready for operation.

In general, the 1-Wire interface bus implemented in the sensor defines several types of signals on the data line: reset pulse, presence pulse, write 0, write 1, read 0, read 1. All these operations are implemented by the master device, with the exception of the presence pulse. It is formed only by the sensor(s).

So, for starters, the master goes into transmitter mode and sets the DQ line to 0 for at least 480 µs (highlighted in bold black). This resets the sensor. Then the line must be released and the master device must be put into the receiver mode, while the pull-up resistor will set the data line to a logic high level (highlighted in thin black). After the sensor senses a rising edge, the sensor will wait 15-60µs and reset the data line to 0 with its hardware interface, and will keep it for 60-240µs. After this time, the sensor will release the line and it will be set to the logic 1 level for at least 480 µs after the sensor detects the reset pulse.

Now let's talk about how the data transfer process is carried out. Generally, bit transfers. The point is the following. A period of time is taken, and during this time the master looks at what we have on the line, let's say 1 - it means we wrote down 1, if 0 - it means we wrote down zero. But this is only an abstract explanation. In fact, there are some nuances associated with the time frame of this whole case.

See pictures:

It all starts with the fact that the master must lower the data line to a logic low level, and from that moment the 1/0 write / read slot begins, lasting from 60 to 120 μs. Between the write / read slots, the data line must be set to 1 for a time not less than the recovery time (1 μs). To organize a write slot 0, it is necessary to keep the data line at 0 all the time of the slot, but if it is necessary to write to the sensor 1, then first reset the data line to 0, then wait at least 1 μs and release the line at 1, during the write slot 1 (60- 120µs) will write 1 to the sensor (see upper right figure).

As a matter of fact, if 1 is detected on the data line within 15-60 µs after the start, then 1 will be written, and if 0 is detected within 60-240 µs, then 0 will be written.

The data reading is accompanied by the master device, when it resets the line, waits for at least 1 µs, and for 15 µs looks at what is happening on the line: if 0 remains, then the sensor transmits 0, if it switched to 1, then 1 was transmitted.

Teams.

ROM commands.

These commands must follow the initialization sequence and contain instructions for finding the appropriate sensor, etc. The capacity of each instruction is 8 bits. After executing the appropriate command, you can send a function command to the sensor.

SEARCH ROM

When the system is initially connected, it must recognize all devices connected to the bus. That's what this command is for. But, since we have only one sensor, we will not use this command.

READ ROM

This command is used only when there is only one sensor on the bus. This allows the master device to read the contents of 64 bits of ROM memory without using the find command. And if you try to use this command with more than 1 connected sensors, all of them will start transmitting the contents of this memory, which will lead to undesirable consequences.

MATCH ROM

This is the ROM match command. The master releases 64 bits of the corresponding ROM of the sensor connected to the bus, and it already determines what to do with it (measure the temperature, etc.). Other sensors on the bus will wait for their turn at this time.

SKIP ROM

This is the ROM skip command. Does not take into account the address of any particular sensor on the bus, but addresses all at once. After this command, you can issue, for example, a temperature conversion command, and all sensors will start the conversion. However, issuing a read memory command after calling this command will lead to unpredictable results (because all sensors will transmit data at once). This means that only with one connected sensor such a situation is possible.

ALARM SEARCH

This command is identical to the first one in this table, except that it searches for sensors on the bus with the alarm flag set.

functional commands.

These commands carry out the functional operations of any processes, for example, starting the temperature conversion operation, copying the memory, etc. There are 6 commands in total, each bit length is 8 bits.

CONVERT T

Start temperature conversion. After executing this command, 2-byte data is entered into the temperature register.

WRITE SCRATCHPAD

Writes data to registers 2-4 starting from the second, least significant bit first. During the transfer of data to the three registers, care must be taken that the master does not reset the sensors, because data loss is possible.

READ SCRATCHPAD

Initiates a data transfer process for all scratchpad memory registers, starting with the low bit of byte 0 and ending with the high bit of byte 8 (CRC).

COPY SCRATCHPAD

This instruction copies the contents of byte registers 2, 3, and 4 to the appropriate EEPROM locations.

RECALL E2

This command copies the data from the EEPROM to the appropriate locations in the scratchpad. As noted earlier, this operation occurs automatically when the power is turned on.

READ POWER SUPPLY

That, in fact, is the whole wisdom of working with the DS18B20 temperature sensor. For more detailed information, please refer to the datasheet (). Now it is necessary to implement all this business in iron.

Schematic diagram of the device:

Assembly drawing of the printed circuit board (sorry for the quality, I just did it to work, for debugging):

Don't forget to mirror the board correctly

Since this is a breadboard, I pulled it out of the old project, so on the board above it’s a little different from what I have (on mine, I now removed everything superfluous and it became exactly like in the pictures above).

Here's what happened to me:

It turned out to be a sandwich

The source code of the program was written in the development environment. I did not try to use the maximum of ready-made avr-gcc compiler libraries, but wrote everything, as they say, “by hand”. My goal is not to demonstrate C virtuosity, but just an example written in an hour that can give beginners a general idea of ​​\u200b\u200bworking with the sensor.
The device is intended for use in a room, therefore it does not provide for the measurement of negative temperatures.

Download sources and printed circuit board LAY you can below

Any additional questions or suggestions are welcome at: [email protected]

This sensor uses exclusively the 1-Wire protocol - this forms a connection that communicates on the bus using only one control signal. The bus must be connected to the power supply through a pull-up resistor.

Specifications DS18B20
ParameterMeaning
IC Output TypeDigital
Sensing Accuracy Range± 0.5°C
temperature sensing range-55°C to +125°C
supply current1mA
Supply Voltage Range3V to 5.5V
Resolution (Bits)9...12
Sensor Case StyleTO-92
no. of Pins3
base number18
Operating Temperature Max85°C
Operating Temperature Min-10°C
operating temperature range-10°C to +85°C
output current4mA
output typeDigital
Package / CaseTO-92
Resolution9...12
Sensor / Transducer Typetemperature
Supply Voltage Max5.5V
Supply Voltage Min3V
Termination TypeHole
Operating temperature, °C0...+55
Relative humidity of operation, %...55
ProductionDallas/Maxim
Warranty period of operation12 months from date of purchase
Weight, g10

DS1820, DS18S20, DS18B20- popular digital temperature sensors from DALLAS-MAXIM with a single-wire 1-Wire interface. Due to the ambiguity of markings and the abundance of circuits on these digital thermal sensors that appeared in amateur radio literature, we consider it necessary to give some explanations.
Chip DS1820 discontinued and a microcircuit is recommended for its replacement DS18S20. However, it should be noted that microcircuits DS18S20 in TO-92 package are labeled "DS1820" (without letter S). New chip DS18S20 software compatible with old DS1820 and, according to the manufacturer, in most cases it can be a direct replacement for the old DS1820. Perhaps the manufacturer wanted to indicate this compatibility by marking without the letter S. Software compatibility of the new DS18S20 with the old DS1820 is guaranteed if the program uses the algorithm from the data sheet.
As can be seen from the table, the new DS18S20 chip is made in a standard TO-92 package, while the old DS1820 had an elongated package. On this basis, you can also make sure that the sellers do not “push” you with an outdated microcircuit.
The DS18B20 chip always has the corresponding marking "DS18B20" and cannot be changed to DS1820/DS18S20 and vice versa without changing the program code.

Specifications digital sensors Maxim temperatures
Sensor typeDS1820 DS18S20DS18B20
MarkingDS1820 DS1820DS18B20
FramePR-35
(extension TO-92)
TO-92TO-92
Bit depth9-bit 9-bit9...12bit
Conversion time200mS (typ.)
500nS(max)
750nS (max)750nS(max)
Measurement accuracy ±0.5%
in the temperature range
0 ….+70°С -10 ….+85°С-10 ….+85°С
Supply voltage
for measuring accuracy ±0.5%
4.3-5.5V 3.0-5.5V3.0-5.5V
Description

The article provides detailed description integrated temperature sensor DS18B20 in Russian. The information was translated into Russian from the official documentation of the sensor manufacturer, Dallas Semiconductor.

General description.

DS18B20 is digital meter temperature, with 9 to 12 digit conversion resolution and temperature monitoring alarm function. Control parameters can be set by the user and stored in the non-volatile memory of the sensor.

DS18B20 communicates with the microcontroller via a single-wire communication line using the 1-Wire interface protocol.

The temperature measurement range is -55 to +125 °C. For the range from -10 to +85 °C, the error does not exceed 0.5 °C.

Each DS18B20 chip has a unique 64-bit serial code that allows multiple sensors to be connected to one common communication line. Those. through one port of the microcontroller, it is possible to exchange data with several sensors distributed over a considerable distance. The mode is extremely convenient for use in environmental control systems, temperature monitoring in buildings, equipment nodes.

Briefly about the features of the DS18B20.

  • For a single-wire 1-Wire interface, one communication port with the controller is sufficient.
  • Each device has a unique 64-bit serial code.
  • Possibility to connect several sensors via one communication line.
  • No need for external components.
  • Ability to receive power directly from the communication line. Supply voltage within 3.0 V ... 5.5 V.
  • Temperature measuring range -55 ... +125 °C.
  • The error does not exceed 0.5 °C in the range -10 ... +85 °C.
  • Conversion resolution 9 ... 12 bits. Set by the user.
  • The measurement time does not exceed 750 ms, with the maximum possible resolution of 12 bits.
  • Possibility of programming alarm parameters.
  • The alarm signal transmits data about the address of the sensor whose temperature is out of the specified limits.
  • Software compatibility with DS1822.
  • Extremely wide areas of application.

Assignment of conclusions.

Overview of the DS18B20 sensor.

Figure 1 is a block diagram of the DS18B20 sensor. The 64-bit ROM stores the device's unique serial code. RAM contains:

  • measured temperature value (2 bytes);
  • upper and lower alarm thresholds (Th, Tl);
  • configuration register (1 byte).

Through the configuration register, you can set the conversion resolution of the temperature sensor. The resolution can be set to 9, 10, 11 or 12 bits. The configuration register and alarm thresholds are stored in non-volatile memory (EEPROM).

Mode - temperature measurement.

The main function of the DS18B20 is to convert the temperature of the sensor into a digital code. The conversion resolution is set to 9, 10, 11, or 12 bits. This corresponds to a resolution of 0.5 (1/2) °C, 0.25 (1/4) °C, 0.125 (1/8) °C and 0.0625 (1/16) °C. On power-up, the state of the configuration register is set to 12 bits of resolution.

After power up, the DS18B20 is in a low-power dormant state. To initiate a temperature measurement, the master (microcontroller) must issue a TEMPERATURE CONVERSION command. After the conversion is completed, the result of the temperature measurement will be in 2 bytes of the temperature register, and the sensor will again go to rest.

If DS18B20 is connected according to the scheme with external power supply, the master can monitor the status of the convert command. To do this, it must read the state of the line (perform a temporary read slot), upon completion of the command, the line will go into a high state. The line is held low while the convert command is being executed.

DS18B20 measures temperature in degrees Celsius. The measurement result is presented as a 16-bit, signed number in an additional code (Fig. 2.) . The sign bit (S) is 0 for positive numbers and 1 for negative numbers. With a resolution of 12 bits, the temperature register has all significant bits, i.e. have valid values. For a resolution of 11 bits, bit 0 is not defined. For a resolution of 10 bits, bits 0, 1 are not defined. With a resolution of 9 bits, bits 0, 1, and 2 have an invalid value. Table 2 shows examples of how digital codes correspond to a temperature value.

For people who are not experienced in binary mathematics, I will write that to calculate the temperature you need:

  • At positive value(S=0) Convert the code to decimal and multiply by 0.0625 °C.
  • With a negative value (S=1), you must first convert the two's complement code into a direct one. To do this, invert each digit of the binary code and add 1. And then convert to decimal and multiply by 0.0625 ° C.

Mode - transmission of an alarm signal.

After executing the temperature conversion command, the measured value is compared with the upper and lower thresholds from the Th, Tl registers (format in Figure 3). These are byte values, signed, in two's complement, S=0 means the number is positive, and S=1 means negative. Threshold values ​​are stored in non-volatile memory (EEPROM). Th and Tl are available for reading and writing through bytes 2, 3 of RAM. More about this in the section.

Due to the different lengths of the TH, TL and temperature registers, they only compare against bits 11 to 4 of the temperature register. If the value of the measured temperature exceeds TH or is lower than TL, then an alarm symptom is generated in DS18B20. The attribute is overwritten with each temperature measurement, and if the temperature returns within the specified limits, it is reset.

The master can check the status of the alarm symptom using the SEARCH FOR ALARM command. Any sensor with an active flag will respond to the search command. In this way, the wizard will determine exactly which DS18B20 is generating the alarm. After changing the values ​​of the TH and TL registers, only the next temperature conversion will generate a valid alarm indication.

Power supply for the DS18B20 temperature sensor.

However, when the DS18B20 performs a temperature conversion or memory copy to EEPROM operation, the current consumption may reach 1.5mA. Such a current can cause a decrease in the supply voltage of the device to an unacceptable value. The pull-up resistor current and the energy stored in Cpp is not enough to power these two modes. In order to ensure sufficient power to the device, it is necessary to provide a powerful pull-up of the bus to a high level at the time when the temperature conversion or memory data copying to EEPROM is in progress. This can be done with a MOSFET, as shown in the schematic (Figure 4). The data bus must be connected to a powerful power supply:

  • within 10 µs after the commands CONVERT and COPY MEMORY ;
  • during the conversion time (tconv) and data transfer (not less than t WR =10ms).

No other operations can be allowed on the bus during this time.

As a rule, for modern microcontrollers, the output current of a high level is quite enough to power the DS18B20. Then there is no need for a MOSFET.

To power the DS18B20, the usual method can be used - connecting an external power supply through the V DD pin (Figure 5). The obvious advantages of this method are that there is no need for a MOSFET and that the bus remains free during conversion and can be used for other purposes.

I, in such cases, use the following DS18B20 connection diagram.

In this circuit, the temperature sensor operates in the external power supply mode, which is stored on an additional capacitor through a diode. The circuit works fine in my devices.

The 64-bit serial code of the device.

sensor memory.

The organization of the DS18B20 memory is shown in Figure 7. All memory includes random access (SRAM) and non-volatile (EEPROM) memory. The EEPROM stores the TH, TL registers and the configuration register. If the alarm function is not used, the TH and TL registers can be used as general purpose registers. All memory management commands are described in detail in the section.

Bytes at addresses 0 and 1 store the low and high bytes of the measured temperature register. These bytes are read-only. 2nd and 3rd bytes - TH and TL registers. Byte 4 is the configuration register. More details about this register in the CONFIGURATION REGISTER section. Bytes 5, 6, 7 are reserved, cannot be written and, when read, always return 1.

Byte 8 is read-only. It contains the cyclic code (CRC) for the first eight bytes. DS18B20 generates this code according to the method described in part .

Data is written to bytes 2, 3 and 4 by the WRITE MEMORY command. Data must be transmitted starting with the least significant bit of byte 2. To check the data write, the memory can be read with the READ MEMORY [code BEh] command. When reading, data is transferred over the bus, in sequence starting from the least significant bit of byte 0. Data TH, TL and the configuration register are written to EEPROM by the COPY MEMORY command.

When the power is turned on, data from the non-volatile EEPROM memory is reloaded into random access memory (SRAM). Reloading data from EEPROM can also be done with the RELOAD E 2 command. The master must monitor the state of the bus to determine if the reboot is complete. A low level read slot means that the reboot has not finished yet. Upon completion of the reboot, the DS18B20 hands over read slot 1.

Temperature sensor configuration register.

Byte 4 of the memory is the configuration register (format in Figure 8). Bits R0, R1 can set the conversion resolution (codes in table 3). When the power is turned on, the state of bits R0, R1 = 11, which corresponds to a resolution of 12 bits. It must be remembered that there is a direct dependence of the conversion time on the resolution. Bits 7 and 0…4 are reserved, cannot be used, they return 1 when read.

Cyclic code generation(CRC)

The cyclic code (CRC) bytes are located in the 64-bit ROM code and in the ninth byte of SRAM. The cyclic code from ROM is calculated for 56 bits of the ROM code and is located in the high byte of the ROM. The cyclic code from SRAM is calculated from bytes 0…7 of SRAM. The cyclic code allows you to control the correctness of reading data from the DS18B20. The master calculates the cyclic code for the received data and compares it with the received code. Based on this, a decision is made about the correctness of the data.

The generating polynomial of the cyclic code looks like this:

C R C = X 8 + X 5 + X 4 + 1

The wizard can calculate the cyclic code using the polynomial generator, as shown in Figure 9. It consists of a shift register and XOR gates. The shift register is initially in state 0. Bits enter the shift register, starting with the least significant bit, code from ROM or from SRAM, one bit per shift cycle. After shifting the 56th bit of ROM or the high bit of the 7th byte of SRAM, the calculated cyclic code will be in the shift register. If you shift 8 bits of ROM or SRAM received from DS18B20 into the generator, then in case of correct data, the shift register will contain all 0.

Single-wire interface 1-Wire

A 1-Wire bus system consists of one master device (MASTER) that controls one or more slave devices (SLAVES). DS18B20 can only be slave. A system with one slave is called a single point. Multi-slave system - multipoint. All commands and exchange data are transmitted over the bus with the least significant bit first. Further information on the 1-Wire interface is divided into three sections: hardware configuration, workflow, and signals (types and timing requirements).

hardware configuration.

The 1-Wire interface has one communication line. Each device (master or slave) is connected to the data bus with an open collector or tri-state output port. This configuration allows each device in the system not to occupy the communication line when it is not active, and keep the bus free for other devices. In the DS18B20 chip, the output (DQ) is an open drain. Its equivalent circuit is shown in Figure 10. The 1-Wire bus requires an external pull-up resistor of approximately 5 kΩ to provide a high signal level when the devices are inactive. If an operation is to be suspended, the bus must be set to idle until the next operation. The bus can be in a high level state for an arbitrarily long time. Driving the bus low for more than 480 µs will cause all system components to reset.

Sequence of operations.

The sequence of operations for accessing the DS18B20 temperature sensor looks like this.

  • Initialization.
  • ROM command (required for any communication).
  • Function command (required for any communication).

This sequence must be strictly observed. Otherwise, the DS18B20 will not respond to commands. ROM SEARCH [code F0h] and TROUBLE SEARCH [code ECh] are exceptions. After generating these two commands, the master device must return to the first step (initialization).

Initialization.

Bus communication always starts with an INITIALIZE operation. For initialization, the master generates a reset pulse, followed by a presence pulse from the slave. The presence pulse informs the master that the slave is present in the system and is ready to perform an operation. The reset and presence pulse timings are described in section .

ROM code commands.

After the master device receives a presence pulse, it can operate on ROM commands. These are commands for operations on the 64-bit individual codes of each slave. They allow the master to select a particular slave from many others. Also, using these commands, you can find out how many slave devices are connected to the bus, their types, select devices in alarm. There are 5 ROM instructions, each 8 bits long. The master must send a ROM command before executing the DS18B20 function commands. The block diagram of the execution of ROM commands is shown in Figure 11.

Search ROM

After power up, the master device must read the ROM codes of all slave devices connected to the bus. This will determine the number of slave devices and their types. The master device learns the ROM codes through the process of identifying the codes of each device on the bus. It must execute the search ROM command as many times as necessary to identify all slaves. With only one slave in the system, it is easier to use the READ ROM command. After searching for the ROM, bus operations must start again with initialization.

Reading ROM

The command is used in single point systems with one slave. It allows the host device to read the 64-bit ROM code without using the SEARCH ROM command. Using the READ ROM command in a multidrop system will result in data conflicts between slaves.

ROM match

The ROM MATCH command, followed by a 64-bit ROM code, allows the master to address a particular slave. Only one slave whose code matches the transmitted code will respond to the function commands. Other slaves will be inactive until the next reset pulse.

Skip ROM

The command allows the master device to access all devices on the bus at the same time, without using ROM codes. For example, you can run a temperature conversion operation on all devices by executing the ROM SKIP followed by TEMPERATURE CONVERSION. A READ MEMORY command can only follow a ROM SKIP command with one slave connected to the link. This sequence of commands significantly saves the exchange time with the sensors. It is especially effective when using a single slave device in the system.

Alarm search

The command is identical to the SEARCH ROM command. It differs in that only slaves in the alarm state will respond to it. The command allows the slave to determine which temperature sensors are in alarm since the last temperature conversion. After each ALARM SEARCH, it is necessary to return to INITIALIZATION.

Function Command Group

After executing the ROM command to select the DS18B20 with the desired code, the master device can send sensor function commands. They allow you to write and read data from the DS18B20 RAM, initiate temperature conversion and determine the power mode. The functional commands of the DS18B20 are described below, collected in Table 4, the algorithm for working with them is shown in Figure 12.

Temperature conversion

Memory recording

The command allows loading 3 bytes into the sensor's RAM. The first byte is written to the Th register (memory byte 2), the second byte to Th (memory byte 3), and the third byte to the configuration register (byte 4). The master transmits data from the least significant bit. All three bytes must be written before the master generates a reset signal.

Memory reading

The command is used to read the device's memory. The data transfer starts from the least significant bit of byte 0 of memory, and continues until all 9 bytes have been read. If only a portion of the data is needed, the host can abort the transmission by issuing a reset pulse.

Memory copy

The command reloads the values ​​of the Th, Tl registers and the configuration register from EEPROM to RAM. After sending the RESET command, the master can execute the read slot and the DS18B20 will report the reset status. Passing 0 will mean that the operation is still in progress, 1 - the operation is completed. The reset operation automatically occurs when the power is turned on. Therefore, the RAM contains valid data immediately after power is applied.

Read power mode

Table 4. DS18B20 function commands.

COMMAND DESCRIPTION THE CODE BUS OPERATIONS NOTE
TEMPERATURE CONVERSION COMMAND
Temperature measurement Initializes temperature measurement 44h DS18B20 sends the temperature conversion operation status to the master 1
MEMORY COMMANDS
Memory reading Reads all RAM, including cyclic CRC code BEh DS18B20 sends up to 9 bytes to master 2
Memory recording Writes bytes 2, 3 and 4 to RAM
(TH, TL and configuration register)
4Eh The master sends 3 bytes to the DS18B20. 3
Memory copy Copies TH, TL, and configuration register from RAM to EEPROM 48h 1
Reloads TH, TL, and configuration register from EEPROM to RAM. B8h DS18B20 sends reset status to master
Read power mode Informs the master about the power mode of the DS18B20. B4h DS18B20 send power mode to master

Notes.

1-wire interface

The DS18B20 uses the 1-Wire interface protocol for data exchange, which provides data integrity control. This protocol defines signals:

  • reset pulse,
  • presence momentum,
  • writing a bit with a value of 0,
  • write bit with value 1,
  • read bit with value 0,
  • read bit with value 1.

All these signals, except for the presence pulse, are generated by the master.

Initialization - reset and presence pulses

Any communication operation of the DS18B20 begins with an initialization sequence that consists of a reset pulse from the master to the slave and a presence response pulse from the DS18B20. This process is shown in Figure 13. The temperature sensor sends a presence pulse in response to the reset pulse to tell the master that it is connected to the bus and ready for use.

During the initialization sequence, the master transmits a reset pulse (Tx), driving the bus low for at least 480 µs. Next, the master releases the bus and enters receive (Rx) mode. When the bus is released, it is pulled up to a high logic level by a 5 kΩ resistor. The sensor detects a positive edge, waits 15-60 µs and transmits a presence pulse, holding the line low for 60-240 µs.

Temporary read and write slots.

Data exchange on the 1-Wire bus occurs in time slots (time slots). One timeslot carries one bit of information.

Temporary recording slots.

The protocol defines two types of data write time slots in the DS18B20: write value 1 and write value 0. The write slot duration is at least 60 µs with a recovery pause between slots of 1.0 µs as a minimum. Any write slot is initiated by the falling edge of the bus signal (Figure 14).

To form write slot 1, after driving the bus low, the master must release the bus for 15 µs. The 5 kΩ pull-up resistor will create a high level voltage on the bus.

To form a write slot 0, after driving the bus low, the master must continue to hold the bus low for the duration of the slot (at least 60 µs).

The DS18B20 checks the signal status between 15 and 60 µs, counting from the start of the write slot. The state of the bus on this segment corresponds to the value of the bit to be written to the sensor.

Temporary read slots.

The duration of the read slot, as well as the write slot, must be at least 60 µs with a recovery pause between slots of 1 µs, at least. Any read slot is initiated by the falling edge of the bus signal (Figure 14).

After the host has initialized the read slot, the DS18B20 transmits a data bit. For a transmission of 1, the sensor leaves the bus free (in a high state), and for a transmission of 0, it forms a low level on the bus.

When transmitting 0, the DS18B20 must release the bus at the end of the slot. The pull-up resistor will form a high level on it. The output of the DS18B20 is valid for 15 µs from the start of the read slot.

On fig. 15 shows that the total sum of the Tinit , TRC and TSAMPLE read slot times must be no more than 15 µs.

Rice. 16 shows that for maximum reliability of data reception, it is necessary to reduce Tinit and TRC and read the bus state at the end of the 15 µs segment.

Example 1 of working with DS18B20.

MASTER MODE

BUS DATA

EXPLANATIONS

TX reset RX Presence TX 55h TX 64-bit ROM code TX 44h The master sends a temperature conversion command. TX TX reset The master generates a reset pulse. RX Presence DS18B20 respond with a presence pulse. TX 55h The master executes the ROM code matching command. TX 64-bit ROM code Master sends ROM code DS18B20. TX BEh RX 9 data bytes

Example 2 of working with DS18B20.

MASTER MODE

BUS DATA

EXPLANATIONS

TX reset The master generates a reset pulse.
RX Presence
TX CCh
TX 4Eh The master executes a memory write command.
TX 9 bytes of data The master sends three bytes (TH, TL, and the configuration register).
TX reset The master generates a reset pulse.
RX Presence The DS18B20 responds with a presence pulse.
TX CCh The master issues a skip ROM command.
TX BEh The master sends a read memory command.
RX 9 data bytes The master reads all RAM, including the cyclic CRC code. It then calculates the CRC for the first eight bytes and compares it with the received code. If the codes are not equal, the master repeats the read operation.
TX reset The master generates a reset pulse.
RX Presence The DS18B20 responds with a presence pulse.
TX CCh The master issues a skip ROM command.
TX 48h The master executes a memory copy command.
TX DQ line connected to power rail The master connects the DQ to the power rail for the duration of the conversion.

Maximum permissible parameters DS18B20

The limit values ​​of the parameters are indicated. Exceeding these parameters is unacceptable. Operation for a long time at the limit values ​​of the parameters may reduce the reliability of the device.

Notes:

AC EEPROM electrical data (- 55 ... + 125 °C, V DD = 3.0 ... 5.5 V).

PARAMETER SYMBOL TERMS MIN. TYPE. MAX. ED. MEAS.
Write cycle time twr 2 10 ms
Number of records NEEWR -55°C - +55°C 50000 cycle
Storage time t EEDR -55°C - +55°C 10 years

AC electrical data (- 55 … + 125 °C, V DD = 3.0 ... 5.5 V).

PARAMETER SYMBOL TERMS MIN. TYPE. MAX. ED. MEAS. PRIME
CHANIE
Temperature conversion time tCONV resolution 9 bit 93.75 ms 1
permission
10 bit
187.5 ms 1
permission
11 bit
375 ms 1
permission
12 bit
750 ms 1
Power connection time t SPON Sending a temperature conversion command 10 ms
Slot time tSLOT 60 120 ms 1
Recovery time tREC 1 ms 1
Recording time 0 r LOW0 60 120 ms 1
Recording time 1 tLOW1 1 15 ms 1
Data reading time tRDV 15 ms 1
Reset high time t RSTH 480 ms 1
Reset low time t RSTL 480 ms 1,2
High Presence Time tPDHIGH 15 60 ms 1
Low Presence Time t PDLOW 60 240 ms 1
Capacity C IN/OUT 25 pkf

Notes:

Figure 18. Timing diagrams.

The description is great. Sensors are not easy to work with. They require quite complex software functions, but from a hardware point of view, the DS18B20 is easy to connect, measure accurately, do not require an ADC, etc.

As an example of using DS18B20 temperature sensors, I can cite my development -. Two temperature sensors are used. One measures the air temperature in, the second - the temperature of the radiator.

Category: . You can bookmark.