[College project] System of locking / unlocking with relay shield

Hello .
I'm in french college and I have a final project for my baccalaureate. I have a machine : with the relay shield v2.1 : http://www.dfrobot.com/index.php?route= ... ymaAoU3OgY , I have to create a new system for replace the switch of the machine and "create" a new "switch" with a RFID shield and the relay shield. When I pass my good swipe card on the shield RFID the machine starts. But before that I have to program the relay shield with Arduino to create this new switch and remove the old switch.
I found a code for the relay shield but I'm not sure if it will help me. I have to create a new code with the relay shield code and the rfid shield code
Code: Select all/* # This Sample code is for testing the Relay shield V2.1 for Arduino. # Editor : Phoebe # Date : 2013.2.28 # Ver : 0.1 # Product: Relay shield for Arduino # SKU : DRI0144 # Hardwares: 1. Arduino UNO 2. Relay Shield For Arduino V2.1 3 Power Supply:7~ 12V */ byte relayPin[4] = { 2,7,8,10}; //D2 -> RELAY1 //D7 -> RELAY2 //D8 -> RELAY3 //D10 -> RELAY char input=0; int val; void setup() { for(int i = 0; i < 4; i++) pinMode(relayPin[i],OUTPUT); Serial.begin(57600); delay(100); Serial.println("Press 1-4 to control the state of the relay"); Serial.println("waiting for input:"); for(int j = 0; j < 4; j++) digitalWrite(relayPin[j],LOW); } void loop() { if (Serial.available()) { char input= Serial.read(); if(input != -1) { switch(input) { case '1': Serial.println("Relay1"); val=digitalRead(relayPin[0]); val=!val; digitalWrite(relayPin[0],val); break; case '2': Serial.println("Relay2"); val=digitalRead(relayPin[1]); val=!val; digitalWrite(relayPin[1],val); break; case '3': Serial.println("Relay3"); val=digitalRead(relayPin[2]); val=!val; digitalWrite(relayPin[2],val); break; case '4': Serial.println("Relay4"); val=digitalRead(relayPin[3]); val=!val; digitalWrite(relayPin[3],val); break; default: if(input != '\r' && input != '\n') Serial.println("invalid entry"); break; } } // else unablerelay(); } }
and this is the RFID code for reading the swipe cards :
Code: Select all#include <Wire.h> #include <Adafruit_NFCShield_I2C.h> #define IRQ (2) #define RESET (3) // Not connected by default on the NFC Shield Adafruit_NFCShield_I2C nfc(IRQ, RESET); void setup(void) { Serial.begin(9600); Serial.println("Hello!"); nfc.begin(); // configure board to read RFID tags nfc.SAMConfig(); Serial.println("Waiting for an ISO14443A Card ..."); } void loop(void) { uint8_t success; uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) // Wait for an ISO14443A type cards (Mifare, etc.). When one is found // 'uid' will be populated with the UID, and uidLength will indicate // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); if (success) { // Display some basic information about the card Serial.println("Found an ISO14443A card"); Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); Serial.print(" UID Value: "); nfc.PrintHex(uid, uidLength); Serial.println(""); if (uidLength == 4) { // We probably have a Mifare Classic card ... Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); // Now we need to try to authenticate it for read/write access // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF Serial.println("Trying to authenticate block 4 with default KEYA value"); uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // Start with block 4 (the first block of sector 1) since sector 0 // contains the manufacturer data and it's probably better just // to leave it alone unless you know what you're doing success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya); if (success) { Serial.println("Sector 1 (Blocks 4..7) has been authenticated"); uint8_t data[16]; // If you don't want to write something to block 4 to test with, comment // the following line and this text should be read back in a minute // WARNING ! Write only on block 4, 5 and 6; 8, 9 and 10; memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data); success = nfc.mifareclassic_WriteDataBlock (4, data); // Try to read the contents of block 4 success = nfc.mifareclassic_ReadDataBlock(4, data); if (success) { // Data seems to have been read ... spit it out Serial.println("Reading Block 4:"); nfc.PrintHexChar(data, 16); Serial.println(""); // Wait a bit before reading the card again delay(1000); } else { Serial.println("Ooops ... unable to read the requested block. Try another key?"); } } else { Serial.println("Ooops ... authentication failed: Try another key?"); } } if (uidLength == 7) { // We probably have a Mifare Ultralight card ... Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)"); // Try to read the first general-purpose user page (#4) Serial.println("Reading page 4"); uint8_t data[32]; success = nfc.mifareultralight_ReadPage (4, data); if (success) { // Data seems to have been read ... spit it out nfc.PrintHexChar(data, 4); Serial.println(""); // Wait a bit before reading the card again delay(1000); } else { Serial.println("Ooops ... unable to read the requested page!?"); } } } }
Thanks for your help , and if you don't understand something you can ask me .
Is quite simple really, I think you might still have time. What are the next steps? Anyway here are some tips:
Use the RFID and simply attach the relay example lines.
Basically there are 3 main points.
- Set your Relay pin
- Set the pinMode() to avoid strange things happening, this is done on the Setup() loop better
- Find the right stop to trigger the Relay when the card is found.
if (success)
then relay on
and in the else
relay off
more coding will be needed, but it might help you to get some ideas.
Cheers^^
btw, thanks for coping the code again


