How to set up smartphones and PCs. Informational portal
  • home
  • Interesting
  • Arduino DS18B20 temperature sensor. Digital thermometer DS18B20 and ARDUINO UNO

Arduino DS18B20 temperature sensor. Digital thermometer DS18B20 and ARDUINO UNO

Now you will learn how to turn an ordinary dial (analog) voltmeter into a digital thermometer using the Arduino platform and a ds18b20 temperature sensor. In principle, this technology is suitable not only for displaying temperature - any other (physical and electrical) quantities from different sensors can be digitally displayed on a dial gauge using this method.

DS18B20 module

The well-known ready-made module ds18b20 is a digital thermometer that provides 9-bit temperature measurement and has a non-volatile programmable upper and lower operating point function. In addition, the ds18b20 communicates over the 1-Wire bus and requires only one data line to communicate with the microprocessor. In addition, the ds18b20 can be powered directly from the data line, eliminating the need for an external power supply.

ds18b20 pinout

In fact, each ds18b20 has a unique 64-bit serial code that allows even multiple DS18B20s to function on the same 1-Wire bus. Thus, using just one microprocessor, it is possible to monitor many temperature sensors distributed over a large area.

Thermometer assembly diagram


Connecting elements to the board

The circuit itself is so simple that there is no point in drawing it - all connections are visible in the photograph. First, connect ds18b20 to pin (-) for Arduino ground, pin (+) for 5V and signal output to digital pin 2.In order to control the voltmeter, connect its positive wire to pin 9 (one of the PWM) and connect minus to GND. After that, to change the scale on the voltmeter to a thermometer, just print the photo that is attached to the article. The voltmeter is 5 V here, but you can take any other indicator, including a regular microammeter, by adding the required resistor in series to it (about 10-100 kOhm).


Pointer thermometer and voltmeter

List of required parts

  • Arduino Uno
  • DS18b20 sensor
  • Pointer voltmeter
  • Multiple wires
  • Power supply (can be battery)

Thermometer parts

Principle of operation

Pulse width modulation, or PWM, is a technique for obtaining analog results using digital means.


Pulse width modulation switch control

When flashing the microcontroller, you must add the “DallasTemperature library” to your Arduino development environment, as this is the library that supports the ds18b20 temperature sensor.

The code has three main parts:

  1. Reading the temperature from the sensor
  2. Converting temperature to PWM
  3. Displaying a value on a scale

Thermometer setting

In the settings, we will read the temperature from the sensor. Then, we convert this value to a PWM function, ranging from 0 to 255. This can be done within a program function. Next, let's apply a signal to pin 9, which is connected to a dial-up voltmeter.

When setting the temperature range, keep in mind that the smaller the gap between the extreme values, the greater the thermometer's resolution. You can download the datasheet for the sensor and the firmware code.

Video of device operation

Learn how to use the 433 MHz RF module with the ATMega328P-PU. In this article, we will assemble a circuit from a DHT11 sensor and an RF transmitter. And also we will assemble a receiving device with a 433 MHz radio receiver and an LCD display.

What do we need

  • a computer with the Arduino IDE installed (I am using version 1.6.5);
  • VirtualWire library (link below);
  • ATMega328P;
  • AVR MKII ISP programmer;
  • DHT11 temperature and relative humidity sensor;
  • components from the list of items below.

Introduction

In this article, I will show you how to assemble a device that measures temperature and relative humidity and sends the measured values ​​using a standard 433 MHz RF module. The temperature and humidity sensor used in the device is DHT11.

There are many ways to transfer small amounts of data using Arduino or ATMega controllers. One of them uses a ready-made library like RCSwitch, Radiohead or VirtualWire. It is also possible to send raw data using the microcontroller's built-in UART module. But it is not recommended to use the built-in UART module, as the receiver will collect all the noise, and the microcontroller will not work as intended. In this article, I use the VirtualWire library to send and receive data. This library works with Arduino IDE 1.6.2 and 1.6.5.

The 433 MHz transmitter module, when not transmitting data, still emits RF waves and transmits noise. It may also interfere with other radio frequency devices. To prevent this, I turn it on when I need to transfer data, and turn it off when the transfer is complete.

Hardware part

We need two structural diagrams. One for the transmitter, the second for the receiver.

Transmitter

We need:

  • microcontroller firmware method → ​​ISP;
  • sensor for measuring temperature and humidity → DHT11;
  • microcontroller for data processing → ATMega32p;
  • wireless data transmission method → ​​433 MHz radio frequency module.

Receiver

We need:

  • radio signal reception method → ​​433 MHz radio frequency module;
  • method of processing received data -> Arduino Mega;
  • temperature and humidity display method → ​​16x2 LCD.

Schematic diagrams

Transmitter


Transmitting part of the wireless thermometer on ATMega328p
()

In this example, I will not output the unused pins of the microcontroller to the external contacts of the thermometer, after which they could be used to further improve the device. Here we are only considering an idea for the device and will only build it on a breadboard.

Receiver



(to zoom in, you can right-click on the picture and select "Open link / image in a new tab / new window")

Please note that the receiver is based on the Arduino Mega board, which is not shown in the diagram. To connect the Arduino Mega board, connect the RF module and LCD display to it according to the label on the diagram.

List of items

Transmitter


List of elements of the transmitting part of the wireless thermometer on ATMega328p
(to zoom in, you can right-click on the picture and select "Open link / image in a new tab / new window")

Receiver



(to zoom in, you can right-click on the picture and select "Open link / image in a new tab / new window")

Program

Transmitter program

First, let's look at the program of the transmitting part:

#include // Define #define dhtPin 4 #define dhtType DHT11 #define txPowerPin 8 // Using the DHT library DHT dht (dhtPin, dhtType); // Variables char msg0; char msg1; int tem = 0; int hum = 0; // Initial setup function - executed only once when enabled void setup () (pinMode (txPowerPin, OUTPUT); pinMode (txPowerPin, LOW); vw_setup (4800); // VirtualWire connection speed vw_set_tx_pin (9); // Transfer output VirtualWire) // Loop function - always executed void loop () (digitalWrite (txPowerPin, HIGH); hum = dht.readHumidity (); // Variable stores humidity tem = dht.readTemperature (); // Variable stores temperature itoa (hum , msg1, 10); // Convert moisture to an array char itoa (tem, msg0, 10); // Convert temperature to an array char strcat (msg0, msg1); // Add / concatenate two arrays vw_send ((uint8_t *) msg0 , strlen (msg0)); // Send message vw_wait_tx (); // Wait for the transfer to complete digitalWrite (txPowerPin, LOW); delay (5000); // Wait 5 seconds and repeat everything again)

To convey humidity and temperature in one message, I link them together. First, the data is read into a variable as integers, then the integers are converted to a character array, and then they are concatenated. On the receiving side, the data will be split into individual characters. In doing this, I limit myself to two digits of degrees. If the sensor is in an environment less than 10 ° C, I will get garbage symbols on the display. For example, if the temperature is 20 ° C and the humidity is 45%, then the message 2045 will be transmitted and everything is fine. If the temperature is 9 ° C and the humidity is 78%, then the message 978x will be transmitted, where “x” is a random character. Therefore, if you are going to collect this wireless thermometer, I advise you to change the program to transmit the correct data when the temperature is less than 10 ° C.

Receiver program

// We include the necessary libraries #include #include // Define LCD connection #define RS 9 #define E 10 #define D4 5 #define D5 6 #define D6 7 #define D7 8 LiquidCrystal lcd (RS, E, D4, D5, D6, D7); // Draw the degrees symbol byte degreesymbol = (B01100, B10010, B10010, B01100, B00000, B00000, B00000, B00000); // Variables int tem = 0; int i; // Initial setup function - executed only once when enabled void setup () (lcd.begin (16,2); // Initialize LCD lcd.createChar (1, degreesymbol); // Create degrees symbol at location 1 Serial.begin (9600); // For debugging vw_setup (4800); // VirtualWire connection speed vw_rx_start (); // Ready to receive vw_set_rx_pin (2); // VirtualWiore receive output lcd.clear (); // Clear LCD) // Loop function - always executed void loop () (uint8_t buf; // Variable for storing received data uint8_t buflen = VW_MAX_MESSAGE_LEN; // Variable for storing the length of received data lcd.setCursor (0,0); lcd.print ("Temp:" ); if (vw_get_message (buf, & buflen)) // If data is received (for (i = 0; i<2;i++) // Получить два первых байта { Serial.write(buf[i]); // Для отладки lcd.write(buf[i]); // Вывести первые байты на LCD } Serial.println(); // Для отладки lcd.write(1); // Вывести символ градусов на LCD lcd.print(" C"); lcd.setCursor(0,1); lcd.print("Hum: "); for (i=2;i<4;i++) // Получаем последние два байта { Serial.write(buf[i]); // Отладка lcd.write(buf[i]); // Вывести последние байты на LCD } lcd.print("% RH"); } }

An interesting way to use the LiquidCrystal library is to create custom symbols. With createChar I created the degrees symbol. You can create your own symbols in the same way. To create a custom symbol or icon, you need to declare it as an array of eight bytes and "draw" which pixels will be on (1 - on, 0 - off).

It would seem, what could be interesting and new in measuring temperature using Arduino? Hundreds of articles have been written, tens of megabytes in size, maybe a little less, and maybe a little more sketches ... And here's my article. What for? To be honest, I also thought that this question was “chewed up and down” until I myself faced the measurement of temperature. And then it got up. Something doesn’t work, something doesn’t work, there are a lot of questions, to which the answers have to be “scratched out” by interrupting half of the Internet, and not only the Russian-speaking one. This article, unlike my previous articles on this resource, is much more practical, but let's start over. Why, in fact, measure the temperature with something new, when thermometers are sold - for every taste and budget? And the fact is that the temperature, often, has to not only be measured, but then, based on the data obtained, do something, or simply register in order to track changes. Connecting, using , a thermal sensor with a relay unit, we will get the simplest thermostat, and if this thermostat can monitor the temperature at several points (zones) and act according to a certain algorithm, we will get a rather serious device, the industrial analogue of which is comparable to the cost of a good laptop. However, the purpose of this article is not to create sophisticated devices. The goal is different - to offer the beginner a simple, field-proven temperature measurement solution. Also, like the previous articles, this one will consist of parts. Each of which will consider its own question. The parts will go in increasing difficulty.

Part one. The simplest, but also useful

So, from words to deeds! To implement this project, at the first stage, we need a digital thermal sensor DS18B20, ARDUINO UNO, a 4.7 kΩ resistor (the power does not really matter, from 0.125 to 2 W is entirely suitable, but the accuracy matters, the more accurate the better), a piece 3-core wire (and separate wiring at the stage of the experiment will also work), and also a few pins for the board. Although it is also possible without them, if carefully, of course. The choice of this sensor is not accidental. The fact is that it can monitor the temperature in the range from -55 ° C to + 125 ° C with an accuracy in the main part of the range of 0.5 ° C, which is quite enough to control both domestic heating and various freezing and refrigeration units, as well as baths, saunas , greenhouses, incubators, nurseries and others. Let me remind you that ARDUINO UNO can be freely purchased here: or here: , thermal sensor DS18B20 - , although personally for me - this: the advantage of mine is its small size, comparable to the size of the cable. Disadvantages - the absence of a board, which in some conditions negatively affects the convenience of installation and the viability of the sensor. Also - at the sensor a resistor is built-in and no more resistors need to be soldered, but the ability to connect several sensors "in a chain" disappears. The connection of the sensor to the Arduino can be seen in Fig. 1 and indicated in Table 1. It is easy to identify the contacts on the thermal sensor. You need to take it so that you look at the cut with numbers, and the legs were at the bottom. The leftmost leg will be GND, the middle DQ, and the rightmost VDD.

Table 1.

Pin Arduino Uno

Note

5V, one leg of the 4.7 kOhm resistor is also soldered.




Figure 1. Connecting one thermal sensor.

The figure shows that two resistors were used. This is due to the fact that the resistor I found with the marking "4K7" actually had a rather high error, which I had to compensate for with the second resistor. The total resistance of this assembly was 4.695 kOhm, which I think is quite acceptable. You can also see in the figure that the sensor is not soldered directly to the wires (a cut of the loop), but is inserted into the connector. This was done for reasons of the development of the experiment. It is highly recommended to solder these sensors. The sketch itself is also quite compact:


Only 14 lines of code with comments. Any beginner will be able to figure it out. As a result of the work, the program will give something like this:



Figure 2. Result of work with one sensor.



Part two. A little complicated.

We'll complicate this part by adding another sensor. Suppose we need to measure the temperature outdoors and indoors. To do this, we just add one sensor "in a chain". Very much like a parallel connection. Electrical connoisseurs will understand what I mean. But there is a difference: in this case, the leads from the center wire should be as short as possible.



Figure 3. Board with two sensors.



The sketch has grown by only 3 lines. Now it has 17 lines:


The results of this sketch can be seen in Figure 4.



Figure 4. Working with two sensors.



Part three. Final.

Now let's connect an LED to Arduino, which will light up when a certain temperature is reached. Such a "threshold signaling device". This requires a conventional LED and a current-limiting resistor. I got a hand on 100 Ohm, and I used it by connecting it to the 7th pin of Arduino. We solder the long leg of the LED (anode) to the resistor, and connect the short leg (cathode) to the GND pin of the Arduino. You should get something like Figure 5.




The sketch also did not grow much at all:


The operation of this program on the computer is displayed in exactly the same way as shown in Figure 4. Naturally, the sensors.getTempCByIndex variable (1) can be operated within a very wide range and LED control is just the simplest example of all possible.

And in conclusion of this article, one more step. Now I will tell you how to connect several "garlands" of these devices to one Arduinka. The fact is that the length of the "garland" cannot be infinite; moreover, it is very limited. In ideal conditions it is 300 meters, but creating “ideal” conditions is a rather expensive pleasure. In real conditions, it is not recommended to exceed 10 meters. For an ordinary "room" thermometer, this is more than enough, but if we are talking about any more serious equipment, this is too little. Moreover, for stable operation, it is necessary that the sensors are located as close as possible to the bus conductors - a "garland". Rejection, of course, is also possible, but the accuracy and noise immunity in this case will be extremely low. So, we connect several "garlands" precisely in order to collect information from a large number of points, while maintaining sufficient accuracy and noise immunity. Add contacts according to table 2:

Pin Arduino Uno

Note

5V, one leg of 4.7 kOhm resistors is also soldered.

Digital input, the second leg of the 4.7 kOhm resistor is also soldered.

Digital input, the second leg of the 4.7 kOhm resistor is also soldered.



As you can see from the table - there is nothing complicated, exactly the same bus, only on a different digital water. I did not solder to pin 9 only for reasons of convenience and soldering speed.
Sketch:


The sketch hardly needs any unnecessary comments.

The result of the sketch looks like this:



Figure 6. Simultaneous operation of two sensor lines.



And the board with two lines connected looks like this:



Figure 7. Board with two buses.



It can be seen from the figure that the 4.7 kOhm resistor is also made composite to improve accuracy.

The libraries used for writing the sketches discussed in the article are located here:



Review prepared by Pavel Sergeev

In this tutorial, we will use a DS18B20 temperature sensor with Arduino UNO to create a thermometer. The DS18B20 is a good option when a high-precision project requires good response. We will show you how to connect the DS18B20 to yours and display the temperature data on a 16x2 LCD.

The DS18B20 sensor communicates with the Arduino via a 1-wire bus. By definition, only one data line (and ground) is required to communicate with the Arduino.

Each DS18B20 has a unique 64-bit serial code or address that allows multiple DS18B20s to operate on the same single-wire bus. Therefore, the use of a microprocessor makes it easy to control multiple DS18B20s distributed over a large area. Applications for this function include environmental control, temperature control systems in buildings and mechanical equipment.

DS18B20 Features

  • Only one single-wire interface is required for communication between the microcontroller and the sensor.
  • Only one external component is required: a 4.7 kΩ resistor.
  • Can be powered from the data line directly, requiring a voltage of 3.0 to 5.5 V.
  • Each device has a unique 64-bit serial code stored on the on-board ROM.
  • Can measure temperatures from -55 ° C to + 125 ° C (-67 ° F to + 257 ° F).
  • Accuracy ± 0.5 ° C from -10 ° C to + 85 ° C.

This project uses a DS18B20 which comes in the form of a temperature probe that is waterproof. The use of a waterproof sensor expands the possibilities - the temperature sensor can measure the temperature of liquids such as water, chemicals, tea and coffee.

Component requirements

The equipment requirements for your thermometer are fairly standard, we will need it:

  • LCD display 16x2
  • DS18B20 temperature sensor
  • Jumper wires
  • 1K resistor
  • Bread board

Connection diagram

Make connections according to the diagram below.

We connect the sensor and Arduino

  • VCC -> Arduino 5V, plus 4.7K resistor going from VCC to Data
  • Data -> Pin 7 Arduino
  • GND -> GND Arduino

Connections for LCD and Arduino UNO

  • Pin 1 -> GND
  • Pin 2 -> VCC
  • Pin 3 -> Arduino Pin 3
  • Pin 4 -> Arduino Pin 33
  • Pin 5 -> GND
  • Pin 6 -> Arduino Pin 31
  • Pin 7-10 -> GND
  • Pin 11 -> Arduino Pin 22
  • Pin 12 -> Arduino Pin 24
  • Pin 13 -> Arduino Pin 26
  • Pin 14 -> Arduino Pin 28
  • Pin 15 -> VCC through 220 ohm resistor
  • Pin 16 -> GND

