Welcome to the tenth tutorial in our raspberry pi programming course. In the last tutorial, we learned how to control a DHT11 sensor with raspberry pi. In light of the preceding, it's safe to say that the Raspberry Pi is the greatest computing platform for IoT, home automation, and other similar applications. On the other hand, sensors are required for any electronic system that wishes to communicate with or gather data from the physical environment. The type of sensor used depends on the environment to be used, and a wide variety of sensors are available for this operation. This guide will teach you how to connect a Raspberry Pi to the DS18B20.
DS18B20 is a popular temperature sensor, especially in severe environments such as chemical plants, mines, and other industrial settings.
What will you learn?
Using the Raspberry Pi and a 16x2 LCD, we'll show you how to display the temperature.
- Raspberry Pi 4
- LCD display screen
- 10k trim pot
- 10k Pull up resistor
- Female to male jumper wires
The DS18B20 Sensor: A Quick Overview
It is a three-terminal temperature sensor. A single external component is all that is needed to get it running. In addition, the MCU/MPU only needs one GPIO pin to interact with it. DS18B20 temperature sensors are shown below with their pin names.
A waterproof variant of this sensor is also available, which encloses the sensor within a cylindrical metal tube. The sensor depicted in the image above will be used in this lesson. To communicate with a microcontroller or processor board like the Raspberry Pi, the DS18B20 uses only one wire, which makes it a 1-wire programmable temperature sensor. The sensor's specifications are shown below.
The principle behind the sensor
To understand how temperature sensors operate, you must understand how diodes work. In response to an increase in voltage, the temperature rises, and the diode's base and emitter terminals experience a voltage drop.
As an alternative, vibrating wire temperature sensors are used since stress levels fluctuate with temperature.
The linear coefficient of expansion of different metals changes with temperature, which is the basis for the vibrating wire temperature meter's theory.
Stretched magnetic wire with high tensile strength is the primary constituent part due to the fact that the strain in the wire is instantly affected by temperature variations and its natural vibration frequency.
Among other metals, aluminum is the notably different metal used in Encardio-Rite sensor. Aluminum by far has a larger coefficient of thermal expansion than steel. It is also possible to utilize vibrating wire sensors to monitor temperature, as the temperature signal is translated into frequency.
Sensor wires from Encardio-rite use vibration to detect temperature changes, which are then converted into electric signals and sent to the readout device.
When the temperature and the wire tension are both known, the frequency can be calculated as follows:
What kinds of temperature sensors are there?
A wide variety of temperature sensors are available. The following are the two most common kinds of temperature sensors:
Some thermometers use direct contact with an object, referred to as "contact type temperature sensors," to measure the degree of warmth or cold. Contact-type temperature sensors are used for this purpose. They are capable of detecting solid materials, liquid materials, and gas in a wide range of temperatures.
Non-Contact sensor: This type of thermometer does not touch the object it is measuring; instead, it measures the temperature based on the radiation emitted from that object.
Applications of temperature sensor
Temperature sensors can be used in a variety of ways, so here are some examples.
- Motors - Temperature monitoring is required for several motor components to ensure that the motor does not overheat.
- Surface plate – In order to accurately detect temperature, a typical type of surface plate temperature sensor is the ring terminal sensor (RTS).
- Home appliances – Temperature sensors will be found in everything from coffee makers to toasters to washing machines and dishwashers.
- Computers– Temperature sensors are built into computers to guard against overheating.
- Industrial equipment – The temperature sensors utilized in these applications must be extremely durable because of the harsh conditions.
- Warming Electrical Radiators– In order to regulate the temperature of electric radiators, NTC thermistors are employed.
- Monitoring of Exhaust Gases from Racing Vehicles – Temperature sensors used in racing must be extremely reliable and long-lasting to ensure that performance does not suffer.
- Food Production; 3D printed chocolates– The melted chocolate used in 3D printing is monitored using temperature sensors.
- Alcohol breathalyzer– Alcohol breathalyzers employ thermistors to gauge the subject's exhaled breath temperature.
Let's get started with Raspberry Pi connecting with the sensor now that we know enough about it.
Using the DS18B20 temperature sensor and a 16x2 LCD display, as described before in this guide, we will see the temperature shown on the screen. It is, therefore, necessary to connect the sensor and the LCD to the Raspberry Pi, as shown below.
Make the connections in accordance with the circuit diagram. Using the Raspberry Pi's 5V pin, the LCD and sensor can be powered by the +5V supply. The GPIO pin 18,23,24,25 and the GPIO pin 7 and 8 are used for the data lines, respectively. Potentiometers are another option for adjusting brightness and contrast on an LCD. GPIO pin four is used to connect the data line of the DS18B20. Also, the data must be pulled as high as shown in the circuit diagram using a 10K resistor.
Using the circuit diagram above or the GPIO pin table, follow along with its numbers to make the connections. Alternatively, you might combine the two ways.
Single-strand wire and jumper connectors will be used to construct the circuit on the breadboard. It's easy to see that the sensor can interface with the board using only one wire, saving both physical space and pin count. When everything is connected, my hardware will look like this. It's finally time to turn on the raspberry pi and get to work on some code.
Raspberry Pi's Adafruit LCD library can be installed using this method.
On a 16*2 LCD screen, the temperature will be displayed. Using Adafruit's library, we can easily run this LCD in 4-bit mode by following the instructions below.
Step 1: Use the following command to install git on your Raspberry Pi. Any project on Github may be cloned using Git. Since the library is hosted on Github, we'll need to set up git in order to get it onto Pi.
Step 2: GitHub link for the library is linked in the next line, so copy the project file to Pi's home directory and run the command.
Step 3: To access the folder we just downloaded, type the command below in the terminal to change the directory. The following is a list of available commands.
Step 4: To get the library, we'll need to run setup.py, which is located in the directory where we placed the setup.py file. To set up the library, enter the following code into your browser:
That's all there is; the library should now be operational. In the same way, let's install Adafruit's DHT library as well.
Using the Pi's One-Wire Interface
If we want to connect with the DS18B20 sensor via a One-Wire approach, we must follow this section's steps.
Step 1: To open the config file, type the following command in the Command Prompt window.
Step 2:- Add the following line to the config file: "dtoverlay=w1-gpio" and save the file.
Step 3:- You can leave the file by hitting Ctrl+X, and then press "Y" to save and then the Enter key. The run the following command to restart the Pi:
Step 4:- Take a look at your terminal after the system has been restarted and type as follows:
This is what you can expect to see in your terminal windows:
Step 5:- To get a unique pi number, type ls. According to the sensor, this number will vary from user to user, but will always begin with 28-. 28-03172337caff is my phone number.
Step 6:- The following commands can be used to verify that the sensor is functioning properly.
You can see the sensor's readings on the terminal as a result of these commands. The image below shows a red arrow points to the temperature. The current temperature in my home is 37 degrees Celsius.
Using Python on a Raspberry Pi
It's time to start programming our Pi for LCD displays and One-Wire communication. Our final program will be able to read temperature values from the sensor and show them on the LCD as a result of this development. I've broken down the code into smaller, more digestible chunks below to make it easier to understand.
Importing the necessary header files is always the first step in a new program. Time and the LCD header are imported here to use the LCD with Pi. Using the os header, you can manage files in the operating system.
It is important to mention the Display pins linked to the Raspberry gpio. Consult the diagram above if you're unsure of the pin numbers for specific GPIO pins. Once the pins on the PI to which the LCD is connected have been declared, use these lines of code to initialize the LCD display.
Once the LCD has been initialized, we show a sample text message. To indicate a new line, the character 'n' is used. There is a delay of two seconds before users can begin reading the intro message.
Make sure you remember what you did in step 4 in order to enable the one-wire interface on your Pi. For this reason, we are using the os.system function to repeat the code. Afterwards, a file path is supplied where the temperature value will be read from. We can't open the folder since we don't know its exact name. The device folder variable connects to the '28-' folder using the * symbol. The temperature value is stored in a file with the name device file, which is also in that folder.
Then we create a get temp method, which specifies how to read the temperature from the file we previously linked. There will be a file containing the temperature data; however, the format will be as follows:
We only need the temperature value, which is 37000, from this. A reading of 37.00°C can be seen in this instance. After trimming all the unnecessary information from this type of text, we must next divide 37000 with 1000. The function shown here is precisely the same as the one presented below.
Using the lines variable, the file's lines can be parsed. A search for "t=" is done in these lines, and in the variable temp string, the value that comes after that letter is recorded. Finally, we perform division on the string value with 1000 in the variable temp c to obtain the temperature value. Finally, the code should output the temp c variable.
To display the current temperature on the LCD panel, we merely need to invoke the previously described function inside the infinite while loop. Every second, we refresh the LCD display to show the current value.
Use the code provided at the end of this page to compile it on your Raspberry Pi and see what happens. Before running the application, Install the display libraries and enable a one-wire connection on the pi as per the steps below. You can now run the application and see if everything went well; if so, the intro text should be visible. Adjust the contrast potentiometer if necessary if you don't see anything after doing so. What you'll see when it's all done is shown in the image below.
In this lesson, we learned how to use the Raspberry Pi's GPIO pins to connect DS18B20 to the Raspberry Pi 4. In addition, we looked into the sensor's fundamental principles and current-day usefulness. We also designed a circuit that we later programmed to collect temperature readings using the temperature sensor and perform a little calculation and display it on the lcd screen in degrees Celsius. See you in the next tutorial.