1. DS18B20

온도를 측정하고 싶을 때, 일반적인 환경에서는 DHT11, DHT22 같은 모듈을 쓰실겁니다.

하지만 좀 더 가혹한 환경(물속이나 먼지가 많은)에서는 이런 센서를 사용하셔야 할겁니다. DS18B20 은 방수가 가능한 온도측정 모듈로 긴 케이블을 이용해 마이크로 프로세서에서 먼 거리에서도 측정이 가능합니다. 125’C 까지 측정이 가능하며 (100’C 이하의 환경 권장) 꽤 정확한 측정치를 보여줍니다.

디지털 핀 하나로 데이터를 전송해주므로 많은 핀이 필요 없으며, 심지어는 여러개의 모듈을 하나의 핀에 연결해도 됩니다. 각 모듈은 64bit ID를 가지고 있어서 서로 구분이 됩니다. 3.0-5V 시스템에서 동작이 가능합니다.

다만 센서가 Dallas 1-Wire protocol 로 보내주는 신호를 처리하는 과정이 좀 복잡해서 라이브러리를 사용해서 처리해줘야 합니다. 두개의 라이브러리를 설치해주면 됩니다. Dallas Temperature Control Arduino libraryOneWire Library.

2. 연결 방법

연결은 아래와 같이 하면 됩니다.

DS18S20-hookup-400x237

데이터 값을 읽는 2번 핀을 기본 HIGH 상태로 맞추기 위해 pull-up 저항을 사용합니다.

3. 소스코드 (스케치)

이제 데이터를 읽는 코드를 보면…

/* YourDuino Electronic Brick Test
Temperature Sensor DS18B20
- Connect cable to Arduino Digital I/O Pin 2
terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <OneWire.h>
#include <DallasTemperature.h>

/*-----( Declare Constants )-----*/
#define ONE_WIRE_BUS 2 /*-(Connect to Pin 2 )-*/

/*-----( Declare objects )-----*/
/* Set up a oneWire instance to communicate with any OneWire device*/
OneWire ourWire(ONE_WIRE_BUS);

/* Tell Dallas Temperature Library to use oneWire Library */
DallasTemperature sensors(&ourWire);

/*-----( Declare Variables )-----*/


void setup() /*----( SETUP: RUNS ONCE )----*/
{
/*-(start serial port to see results )-*/
delay(1000);
Serial.begin(9600);
Serial.println("YourDuino.com: Electronic Brick Test Program");
Serial.println("Temperature Sensor DS18B20");
delay(1000);

/*-( Start up the DallasTemperature library )-*/
sensors.begin();
}/*--(end setup )---*/


void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
Serial.println();
Serial.print("Requesting temperature...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");

Serial.print("Device 1 (index 0) = ");
Serial.print(sensors.getTempCByIndex(0));
Serial.println(" Degrees C");
Serial.print("Device 1 (index 0) = ");
Serial.print(sensors.getTempFByIndex(0));
Serial.println(" Degrees F");

}/* --(end main loop )-- */

/* ( THE END ) */

두 개의 라이브러리에서 대부분의 작업을 해줍니다. 그래서 상단에 데이터를 입력받을 디지털 핀 번호만 확인해주시면 됩니다.

#define ONE_WIRE_BUS 2 /*-(Connect to Pin 2 )-*/

사실 Dallas 라이브러리는 필수는 아닙니다. One Wire 라이브러리만 있어도 계산으로 값을 뽑아낼 수 있습니다. 아래 링크를 통해 소스를 받아보실 수 있습니다.

http://bildr.org/2011/07/ds18b20-arduino/

4. TMP36

이게 복잡하다 느껴지시는 분은 TMP36 모듈을 구입하시길 바랍니다. 이건 디지털 신호가 아니라 온도의 변화를 전압의 변화로 바꿔줍니다. 아두이노의 Analog 핀에 연결해서 변화된 전압을 읽으면 간단한 산수로 온도를 계산해 낼 수 있습니다.

Temp °C = 100*(reading in V) – 50

다만 TMP36 모듈은 DS18B20 모듈처럼 긴 방수 케이블 형태로 나오지는 않습니다.

TMP36 모듈의 경우 아래와 같이 연결하면 됩니다.

temperature_tmp36pinout

코드는 아래와 같이 스시면 됩니다. 여기서는 A0 핀으로 데이터를 받는 경우입니다.

//TMP36 Pin Variables
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
                        //the resolution is 10 mV / degree centigrade with a
                        //500 mV offset to allow for negative temperatures
 
/*
 * setup() - this function runs once when you turn your Arduino on
 * We initialize the serial connection with the computer
 */
void setup()
{
  Serial.begin(9600);  //Start the serial connection with the computer
                       //to view the result open the serial monitor 
}
 
void loop()                     // run over and over again
{
 //getting the voltage reading from the temperature sensor
 int reading = analogRead(sensorPin);  
 
 // converting that reading to voltage, for 3.3v arduino use 3.3
 float voltage = reading * 5.0;
 voltage /= 1024.0; 
 
 // print out the voltage
 Serial.print(voltage); Serial.println(" volts");
 
 // now print out the temperature
 float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                               //to degrees ((voltage - 500mV) times 100)
 Serial.print(temperatureC); Serial.println(" degrees C");
 
 // now convert to Fahrenheit
 float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
 Serial.print(temperatureF); Serial.println(" degrees F");
 
 delay(1000);                                     //waiting a second
}