完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
ARDUINO入门教程(二)基本传感器的使用
温湿度(HTU21DF)、土壤湿度(Capacitive)、大气压强(GY-68 BMP180)、光传感器以及RFID的读取 这里是认知学习的第二堂课,主要内容是使用不同的传感器进行一些数据的采集 几种传感器 基本传感器的使用(以土壤湿度为例) const int AOUT = A0; //定义一个信号输入接口,将传感器上的AOUT接到A0 int inputValue = 0; void setup() { pinMode(AOUT, INPUT); digitalWrite(AOUT,LOW); Serial.begin(9600); } void loop() { inputValue = analogRead(AOUT); Serial.println(inputValue); delay(500); } rfid #include #include #include /*#include #include "Adafruit_HTU21DF.h" #include Adafruit_BMP085 bmp;*/ #define SS_PIN 4 #define RST_PIN 9 //定义引脚 /*MAX44009 light; Adafruit_HTU21DF htu = Adafruit_HTU21DF();*/ MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class MFRC522::MIFARE_Key key; /*const int analogInPin = A0;*/ long now; byte nuidPICC[4]; int flag=1; String num=""; int sensorValue = 0; void setup() { Serial.begin(115200); /*Serial.println("HTU21D-F test"); if (!htu.begin()) { Serial.println("Couldn't find sensor!"); while (1); }*/ /*gy-40*/ Wire.begin(); delay(500); /* if(light.begin()) { Serial.println("Could not find a valid MAX44009 sensor, check wiring!"); while(1); }*/ /*BMP085*/ /* if (!bmp.begin()) { Serial.println("Could not find a valid BMP085 sensor, check wiring!"); while (1) {} }*/ SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 for (byte i = 0; i < 6; i++) { key.keyByte = 0xFF; Serial.print("1"); } } void loop() { /*if(millis()-now>2000){ flag++; now=millis(); switch(flag){ case 1: num="{"params":{"Temp":"; //温度 num+=htu.readTemperature(); num+="}}"; Serial.println(num); num=""; num="{"params":{"Humidity":"; //湿度 num+=htu.readHumidity(); num+="}}"; Serial.println(num); num=""; break; case 2: num="{"params":{"Light":"; //光强 num+=light.get_lux(); num+="}}"; Serial.println(num); num=""; break; case 3: num="{"params":{"Pressure":"; //气压 num+=bmp.readPressure(); num+="}}"; Serial.println(num); num=""; break; case 4: sensorValue = analogRead(analogInPin); //土壤湿度 num="{"params":{"Soil":"; num+=sensorValue; num+="}}"; Serial.println(num); num=""; break; default: flag=0;break; } }*/ if ( ! rfid.PICC_IsNewCardPresent()) return; if ( ! rfid.PICC_ReadCardSerial()) return; printHex(rfid.uid.uidByte, rfid.uid.size); Serial.println(); rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); } void printHex(byte *buffer, byte bufferSize) { num="{"params":{"Rfid":""; //rfid for (byte i = 0; i < bufferSize; i++) { num+=(String)buffer; //Serial.print(buffer, HEX); } num+=""}}"; Serial.print(num); num=""; } 温湿度&压力传感器 /*#include #include #include /*#include #include "Adafruit_HTU21DF.h" #include Adafruit_BMP085 bmp; /*#define SS_PIN 4 #define RST_PIN 9 //定义引脚 MAX44009 light;*/ Adafruit_HTU21DF htu = Adafruit_HTU21DF(); /*MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class MFRC522::MIFARE_Key key; const int analogInPin = A0;*/ long now; byte nuidPICC[4]; int flag=1; String num=""; int sensorValue = 0; void setup() { Serial.begin(115200); Serial.println("HTU21D-F test"); if (!htu.begin()) { Serial.println("Couldn't find sensor!"); while (1); } /*gy-40*/ Wire.begin(); delay(500); /* if(light.begin()) { Serial.println("Could not find a valid MAX44009 sensor, check wiring!"); while(1); }*/ /*BMP085*/ if (!bmp.begin()) { Serial.println("Could not find a valid BMP085 sensor, check wiring!"); while (1) {} } /* SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 for (byte i = 0; i < 6; i++) { key.keyByte = 0xFF; Serial.print("1"); }*/ } void loop() { if(millis()-now>2000){ flag++; now=millis(); switch(flag){ case 1: num="{"params":{"Temp":"; //温度 num+=htu.readTemperature(); num+="}}"; Serial.println(num); num=""; num="{"params":{"Humidity":"; //湿度 num+=htu.readHumidity(); num+="}}"; Serial.println(num); num=""; break; /* case 2: num="{"params":{"Light":"; //光强 num+=light.get_lux(); num+="}}"; Serial.println(num); num=""; break;*/ case 2: num="{"params":{"Pressure":"; //气压 num+=bmp.readPressure(); num+="}}"; Serial.println(num); num=""; break; /* case 4: sensorValue = analogRead(analogInPin); //土壤湿度 num="{"params":{"Soil":"; num+=sensorValue; num+="}}"; Serial.println(num); num=""; break;*/ default: flag=0;break; } } /* if ( ! rfid.PICC_IsNewCardPresent()) return; if ( ! rfid.PICC_ReadCardSerial()) return; printHex(rfid.uid.uidByte, rfid.uid.size); Serial.println(); rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); } void printHex(byte *buffer, byte bufferSize) { num="{"params":{"Rfid":""; //rfid for (byte i = 0; i < bufferSize; i++) { num+=(String)buffer; //Serial.print(buffer, HEX); } num+=""}}"; Serial.print(num); num=""*/; } rfid&温湿度 #include #include #include /*#include #include "Adafruit_HTU21DF.h" /*#include Adafruit_BMP085 bmp;*/ #define SS_PIN 4 #define RST_PIN 9 //定义引脚 /*MAX44009 light;*/ Adafruit_HTU21DF htu = Adafruit_HTU21DF(); MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class MFRC522::MIFARE_Key key; /*const int analogInPin = A0;*/ long now; byte nuidPICC[4]; int flag=0; String num=""; int sensorValue = 0; void setup() { Serial.begin(115200); /*Serial.println("HTU21D-F test"); if (!htu.begin()) { Serial.println("Couldn't find sensor!"); while (1); }*/ /*gy-40 Wire.begin(); delay(500); if(light.begin()) { Serial.println("Could not find a valid MAX44009 sensor, check wiring!"); while(1); } */ /*BMP085*/ /* if (!bmp.begin()) { Serial.println("Could not find a valid BMP085 sensor, check wiring!"); while (1) {} }*/ SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 for (byte i = 0; i < 6; i++) { key.keyByte = 0xFF; Serial.print("1"); } } void loop() { if(millis()-now>2000){ flag++; now=millis(); /* switch(flag){ case 1:*/ num="{"params":{"Temp":"; //温度 num+=htu.readTemperature(); num+="}}"; Serial.println(num); num=""; num="{"params":{"Humidity":"; //湿度 num+=htu.readHumidity(); num+="}}"; Serial.println(num); num=""; /* break; case 2: num="{"params":{"Light":"; //光强 num+=light.get_lux(); num+="}}"; Serial.println(num); num=""; break; case 3: num="{"params":{"Pressure":"; //气压 num+=bmp.readPressure(); num+="}}"; Serial.println(num); num=""; break; case 4: sensorValue = analogRead(analogInPin); //土壤湿度 num="{"params":{"Soil":"; num+=sensorValue; num+="}}"; Serial.println(num); num=""; break; default: break; }*/ flag=0; } if ( ! rfid.PICC_IsNewCardPresent()) return; if ( ! rfid.PICC_ReadCardSerial()) return; printHex(rfid.uid.uidByte, rfid.uid.size); Serial.println(); rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); } void printHex(byte *buffer, byte bufferSize) { num="{"params":{"Rfid":""; //rfid for (byte i = 0; i < bufferSize; i++) { num+=(String)buffer; Serial.print(buffer, HEX); } num+=""}}"; Serial.print(num); num=""; } |
|
|
|
开启u***–arduino功能
这里只有3和4拨到ON其余拨到数字一端 接线方式 注意:所有传感器的vcc均使用3.3v! [tr]rc522arduino[/tr]
接线时注意尽量避免杜邦线的交错缠绕,为出错修后改做准备; 相应库文件的下载和安装方法 打开arduino IDE编译环境 点击 工具 下 管理库 在搜索框输入传感器型号,在这里直接输入头文件名称即可; 选择版本点击 安装 代码 #include #include #include #include #include "Adafruit_HTU21DF.h" #include Adafruit_BMP085 bmp; #define SS_PIN 4 #define RST_PIN 9 //定义引脚 MAX44009 light; Adafruit_HTU21DF htu = Adafruit_HTU21DF(); MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class MFRC522::MIFARE_Key key; const int analogInPin = A0; long now; byte nuidPICC[4]; int flag=1; String num=""; int sensorValue = 0; void setup() { Serial.begin(115200); Serial.println("HTU21D-F test"); if (!htu.begin()) { Serial.println("Couldn't find sensor!"); while (1); } /*gy-40*/ Wire.begin(); delay(500); if(light.begin()) { Serial.println("Could not find a valid MAX44009 sensor, check wiring!"); while(1); } /*BMP085*/ if (!bmp.begin()) { Serial.println("Could not find a valid BMP085 sensor, check wiring!"); while (1) {} } SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 for (byte i = 0; i < 6; i++) { key.keyByte = 0xFF; Serial.print("1"); } } void loop() { if(millis()-now>2000){ flag++; now=millis(); switch(flag){ case 1: num="{"params":{"Temp":"; //温度 num+=htu.readTemperature(); num+="}}"; Serial.println(num); num=""; num="{"params":{"Humidity":"; //湿度 num+=htu.readHumidity(); num+="}}"; Serial.println(num); num=""; break; case 2: num="{"params":{"Light":"; //光强 num+=light.get_lux(); num+="}}"; Serial.println(num); num=""; break; case 3: num="{"params":{"Pressure":"; //气压 num+=bmp.readPressure(); num+="}}"; Serial.println(num); num=""; break; case 4: sensorValue = analogRead(analogInPin); //土壤湿度 num="{"params":{"Soil":"; num+=sensorValue; num+="}}"; Serial.println(num); num=""; break; default: flag=0;break; } } if ( ! rfid.PICC_IsNewCardPresent()) return; if ( ! rfid.PICC_ReadCardSerial()) return; printHex(rfid.uid.uidByte, rfid.uid.size); Serial.println(); rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); } void printHex(byte *buffer, byte bufferSize) { num="{"params":{"Rfid":""; //rfid for (byte i = 0; i < bufferSize; i++) { num+=(String)buffer; //Serial.print(buffer, HEX); } num+=""}}"; Serial.print(num); num=""; } 串口观察数据 波特率设置为115200 |
|
|
|
只有小组成员才能发言,加入小组>>
3290 浏览 9 评论
2963 浏览 16 评论
3467 浏览 1 评论
9011 浏览 16 评论
4056 浏览 18 评论
1131浏览 3评论
580浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
575浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2307浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1869浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-1 10:59 , Processed in 1.218049 second(s), Total 80, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号