Is quite simple really, I think you might still have time. What are the next steps? Anyway here are some tips:
Use the RFID and simply attach the relay example lines.
Basically there are 3 main points.
- Set your Relay pin
- Set the pinMode() to avoid strange things happening, this is done on the Setup() loop better
- Find the right stop to trigger the Relay when the card is found.
if (success)
then relay on
and in the else
relay off
more coding will be needed, but it might help you to get some ideas.
Cheers^^
btw, thanks for coping the code again


The RFID code :
#include <Wire.h> #include <Adafruit_NFCShield_I2C.h> #define IRQ (2) #define RESET (3) // Not connected by default on the NFC Shield Adafruit_NFCShield_I2C nfc(IRQ, RESET); void setup(void) { Serial.begin(9600); Serial.println("Hello!"); nfc.begin(); // configure board to read RFID tags nfc.SAMConfig(); Serial.println("Waiting for an ISO14443A Card ..."); } void loop(void) { uint8_t success; uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) // Wait for an ISO14443A type cards (Mifare, etc.). When one is found // 'uid' will be populated with the UID, and uidLength will indicate // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); if (success) { // Display some basic information about the card Serial.println("Found an ISO14443A card"); Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); Serial.print(" UID Value: "); nfc.PrintHex(uid, uidLength); Serial.println(""); if (uidLength == 4) { // We probably have a Mifare Classic card ... Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); // Now we need to try to authenticate it for read/write access // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF Serial.println("Trying to authenticate block 4 with default KEYA value"); uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // Start with block 4 (the first block of sector 1) since sector 0 // contains the manufacturer data and it's probably better just // to leave it alone unless you know what you're doing success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya); if (success) { Serial.println("Sector 1 (Blocks 4..7) has been authenticated"); uint8_t data[16]; // If you don't want to write something to block 4 to test with, comment // the following line and this text should be read back in a minute // WARNING ! Write only on block 4, 5 and 6; 8, 9 and 10; memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data); success = nfc.mifareclassic_WriteDataBlock (4, data); // Try to read the contents of block 4 success = nfc.mifareclassic_ReadDataBlock(4, data); if (success) { // Data seems to have been read ... spit it out Serial.println("Reading Block 4:"); nfc.PrintHexChar(data, 16); Serial.println(""); // Wait a bit before reading the card again delay(1000); } else { Serial.println("Ooops ... unable to read the requested block. Try another key?"); } } else { Serial.println("Ooops ... authentication failed: Try another key?"); } } if (uidLength == 7) { // We probably have a Mifare Ultralight card ... Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)"); // Try to read the first general-purpose user page (#4) Serial.println("Reading page 4"); uint8_t data[32]; success = nfc.mifareultralight_ReadPage (4, data); if (success) { // Data seems to have been read ... spit it out nfc.PrintHexChar(data, 4); Serial.println(""); // Wait a bit before reading the card again delay(1000); } else { Serial.println("Ooops ... unable to read the requested page!?"); } } } }
/* # Product: 16A Relay Module # SKU : DFR0251 # Description: # This sample code is combined with the figure above to test the relay module whether works normally. # Link: # D -- 2 // Signal # GND -- GND # VCC -- VCC # COM -- 13 // INPUT # NO -- LED + // OUTPUT # LED - -- GND */ int Relay = 3; void setup() { pinMode(13, OUTPUT); //Set Pin13 as output digitalWrite(13, HIGH); //Set Pin13 High pinMode(Relay, OUTPUT); //Set Pin3 as output } void loop() { digitalWrite(Relay, HIGH); //Turn off relay delay(2000); digitalWrite(Relay, LOW); //Turn on relay delay(2000); }

The RFID code :
#include <Wire.h> #include <Adafruit_NFCShield_I2C.h> #define IRQ (2) #define RESET (3) // Not connected by default on the NFC Shield Adafruit_NFCShield_I2C nfc(IRQ, RESET); void setup(void) { Serial.begin(9600); Serial.println("Hello!"); nfc.begin(); // configure board to read RFID tags nfc.SAMConfig(); Serial.println("Waiting for an ISO14443A Card ..."); } void loop(void) { uint8_t success; uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) // Wait for an ISO14443A type cards (Mifare, etc.). When one is found // 'uid' will be populated with the UID, and uidLength will indicate // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); if (success) { // Display some basic information about the card Serial.println("Found an ISO14443A card"); Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); Serial.print(" UID Value: "); nfc.PrintHex(uid, uidLength); Serial.println(""); if (uidLength == 4) { // We probably have a Mifare Classic card ... Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); // Now we need to try to authenticate it for read/write access // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF Serial.println("Trying to authenticate block 4 with default KEYA value"); uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // Start with block 4 (the first block of sector 1) since sector 0 // contains the manufacturer data and it's probably better just // to leave it alone unless you know what you're doing success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya); if (success) { Serial.println("Sector 1 (Blocks 4..7) has been authenticated"); uint8_t data[16]; // If you don't want to write something to block 4 to test with, comment // the following line and this text should be read back in a minute // WARNING ! Write only on block 4, 5 and 6; 8, 9 and 10; memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data); success = nfc.mifareclassic_WriteDataBlock (4, data); // Try to read the contents of block 4 success = nfc.mifareclassic_ReadDataBlock(4, data); if (success) { // Data seems to have been read ... spit it out Serial.println("Reading Block 4:"); nfc.PrintHexChar(data, 16); Serial.println(""); // Wait a bit before reading the card again delay(1000); } else { Serial.println("Ooops ... unable to read the requested block. Try another key?"); } } else { Serial.println("Ooops ... authentication failed: Try another key?"); } } if (uidLength == 7) { // We probably have a Mifare Ultralight card ... Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)"); // Try to read the first general-purpose user page (#4) Serial.println("Reading page 4"); uint8_t data[32]; success = nfc.mifareultralight_ReadPage (4, data); if (success) { // Data seems to have been read ... spit it out nfc.PrintHexChar(data, 4); Serial.println(""); // Wait a bit before reading the card again delay(1000); } else { Serial.println("Ooops ... unable to read the requested page!?"); } } } }
/* # Product: 16A Relay Module # SKU : DFR0251 # Description: # This sample code is combined with the figure above to test the relay module whether works normally. # Link: # D -- 2 // Signal # GND -- GND # VCC -- VCC # COM -- 13 // INPUT # NO -- LED + // OUTPUT # LED - -- GND */ int Relay = 3; void setup() { pinMode(13, OUTPUT); //Set Pin13 as output digitalWrite(13, HIGH); //Set Pin13 High pinMode(Relay, OUTPUT); //Set Pin3 as output } void loop() { digitalWrite(Relay, HIGH); //Turn off relay delay(2000); digitalWrite(Relay, LOW); //Turn on relay delay(2000); }





I'd appreciate a picture of your machine, and your current electronics, not exactly the plan. But you can add the plan as well, more information will let us recommend you better.

I'd appreciate a picture of your machine, and your current electronics, not exactly the plan. But you can add the plan as well, more information will let us recommend you better.



Have you considered this module:
https://www.dfrobot.com/index.php?route= ... uct_id=992
You don't really need an LED, is just a test approach. You could use the Serial port directly I guess. You don't have any LED around?
Do you have a picture of your current hardware system?

Have you considered this module:
https://www.dfrobot.com/index.php?route= ... uct_id=992
You don't really need an LED, is just a test approach. You could use the Serial port directly I guess. You don't have any LED around?
Do you have a picture of your current hardware system?

Well , I need a LED then ? To try the reading of cards ?
I saw technical caracteristics and the shield is not enough powerful so I need another one which is more powerful , do you have another shield to recommend me ? I can't try it now until I have another one. The tension of the motor of the machine is 24VDC and the power is 200 W.
Thanks for help Jose.

Well , I need a LED then ? To try the reading of cards ?
I saw technical caracteristics and the shield is not enough powerful so I need another one which is more powerful , do you have another shield to recommend me ? I can't try it now until I have another one. The tension of the motor of the machine is 24VDC and the power is 200 W.
Thanks for help Jose.

This project looks very interesting, This machine is like a segway? I don't see the wheels, so I am quite confused about how it works.
I don't have the adafruit NFC module, but the relay shield is very simple to use! is like light an LED. The only thing you need to take care of are the pins so its not in conflict with the other attached hardware.
I think you can try the NFC example, if the card is read, lit the LED. Just for test, stand alone and no relay. If this works, attach the Relay shield and change the LED pin to the Relay pin and try again.
Keep in mind, this module needs external power supply:7~ 12V testing from the usb might not work well if the whole hardware setup requires more power.

This project looks very interesting, This machine is like a segway? I don't see the wheels, so I am quite confused about how it works.
I don't have the adafruit NFC module, but the relay shield is very simple to use! is like light an LED. The only thing you need to take care of are the pins so its not in conflict with the other attached hardware.
I think you can try the NFC example, if the card is read, lit the LED. Just for test, stand alone and no relay. If this works, attach the Relay shield and change the LED pin to the Relay pin and try again.
Keep in mind, this module needs external power supply:7~ 12V testing from the usb might not work well if the whole hardware setup requires more power.


I'm in french college and I have a final project for my baccalaureate. I have a machine : http://www.ewee.fr/boutique/ewee-pt.htm ... re=default and with the relay shield v2.1 : https://www.dfrobot.com/index.php?route= ... ymaAoU3OgY , I have to create a new system for replace the switch of the machine and "create" a new "switch" with a RFID shield and the relay shield. When I pass my good swipe card on the shield RFID the machine starts. But before that I have to program the relay shield with Arduino to create this new switch and remove the old switch.
I found a code for the relay shield but I'm not sure if it will help me. I have to create a new code with the relay shield code and the rfid shield code
/* # This Sample code is for testing the Relay shield V2.1 for Arduino. # Editor : Phoebe # Date : 2013.2.28 # Ver : 0.1 # Product: Relay shield for Arduino # SKU : DRI0144 # Hardwares: 1. Arduino UNO 2. Relay Shield For Arduino V2.1 3 Power Supply:7~ 12V */ byte relayPin[4] = { 2,7,8,10}; //D2 -> RELAY1 //D7 -> RELAY2 //D8 -> RELAY3 //D10 -> RELAY char input=0; int val; void setup() { for(int i = 0; i < 4; i++) pinMode(relayPin[i],OUTPUT); Serial.begin(57600); delay(100); Serial.println("Press 1-4 to control the state of the relay"); Serial.println("waiting for input:"); for(int j = 0; j < 4; j++) digitalWrite(relayPin[j],LOW); } void loop() { if (Serial.available()) { char input= Serial.read(); if(input != -1) { switch(input) { case '1': Serial.println("Relay1"); val=digitalRead(relayPin[0]); val=!val; digitalWrite(relayPin[0],val); break; case '2': Serial.println("Relay2"); val=digitalRead(relayPin[1]); val=!val; digitalWrite(relayPin[1],val); break; case '3': Serial.println("Relay3"); val=digitalRead(relayPin[2]); val=!val; digitalWrite(relayPin[2],val); break; case '4': Serial.println("Relay4"); val=digitalRead(relayPin[3]); val=!val; digitalWrite(relayPin[3],val); break; default: if(input != '\r' && input != '\n') Serial.println("invalid entry"); break; } } // else unablerelay(); } }
#include <Wire.h> #include <Adafruit_NFCShield_I2C.h> #define IRQ (2) #define RESET (3) // Not connected by default on the NFC Shield Adafruit_NFCShield_I2C nfc(IRQ, RESET); void setup(void) { Serial.begin(9600); Serial.println("Hello!"); nfc.begin(); // configure board to read RFID tags nfc.SAMConfig(); Serial.println("Waiting for an ISO14443A Card ..."); } void loop(void) { uint8_t success; uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) // Wait for an ISO14443A type cards (Mifare, etc.). When one is found // 'uid' will be populated with the UID, and uidLength will indicate // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); if (success) { // Display some basic information about the card Serial.println("Found an ISO14443A card"); Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); Serial.print(" UID Value: "); nfc.PrintHex(uid, uidLength); Serial.println(""); if (uidLength == 4) { // We probably have a Mifare Classic card ... Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); // Now we need to try to authenticate it for read/write access // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF Serial.println("Trying to authenticate block 4 with default KEYA value"); uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // Start with block 4 (the first block of sector 1) since sector 0 // contains the manufacturer data and it's probably better just // to leave it alone unless you know what you're doing success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya); if (success) { Serial.println("Sector 1 (Blocks 4..7) has been authenticated"); uint8_t data[16]; // If you don't want to write something to block 4 to test with, comment // the following line and this text should be read back in a minute // WARNING ! Write only on block 4, 5 and 6; 8, 9 and 10; memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data); success = nfc.mifareclassic_WriteDataBlock (4, data); // Try to read the contents of block 4 success = nfc.mifareclassic_ReadDataBlock(4, data); if (success) { // Data seems to have been read ... spit it out Serial.println("Reading Block 4:"); nfc.PrintHexChar(data, 16); Serial.println(""); // Wait a bit before reading the card again delay(1000); } else { Serial.println("Ooops ... unable to read the requested block. Try another key?"); } } else { Serial.println("Ooops ... authentication failed: Try another key?"); } } if (uidLength == 7) { // We probably have a Mifare Ultralight card ... Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)"); // Try to read the first general-purpose user page (#4) Serial.println("Reading page 4"); uint8_t data[32]; success = nfc.mifareultralight_ReadPage (4, data); if (success) { // Data seems to have been read ... spit it out nfc.PrintHexChar(data, 4); Serial.println(""); // Wait a bit before reading the card again delay(1000); } else { Serial.println("Ooops ... unable to read the requested page!?"); } } } }

