12293浏览
查看: 12293|回复: 11

[项目] 液晶温湿度计兼时钟-DHT11,ds1307,5110液晶

[复制链接]
本项目目标:DHT11,ds1307,5110液晶综合运用来做一个带温度,湿度的时钟,使用9V供电


本项目使用到的模块:
Arduino uno主控*1(包括USB线)
Nokia 5110 LCD液晶模组*1
DHT11温度传感器*1
DS1307时钟RTC模块*1
杜邦线*大量
木板雕刻件(参考附件图纸)
螺丝M3*10及螺母*30组(容易掉多备点)
9V电池及接插件

本项目使用到的工具:
万用表(有了比较方便)
螺丝刀适合M3螺丝的就行
热熔胶枪(也可以用胶水之类的,主要用来固定线缆保证可靠性)


本项目使用到的软件及库文件:
Arduino IDE1.05R2
用到的库DHT11,DS1307
机械方面需要用到Solidworks,autocad
库文件   DHT11及RTClib









  1. /*
  2. Author:simon
  3. Date:2016Feb28
  4. IDE Version:1.0.5R2
  5. Mission:液晶温湿度计兼时钟-DHT11,ds1307,5110液晶综合运用
  6. */
  7. #include <dht11.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <Wire.h>
  11. #include <RTClib.h>
  12. void printDateTime(DateTime dateTime);
  13. //创建实例
  14. RTC_DS1307 RTC;
  15. //RTC时钟DS1307模块====SCL连主控板A5
  16. //RTC时钟DS1307模块====SDA连主控板A4
  17. //RTC时钟DS1307模块====GND连主控板GND
  18. //RTC时钟DS1307模块====VCC连主控板5V
  19. /* 日期变量缓存 */
  20. char buf[50];
  21. char day[10];
  22. /* 串口数据缓存 */
  23. String comdata = "";
  24. int numdata[7] ={0}, j = 0, mark = 0;
  25. dht11 DHT11;
  26. #define DHT11PIN A0 ///温度传感器连接到A0
  27. //端口定义
  28. ///液晶模块 VCC连到5V
  29. ///液晶模块 GND连到GND
  30. ///液晶模块 BLK连到5V背光LED
  31. int LCD_CE=3;   //液晶模块 CE连到3
  32. int LCD_RST=2;//液晶模块 RST连到2
  33. int SCLK=7;//液晶模块 CLK连到7
  34. int SDIN=6;//液晶模块 Din连到6
  35. int LCD_DC=5;//液晶模块 DC连到5
  36. double Fahrenheit(double celsius)
  37. {
  38. return 1.8 * celsius + 32;
  39. } //摄氏温度度转化为华氏温度
  40. double Kelvin(double celsius)
  41. {
  42. return celsius + 273.15;
  43. } //摄氏温度转化为开氏温度
  44. // 露点(点在此温度时,空气饱和并产生露珠)
  45. // 参考: http://wahiduddin.net/calc/density_algorithms.htm
  46. double dewPoint(double celsius, double humidity)
  47. {
  48. double A0= 373.15/(273.15 + celsius);
  49. double SUM = -7.90298 * (A0-1);
  50. SUM += 5.02808 * log10(A0);
  51. SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
  52. SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
  53. SUM += log10(1013.246);
  54. double VP = pow(10, SUM-3) * humidity;
  55. double T = log(VP/0.61078); // temp var
  56. return (241.88 * T) / (17.558-T);
  57. }
  58. // 快速计算露点,速度是5倍dewPoint()
  59. // 参考: http://en.wikipedia.org/wiki/Dew_point
  60. double dewPointFast(double celsius, double humidity)
  61. {
  62. double a = 17.271;
  63. double b = 237.7;
  64. double temp = (a * celsius) / (b + celsius) + log(humidity/100);
  65. double Td = (b * temp) / (a - temp);
  66. return Td;
  67. }
  68. //****************************定义ASCII字符**********************//
  69. /**********************************
  70. 6 x 8 font
  71. 1 pixel space at left and bottom
  72. index = ASCII - 32
  73. ***********************************/
  74. const unsigned char font6x8[][6] =
  75. {
  76. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // sp
  77. { 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 }, // !
  78. { 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 }, // "
  79. { 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 }, // #
  80. { 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 }, // $
  81. { 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 }, // %
  82. { 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 }, // &
  83. { 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 }, // '
  84. { 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 }, // (
  85. { 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 }, // )
  86. { 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 }, // *
  87. { 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 }, // +
  88. { 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 }, // ,
  89. { 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 }, // -
  90. { 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 }, // .
  91. { 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 }, // /
  92. { 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E }, // 0
  93. { 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 }, // 1
  94. { 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 }, // 2
  95. { 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 }, // 3
  96. { 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 }, // 4
  97. { 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 }, // 5
  98. { 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 }, // 6
  99. { 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 }, // 7
  100. { 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 }, // 8
  101. { 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E }, // 9
  102. { 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 }, // :
  103. { 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 }, // ;
  104. { 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 }, // <
  105. { 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 }, // =
  106. { 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 }, // >
  107. { 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 }, // ?
  108. { 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E }, // @
  109. { 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C }, // A
  110. { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 }, // B
  111. { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 }, // C
  112. { 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C }, // D
  113. { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 }, // E
  114. { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 }, // F
  115. { 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A }, // G
  116. { 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F }, // H
  117. { 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 }, // I
  118. { 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 }, // J
  119. { 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 }, // K
  120. { 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 }, // L
  121. { 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F }, // M
  122. { 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F }, // N
  123. { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E }, // O
  124. { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 }, // P
  125. { 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E }, // Q
  126. { 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 }, // R
  127. { 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 }, // S
  128. { 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 }, // T
  129. { 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F }, // U
  130. { 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F }, // V
  131. { 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F }, // W
  132. { 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 }, // X
  133. { 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 }, // Y
  134. { 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 }, // Z
  135. { 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 }, // [
  136. { 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55 }, // 55
  137. { 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 }, // ]
  138. { 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 }, // ^
  139. { 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 }, // _
  140. { 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 }, // '
  141. { 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 }, // a
  142. { 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 }, // b
  143. { 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 }, // c
  144. { 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F }, // d
  145. { 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 }, // e
  146. { 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 }, // f
  147. { 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C }, // g
  148. { 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 }, // h
  149. { 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 }, // i
  150. { 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 }, // j
  151. { 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 }, // k
  152. { 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 }, // l
  153. { 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 }, // m
  154. { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 }, // n
  155. { 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 }, // o
  156. { 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 }, // p
  157. { 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC }, // q
  158. { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 }, // r
  159. { 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 }, // s
  160. { 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 }, // t
  161. { 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C }, // u
  162. { 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C }, // v
  163. { 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C }, // w
  164. { 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 }, // x
  165. { 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C }, // y
  166. { 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 }, // z
  167. { 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 } // horiz lines
  168. };
  169. /************************LCD初始化函数********************************/
  170. void LCD_init(void)
  171. {
  172. //先设置为输出
  173. pinMode(SCLK,OUTPUT);
  174. pinMode(SDIN,OUTPUT);
  175. pinMode(LCD_DC,OUTPUT);
  176. pinMode(LCD_CE,OUTPUT);
  177. pinMode(LCD_RST,OUTPUT);
  178. // 产生一个让LCD复位的低电平脉冲
  179. digitalWrite( LCD_RST, LOW);
  180. delayMicroseconds(1);
  181. digitalWrite( LCD_RST, HIGH);
  182. // 关闭LCD
  183. digitalWrite( LCD_CE, LOW);
  184. delayMicroseconds(1);
  185. // 使能LCD
  186. digitalWrite( LCD_CE, HIGH); //LCD_CE = 1;
  187. delayMicroseconds(1);
  188. LCD_write_byte(0x21, 0); // 使用扩展命令设置LCD模式
  189. LCD_write_byte(0xb8, 0); // 设置偏置电压
  190. LCD_write_byte(0x06, 0); // 温度校正
  191. LCD_write_byte(0x13, 0); // 1:48
  192. LCD_write_byte(0x20, 0); // 使用基本命令
  193. LCD_clear(); // 清屏
  194. LCD_write_byte(0x0c, 0); // 设定显示模式,正常显示
  195. // 关闭LCD
  196. digitalWrite( LCD_CE, LOW); //LCD_CE = 0;
  197. }
  198. /************************LCD清屏函数*************Robocat.com.cn******************/
  199. void LCD_clear(void)
  200. {
  201. unsigned int i;
  202. LCD_write_byte(0x0c, 0);
  203. LCD_write_byte(0x80, 0);
  204. for (i=0; i<504; i++)
  205. {
  206. LCD_write_byte(0, 1);
  207. }
  208. }
  209. /*************************设置字符位置函数**************************/
  210. void LCD_set_XY(unsigned char X, unsigned char Y)
  211. {
  212. LCD_write_byte(0x40 | Y, 0);// column
  213. LCD_write_byte(0x80 | X, 0);// row
  214. }
  215. /*************************ASCII字符显示函数*************************/
  216. void LCD_write_char(unsigned char c)
  217. {
  218. unsigned char line;
  219. c -= 32;
  220. for (line=0; line<6; line++)
  221. {
  222. LCD_write_byte(font6x8[c][line], 1);
  223. }
  224. }
  225. /*******************************************************************/
  226. /*-------------------------------------------------
  227. LCD_write_english_String : 英文字符串显示函数
  228. 输入参数:*s :英文字符串指针;
  229. X、Y : 显示字符串的位置,x 0-83 ,y 0-5
  230. --------------------------------------------------*/
  231. void LCD_write_english_string(unsigned char X,unsigned char Y,char *s)
  232. {
  233. LCD_set_XY(X,Y);
  234. while (*s)
  235. {
  236. LCD_write_char(*s);
  237. s++;
  238. }
  239. }
  240. /******************************************************************/
  241. /*---------------------------------------------
  242. LCD_write_byte : 写数据到LCD
  243. 输入参数:data :写入的数据;
  244. command :写数据/命令选择;
  245. ---------------------------------------------*/
  246. void LCD_write_byte(unsigned char dat, unsigned char command)
  247. {
  248. unsigned char i;
  249. digitalWrite( LCD_CE, LOW); // 使能LCD_CE = 0
  250. if (command == 0)
  251. {
  252. digitalWrite( LCD_DC, LOW);// 传送命令 LCD_DC = 0;
  253. }
  254. else
  255. {
  256. digitalWrite( LCD_DC, HIGH);// 传送数据LCD_DC = 1;
  257. }
  258. for(i=0;i<8;i++)
  259. {
  260. if(dat&0x80)
  261. {
  262. digitalWrite( SDIN, HIGH);//SDIN = 1;
  263. }
  264. else
  265. {
  266. digitalWrite( SDIN, LOW);//SDIN = 0;
  267. }
  268. digitalWrite( SCLK, LOW);//SCLK = 0;
  269. dat = dat << 1;
  270. digitalWrite( SCLK, HIGH);//SCLK = 1;
  271. }
  272. digitalWrite( LCD_CE, HIGH);//LCD_CE = 1;
  273. }
  274. /******************************************************************/
  275. void setup()
  276. {
  277. delay(10);
  278. Serial.begin(9600);
  279. LCD_init();//初始化液晶
  280. LCD_clear();
  281. delay(10) ;
  282. //初始化总线
  283. Wire.begin();
  284. //初始化实时时钟
  285. RTC.begin();
  286. }
  287. void loop()
  288. {
  289. DateTime now = RTC.now();
  290. char str1[10]="",str2[10];
  291. String str=String(DHT11.temperature);
  292. str1[0]=str[0];str1[1]=str[1];str1[2]=str[2];str1[3]=str[3];str1[4]=str[4];str1[5]=str[5];str1[6]=str[6];str1[7]=str[7];
  293. //strcpy(str1,str2);
  294. LCD_write_english_string(0,0,"Temp(oC): ") ;
  295. int chk = DHT11.read(DHT11PIN);
  296. LCD_write_english_string(72,0,str1) ;
  297. str=String(DHT11.humidity);
  298. str1[0]=str[0];str1[1]=str[1];str1[2]=str[2];str1[3]=str[3];str1[4]=str[4];str1[5]=str[5];str1[6]=str[6];str1[7]=str[7];
  299. //strcpy(str1,str2);
  300. LCD_write_english_string(0,2,"Humid ity:") ;
  301. chk = DHT11.read(DHT11PIN);
  302. LCD_write_english_string(72,2,str1) ;
  303. LCD_write_english_string(0,3,"Robocat") ;
  304. //rtc
  305. LCD_write_english_string(0,4,"T:") ;
  306. String str_second="";
  307. if (now.second()<10)
  308. {
  309. str_second="0";
  310. }
  311. String str_minute="";
  312. if (now.minute()<10)
  313. {
  314. str_minute="0";
  315. }
  316. String str_hour="";
  317. if (now.hour()<10)
  318. {
  319. str_hour="0";
  320. }
  321. str=str_hour+String(now.hour()) +":"+str_hour+String(now.minute())+":"+str_second+String(now.second());
  322. str1[0]=str[0];str1[1]=str[1];str1[2]=str[2];str1[3]=str[3];str1[4]=str[4];str1[5]=str[5];str1[6]=str[6];str1[7]=str[7];
  323. LCD_write_english_string(20,4,str1) ;
  324. LCD_write_english_string(0,5,"D:") ;
  325. str=String(now.year()) +"-"+String(now.month())+"-"+String(now.day());
  326. str1[0]=str[0];str1[1]=str[1];str1[2]=str[2];str1[3]=str[3];str1[4]=str[4];str1[5]=str[5];str1[6]=str[6];str1[7]=str[7];
  327. str=String(" ");
  328. LCD_write_english_string(18,5,str1) ;
  329. LCD_write_english_string(18,5,str1) ;
  330. //Serial.print("Read sensor: ");
  331. switch (chk)
  332. {
  333. case DHTLIB_OK:
  334. Serial.println("OK");
  335. break;
  336. case DHTLIB_ERROR_CHECKSUM:
  337. Serial.println("Checksum error");
  338. break;
  339. case DHTLIB_ERROR_TIMEOUT:
  340. Serial.println("Time out error");
  341. break;
  342. default:
  343. Serial.println("Unknown error");
  344. break;
  345. }
  346. Serial.print("Hour;");
  347. Serial.println(now.hour());
  348. delay(200) ;
  349. }
复制代码



下载附件气象站.rar

iooops  中级技匠

发表于 2016-3-4 21:28:40

啊好像很复杂的样子…………
回复

使用道具 举报

大连林海  初级技神
 楼主|

发表于 2016-3-4 21:30:41

iooops 发表于 2016-3-4 21:28
啊好像很复杂的样子…………

一个朋友做的 很牛的
回复

使用道具 举报

iooops  中级技匠

发表于 2016-3-4 22:59:39

大连林海 发表于 2016-3-4 21:30
一个朋友做的 很牛的

回复

使用道具 举报

dsweiliang  初级技神

发表于 2016-3-5 14:01:53

你朋友做的,算不算是原创贴?
回复

使用道具 举报

virtualwiz  中级技匠

发表于 2016-3-5 19:42:04

有激光机真好呀,分分钟做外壳:lol
回复

使用道具 举报

大连林海  初级技神
 楼主|

发表于 2016-3-6 07:13:39

dsweiliang 发表于 2016-3-5 14:01
你朋友做的,算不算是原创贴?

朋友的原创 不过他发在其他的论坛了 我觉得很好 就发过来 让大家看看
回复

使用道具 举报

大连林海  初级技神
 楼主|

发表于 2016-3-6 07:13:51

virtualwiz 发表于 2016-3-5 19:42
有激光机真好呀,分分钟做外壳

可惜 我没有 领导不给买
回复

使用道具 举报

maomaopcy  学徒

发表于 2018-3-16 20:06:20

dht11这个库如何安装呢
回复

使用道具 举报

kimi423520  学徒

发表于 2018-6-24 14:37:41

温湿度计用DHT-22 是不是会更好,
回复

使用道具 举报

Johnnie168  见习技师

发表于 2018-11-18 14:28:10

感谢分享!
回复

使用道具 举报

jfMTScEaoh0s  见习技师

发表于 2022-3-5 20:22:42

啊,不用U8GLIB吗?直接通信很烦
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail