บทความประกอบการเรียนรู้ => ไมโครคอนโทรลเลอร์ (Arduino) => ข้อความที่เริ่มโดย: admin ที่ กุมภาพันธ์ 15, 2016, 02:19:55 PM

หัวข้อ: งานครั้งที่ 43 เขียนโปรแกรมอ่านค่าอุณหภูมิจาก DS18B20 แสดงผลที่ LCD16x2
เริ่มหัวข้อโดย: admin ที่ กุมภาพันธ์ 15, 2016, 02:19:55 PM
เขียนโปรแกรมอ่านค่าอุณหภูมิจาก DS18B20 แสดงผลที่  LCD16x2

ไลบารี่เพิ่มเติม
-OneWire.h https://github.com/PaulStoffregen/OneWire (https://github.com/PaulStoffregen/OneWire)
-DallasTemperature.h https://github.com/milesburton/Arduino-Temperature-Control-Library (https://github.com/milesburton/Arduino-Temperature-Control-Library)
*ดูวิธีติดตั้งในงานครั้งที่ 20 (http://www.praphas.com/forum/index.php?topic=266.0)
*ไลบารี่ LiquidCrystal.h มีมาให้แล้วพร้อมโปรแกรม Arduino IDE ไม่ต้องติดตั้งเพิ่ม

โจทย์การทดลอง
-เขียนโปรแกรมรับค่าอุณหภูมิที่อ่านจาก DS18B20 แสดงผลที่  LCD16x2
-โปรแกรมรายละเอียดพิเศษรายกลุ่ม (แจ้งให้ทราบเมื่อถึงชั่วโมงเรียน)

วงจรที่ใช้ในการทดลองสำหรับผู้ที่ใช้บอร์ด Arduino
(http://www.praphas.com/PhotoForum/arduino/Lab-43-01.png)

วงจรที่ใช้ในการทดลองสำหรับผู้ที่ใช้ไอซี ATmega328 (ที่มี Boot Loader Arduino)
(http://www.praphas.com/PhotoForum/arduino/Lab-43-02.png)

ตัวอย่างโปรแกรมอ่านค่าจาก DS18B20
โค๊ด: [Select]
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(2);                 //Connect to Pin 2 (D2)
DallasTemperature sensors(&oneWire);
void setup(void)
{
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC DS18B20");
  sensors.begin();
}
void loop(void)
{
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures();            // Send the command to get temperatures
  Serial.println("DONE");
  Serial.print("Temperature is: ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.println(" Degrees C");
  Serial.print("Temperature is: ");
  Serial.print(sensors.getTempFByIndex(0));
  Serial.println(" Degrees F");
}

ตัวอย่างโปรแกรมแสดงผล LCD16x2
โค๊ด: [Select]
#include <LiquidCrystal.h>
/* The circuit:
 * LCD RS pin to digital pin 7
 * LCD E  pin to digital pin 6
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
*/
LiquidCrystal lcd(7,6,5,4,3,2);       // set up the LCD's connection pins
void setup()
{
  lcd.begin(16, 2);       // set up the LCD's number of columns and rows:
  lcd.print("hello, world!");
}
void loop() {}