본문 바로가기
코딩/아두이노

아두이노 ESP8266 에서 DS18B20 방수 온도 센서 프로브 사용방법

by DIYver 2023. 8. 13.

ESP8266 에서 아두이노 IDE를 이용하여 DS18B20 방수형 온도 센서 프로브를 사용하는 방법을 알아보자.

 

<다뤄볼 센서>

DS18B20 방수형 온도센서

 

방수형 온도센서 프로브 안에는 이런 센서가 들어가 있는데
센서 원형을 사용해도 된다.

 

 


<준비물>

ESP8266 ( 다른 아두이노 보드도 가능 ), 4.7k 저항, DS18B20 센서, 브레드 보드, 점퍼와이어

 

 


<회로도>

기생 모드  -  센서라인으로 전원을 끌어와서 센서를 사용

 

일반 모드  -  전원을 직접 공급해주어 센서를 사용

 

 

 

 


<코드>

코드를 업로드 하기전에 ESP8266을 사용하는 경우,
아두이노 IDE( 코드 업로드 프로그램 ) 에서 ESP8266 관련 설정이 되어 있어야 한다.

 

아두이노 IDE 에서 라이브러리 매니저를 열어서 관련 라이브러리를 설치해주어야 한다.

ESP8266 에는 OneWire.h 라이브러리가 기본적으로 없기 때문에,
따로 설치해주어야 한다.
(아두이노 우노, 나노 사용하는 경우 생략 가능)

onewire 를 검색하고

OneWire by Paul Stoffregen 를 설치해주면 된다.

 

이제 온도센서인 DS18B20 을 검색해서 라이브러리를 설치해준다.

Dallas Temperature 
라이브러리를 설치해주면 된다.

 

 

 

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

// GPIO where the DS18B20 is connected to
const int oneWireBus = D4;     

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

void setup() {
  // Start the Serial Monitor
  Serial.begin(115200);
  // Start the DS18B20 sensor
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures(); 
  float temperatureC = sensors.getTempCByIndex(0);
  float temperatureF = sensors.getTempFByIndex(0);
  Serial.print(temperatureC);
  Serial.println("ºC");
  Serial.print(temperatureF);
  Serial.println("ºF");
  delay(5000);
}

필자의 경우 D4 핀에 연결을 해주었는데, 
원하는 핀이 별도로 있다면 원하는 핀에 연결해서 사용하면 되겠다.

 

 

아두이노 우노, 나노, 메가, 프로미니 사용하는 경우 아래 코드로 사용

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

// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;     

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

void setup() {
  // Start the Serial Monitor
  Serial.begin(115200);
  // Start the DS18B20 sensor
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures(); 
  float temperatureC = sensors.getTempCByIndex(0);
  float temperatureF = sensors.getTempFByIndex(0);
  Serial.print(temperatureC);
  Serial.println("ºC");
  Serial.print(temperatureF);
  Serial.println("ºF");
  delay(5000);
}

데이터 신호핀은 디지털 4번 핀을 이용

 


<실행 결과>

 

코드를 업로드하고 시리얼 모니터를 확인해보면
온도가 정상적으로 잘 뜨는 모습을 볼 수 있다.

손으로 센서를 잡고 있었더니 35도 까지 온도가 올라가는 것을 확인할 수 있었다.

 

 


<고찰>

필자의 경우 D2 핀에 연결했을 때에는 센서사용이 정상적으로 안되었었다.

이건 보드마다 다를 수 있을 거 같은 부분인데,
혹시나 자기가 D2 핀에 연결해서 사용하려 했는데 잘 안된다면 
필자처럼 다른 핀에 연결해서 사용하면 될 것 같다.

 

 

 

 

 

더 자세한 정보는 아래 자료를 참고하면 된다.

https://randomnerdtutorials.com/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide/

 

ESP8266 DS18B20 Sensor Web Server Arduino IDE (Single, Multiple) | Random Nerd Tutorials

DS18B20 OneWire Temperature sensor with ESP8266 using Arduino IDE. Learn how to read temperature from one or multiple sensors and build a web server to display sensor readings.

randomnerdtutorials.com

 


※ 궁금하시거나 질문사항이 있으시면 댓글로 작성해주시면 답변해 드릴 수 있는 부분에서 친절히 답변드리겠습니다!

 

 

댓글