rfid practical projects
Now that we have discussed the basics of RFID and gotten some hands on experience, it is time to build some more advanced projects to harness the full power of RFID Technology.
LEDs and touchless control
The first advanced project will be fairly simple but will illustrate what kind of projects we can build with the RFID technology. With this project we will assign different colors to different RFID tags. We will then show the color with an rgb led. Alternatively, you could connect an rgb led strip or ws2812b led strip to the Pi. To get started, we need to connect the RGB-LED to the Pi
|Connections Pi||Connections Breadboard|
|GPIO 18||1. connector RGB LED|
|GND||2. Connecto (the longest connecot) RGB LED|
|GPIO23||3. connector RGB LED|
|GPIO 24||4. connector RGB LED|
Once again, we will use the Read.py program as a basis for our programming. Just copy this into rfid-led.py. We need to add code to 3 different places to make the program work so you will want to work with us step by step.
It is important for the next step that you are actually using the
rfid-led.py line 1
import RPi.GPIO as gpio
In the next step we need to configure the gpio pins so they know that they are controlling a led. To do this we assign every color to a gpio pin and set them as an output. The next step is defining the function ledsOff() which just turns off all of the LEDs. Later you will see why this makes the rest of our coding a bit easier. After defining the off function we also create the end_read() function. This function’s main purpose is to call the
gpio.cleanup() function so that there is no problem with the pins if we start the program again.
line 11 to 28
# LED Setup red = 24 green = 23 blue = 18 gpio.setmode(gpio.BCM) gpio.setup(red, gpio.OUT) gpio.setup(green, gpio.OUT) gpio.setup(blue, gpio.OUT) def ledsOff(): gpio.output(red, gpio.LOW) gpio.output(green, gpio.LOW) gpio.output(blue, gpio.LOW) def end_read(signal,frame): global continue_reading continue_reading = False gpio.cleanup()
After creating the setup for the LEDs, we need to change part of code so that it can interface correctly with the RFID Tags. For this we will create 3 different if clauses, one for each color. First we need to check if the variable data has a list. We can do that with
if type(data) is list:. This is important because sometimes these functions get called even if the RFID reader couldn’t read any data from the RFID tag. This can happen if, for example, you move the card too fast above the RFID reader. In the next step we will translate the ASCII numbers into a string in order to make the program more human friendly. Then we will use the if clauses.
if "green" in text is really simple and reliable for that. We also could use
if “green” == text but in this case that statement could get us unintended results. In the code we first turn off all LEDs and then only turn the correct LED on.
if status == MIFAREReader.MI_OK: data = MIFAREReader.MFRC522_Read(8) MIFAREReader.MFRC522_StopCrypto1() if type(data) is list: text = "".join(chr(x) for x in data) print text if "green" in text: ledsOff() gpio.output(green, gpio.HIGH) elif "red" in text: ledsOff() gpio.output(red, gpio.HIGH) el if "blue" in text: ledsOff() gpio.output(blue, gpio.HIGH) else: print "An error occured:("
Above is all the code you need. Before you can successfully test your new program, you first need to create some RFID tags with the correct names on them. You can easily do that by utilizing the MyWrite.py program we wrote previously and some of your RFID tags. Please keep in mind that you will need to specifically use the terms red, green and blue.
If you can’t get your version of the code to work, you can download our code and test with it or compare it to yours to check for inconsistencies:
The RFID Soundboard
Having a touchless controller for your light is pretty cool but these projects should have shown you that you can also build many other interesting projects. In this next topic we will build an interactive RFID soundboard. Every RFID tag will be connected to a different sound. With the RFID reader we will read those sounds from the tag and then play the intended sound. Needless to say, you can use any sound you want. We have taken the liberty however to make some animal sounds readily available which can be downloaded at:
git clone https://github.com/coding-world/animal-sounds
You can find even more sounds on https://www.freesound.org/. All of the sounds on these website are under a Creative Commons Website and can be freely used for your projects. You are even allowed to change/remix the sounds.
Before we start programing this project, we need to make sure that you are using the right sound settings. The Raspberry Pi now has audio output so you will need to connect either an external speaker or your headphones to the Pi. In our case we used some external speakers which were powered through USB. If you use the headphone jackets you should check this options in the config to ensure the audio is being routed to the correct output device:
sudo raspi-config -> 9 Advanced Options -> A9 Audio -> 1 Force 3.5mm [...]
If you use a monitor or tv with built in speakers, you can also use the HDMI option for the sound. You can use the following terminal command to check if you are getting audio from your preferred output location (for this you need to have downloaded the sound files or placed your own on the Pi):
If everything is connected properly, and you used the command above, you should hear a cow.
Before we start with the RFID soundboard, we should first discuss how to play sound files with Python. To do this we will utilize the pre-installed Python library, pygame. This library is great for creating games on your Raspberry Pi but can also be used for playing simple sound files. For our purposes, we are taking advantage of the
mixer component in the library.
import pygame pygame.mixer.init() pygame.mixer.music.load("animal-sounds/cow.wav") pygame.mixer.music.play() while pygame.mixer.music.get_busy() == True: continue
The code above is quite simple. In line 1 we import the pygame library and in line 2 we initiate the mixer component from the library. In line 3 we load the music file by calling the function
.load() with the correct music path. The parameter for the function is the path to the downloaded sound file. The path needs to be from the path of your program to the sound file. The loaded sound files get played by the command on line 4.
The only problem is that Python normally closes the program after executing the last line of code. This is a feat we do not desire because the playing of the sound file would stop. In order to keep the file open as long as the sound file is playing, we construct a while loop in line 5. While
.get_busy() is True, this loops continues and does not stop.
Note that you don’t necessarily need to play the cow sound. You can find all of the sounds available in the directory (
cd animal-sounds) and list all the items with
Now that we have the knowledge to play sound files inside a Python program, we need to implement this with into our RFID reader program. For this we will use the programming already set in the rfid-colors.py program. Just copy this into rifd-sounds.py. Remember in line 1 we need to import the pygame library.
if "cow" in text: pygame.mixer.music.load("animal-sounds/cow.wav") pygame.mixer.music.play() while pygame.mixer.music.get_busy() == True: if "chicken" in text: pygame.mixer.music.load("animal-sounds/chicken.wav") pygame.mixer.music.play() while pygame.mixer.music.get_busy() == True: if "elephant" in text: pygame.mixer.music.load("animal-sounds/elephant.wav") pygame.mixer.music.play() while pygame.mixer.music.get_busy() == True:
The programming logic is the same as with the LEDs. Instead of turning the LEDs on however, we are playing the sounds files. Please keep in mind that you also need to change the data on the cards for this example to work.
Always keep notes!