9064浏览
查看: 9064|回复: 0

Arduino控制的温度、湿度、亮度测量仪

[复制链接]
温度、湿度、亮度测量仪
介绍:
大家好,我是Jason Chen,一个来自上海的学生。由于是电子信息专业的学生,所以希望能够应用课堂知识做些东西出来。但由于这是第一次接触Arduino平台,所以先做一个简单的小东西和大家分享一下。今天我做的这个东西是一个比较常见的温度、湿度、亮度测量仪。在现在这个社会上到处都需要这么一个这类装置,甚至家中都可以有这么一个小型的测量仪来监控家里的环境。
通过一个温度湿度传感器和一个亮度传感器,我能读到相关数值。再通过一个显示屏将读取到的数据显示出来。制作三个显示页面,分别显示温度、湿度、亮度和传感器上读到的数据。并用上下按键切换显示页面。
硬件:
软件:
l  Arduino1.0.5
l  LiquidCrystallibrary
教程:
1、 连接硬件
首先将arduino板和带扩展板的lcd屏幕连在一起。将扩展板背后的长脚接口与arduino板上的接口对其插紧
Arduino控制的温度、湿度、亮度测量仪图6Arduino控制的温度、湿度、亮度测量仪图4
然后分别将一个温度湿度传感器和一个亮度传感器连接在扩展板的右下角的模拟接口上。由于拿到的传感器是将三根导线排在一起的,但是顺序却跟扩展板上的接口顺序不一样。这个时候就需要将三个线的顺序拔出调整,并对好黑线接地,红线接5V,蓝线接模拟口。需要注意的是,uno板上有六个模拟口,但是由于扩展板上有按键,占据了analog 0,只剩下5个模拟口可供感应器连接。
Arduino控制的温度、湿度、亮度测量仪图1Arduino控制的温度、湿度、亮度测量仪图5

Arduino控制的温度、湿度、亮度测量仪图2Arduino控制的温度、湿度、亮度测量仪图3

分别组装好的时候是这样的:
Arduino控制的温度、湿度、亮度测量仪图7Arduino控制的温度、湿度、亮度测量仪图8
2、 编程
首先为了熟悉两个感应器的运行,先将产品wiki上的sample code输入arduino 1.0.5中,并upload进arduino板中。如果连接正确这时候屏幕上应该是有正确的示数。如果不能正确显示请检查sample code中定义的感应器的接口是否正确(默认的接口是0,但扩展板上的模拟0接口已被按键占用)
我自己编写的代码是这样的:
  1. #include <LiquidCrystal.h>
  2. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  3.   int n,m;
  4.   // define some values used by the panel and buttons
  5. int lcd_key     = 0;
  6. int adc_key_in  = 0;
  7. #define btnRIGHT  0
  8. #define btnUP     1
  9. #define btnDOWN   2
  10. #define btnLEFT   3
  11. #define btnSELECT 4
  12. #define btnNONE   5
  13. #define DHT11_PIN 1
  14. // read the buttons
  15. int read_LCD_buttons()
  16. {
  17. adc_key_in = analogRead(0);      // read the value from the sensor
  18. // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  19. // we add approx 50 to those values and check to see if we are close
  20. if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  21. // For V1.1 us this threshold
  22.    if (adc_key_in < 50)   return btnRIGHT;
  23.      if (adc_key_in < 250)  return btnUP;
  24.        if (adc_key_in < 450)  return btnDOWN;
  25.          if (adc_key_in < 650)  return btnLEFT;
  26.            if (adc_key_in < 850)  return btnSELECT;
  27. return btnNONE;  // when all others fail, return this...
  28. }
  29. byte read_dht11_dat()
  30. {
  31.   byte i = 0;
  32.   byte result=0;
  33.   for(i=0; i< 8; i++)
  34.   {
  35.     while(!(PINC & _BV(DHT11_PIN)))
  36.     {};  // wait  forever until anlog input port 0 is '1'   (NOTICE: PINC reads all the analog input ports
  37.     //and  _BV(X) is the macro operation which pull up positon 'X'to '1' and the rest positions to '0'. it is equivalent to 1<<X.)
  38.     delayMicroseconds(30);
  39.     if(PINC & _BV(DHT11_PIN))  //if analog input port 0 is still '1' after 30 us
  40.       result |=(1<<(7-i));     //this position is 1
  41.     while((PINC & _BV(DHT11_PIN)));  // wait '1' finish
  42.     }
  43.     return result;
  44. }
  45. void setup() {
  46.    Serial.begin(9600);
  47.    Serial.println("Start");
  48.   lcd.begin(16, 2);
  49.    DDRC |= _BV(DHT11_PIN);   //let analog port 0 be output port
  50.   PORTC |= _BV(DHT11_PIN);
  51. }
  52. void loop() {
  53.   lcd.setCursor(0,0);
  54.   lcd_key = read_LCD_buttons();
  55. Serial.println(lcd_key);
  56.   byte dht11_dat[5];   
  57.   byte dht11_in;
  58.   byte i;// start condition
  59.   PORTC &= ~_BV(DHT11_PIN);    // 1. pull-down i/o pin for 18ms
  60.   delay(18);
  61.   PORTC |= _BV(DHT11_PIN);     // 2. pull-up i/o pin for 40us
  62.   delayMicroseconds(1);
  63.   DDRC &= ~_BV(DHT11_PIN);     //let analog port 0 be input port
  64.   delayMicroseconds(40);     
  65.   
  66.   dht11_in = PINC & _BV(DHT11_PIN);  // read only the input port 0
  67.   if(dht11_in)
  68.   {
  69.     Serial.println("dht11 start condition 1 not met"); // wait for DHT response signal: LOW
  70.     delay(1000);
  71.     return;
  72.   }
  73.   delayMicroseconds(80);
  74.   dht11_in = PINC & _BV(DHT11_PIN); //
  75.   if(!dht11_in)
  76.   {
  77.     Serial.println("dht11 start condition 2 not met");  //wair for second response signal:HIGH
  78.     return;
  79.   }
  80.   
  81.   delayMicroseconds(80);// now ready for data reception
  82.   for (i=0; i<5; i++)
  83.   {  dht11_dat[i] = read_dht11_dat();}  //recieved 40 bits data. Details are described in datasheet
  84.   
  85.   DDRC |= _BV(DHT11_PIN);      //let analog port 0 be output port after all the data have been received
  86.   PORTC |= _BV(DHT11_PIN);     //let the  value of this port be '1' after all the data have been received
  87.   byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];// check check_sum
  88.   if(dht11_dat[4]!= dht11_check_sum)
  89.   {
  90.     Serial.println("DHT11 checksum error");
  91.   }
  92.      switch (lcd_key)
  93.       {
  94.      case btnUP:
  95.        {
  96.        n=n+1;
  97.        if(n==3)n=0;
  98.        break;
  99.        }
  100.       case btnDOWN:
  101.        {
  102.        n=n-1;
  103.        if(n==-1)n=2;
  104.         break;
  105.       }
  106.      }
  107.   switch(n)
  108.   {
  109.     case 0:{
  110.     lcd.clear();
  111.     lcd.write("TEMPERATURE:");
  112.     Serial.println("TEMPERATURE");
  113.     lcd.print(dht11_dat[2], DEC);
  114.     lcd.print(".");
  115.     lcd.print(dht11_dat[3], DEC);
  116.     lcd.println("C  ");
  117.     delay(200);
  118.     break;
  119.    }
  120.    case 1:{
  121.     lcd.clear();
  122.     lcd.write("HUMIDITY:");
  123.     Serial.println("HUMIDITY");
  124.     lcd.print(dht11_dat[0], DEC);
  125.     lcd.print(".");
  126.     lcd.print(dht11_dat[1], DEC);
  127.     lcd.print("%  ");
  128.     delay(200);
  129.     break;
  130.    }
  131.    case 2:{
  132.     lcd.clear();
  133.     lcd.write("LIGHT:");
  134.     Serial.println("LIGHT");
  135.     int sensorValue = analogRead(A5);
  136.     lcd.setCursor(7,0);
  137.     lcd.print(sensorValue);
  138.     delay(200);
  139.     break;
  140.    }
  141.   }
  142. }
复制代码
总结:
在做每个小产品之前,首先得熟悉每个材料的工作原理,通过查询每个器件的wiki,了解原理图和连接方式。例如将带键盘扩展板的1602 屏幕连在uno板之后,运行samplecode将在屏幕上显示按键信息和运行时间。在连接亮度传感器或温度湿度传感器的时候需要注意导线的排列方式。以及samplecode默认的接口和真实接的是不同的。此处的感应器的接口将在编写代码的时候修改到。编写代码的时候主要用到两个传感器和屏幕按键的代码。但是在合并的时候却会有许多的问题。我并没有使用菜单库,而是使用了最基本的switch语句,分别设置三个显示页面分别为n = 0、1、2,在按UP键的时候执行n+1,按down的时候执行n-1,并判断当n=3的时候执行n=0,当n=-1的时候执行n=2,这样就能够变成循环切换菜单。这个时候还需要在每个显示页面输入一个清屏代码,不然的话,再切换的时候,会出现上一个页面的残留显示。这时候还会出现一个问题,按一次键的时候会执行好几次循环,导致菜单不停的跳。这个时候就需要设置按键释放检测,判断按键是否是一直按住,否则时间过长就会导致不停的做循环。(其实这一步我自己还没有在代码中体现出来,希望大家能提供一些意见)
这是我第一次发表文章,还有很多改进的地方,希望大家能共同学习,共同进步!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
关于楼主
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail