基于arduino的农业大棚环境监测系统
注:本文系湛江市第十七中学星火创客团队及岭南师范学院物联网俱乐部原创部分参赛项目,转载请保留声明。
一、硬件准备
二、软件准备
开发环境 版本 arduino IDE 1.8.13(自己看着办) windows 10(自己看着办)1、arduino IDE下载:https://www.arduino.cc/en/software
三、项目部分
注:本项目按照本人习惯来进行,谢谢谅解!
1、DHT11温湿度传感器测试
(1)接线
(2)相关代码
#include "DHT.h" //自己看哪个数字引脚顺眼就定义那个 #define DHTPIN 2 // Digital pin connected to the DHT sensor // Uncomment whatever type you're using! //自己用的什么类型自己选择,看着办。 #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 //#define DHTTYPE DHT21 // DHT 21 (AM2301) DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); Serial.println(F("DHTxx test!")); dht.begin(); } void loop() { // Wait a few seconds between measurements. delay(2000); // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println(F("Failed to read from DHT sensor!")); return; } // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false); Serial.print(F("Humidity: ")); Serial.print(h); Serial.print(F("% Temperature: ")); Serial.print(t); Serial.print(F("°C ")); Serial.print(f); Serial.print(F("°F Heat index: ")); Serial.print(hic); Serial.print(F("°C ")); Serial.print(hif); Serial.println(F("°F")); }
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758(3)效果图
2、光照强度传感器测试
(1)接线
(2)相关代码
//自己看哪个模拟输出引脚顺眼就定义那个 #define Sunshine A0 //定义AO 引脚 为 IO-A0 //#define DO 7 //定义DO 引脚 为 IO-7,如果接线为7,代码就定义为7 void setup() { pinMode(Sunshine, INPUT);//定义A0为输入模式,将AO所测得的数据输入到计算机中 // pinMode(DO, INPUT);//定义DO为输入模式,将DO所测得的数据输入到计算机中 Serial.begin(9600);/*使用串口与计算机通信,需要先使用Serial.begin() 初始化Arduino的串口通信功能*/ } void loop() { sunshine_detection(); } //Sunshine值越高,光照强度值越小,Sunshine值越低,光照强度值越大 void sunshine_detection(void){ //串口返回测量数据 Serial.print("Sunshine=");/*串口初始化完成后,我们便可以使用Serial.print() 或Serial.println() 向计算机发送信息了。*/ Serial.println(analogRead(Sunshine));/*读取AO的数值。它可以将外部输入的模拟信号转换为芯片运算时可以识别的数字信号,从而实现读入模拟值的功能。模拟输入功能需要使用analogRead() 函数。*/ // Serial.print("|DO="); // Serial.println(digitalRead(DO));/*读取DO的数值。返回值为获取到的信号状态,1为高电平,0为低电平。*/ delay(2000); }
1234567891011121314151617181920212223242526(3)效果图
3、LCD1602和按键测试
(1)接线
arduino引脚 LCD1602(这里已经接了一个IIC模块) GND GND 5V VCC A5 (orSCL) SCL A4(orSDA) SDA arduino引脚 4×4键盘 3 R1 4 R2 5 R3 6 R4 7 C1 9 C2 10 C3 11 C4(2)相关代码
#include <stdio.h> #include <string.h> #include<Keypad.h> //LingShun lab #1234