The goal of my project is to read a RFID tag and activate a relais, which opens a door. The part of reading the UID of an tag works properly.
Because this construction should be battery powerd and last a long time, I tried to lower the powerconsumption.
The programm i have, first checks if a RFID tag is available, if so it reads the UID and displays it on the serial monitor. After that or if no RFID tag is available the programm waits 2 seconds before putting the Arduino to sleep. After 5 seconds in sleepmode the Arduino wakes up and checks again for RFID tags.
The Arduino is put into PWR_DOWN mode and several other none used peripherals are turned of.
I have been measuring the powerconsumption of the Arduino with a Multimeter.
The lowest the Arduino goes is 25mA but only if i disconnect everything from the Arduino. If i connect everything back the current goes up to 52mA in sleep mode.
52mA is obviously to much power draw to power the Arduino for a long time via batteries.
I have read, that the arduino only uses so micro amps when in deep sleep, which mine is far off.
I already searched the forum and the internet but could only find solutions for the older version of the Arduino Nano with the ATMega3809 i think it was.
The other thing is, that if i put the Arduino to sleep he will stay in sleep mode. I know that I could use a Interrupt pin to wake him back up again but thats not what i want. The Arduino should wake up check for a RFID tag and go back to sleep in a specific intervall.
Here is the code I am using:
#include <SPI.h>
#include <MFRC522.h>
#include <avr/sleep.h>
const unsigned long awakeInterval = 5000; // Time in milliseconds in which the Arduino should stay awake
const unsigned long sleepInterval = 8000; // Time in milliseconds after which the Arduino should sleep
const unsigned long wakeupDelay = 2500; // Time in milliseconds that the Arduino should wait after waking up
#define SS_PIN 9
#define RST_PIN 5
#define RELAY_PIN A5 // the Arduino pin connects to relay
MFRC522 rfid(SS_PIN, RST_PIN);
byte managerKeyUID[4] = {0x28, 0x0F, 0xA0, 0x6E}; //28 0F A0 6E
byte secretaryKeyUID[4] = {0x30, 0x01, 0x8B, 0x15};
int k = 0, led = 2;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
SPI.begin(); // init SPI bus
rfid.PCD_Init(); // init MFRC522
pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output.
digitalWrite(RELAY_PIN, HIGH); // lock the door
Serial.println("Tap RFID/NFC Tag on reader");
}
void loop() {
if (rfid.PICC_IsNewCardPresent()) { // new tag is available
if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
if (rfid.uid.uidByte[0] == managerKeyUID[0] &&
rfid.uid.uidByte[1] == managerKeyUID[1] &&
rfid.uid.uidByte[2] == managerKeyUID[2] &&
rfid.uid.uidByte[3] == managerKeyUID[3] ) {
Serial.println("Access is granted for manager");
digitalWrite(RELAY_PIN, LOW); // unlock the door for 2 seconds
delay(2000);
digitalWrite(RELAY_PIN, HIGH); // lock the door
}
else
if (rfid.uid.uidByte[0] == secretaryKeyUID[0] &&
rfid.uid.uidByte[1] == secretaryKeyUID[1] &&
rfid.uid.uidByte[2] == secretaryKeyUID[2] &&
rfid.uid.uidByte[3] == secretaryKeyUID[3] ) {
Serial.println("Access is granted for secretary");
digitalWrite(RELAY_PIN, LOW); // unlock the door for 2 seconds
delay(2000);
digitalWrite(RELAY_PIN, HIGH); // lock the door
}
else
{
Serial.print("Access denied for user with UID:");
for (int i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();
}
rfid.PICC_HaltA(); // halt PICC
rfid.PCD_StopCrypto1(); // stop encryption on PCD
}
} else {
delay(2000); // 2-second delay before going to sleep
Serial.println("Going to sleep.");
goToSleep();
}
}
void goToSleep() {
Serial.flush(); // Wait for all output to finish
// Activate sleep mode
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Deep sleep mode
sleep_enable(); // Activate sleep mode
// Activate the System-Timer
_PROTECTED_WRITE(CLKCTRL_MCLKCTRLA, CLKCTRL_CLKSEL_OSC20M_gc);
// Set the Prescalers auf 1/2, to increase the clock frequenzy to 10MHz
_PROTECTED_WRITE(CLKCTRL_MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm);
//Deactivate unused peripherals
ADC0.CTRLA &= ~ADC_ENABLE_bm; // Deactivate ADC
SPI0.CTRLA &= ~SPI_ENABLE_bm; // Deactivate SPI
USART0.CTRLA &= ~USART_RXEN_bm & ~USART_TXEN_bm; // Deactivate USART0
TWI0.MCTRLA &= ~TWI_ENABLE_bm; // Deactivate TWI
RTC.CLKSEL = RTC_CLKSEL_INT1K_gc; // Run low power oscillator (OSCULP32K) at 1024Hz for long term sleep
RTC.PITINTCTRL = RTC_PI_bm; // PIT Interrupt: enabled */
// Sleep for the specified time
sleep_mode();
// Waiting time after waking up
delay(wakeupDelay);
// Turn off sleep mode
sleep_disable();
Serial.println("Deactivate sleep mode.");
// Activating the System Timer
_PROTECTED_WRITE(CLKCTRL_MCLKCTRLA, CLKCTRL_CLKSEL_OSC20M_gc);
// Set the prescaler to 1/64 to increase the clock speed to 20 MHz
_PROTECTED_WRITE(CLKCTRL_MCLKCTRLB, 0);
// Activate unused peripherals
ADC0.CTRLA |= ADC_ENABLE_bm; // Activate ADC
SPI0.CTRLA |= SPI_ENABLE_bm; // Activate SPI
USART0.CTRLA |= USART_RXEN_bm | USART_TXEN_bm; // Activate USART0
TWI0.MCTRLA |= TWI_ENABLE_bm; // Activate TWI
RTC.CLKSEL = RTC_CLKSEL_INT1K_gc;
RTC.PITINTCTRL = RTC_PI_bm;
}
I am new to the forum, so if I did something wrong with my post please let me know