Skip to content

Using serial communication with RFID RC522

Aleksandar Atanasov edited this page Sep 10, 2015 · 2 revisions

Abstract

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.

Enabling the UART

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.

References and further information:

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.

  1. 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)

  2. Change the working directory to boot by executing

     sudo 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 with cmdline_backup.txt, which will rollback your kernel boot parameters to their original working state.

  3. Open cmdline.txt with nano, vi, vim or another terminal-based text editor and look for the tty that is used by the UART. Broadcome, as mentioned above, uses /dev/ttyttyAMA0 so you have to look for the parameter console=ttyAMA0, 115200 (or similar but ttyAMA0 must be present!). Delete it (both parameter and its value).

  4. Check if you haven't missed anything that mentions ttyAMA0. The original cmdline.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
    
  5. Last thing before reboot is to change /etc/inittab. Again use a text editor and look for the ttyAMA0 (in nano 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
    
  6. Reboot by typing

     sudo reboot
    

The serial console should now be disable and we can proceed to the next section.

Testing the UART

Here we will do a small check if the UART is indeed responding (both read and write access).

References and further information: