Sometimes, it is necessary to add a temperature indicator into your projects. Therefore, in this tutorial you will learn how to hack your analog Voltmeter and convert into an analog Thermometer using Arduino and a DS18B20 temperature sensor.
In the first place, the Maxim DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile user-programmable upper and lower trigger points. Also, the DS18B20 communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.
In fact, each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18B20s distributed over a large area.
Parts you will need
Fortunately, the circuit is so simple. At the beginning, connect the DS18b20 pin with the (-) sign to Arduino GND, the pin with (+) sign to 5V and the signal pin to digital pin 2, in order to start sensing the temperature.
Secondly, in order to control the voltmeter we connect the positive side to digital pin 9 (one of the PWM pins) and the negative to GND. Afterward, to change the labels of voltmeter to a Celsius thermometer, just download the photo in the attachments and print it out!
Pulse Width Modulation
In a nutshell, Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Therefore, instead of writing high to the digital pin in PWM, we can send a pulse. Accordingly, some digital pins of Arduino UNO support PWM and you can recognize them by this signal (~) printed beside.
First of all, you need to add “DallasTemperature” library to your Arduino IDE, since it is a library that supports Maxim temperature ICs including our DS18B20.
The code consists of three main ideas:
- Reading the temperature from the sensor.
- Converting the temperature into a PWM value
- Showing the value on the thermometer
First, in the setup we will read the temperature from the sensor. Then, we will pass it to the PWM function to convert the acquired value into a PWM value within the range of 0 to 255. This can be done inside the function with the help of “map” function. Next, we will write it into pin 9 to be shown on the voltmeter.
However, you can assign your preferable minimum and maximum temperature degrees, but you should notice: the smaller the gap between those two values, the bigger the resolution of the thermometer.
Let’s have a look at the code:
Finally, this tutorial is made by educ8s.tv channel, and you can find the tutorial video below: