arduino+继电器+电机
应用场合:加湿器、自动浇花、智能门锁、报警系统.......总之很多场合都适用。本章就介绍利用RFID卡输入,驱动水泵。
/* * -------------------------------------------------------------------------------------------------------------------- * Typical pin layout used: * ----------------------------------------------------------------------------------------- * MFRC522 Arduino Arduino Arduino Arduino Arduino * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro * Signal Pin Pin Pin Pin Pin Pin * ----------------------------------------------------------------------------------------- * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST * SPI SS SDA(SS) 10 53 D10 10 10 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 */#include #include #define SS_PIN 10#define RST_PIN 9 MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the classbyte nuidPICC[4] = {0X19,0XDF,0XA0,0XB8}; //判断是否是正确触发的卡号byte nuidPICC1[4] = {0XA0,0X14,0XC8,0X32};void setup() { Serial.begin(9600); SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 pinMode(2, OUTPUT); } void loop() { digitalWrite(2,LOW); // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. if ( ! rfid.PICC_IsNewCardPresent()) return; // Verify if the NUID has been readed if ( ! rfid.PICC_ReadCardSerial()) return; if (rfid.uid.uidByte[0] == nuidPICC[0] && rfid.uid.uidByte[1] == nuidPICC[1] && rfid.uid.uidByte[2] == nuidPICC[2] && rfid.uid.uidByte[3] == nuidPICC[3] ) { Serial.println(F("Card read OK.")); digitalWrite(2,LOW); delay(1000); digitalWrite(2,HIGH); delay(10000); } else if(rfid.uid.uidByte[0] == nuidPICC1[0] && rfid.uid.uidByte[1] == nuidPICC1[1] && rfid.uid.uidByte[2] == nuidPICC1[2] && rfid.uid.uidByte[3] == nuidPICC1[3] ) { Serial.println(F("Card read OK.")); digitalWrite(2,LOW); delay(1000); digitalWrite(2,HIGH); delay(10000); } else Serial.println(F("Card read previously."));}
这段代码也是参考官方的自己改的,亲测成功。需要注意的是供电问题,我测试时是用电脑USB给arduino供的电,另外继电器和水泵的电源都接到了arduino上,结果烧录完代码,继电器是不受控制的,吸合一下子就跳开,驱动不了水泵。然后我就把它们的电源分开供电之后问题解决。我也有看别人做的时候也会出现各种问题,就是提醒一下,希望新手在尝试时多注意一下干扰问题和电源问题。附上一个用51单片机和AVR驱动水泵的链接,可以一起参考。
http://www.51hei.com/bbs/dpj-185543-1.html