Introduction: RFID Gate Controller
An RFID reader for a parking gate switch
a ESP32 dev board kit C v4 (and initially an arduino nano v3)
a RFID reader/writer RC522
a KF-301 relay
a 9V battery 6LP3146
an IP55 junction box (3.4”x3.4”x2”)
some wires, soldered board, screws
a transistor (for 1 2N2222)
a 4.7k 1/4W resistor
Step 1: Step 1 : Introduction
I have made a mistake : I passed through only a 3-wires cable underneath my automatic parking gate. I can’t add any RFID reader nor keypad gate opener because they require a power supply cable set and a command control cable set. A minimum of 4 wires would have worked.
I could have purchased a standard key opener to activate the gate. But I decided to take this opportunity to create my own battery-powered RFID gate opener so that my kid can have his RFID tag with him and open the door without bringing an additional RF controller. And I will use this 3-wire cable which is still going out off the wall.
The good thing is that almost everybody knows how to do these kinds of things if we look for infos. So here is another version that may help few people that may have not understood otherwise. And it’s fun.
Step 2: Step 2 : Purchase List
Okay, so let’s try :
I need a micro-controller (board) that can be battery powered for a long idle time period; I need a RFID reader/writer that can make the difference between a good and a bad RFID tag; I need a relay, an electronic device that can control a much higher power than usually used for micro-controllers. I need a battery which can be changed or charged. I need a package that can resist temperature variations, humidity, rains, snow, storms,...
And that’s all ! Done. Almost.
Here is my initial purchasing list :
- an arduino nano v3 (€4.33 / $5)
- a RFID reader/writer RC522 (€6.90/$8.06)
- a KF-301 relay (€2.10 / $2.45)
- a 9V 6LP3146 (€2.50/$2.93)
- an IP55 junction box (3.4”x3.4”x2”) (€1.40/$1.64)
- some wires, soldered board, screws → additional €5.00/$5.58
So a total of €22.23/$25.56. Actually it is better to buy twice everything so that you are not stuck when the part gets missing or “untrusted”.
My second purchasing list adds :
- a ESP32 dev board kit C v4 (€9.79/$11.45)
- a bunch of transistors (for 1 2N2222) (€9.99/$11.69)
- a bunch of 4.7k 1/4W resistors (for 1) (€3.60/$4.21)
Also I don’t count the parts that I use to validate each step so that I understand what I am doing.
Step 3: Step 3 : the Connections
Let’s first show up on a schematic the connections between the parts.
Electric diagrams with the Arduino nano v3 and with the ESP32 dev. kit C
The RFID reader with SPI connection
The RFID reader RC522 is connected through SPI bus, which uses 4 pins for a 2-channel communication between the controller and the reader. Most controllers have dedicated pins for SPI communication. So the system won’t be functional if you don’t follow MCU’s or dev boards pinouts recommendations.
The 5V relay has a 3-inputs connexion that provides power supply VCC and signal control IN. GND is the third one. Even if the relay is claimed to be functional at 5V, the specifications may show that it can work at 3.3V. So VCC can be connected to the 5V pin of the board or to the 3.3V pin. The IN pin is controlled by a digital output from the board (GPIO #25 for the ESP32, D4 for the arduino). This signal is an ON/OFF switch. Here is the workflow for each possible case :
Relay type IO Pin state Relay state Connection Low level trigger LOW (0, false) on COM-NC Low level trigger HIGH (1, true) off COM-NO High level trigger HIGH (1, true) on COM-NC High level trigger LOW (0, false) off COM-NO
Low-level trigger relays are used for use cases when the standby mode is usually on. High-level trigger relays are used for use cases when the standby mode is usually off (lights on for a few hours per day). Of course, I have not taken care of this detail when purchasing mine. So I have a low-level trigger. Which can be used for initial tests anyway.
The logic gate
The GND pin of the relay connection is the most interesting one. I could have plugged the GND directly to the GND pin of the microcontroller (MCU) board. In such case, the relay would be continuously powered and switched on if it is a low-level trigger. The MCU board firmware should switch the IN pin to HIGH to switch off the relay.
But we have 2 constraints. The first one is the MCU board protection : when connected to a higher power load (lamp, gate,...), there is a possibility of reverse spike flowing back to the MCU board at the VCC pin which could potentially damage it. The second one is the power consumption. Powering on continuously the relay affects the autonomy of the system.
To avoid these 2 constraints I add a digital switch with a transistor. The transistor allows the current from the Vin pin to flow to the relay only when a digital pin goes in HIGH state. In the diagram the selected digital pin are GPIO #27 for the ESP32 or D2 for the arduino.
I use 2 different pins for the IN signal and for the VCC input of the relay to sequentially power on the relay and switch the relay state to HIGH. I think that using the same pin may work but the balance in current between both might trigger some unwanted effects. The resistance before the transistor limits the voltage applied to the transistor, so that it keeps its functionality in the linear state, not reaching saturation. The value is set to 4.7k as a default value. With a potentiometer (variable resistor) we could optimize its value for a more stable regime.
Step 4: Step 4 : RFID Tag Reader Assembly
Now that all connections are explained and defined, I can start wiring the system.
Wiring the system without soldering is possible but usually very unstable. A mix can help to validate the functionality. I first start to solder the RFID-RC22 board.
I use AWG22 single wire cables to ease the soldering and the electrical connections. As I have only 6 colors, I spaced the blue wires not to mix them up.
A solder gun holder is very useful to hold the board and maintain the wires in place when soldering. Just make sure to prevent direct contact between the solder wire and the gun so that the soldering wire will become liquid when in contact to the cable, not the gun. No pressure is needed anywhere, nor high quantity of solder.
A second step is to connect to the board. Here is how I did it onto the Arduino nano. I just fold the cables to create contacts on the pin. It is not very stable but I found some positions where the connections are good and the RFID reader can provide data.
Step 5: Step 5 : Arduino Firmware for Tag Read and Write
Let’s see how to read a tag.
Luckily the libraries are well known and can be easily implemented. I used the library MFRC522 made by Miguel Balboa available on github. I also tested the RFID library rfid.h on arduino IDE but it is not accessible easily through PlatformIO.
I tend to use PlatformIO in Visual Studio Code because debugging is our biggest activity and this application facilitates library management, code debugging, MCU board management. Although it is very convenient, it took me a bit of time to get familiar with it. I am still learning it by the way.
And I am still using arduino IDE for quick debugging because I am used to getting access to examples provided with the library. To install ESP32 onto the arduino IDE, you may follow this link. The Dumpinfo firmware is very convenient because it reads and displays all the info of the RFID tag. If this program does not work, the connections or the hardware are incorrect. Most probably the connections.
Once I finally tested all the tags I have, I started writing on the tag memory some specific values on specific areas of the card. It made them not recognizable by anyone other than me so that a default tag is not considered as a good one. I wrote one card and I kept one card as a default one. To make this happen, I worked mainly on the example program ReadandWrite. It is now my basis for the final code accessible on github. I temporarily added some LEDs to validate whether the card is custom or not. Here is a small video showing the result
Step 6: Step 6 : Power Consumption
With this setup, I can check the power consumption of the system by inserting my multimeter into the circuit between the + of the battery and the GND of the breadboard. The current flow reaches around 35mA without any card and 60mA when reading a card.
Is it high or low ? A bit of maths ? I have a 9V battery pack, with an average current capacity of 500mAh. So 9*500 = 4500mWh. If I consume on average 50mA at 9V continuously, I have 9x50=450mWh. Without power losses, I reach 4500 / 450 = 10hrs. Less than half a day. So it is mandatory to reduce the power consumption.
Power consumption of an arduino can be reduced by disabling some portions of the development board. We can first reduce the MCU frequency by a factor of 2 by a simple commands :
- CLKPR = 0x80; // (1000 0000) enable change in clock frequency
- CLKPR = 0x01; // (0000 0001) use clock division factor 2 to reduce the frequency from 16 MHz to 8 MHz
With the library “LowPower.h”, a deep sleep mode can also be activated during 2s, switching off the ADC and the BOD. It is achieved with this code line :
- LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF); for Arduino
Using these features, the power consumption test provides a reduction from 35mA down to 20mA. It is far from being enough to get autonomy for months or years.
So I decided to switch to the ESP32 MCU with much better power modes. After plugging the relay and updating the firmware to drive it accordingly, I have reduced the standby mode to 8mA. In idle mode, the consumption becomes 9V x 8 mA = 72mW. With a 9V 4500mWh battery, it can last 62.5hrs (2.6 days). Still far too low. These current values are indicative because my multimeter is not the best tool to validate the time-dependent current flow. But the reduction is quite significant. Let’s give it a try.
The following step is to implement the digital switch with the transistor to see how the current flows in standby mode. Changing the relay to a high level trigger shows that the idle state (pin at LOW) will consume less power. Indeed the relay requires up to 39mA when signal is on an 0mA when it is off (as the video shows relay current when scanning RFID tags). So the use of a digital switch is now only required for power protection. Maybe a protection diode would have been enough here.
Step 7: Step 7 : Final Assembly & Test
Using a standard IP55 external electrical 80x80x45 box does not block the lights from the ESP32-DEV kit and from the MFR522 RFID board. First opaque tape is used to block this light and also some light protections are used to reduce the light at the inner surface of the box. But a good thing about this transparency is that we see clearly when the relay and the bad scan related red light switches on/off.
My first tests with the LEDs in place, a 3s period for checking and a 1s for switching on the HIGH level relay has lasted 3 days with a 9V battery and the relay being driven with the 3V3 MCU pin. I have used my parking gate once or twice a day. So I confirm that it is not a viable solution.
Opening the gates is representing max. 10min out of 3 days is 0.2% of the time. Considering a 50mA current at 9V, we have spent 0.45Wx10min = 0.45*600 s = 270 J. My 9V battery is rated to deliver about 550mA at full discharge, or 0.55 Ah = 0.55*3600 J = 1980 J. A more realistic (non full discharge) value can be 70%, which is 1386 J. So this 10min could represent close to 20% of the total consumed power. Even if the energy consumed to drive the gate may be overestimated in terms of duration and current, we still have a significant influence on the operation time as compared to the idle time. It is then important to significantly reduce this power during operation time.
Step 8: Step 8 : Next Improvement Tests
So optimizing the idle time is key. My ESP32 dev kit C is clearly not the right choice for low power consumption. Removing the unnecessary LEDs can also help to reduce the overall power consumption.
Next changes will include :
- a tinier MCU dedicated to this application (no wireless communication possible)
- a power supply at 6.6V instead of 9V (the voltage divider is probably throwing away a lot of energy into its resistance).
- The removal of the constantly functioning LEDs
- The further reduction of relay switching time delays during operation to reduce consumption
- The power supply of 5V to the relay to provide more induced current to the relay (which was the cause for stopping the first test.
I hope you will find valuable information in this instructable to play around with battery-powered RF reader and gate actuators. Feel free to add any comment or questions and I’ll try to answer them as much as I can.
Happy hacking !!