โมดูล AM2301 (DHT21) เป็นเซนเซอร์วัดอุณหภูมิและความชื้นสัมพัทธ์แบบดิจิทัล และเชื่อมต่อด้วยสัญญาณเพียงเส้นเดียวแบบสองทิศทาง (bidirectional) ใช้แรงดันไฟเลี้ยงได้ในช่วง 3.3V ถึง 5.2V สามารถวัดค่าอุณหภูมิได้ในช่วง -40..80°C ความละเอียดในการวัดอุณหภูมิและความชื้น คือ 0.5°C และ 0.1%RH และมีความแม่นยำ ±0.5°C และ ±3%RH ตามลำดับ ใช้ขาเชื่อมต่อเพียง 3 ขา ได้แก่ VCC, GND และ SDA (serial data) ในการอ่านข้อมูลแต่ละครั้ง จะอ่านข้อมูลทั้งหมด 40 บิต แบ่งเป็น 16 บิต สำหรับค่าความชื้น 16 บิต สำหรับค่าอุณหภูมิ และ 8 บิต สำหรับตรวจสอบค่า parity bits เพื่อดูว่าอ่านค่าได้ถูกต้องหรือไม่ ![]() รูปแสดงโมดูลเซนเซอร์วัดอุณหภูมิและความชื้นสัมพัทธ์ โมเดล AM2301 (ด้านหลัง) ![]() รูปแสดงขาสำหรับการเชื่อมต่อของโมดูล AM2301 สายสีแดงสำหรับ VCC (ป้อนแรงดัน +5V) สายสีดำคือ GND และสายสีเหลืองคือ SDA ![]() รูปแสดงโปรโตคอลในการรับส่งข้อมูลโดยใช้สายสัญญาณเส้นเดียวของโมดูล AM2301 ศึกษารายละเอียดเพิ่มเติมได้จากเอกสารของผู้ผลิต: am2301_datasheet.pdf ![]() รูปแสดงตัวอย่างคอนเนคเตอร์ (female housing and crimp pins) ![]() รูปแสดงสายไฟที่ได้ใส่คอนเนคเตอร์แบบตัวเมีย เพื่อให้สะดวกต่อการนำไปใช้งาน |
Arduino Sketchตัวอย่างโค้ดนี้ สาธิตการอ่านค่าจากโมดูล AM2301 โดยต่อกับขา Arduino D2 และจะอ่านค่าใหม่เว้นระยะประมาณ 2 วินาที ค่าที่ได้จะส่งออกทาง Serial (ใช้ baudrate 115200) File: am2301_sensor_reading.ino ///////////////////////////////////////////////////////////////////////////////////// // Author: RSP @ ESL (Embedded System Lab), KMUTNB, Thailand // Date: 2014-03-12 // Board: Arduino with ATmega168/328P (5V/16MHz) // Arduino IDE: version 1.0.5 // Description: // This Arduino sketch demonstrates how to read the temperature // and relative humidity values from the AM2301 digital sensor module // manufactured by Aosong Electronics Co.,Ltd. // // For wiring: // Black wire = GND // Red wire = +5V (recommended) // Yellow wire = SDA (serial data) signal connected to the Arduino D2 pin #define AM2301_SDA_PIN 2 #define IS_SDA_HIGH() digitalRead( AM2301_SDA_PIN ) #define SDA_HIGH() digitalWrite( AM2301_SDA_PIN,HIGH ) #define SDA_LOW() digitalWrite( AM2301_SDA_PIN, LOW ) #define SDA_INPUT() pinMode( AM2301_SDA_PIN,INPUT ) #define SDA_OUTPUT() pinMode( AM2301_SDA_PIN,OUTPUT ) // Read one byte from the SDA line (MSB first) // When calling this function, the SDA line must be LOW. byte read_byte() { byte result = 0x00; for( byte i=0; i< 8; i++ ) { while( !IS_SDA_HIGH() ); // wait until SDA goes HIGH. delayMicroseconds(30); // wait for 30 usec result <<= 1; if ( IS_SDA_HIGH() ) { // if SDA is HIGH, this bit is 1. result |= 1; } while( IS_SDA_HIGH() ); // wait until SDA goes LOW. } return result; } int read_sensor_values( int16_t values[2] ) { byte buf[5]; byte checksum = 0x00; // send start signal SDA_LOW(); delay(1); SDA_HIGH(); // release the bus and wait for the response signal SDA_INPUT(); delayMicroseconds(30); if ( IS_SDA_HIGH() ){ // LOW expected //Serial.println( "SDA is expected to be LOW."); SDA_OUTPUT(); SDA_HIGH(); return -1; } delayMicroseconds(80); if ( !IS_SDA_HIGH() ){ // HIGH expected //Serial.println( "SDA is expected to be HIGH."); return -2; } delayMicroseconds(80); // Now the SDA line must be LOW. for ( byte i=0, value; i<5; i++ ) { value = read_byte(); buf[i] = value; if ( i < 4 ) { checksum += value; } } SDA_OUTPUT(); SDA_HIGH(); if ( buf[4] != checksum ) { return -3; // checksum error } int16_t t = (buf[2] << 8) + buf[3]; // temperature if (t & 0x8000) { t &= 0x7fff; t = -t; } values[0] = t; values[1] = (buf[0] << 8) + buf[1]; // relative humidity return 0; } void setup() { SDA_OUTPUT(); SDA_HIGH(); Serial.begin( 115200 ); } char sbuf[20]; void loop() { int16_t v, values[2]; if ( !read_sensor_values( values ) ) { v = values[0]; sprintf( sbuf, "Temperature= %d.%d C", v/10, v%10 ); Serial.print( sbuf ); v = values[1]; sprintf( sbuf, ", Humidity= %d.%d%c", v/10, v%10, '%' ); Serial.println( sbuf ); values[0] = values[1] = 0; } else { Serial.println( "Sensor reading error!" ); } delay(2000); // wait at least 2 seconds } ///////////////////////////////////////////////////////////////////////////////////// ![]() รูปแสดงการต่อวงจรเชื่อมต่อระหว่างบอร์ด Arduino และโมดูล AM2301 ![]() รูปแสดงค่าที่ได้จากโมดูลเซนเซอร์ซึ่งปรากฏในหน้าต่างของ Arduino Serial Monitor เมื่อโมดูลเซนเซอร์ว่างอยู่ใกล้ช่องระบายลมร้อนของเครื่องคอมพิวเตอร์ และเห็นได้ว่า อุณหภูมิจะค่อยๆสูงขึ้น ![]() รูปแสดงการใช้งานบอร์ด Arduino ร่วมกับโมดูล AM2301 และ 16x2 LCD (ต่อผ่านไอซี PCF8574 แบบบัส I2C) เพื่อวัดค่าจากเซนเซอร์และแสดงผล Arduino Sketch: am2301_sensor_reading_with_lcd.ino |
ที่มา
http://cpre.kmutnb.ac.th/esl/learning/index.php?article=am2301-sensor
ขอบคุณครับ ที่แวะมาอุดหนุนทางร้าน