Introduction: Stuffed Animal X-Ray Machine
So one day I took the kids to the Orpheum Children's Museum in Champaign, IL to have a little fun, and they spent a long time playing in the vet clinic there, "fixing" the stuffed animals. An integral part of that exhibit was an x-ray machine for stuffed animals, where the child could take a stuffed animal over to the machine and get an x-ray of that particular type of animal shown on a PC monitor so the child could then "treat" it. I thought it might be pretty straightforward to create my own version for them at home, and sure enough, it's not so tough!
Step 1: What You'll Need:
(note: all links are for clarification only, better prices can be found on Ebay or elsewhere for many components. The prices I listed are what I paid, not necessarily what the link shows. I recommend splurging for a UL-listed power supply, though, to be on the safe side when dealing with 120V)
- Raspberry Pi Zero ($5)
- Mifare RC522 Reader ($2)
- Mini-HDMI to HDMI cable ($1)
- 5V 2A power supply with Micro USB connector ($8)
- Female-female jumper wires , 10 cm or longer recommended ($1)
- MicroSD memory card, 8 GB minimum ($4)
- 13.56 MHz RFID keychain tags (1 per animal, ~$0.20 each)
- 13.56 MHz RFID S50 Cards (1 per child, ~$0.25 each)
- pin headers (generally come included with the RC522 reader)
- 6" x 6" x 4" PVC junction box ($11)
- Red Felt (~ 2" x 2" for each stuffed animal you're going to tag, $1)
- White muslin cloth, about 1 yard ($1.50, optional for bandages)
- TV or PC monitor for a display
Supplies and Tools:
- Soldering Iron
- Raspberry Pi (A, B, B+, 2, or 3)
- OR ethernet and USB breakouts to use on the Pi Zero. My instructions assume you have a full size Pi. (frankly, building the program on a Pi 3 was pretty easy, and you can always use another Pi, right?)
- 3D printer plus:
- Red filament (or other color of your choice)
- Black filament (or other color of your choice)
- OR other decoration method or way to close holes you will cut in the PVC enclosure
Step 2: Soldering and Electrical Connections
For this step you will need:
- Soldering Iron
- Pi Zero
- Mifare RC522 card
- Pin headers
- female-female jumper wires
- Needle nose pliers
We're going to solder in the header pins into both the Raspberry Pi Zero and the RC522 card reader. With the card reader, I found the angled header pins that came with it worked nicely when installed from the side of the card with the surface mount components, and the pins pointing back towards the center of the card. It really is pretty much up to you here to do as you like, but we want to get the card to lay flat, so this orientation does work well. Simply solder all the way across the row of pins to get a connection on each one. Be careful not to overheat the board while you're doing this. I like to work back and forth across the board simply to avoid too much heating in one spot. Your soldering doesn't need to be pretty - mine never is - but you'll need good connections, and make sure you don't bridge any pins.
With the RC522 card soldered, it is time to solder the Raspberry Pi. We'll need to solder in 7 pins on the Pi, so take your header pins, and break off one set of two pins, one set of three pins, and two single pins with your needlenose. On the Pi's board, the I/O pins are the two rows of copper colored holes running along one side of the board. The hole that has a square copper pad is pin #1, and the odd numbered pins go down that column and the even (starting with #2) go down the second column. We're going to solder a single header into pin #1 and another into pin #6. The three header pin group goes into pins 19-23, and the double header goes into pins 22 & 24 (see image for clarification)
Now we need to look at connecting the Pi and the RC522. On the face of the RC522, you should notice a label for the pins We'll use this to connect to the pins on the Pi. You'll need 7 wires, so go ahead and peel off a group of seven wires from your female-female jumper set. On the RC522, connect all the pins except for the one marked IRQ. The order of the pins should be SDA/SCK/MOSI/MISO/IRQ/GND/RST/3.3V.
NOTE: At this point, you may want to connect your RC522 to a full-size Pi board as it is easier to build the software on a full-size Pi. I'm using a Pi 3 here. If you don't go this route, you'll need to have a USB hub you can plug into the Zero's mini USB port and you'll have to set up a network connection on the Zero or use the disk image I'll provide later. The pin numbering scheme on the larger Pi boards is identical to the Zero, so swapping is easy - and that's one of the points of using the jumper cables.
To connect the Pi to the RC522, make the following connections:
- SDA to Pin #24
- SCK to Pin #23
- MOSI to Pin #19
- MISO to Pin #21
- GND to Pin #6
- RST to Pin #22
- 3.3V to Pin #1
With that, your electronic build is done and we're ready to start installing the OS and program.
Step 3: Initial Raspberry Pi Setup
For this step you will need:
- MicroSD cards
- Computer with
Insert the SD card into your computer and format it using SDFormatter
Go to https://www.raspberrypi.org/downloads/raspbian/ and download the latest Raspbian distribution. I used the full distributable, but the lite version may suffice. If, when you unzip the file, your computer tells you that you lack the space necessary and that it needs 734 PB or something ludicrous, install 7-Zip on your machine and it should unpack just fine.
Now open Win32DiskImager and select the disk image and make sure the drive is set to the location where your SD card is inserted. Select Write and your SD card will have the disk image written to it.
Step 4: Installing the RFID Reader Software
Next we need to install the software to read an RFID tag. Every RFID tag comes with its own Unique IDentifier. These tags can hold a lot more information than we'll need on its various sectors, but for our purposes, all we need is the UID so we know what animal is being scanned. So we just need to read the UID, and fortunately, most of the work has been done for us. So let's configure the Pi so it can read card.
- Plug in your Pi to a monitor via an HDMI cable
- Connect your pi to the internet (ethernet cable or wifi - this is where having the full-size pi comes in helpful, as it is easier to connect)
- Attach a keyboard and mouse to the Pi
- Plug in the power adapter
The Pi will now boot and we'll make a few configuration changes
Open a terminal window and at the command prompt enter the following commands in order:
sudo apt-get upgrade
sudo apt-get update
Now go to the raspberry in the upper left corner of the screen, pull down that menu and select preferences->configuration->interfaces, and then click on enable spi. Go to the system window of the config menu and click on expand filesystem. Select ok, and the pi will prompt you for a restart. Go ahead and restart
Once it has rebooted, open a terminal window again, then enter the following commands in order:
git clone https://github.com/lthiery/SPI-Py
sudo apt-get install python-dev git
sudo python setup.py install
git clone https://github.com/mxgxw/MFRC522-python.git
sudo python Read.py
Your Pi should now start the script to be reading RFID tags. Pick any of your tags and hold it to the Pi. You should see its UID pop up and it will print the information that is on sector 8 (all blank info). Try another tag, and you'll notice that it's UID is different.
Congratulations! Your Raspberry Pi is now able to read RFID tags and we're ready to start recording UID numbers from your tags. At this point, if you need to restart your Pi for any reason, you would only need to switch into the MFRC522-python and execute the sudo python Read.py command.
Step 5: Determining the UID of Each RFID Tag
Welcome to what is the single most tedious step of this process, but also arguably the easiest. Before we get to reading in all the UIDs, let's edit the Read.py script to display the fifth numerical entry of the UID tag number. This is pretty straightforward and shown in the images above - under the #Print UID line, add the string +","+str(uid) to the end of the line to get the last entry to print.
Now make sure your pi is running the Read.py script as described in the previous step, and start scanning your tags and cards. For each tag, record the UID displayed on the screen. I like to do this using masking tape applied to the tag itself, and just writing the UID on the tape. That way there is no chance of getting numbers mismatched. You should not find that any two tags have the same UID, as the odds of this occurring are infinitesimal, but in case you do, make sure to only use one of them and buy a lottery ticket while you're at it.
One final note: You will need to discover the UID of a key tag for each animal you're going to use plus one card for each child you want an ID badge for. I would also recommend one additional tag or card that you can use for an "escape" function. When we get the program up and running, we will have it start automatically on bootup and in fullscreen mode - stopping the program to edit it is very difficult without an escape function, and the easiest way to do that is to have one RFID tag that will cause the program to terminate.
Step 6: Programming in Each RFID Tag
Now that you know the UIDs of each tag, it's time to start altering the Read.py script to do what we want. Basically, all we're going to do is add if statements to the script to allow for displaying a specific image (or one from an assortment of images) whenever a given tag is scanned.
I like to do this by defining each tag right after the import statements at the top of the script. For each tag and card, we'll give a different name. For instance, if the UID of a tag was 181,165,221,82,159, we could define that as tag1 and use the line:
tag1 = [181, 165, 221, 82, 159]
So once again, use the sudo nano Read.py command at the command prompt to edit the python script. Add in each of the tags you recorded, and write the tag # on the masking tape for each tag, just for clarification and ease as we get to affixing them to the stuffed animals later.
When you're done, your script should look like the image above, only with your UID tag numbers used.
Now scroll down to just beneath the print "Card read UID: " line, and we're going to add if statements for each tag. The basic block should be as follows for each tag (remove the . at the start of the second line - I added that simply to make the indent show up on this instructable)
if uid == tag 1:
. print "tag 1"
Repeat this for each tag, but substitute elif for if on every tag after the first one, and be VERY careful to match the indents, so each if statement should start indented the same amount as the print "Card read UID: " line, and the print line in each block you add should be indented from the if statement. If you fail to do this, python is likely to throw a fit at you. Once you've added all your blocks, save the script and exit the editor. Now launch the python script with the sudo python Read.py command and verify each of your tags is printing the correct id to the screen. If not, check the tag definitions in your script.
When you verify that all the tags are reading properly and printing their tag # to the screen, it is time to clear out some code we won't need anymore. To do this, you can comment out the print "Card read UID:" line as well as the authentication lines (see the images above). Simply use a hashtag at the start of each line to comment it out.
Step 7: Adding and Displaying Images
Believe it or not, we're close to finishing the programming. Next thing you need to do is find x ray images for each animal you want to tag. Just find some images online to use or even ask a vet if they have any interesting ones. You'd be amazed at what you can find. For additional fun, get several images per animal, and I'll show you how you can use them all on one animal. Also feel free to use silly images as well. I've used "xrays" of stuffed bears instead of real bears and a trojan horse diagram instead of a real horse. As you find images, make sure that they're sized to the dimensions of your screen (ie, 1920x1080 or 1280x720). If they can't fill the entire screen, consider expanding the image canvas in a photo editor and leaving the margins black. Also, feel free to add highlights to point out the issue, such as a broken bone, and provide help to the child "diagnosing" the illness.
You also need to create additional images - one image for the welcome splash screen, one for each ID card to welcome each child when they "check in", and one for your shutdown card (see above for ideas)
When you have your images, get them onto your pi. If you collected them on the pi already, good news - just move them to your MFRC522-python directory. If not, connect your pi to your network and ftp them over to the Pi. If you aren't sure of your Pi's IP number, type ifconfig at the command prompt and you should be able to find it.
Now we're going back to editing the Read.py script. After the import commands, add the following three import lines:
After you've defined your cards and tags, add in the following block:
from pygame.locals import *
windowSurface= pygame.display.set_mode((WIDTH, HEIGHT), 0, 24)
Now we need to also add a command to the beginning of the loop where the program looks for cards to display that image on the screen. Below the "while continue_reading:" statement, add the following:
While you're at this section of the code, consider adding the following statement right before the "while continue_reading:" statement: (not pictured above)
This will ensure that your reader is operating at the max gain it can so it can read cards easily.
Next go to each of your if blocks for the tags. If you only have one image, you can add the following at the same indent level as the print "tag #" statement:
If you have more than one image, it is only slightly more complicated. For these cases, you'll need to select a random number (by default, this is a uniform distribution between 0 and 1), and then assign probabilities to each image. In the following example, I had two images for a seal, one I wanted to show 2/3 of the time, and the other I wanted to show the remainder of the time.
- if uid == tag1:
- if x < 0.666:
- img = pygame.image.load("seal1.jpg")
- windowSurface.blit(img, (0,0))
- img = pygame.image.load("seal2.jpg")
- windowSurface.blit(img, (0,0))
Just follow this basic structure (remove the leading . I put in to get the indents clear) and have the script call your image files accordingly. Calling the images for the ID badges is identical in form, though you would only have a single image that would load. For the shutdown card, the function is slightly different, as we want it to call a system shutdown command. For this card, use the following:
- elif uid=idcard3:
- img = pygame.image.load("goodbye.jpg")
- windowSurface.blit(img, (0,0))
- os.system("shutdown now -h")
At this point, all the programming should be done. If you run this Read.py script, you should see full functionality of your xray machine. Now it's time to set it to automatically start the script when you boot up.
Step 8: Setting the Pi to Autostart the Program
This is a fairly uncomplicated step for once:
- Copy all the images you're using from the /home/pi/MFRC522-python directory back to the /home/pi directory. When you autostart the program, it will do so from the /home/pi directory, so you either have to have the images there or you have to edit your path so it can find it. Copying them is easier. Assuming you're still in the MFRC522-python directory, you can just run the following command:
cp *.jpg ~
- enter the following commands in the MFRC522-python directory:
chmod +x Read.py
sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart
Then in the editor, add the following line at the end of the file:
Save this file, and from now on, when you start your pi, it will automatically boot into the x-ray script. You may want to make a backup image of your sd card at this point using Win32diskimager (clicking on read instead of write).
NOTE: Once you set the pi to autostart the program, it will be harder to edit the script, so ensuring everything is working well before setting the autostart is helpful
Now, if you've been using a full size Pi for development, you can remove the SD card and switch your wiring back to the Pi Zero - see the prior step if you need a refresher on the wiring connections.
Step 9: Finishing the Enclosure and Decoration
For this step you will need:
- 3d printer and black filament (or color of your choice) OR:
- other method of your choice to make covers for cutouts
- 3d printer with red and black filament OR
- other method of your choice to decorate the front of the machine
- Drill / drill press
- Keyhole saw
Now we're going to modify the enclosure for the machine. There are two cutaways we'll make in the enclosure - one on the side, and one on the bottom. The side cutout is to allow for the power cord and the HDMI cable to go through the enclosure, and the bottom one is simply a cutout to allow for a thinner structure for the RFID scanner to penetrate. I've included .stl files above for the two parts I designed to close these openings once we've made them and fed wires through. You don't need to use these, but you'll want something to close the opening around the cables at the very least. The one for the RFID reader is a little more optional, but it does also help your child find the spot to hold the stuffed animal next to.
If you do choose to use these stls, the files are defined with millimeters as the unit of measure, and you'll need to print them on a 3D printer or find a printing service. It is also worthwhile to check with your local library - many now have 3D printers as a service. I printed these myself, using black ABS filament. Color is your choice, and PLA would work fine, if you desire.
When you have your parts, just use a permanent marker or awl to mark the corners where you want each cutout to go on your enclosure (see pictures). Be sure to use the inner ring of the pieces, not the outer. Then simply connect the dots to get your cutout lines. You can cutout along the lines in any manner you choose, though here I use a drill press to create a series of holes along the outline, which I then connect using a keyhole saw and file down until the inserts fit.
When you can put the inserts in, simply hot glue them in place. Be sure to feed your cables through the cable opening before gluing it in place, naturally.
To decorate the front of the device, I printed out a cross in red PLA on my printer and the word X-RAY in black ABS. The stls are provided above, but you can decorate any way you prefer. For attaching them to the box, I found hot glue works fine. It gets a little messy around the letters, but once it is cool, you can easily trim out any slop with a hobby knife.
As one final step, i like to print out a case for the Pi Zero. You could purchase one, or risk going without. The risk is pretty minimal, but it is nice to isolate the electronics. I used this case from Thingiverse and printed it in black ABS. The Pi simply presses in and the cover snaps on. If the cover wants to pop off, you can secure it with a dab or two of hot glue strategically placed on the outside. You can also glue the case to the back of the enclosure if you would like, or leave it floating. Place the RFID reader in the pocket in the cover, and use a couple light dabs of hot glue to hold it in place. Plug in the micro USB adapter and miniHDMI into the pi, insert the SD card, and close up the cover for the device. Congratulations, your x-ray is done and working. Now to finish the ID cards and tag the animals.
Step 10: Creating ID Cards
For this step, you will need:
- Your cards you previously programmed into the device
- Spray Adhesive
- Mod Podge
This is pretty straightforward - print out an image you would like for the front of your card (see pictures if you want to see my approach). Use a spray adhesive to stick it to the front of the card, and once the adhesive is dry, apply several coats (3+) of Mod Podge with a brush to seal the image in and protect it. That's it - pretty simple!
Step 11: Sewing RFID Tags to Stuffed Animals
For this step you will need:
- Red felt
- Your RFID tags you've programmed in
- Needle and thread
This is easy enough, but it does take awhile. I didn't want to permanently harm any stuffed animals, and the RFID tag can't be buried too deep, since these are low-power devices. In addition, the child needs to be able to locate the RFID tag to get it close enough to the reader. All I did here was to cut a heart out of the felt large enough that the RFID tag can fit entirely behind the felt. Then sew the felt heart to the stuffed animal, with the tag under the felt. In the end, all you see is the red heart, and kids easily recognize this as where they need to scan the animal under their x-ray. Sewing these on by hand takes awhile, but a simple running stitch will hold them on, even if it does take a few circuits around the heart to get enough stitches in to hold things together.
Step 12: Create Diagnostic Forms
This is an optional step, but it does aid in creative play
Once the child has scanned an animal with their x-ray, it is nice to have them be able to fill out a "chart" of sorts for their animal and prescribe treatment. There are many ways you can do this, so this is largely up to you, but I have provided an example above (sans clipart in the upper left corner). I've found you can print two of these on a standard sheet of paper - any more and they're too small, any less and you're looking at excessive amounts of paper being used.
Step 13: Sew Bandages
This is an optional step, but it does aid in creative play
In addition to having diagnostic forms, it is nice to have bandages the child can wrap their wounded stuffed animal in. For this, you'll need some white cloth and a sewing machine. I used simple white muslin that you can easily get for around $1 per yard. I cut strips 6" x 36" for extra long bandages, and 6" x 18" for shorter bandages. With each piece, fold in half along the long side to form a 3" wide strip, then sew along the long side leaving a 1/4" seam allowance. When you are about 1/4" from the end, turn the fabric to sew along the short side. Then clip the corners of the fabric as shown to help the bandage from bunching too much in the corner, and turn the bandage inside out ( you can also trim the seam allowance down to around 1/8" if you'd like, but it is unnecessary). Once the bandage is inside out, tuck the remaining unfinished edge into the bandage, and sew the end closed, being certain to reverse your stitch at the beginning and end to lock it in place. Simply press the bandage flat, and you're ready to use...
Step 14: PLAY!
Mount the x-ray near your montitor/tv, plug it in, and let your kids have countless hours of creative play!
We have a be nice policy.
Please be positive and constructive.