Arduino Analog Thermometer With DS18b20 Module

        
1718
Project tested
View Large Image

Introduction

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.

DS18B20 Module

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

The Circuit

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!

source: educ8s.tv

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.

The Code

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:

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int MIN_TEMP = 16;
int MAX_TEMP = 28;

void setup() {

pinMode(9,OUTPUT);
Serial.begin(9600);
sensors.begin();

}

void loop() {

  float temperature = getTemperature();
  int voltage = temperatureToPWM(temperature);
  analogWrite(9,voltage);
  delay(500);
}

float getTemperature()
{
  float temperature =0;
  sensors.requestTemperatures(); // Send the command to get temperatures
  temperature = sensors.getTempCByIndex(0);
  Serial.println(temperature);
  return temperature;
}

int temperatureToPWM(float temperature)
{
  float temp=0;
  float voltage = 0;
  temp = temperature*10;
  voltage = map(temp,MIN_TEMP*10,MAX_TEMP*10,0,250);
  return voltage;
}

Finally, this tutorial is made by educ8s.tv channel, and you can find the tutorial video below:

Leave a Reply