Using serial communication with RFID RC522
In this article we will see how to enable the serial communication interface (UART or Universal Asynchronous Receiver/Transmitter) on the Raspberry Pi 2 and interface the RFID-RC522 board for reading RFID cards.
UART communication is often used in combination with the RS232 (or similar) communication protocol. We can use it for many things but for now we will restrict ourselves to only what the RFID-RC522 board requires.
By default the way Raspbian's kernel boots is to use the UART as a serial console under the devious name of /dev/ttyACM0 (for Broadcom in specific but it can also be found in other scenarios). In simple words means that you as a developer/user cannot use the UART because it is already occupies with something else. In order to do change this we need to tinker with the boot parameters of our kernel.
Note: The steps below can also be done in a desktop environment however doing this via terminal is much faster since you need sudo
.
-
Open a terminal (if you have booted straight to a desktop environment with your RPi2; if you use SSH without X or boot to a terminal by default you don't need to do anything here)
-
Change the working directory to
boot
by executingsudo cp cmdline.txt cmdline_backup.txt.
This creates a backup in case we screw something up. Note that if something bad happens after editing this file we might be unable to boot our Raspbian at all! However this is not such a big issue since you can always open the SD card with your OS and replace the edited
cmdline.txt
withcmdline_backup.txt
, which will rollback your kernel boot parameters to their original working state. -
Open
cmdline.txt
withnano
,vi
,vim
or another terminal-based text editor and look for thetty
that is used by the UART. Broadcome, as mentioned above, uses /dev/ttyttyAMA0 so you have to look for the parameterconsole=ttyAMA0, 115200
(or similar butttyAMA0
must be present!). Delete it (both parameter and its value). -
Check if you haven't missed anything that mentions
ttyAMA0
. The originalcmdline.txt
for Raspbian Wheezy looks like this:dwc_otg.lpm_enable=0 console=tty1 console=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait fbcon=map:10 fbcon=font:ProFont6x11 logo.nologo
After step 3 it should look like this:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait fbcon=map:10 fbcon=font:ProFont6x11 logo.nologo
-
Last thing before reboot is to change
/etc/inittab
. Again use a text editor and look for thettyAMA0
(innano
you can simply press Ctrl+W, enter the previously mentioned string and press enter to go the line where it is used). In most case it will probably be somewhere at the end. All you have to do is comment out the whole line:#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
-
Reboot by typing
sudo reboot
The serial console should now be disable and we can proceed to the next section.
Here we will do a small check if the UART is indeed responding (both read and write access).