Connect a potentiometer as shown above to pin 3 on the LCD to control the contrast.

This project operates at temperatures up to 125 ° C. If there is some dissonance in the temperature shown, double check the connections with the resistor connected to the DS18B20. After connecting everything described above, we can move on to programming.

Source code for thermometer

Before downloading the source code, you need to set up the two libraries required to run this code in the Arduino environment.

  • The first library is called OneWire ().
  • The second library is called DallasTemperature ().

After downloading both libraries, move the files to the default Arduino libraries folder. Then copy the code in and upload it after double checking your sensor is connected correctly.

// Code begins #include #include #include #define ONE_WIRE_BUS 7 OneWire oneWire (ONE_WIRE_BUS); DallasTemperature sensors (& oneWire); float tempC = 0; float tempF = 0; LiquidCrystal lcd (12,11,5,4,3,2); void setup () (sensors.begin (); lcd.begin (16,2); lcd.clear (); pinMode (3, OUTPUT); analogWrite (3, 0); Serial.begin (9600);) void loop () (sensors.requestTemperatures (); tempC = sensors.getTempCByIndex (0); tempF = sensors.toFahrenheit (tempC); delay (1000); Serial.println (tempC); lcd.setCursor (0,0); lcd. print ("C:"); lcd.print (tempC); lcd.print ("degrees"); lcd.setCursor (0,1); lcd.print ("F:"); lcd.print (tempF); lcd.print ("degrees");)

It looks like this:

We were able to measure temperatures up to 100 ° C with this sensor! He is very responsive.

After you have created a project, test the device by immersing the sensor in hot and cold water.

Today we are going to take a look at an Arduino project for engineering students. This article will show you how to make a thermometer on Arduino. The advantage of building a thermometer on Arduino is the simplicity of construction. We already have its features. Arduino programming is much easier than you might think.

This project is a digital thermometer (Arduino digital temperature sensor) based on a precision integrated LM35 sensor.

Digital thermometers are widely used in many electronic devices such as air conditioners to provide information about the temperature level and control the processes of the refrigeration system.

This circuit uses the LM35 temperature sensor to determine the temperature level, which can measure from -55 to 150 ° C. The measured temperature is displayed on a 16x2 LCD using an Arduino.

Required components for the project "Thermometer on Arduino"

The Arduino digital thermometer uses readily available components that anyone can purchase from any electronics store.

  • Arduino
  • Temperature sensor LM35
  • LCD display 16x2
  • Potentiometer 1kOhm

Thermometer operation

The output voltage of the LM35 sensor is linearly proportional to temperature (Celsius).

The LM35 output has a scale factor of 10mV / ° C, which means that for every 1 ° C increase in temperature, there will be a corresponding 10mV increase in the output voltage, so we can easily read the measured temperature value.

The Arduino has an analog pin (A0) capable of reading analog signals from any sensor. As shown in the circuit diagram, the Arduino's analog pin A0 connects to the LM35 pin.

LM35 temperature sensor pinout

The Arduino has a 10-bit onboard ADC, so it can read a value from 0 to 1023, that is, for zero voltage it reads 0000 and for VCC (5V) it reads 1023.

So we are scaling the 0-1023 values ​​to the new 0-500 range, because the LM35 outputs values ​​in 10mV increments per degree, so the maximum possible gain is 500 (5V / 0.01V).

Using this mapping, we can take each increment in the ADC value as an upscale step. If the ADC value is 27, then the measured temperature value is 27 ° C.

Thermometer sketch

#include long A; int B; LiquidCrystal lcd (12, 11, 5, 4, 3, 2); void setup () (lcd.begin (16,2); lcd.print ("THERMO METER"); pinMode (A0, INPUT);) void loop () (A = analogRead (A0); B = map (A, 0,1023,0,500); lcd.setCursor (0,0); lcd.print ("TEMPERARTURE:"); lcd.setCursor (0,1); lcd.print (B); lcd.print ("'C" );)

Now let's move on to the logic of the program. First, we need to declare a long variable "A" to store the value read from the LM35, which has a range of 0 to 1023. Then we declare another integer variable "B" to store the displayed (converted) value.

The temperature value is read from pin A0 and stored in variable "A". Then the variable "A" is converted to the range from 0 to 500 and stored in the variable "B". The "B" value is displayed directly on the 16x2 LCD screen.

Top related articles