Arduino ตอน7 ใช้ DS18B20 (Digital Temperature Sensor) กับ Arduino

30 Jun 2019,
Share: 
Cover image

สวัสดีครับ ในบทความนี้จะมีเนื้อหาเกี่ยวกับวิธีการใช้งาน DS18B20 (Digital Temperature Sensor) กับ Arduino กันนะครับ เพื่อเรียนรู้การใช้งาน Arduino กับ Digital Temperature Sensor ผ่าน 1-Wire Protocol

Step1: ข้อมูลเกี่ยวกับ DS18B20 (Digital Temperature Sensor)

Image

DS18B20 คือ 1-wire digital temperature sensor ที่ผลิดโดย Maxim IC สามารถใช้วัดอุณหภูมิในหน่วยองศาเซลเซียสตั้งแต่ -55 to 125 (+/-0.5) (ความละเอียด 9 ถึง 12-bit) Sensor แต่ละตัวจะมี Serial number (unique 64-Bit Serial number) ที่ไม่ซํ้ากันอยู่เพื่อใช้ระบุ Address แต่ละตัวในกรณีที่เชื่อมต่อ Sensor หลายตัวในสาย Data เส้นเดียว (1-wire)

Features:

  • Unique 1-Wire® interface requires only one port pin for communication
  • Each device has a unique 64-bit serial code stored in an onboard ROM
  • Multidrop capability simplifies distributed temperature sensing applications
  • Requires no external components
  • Can be powered from data line.
  • Power supply range is 3.0V to 5.5V
  • Measures temperatures from –55°C to +125°C (–67°F to +257°F)±0.5°C accuracy from –10°C to +85°C
  • Thermometer resolution is user-selectable from 9 to 12 bits
  • Converts temperature to 12-bit digital word in 750ms (max.)
  • User-definable nonvolatile (NV) alarm settings
  • Alarm search command identifies and addresses devices whose temperature is outside of programmed limits (temperature alarm condition)
  • Applications include thermostatic controls, industrial systems, consumer products, thermometers, or any thermally sensitive system

Step2: สิ่งที่ต้องมีสําหรับ Project นี้

  • Arduino board (UNO, DUE, Micro, อื่นๆ..).
  • DS18B20 sensor
  • ตัวต้านทาน 4.7k

Step3: เชื่อมต่อวงจร DS18B20 sensor กับ Arduino

Image

ให้เชื่อมต่อวงจรดังรูปครับ

สําหรับ DS18B20 sensor จะมีอยู่ 3 ขาดังนี้

  • GND
  • Data
  • VCC (+5V หรือ +3.3V)

มีจุดสังเกตุคือในการต่อใช้งาน DS18B20 sensor จะต้องมีตัวต้านทานขนาด 4.7k Ohm ต่อ Pull Up เนื่องจาก DS18B20 sensor ใช้การสื่อสารแบบ 1-wire

Step5: Coding

Download Library สองตัวนี้มาก่อนะครับ

กลับไปที่ Arduino IDE เพื่อสร้าง Arduino Project ขึ้นมาโดยเข้าไปที่เมนู File—>new

แล้ว Import Library ที่โหลดมา โดยเข้าไปที่ Sketch —> Include Library —> Add .ZIP Library

Image

เมื่อทุกอย่างเรียบร้อยเราก็มาลงมือ Coding กันครับ

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

// กําหนด Pin 2 สําหรับ รับ-ส่ง ข้อมูล
#define ONE_WIRE_BUS 2

// สร้าง Instance ของ OneWire
OneWire oneWire(ONE_WIRE_BUS);

// สร้าง Instance ของ DallasTemperature
DallasTemperature sensors(&oneWire);

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}

void loop(void)
{
  // Send the command to get temperatures
  sensors.requestTemperatures();

  Serial.print("Temperature is: ");

  // อ่านข้อมูลจาก DS18B20 พร้อมทั้งส่งข้อมูลไปแสดงผลที่ Computer ผ่าน Serial port
  Serial.println(sensors.getTempCByIndex(0));

  delay(1000);
}

จากตัวอย่าง Code ด้านบนมีการเรียกใช้ Library 2 ตัวคือ OneWire.h และ DallasTemperature.h ซึ่ง 2 ตัวนี้จะทํางานร่วมกันดังนี้ OneWire.h จะทําหน้าที่ส่ง และรับข้อมูลในรูปแบบ 1-wire และ DallasTemperature.h จะทําหน้าที่ควบคุมการอ่านและเขียนข้อมูลสําหรับ DS18B20 Sensor

การทํางานของโปรแกรมด้านบนคือ โปรแกรมจะอ่านข้อมูลจาก DS18B20 Sensor ทุกๆ 1 วินาที แล้วส่งข้อมูลที่อ่านได้ไปแสดงผลบนคอมพิวเตอร์ผ่าน Serial port

เมื่อลอง Compile และ Upload code ลงบน Arduino board แล้วดูผลลัพธ์ทาง Serial monitor ก็จะแสดงข้อมูลของอุณหภูมิทุกๆ 1 วินาที แบบนี้

Image

ก็จะเป็นประมาณนี้นะครับ ติดตรงไหนสอบถามได้นะครับ สําหรับบทความนี้พอแค่นี้ก่อน แล้วเจอกันใหม่บทความหน้าครับ :)

ขอบคุณข้อมูล และรูปภาพจาก arduino.cc

Suggestion blogs

[Rectifier] วงจรเร็กติไฟร์

วงจรเร็กติไฟร์ หรือเรียกเป็นภาษาไทยว่า วงจรเรียงกระแส วงจรนี้จะทําหน้าที่แปลงไฟฟ้ากระแสสลับให้เป็นไฟฟ้ากระแสตรง โดยจะใช้ไดโอดเป็นอุปกรณ์หลักของวงจร ซึ่งชนิดของไดโอดที่นิยมนํามาใช้คือ ชนิดซิลิกอน วงจรเร็กติไฟร์มีอยู่ด้วยกัน 3 แบบ ดังนี้

แก้ปัญหาสแกน QR Code ของ EV Station PluZ ไม่ได้

สวัสดีครับ หลายๆคนที่ใช้งานรถไฟฟ้ามาได้สักระยะนึงก็อาจจะพบปัญหาในบ้างครั้งที่เราไปชาร์จรถที่ตู้ EV Station PluZ แล้ว QR Code ที่ตู้ไม่สามารถสแกนได้เนื่องจากถูกทําลาย และที่หนักไปกว่านั้น ที่หน้าจอก็ยังไม่ชัดอีก จนทําให้ไม่สามารถสแกน QR Code เพื่อจะชาร์จรถได้

Review Sierra 760S

สวัสดีครับ วันนี้จะมารีวิว Sierra 760S กันครับ Sierra 760S เป็น hotspot wifi ส่วนตัว หรือนิยมเรียกกันว่า mifi มีไว้สําหรับ share internet จาก sim card(AIS, true, Dtac, ฯลฯ) อุปกรณ์อื่นๆ ผ่านทางสัญญาน wifi


Copyright © 2019 - 2025 thiti.dev |  v1.58.0 |  Privacy policy | 

Build with ❤️ and Astro.

Github profile   Linkedin profile   Instagram   X profile   Nostr   Youtube channel   Telegram   Email contact   วงแหวนเว็บ