كود البحث

الدرس 36: استخدام مستشعر الحرارة HTU21D مع شاشة LCD دورة خطوة بخطوة لأردوينو

الدرس 36: استخدام مستشعر الحرارة HTU21D مع شاشة LCD دورة خطوة بخطوة لأردوينو

يعتبر حساس الحرارة والرطوبة HTU21D خيارًا شائعًا للعديد من مشاريع Arduino نظرًا لسهولة استخدامه ودقته. في هذا الدرس، سنقوم ببناء مشروع بسيط يقرأ بيانات الحرارة والرطوبة من الحسّاس ويعرضها على شاشة LCD. بنهاية هذا الدليل، سيكون لديك إعداد يعمل يعرض باستمرار درجة الحرارة بالدرجات المئوية والفهرنهايت والكلفن، بالإضافة إلى نسبة الرطوبة النسبية. لمزيد من التوضيح، يمكنك الرجوع إلى الفيديو (في الفيديو عند :ss).

شرح الأجهزة

المكون الرئيسي في هذا المشروع هو مستشعر الحرارة والرطوبة HTU21D، الذي يتواصل عبر I2C. يعمل هذا المستشعر ضمن نطاق جهد يتراوح بين 1.5 إلى 3.6 فولت، مما يجعله مناسبًا لأنظمة 3.3 فولت و5 فولت. يستهلك طاقة قليلة جدًا، عادةً فقط 0.02 ميكرو أمبير في حالة الخمول و450 ميكرو أمبير أثناء القياس. بالإضافة إلى HTU21D، سنستخدم أيضًا شاشة LCD، تحديدًا LCD1602 مع I2C. هذه الشاشة تتيح إخراج بيانات نصية بسهولة وتتطلب فقط دبابيسين للتواصل: SDA (خط البيانات) وSCL (خط الساعة). سيمكننا دمج هذه المكونات من إنشاء عرض معلوماتي لمراقبة مستويات الحرارة والرطوبة.

تفاصيل ورقة البيانات

شركة التصنيعتي إي كونكتيفيتي
رقم الجزءHTU21D-F
جهد المنطق/المدخلات والمخرجات1.5 - 3.6 ف
جهد الإمداد3.3 فولت (متوسط)
تيار الخرج (متوسط)٠٫٠٢ ميكروأمبير (خامل)، ٤٥٠ ميكروأمبير (قياس)
مجموعة درجات الحرارة-40 إلى +125 درجة مئوية
نطاق الرطوبة0 إلى 100 ٪ رطوبة نسبية
دقة (درجة الحرارة)0.01 درجة مئوية
الدقة (الرطوبة)0.04 % رطوبة نسبية
حزمةDFN-6

  • استخدم مقاوم سحب لأعلى لخطوط SDA و SCL إذا لم تكن مدمجة.
  • تأكد من توفير جهد مصدر الطاقة الصحيح لتفادي تلف المستشعر.
  • الحفاظ على الأسلاك بشكل صحيح لمنع أخطاء الاتصال.
  • تحقق من عنوان I2C إذا لم يستجب المستشعر.
  • استخدم فترة تأخير بين القياسات لمنع تحميل المستشعر.
  • تأكد من أن شاشة LCD متوافقة مع التواصل I2C.

تعليمات التوصيل

لربط مستشعر HTU21D وعرض LCD، ابدأ باتصالات الطاقة. قم بتوصيل الطرف الأيسر من HTU21D بمصدر الطاقة 3.3 فولت، والطرف الثاني (غالبًا أحمر) بالأرض. بعد ذلك، قم بتوصيل طرف SDA من HTU21D بالطرف التناظري A4 في الأردوينو، وطرف SCL بالطرف التناظري A5. بالنسبة لعرض LCD1602، قم بتوصيل الطرف VCC بنفس مصدر الطاقة 3.3 فولت، وقم بتوصيل الطرف GND بالأرض. يجب أن يتصل الطرف SDA في LCD أيضًا بـ A4، ويجب أن يتصل الطرف SCL بـ A5، مما يسمح لكلا المكونين بمشاركة ناقل I2C. تأكد من أن جميع الاتصالات آمنة لتسهيل الاتصال الصحيح بين الأردوينو والمستشعر والعرض.

أمثلة الشفرة وشرح الخطوات

الكود التالي يقوم بتهيئة مستشعر HTU21D وشاشة LCD. في دالة الإعداد، يتم تحضير شاشة LCD للاستخدام والتحقق من الاتصال بالمستشعر:

void setup() {
  lcd.begin();
  lcd.backlight();
  if (!htu.begin()) {
      lcd.print("Robojax HTUD1DF");
      lcd.setCursor(0,1);
      lcd.print("sensor missing"); 
      while (1);
  } else {
      lcd.print("Robojax HTUD1DF");
      lcd.setCursor(0,1);
      lcd.print("Demo"); 
  }
  delay(2000);   
}

يتحقق هذا المقطع مما إذا كان المستشعر متصلاً بشكل صحيح. إذا لم يكن كذلك، فإنه يعرض رسالة خطأ على شاشة LCD ويتوقف البرنامج. إذا كان المستشعر يعمل، فإنه يعرض رسالة تجريبية لمدة ثانيتين. وظيفة الحلقة هي المكان الذي تحدث فيه القراءة الرئيسية والعرض. هنا، نستدعي دالة `lcdDisplay` لعرض درجة الحرارة بوحدات مختلفة:

void loop() {
   lcd.clear(); // clear previous values from screen
   lcdDisplay(0, 0, "Celsius: ", 10, 0, getHTU('C'), 'd');  
   lcdDisplay(0, 1, "Fahrenheit: ", 10, 1, getHTU('F'), 'd');     
   delay(5000);
}

في هذه الحلقة، يتم مسح شاشة LCD، وعرض قراءة درجات الحرارة بالدرجات المئوية والفهرنهايت. يتم استدعاء دالة `getHTU` بالحرف 'C' للدرجات المئوية و'F' للفهرنهايت، على التوالي. أخيرًا، يتم تعريف دالة `getHTU` لقراءة درجة الحرارة أو الرطوبة بناءً على الحرف المدخل:

float getHTU(char type) {
   float temp = htu.readTemperature();
   float rel_hum = htu.readHumidity();
   if(type =='F') {
       return temp * 9/5 + 32; // convert to Fahrenheit 
   } else if(type =='K') {
       return temp + 273.15; // convert to Kelvin
   } else if(type =='H') {
       return rel_hum; // return relative humidity
   } else {
       return temp; // return Celsius
   }
}

تقوم هذه الوظيفة بقراءة درجة الحرارة والرطوبة من المستشعر وتحويل درجة الحرارة إلى الوحدة المطلوبة. تأكد من مراجعة الشيفرة الكاملة التي تم تحميلها أسفل المقال للحصول على تفاصيل إضافية.

عرض / ماذا تتوقع

عند الانتهاء من توصيل الأسلاك وتحميل الكود، ينبغي أن ترى قيم درجة الحرارة والرطوبة تعرض على شاشة LCD. ستتحدث القراءات كل few seconds، تعكس الظروف الحالية. إذا قمت بتطبيق الحرارة على المستشعر، ينبغي أن تلاحظ ارتفاع درجة الحرارة بشكل متناسب، بينما ينبغي أن تنخفض الرطوبة قليلاً. كن حذرًا من الحد الأقصى لدرجة الحرارة للمستشعر؛ حيث أن تجاوز ذلك قد يؤدي إلى قراءات غير دقيقة أو فشل المستشعر (في الفيديو عند mm:ss).

طوابع الفيديو

  • ٠٠:٠٠- مقدمة عن المشروع
  • ١:١٥- تعليمات التوصيل
  • 03:30- استعراض الشيفرة
  • 10:00- عرض الإعداد
511-Lesson 36: Using the HTU21D Temperature Sensor with an LCD and Arduino: A Step-by-Step Course
اللغة: C++
/*
 * Robojax Arduino Step-by-Step Course
 * Part 4: Temperature Sensors
 * Lesson 36: HTU21D Temperature Sensor with LCD1602 and LCD2004 Display
 


 * Display Temperature from HTU21DF on LCD1602-I2C or LCD2004
 * Updated by Ahmad Shamshiri on July 13, 2019



  Please watch video instructions here https://youtu.be/SrFJKbmiaPM
 This code is available at http://robojax.com/course1/?vid=lecture36
 
with over 100 lectures free on YouTube. Watch it here http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339  

If you found this tutorial helpful, please support me so I can continue creating 
and make a donation using PayPal http://robojax.com/L/?id=64



*
  This is an example for the HTU21D-F Humidity & Temp Sensor

  Designed specifically to work with the HTU21D-F sensor from Adafruit
  ----> https://www.adafruit.com/products/1899

  These displays use I2C to communicate; 2 pins are required to
  interface

 Watch Introduction to a 360 Servo video with code: https://youtu.be/b_xvu6wWafA
You can get the wiring diagram from my Arduino Course at Udemy.com
Learn Arduino step by step with all libraries, codes, and wiring diagrams all in one place.
Visit my course now http://robojax.com/L/?id=62

If you found this tutorial helpful, please support me so I can continue creating 
content like this. 
or make a donation using PayPal http://robojax.com/L/?id=64

 * Code is available at http://robojax.com/learn/arduino

 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. 

*/

#include <Wire.h>
#include "Adafruit_HTU21DF.h"

// Connect Vin to 3-5VDC
// Connect GND to ground
// Connect SCL to I2C clock pin (A5 on UNO)
// Connect SDA to I2C data pin (A4 on UNO)

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

// start of settings for LCD1602 with I2C
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
// end of settings for LCD1602 with I2C

void setup() {
	//Get the code for the course: http://robojax.com/L/?id=339  
  lcd.begin();  
  lcd.backlight();
  if (!htu.begin()) {
      lcd.print("Robojax HTUD1DF");
      lcd.setCursor(0,1);
      lcd.print("sensor missing"); 
    while (1);
  }else{
  // initialize the LCD

  lcd.print("Robojax HTUD1DF");
  lcd.setCursor(0,1);
  lcd.print("Demo"); 
  }
  delay(2000);   
}

void loop() {
//Get the code for the course: http://robojax.com/L/?id=339  	
   lcd.clear();// clear previous values from screen
lcdDisplay(
             // to print Celsius:
             0, // character 0 
             0, // line 0
             "Celsius: ", 

             // to print Celsius
             10, // character 10
             0, // line 0
             getHTU('C'),
             'd'
             );  

  lcdDisplay(
             // to print fahrenheit:
             0, // character 0 
             1, // line 1
             "Fahrenheit: ", 

             // to print Fahrenheit
             10, // character 9
             1, // line 0
             getHTU('F'),
             'd'
             );     
    delay(5000);
lcdDisplay(
             // to print Kelvin:
             0, // character 0 
             0, // line 0
             "Kelvin: ", 

             // to print Celsius
             9, // character 10
             0, // line 0
             getHTU('K'),
             'k'
             );      
  lcdDisplay(
             // to print humidity text
             0, // character 0 
             1, // line 1
             "Humidity: ", 

             // to print humidity
             10, // character 9
             1, // line 1
             getHTU('H'),
             '%' 
             );  
   
        delay(5000);
}

/*
 * @brief returns temperature or relative humidity
 * @param "type" is character
 *     C = Celsius
 *     K = Kelvin
 *     F = Fahrenheit
 *     H = Humidity
 * @return returns one of the values above
 * Usage: to get Fahrenheit type: getHTU('F')
 * to print it on serial monitor Serial.println(getHTU('F'));
 * Written by Ahmad Shamshiri on July 13, 2019
 * in Ajax, Ontario, Canada
 * www.Robojax.com 
 */
float getHTU(char type)
{
	//Get the code for the course: http://robojax.com/L/?id=339  
  float value;
    float temp = htu.readTemperature();
    float rel_hum = htu.readHumidity();
   if(type =='F')
   {
    value = temp *9/5 + 32;//convert to Fahrenheit 
   }else if(type =='K')
   {
    value = temp + 273.15;//convert to Kelvin
   }else if(type =='H')
   {
    value = rel_hum;//return relative humidity
   }else{
    value = temp;// return Celsius
   }
   return value;
}//


/*
 * lcdDisplay(int tc, int tr, String title, int vc, int vr, float value)
 * displays value and title on LCD1602
 * How to use:
 * If you want to display: "Voltage: 13.56mV" starting from the first character
 * on the second row.
 * use:
 * lcdDisplay(0, 1, "Voltage: ", 13.56,'d')
 *   
 *   'd' is degree symbol
 * tc  is character number  (0)
 * tr is row in the lcd (1)
 * title is the text (Voltage:)
 * vc value for character 
 * vr value for  row or line
 * value is the value (13.56)
 */
void lcdDisplay(int tc, int tr, String title, int vc, int vr, float value,char symbol)
{
   // Robojax.com LCD1602 for HTU21D Demo
   lcd.setCursor (tc,tr); //
   lcd.print(title);
   
   lcd.setCursor (vc,vr); //
   lcd.print(value);
   if(symbol == 'd')
   {
    lcd.print((char)223);
   }else if(symbol =='%')
   {
    lcd.print("%");
   }else if(symbol =='k')
   {
    lcd.print("K");
   }
 // Robojax.com LCD1602 for HTU21D Demo
}

الأشياء التي قد تحتاجها

ملفات📁

ورقة البيانات (pdf)