Introduction: RFID + Colour Sensor IoT Project

The idea of this project is to reward people who drink out of coffee cups and properly dispose of them. Take a company like Tim Hortons for example; in 2014, they recorded serving 2 billion cups of coffee annually. Although their cups can be recycled, they are not accepted for recycling everywhere at this time. As stated on their website, “we currently have programs in a number of restaurants across Canada where we capture our paper cup (and other packaging) for recycling or composting. The number of locations where we offer in-store recycling continues to grow as we work with local waste management companies to expand our program…”

So, as they continue to expand and climb the industry as a environmentally caring corporation, we believe they can also increase the amount of customers and raise awareness by creating an incentive awards system.

This project is an attempt to develop a basic system around this idea.

Step 1: Parts

  • Raspberry Pi 3
  • Arduino Mega
  • Mini breadboard
  • Female.male jumper wires
  • 2 x LEDs (red & green)
  • 2 x 330 Ohm Resistors
  • TCS3200 Colour Sensor
  • RC522 RFID Reader and Tags
  • USB 2.0 Cable Type A/B
  • Computer and Internet
  • Coffee Cup

Step 2: Setting Up Hardware

Connecting the TCS 3200 to the Arduino

VCC --> 5V

GND --> GND

S0 --> 4

S1 --> 5

S2 --> 6

S3 --> 7

E0 --> GND

OUT --> 8

Connect RFID/LEDs to Raspberry Pi

SDA --> 24

SCK --> 23

MOSI --> 19

MISO --> 21

GND --> 6

RST --> 22

3.3V --> 1

GREEN LED --> 12 and 330 Ohm to GND

RED LED --> 11 and 330 Ohm to GND

Step 3: Programming Colour Sensor

TCS3200 senses color light through an internal 8 x 8 photodiode array. A current-frequency converter is used to convert the reading from the photodiode into a square wave whose frequency is proportional to the intensity of the light.

Photodiodes have three different color filters. There are 16 red filters, 16 green filters, 16 blue filters, and other 16 photodiodes without filter.

In order to let the photodiode to read the color, we need to control pins S2 and S3.

Color Table:

Color S2 S3

Red LOW LOW

Blue LOW HIGH

Green HIGH HIGH

Each sensor maybe vary slightly in frequency. Bringing the sensor close to the cup. Use the Colour_Tester.ino code to find the correct values for your particular cup and remember the given values in the Serial Monitor. Then write if statements to output a "yes" or "no" if the sensor is detecting the cup as shown in Sensor_Data.ino.

Once completed, connect the Arduino to the Raspberry Pi via USB cable.

Step 4: Setting Up Raspbian for RFID RC522

1. Enable the SPI (Serial Peripheral Interface), opening the terminal and running the following command.

sudo raspi-config

2. Use the arrow keys to select “5 Interfacing Options“ and press Enter.

3. Use your arrow keys to select “P4 SPI“, and press Enter.

4. You will now be asked if you want to enable the SPI Interface, select Yes with your arrow keys and press Enter to proceed.

5. Once the SPI interface has been successfully enabled you should see the following text appear on the screen, “The SPI interface is enabled“. Restart the Raspberry Pi by pressing Enter and then ESC. Type the following command into the terminal on your Raspberry Pi to restart your Raspberry Pi.

sudo reboot

6. Once your Raspberry Pi has finished rebooting, check to make sure that it has in fact been enabled. Run the following command to see if spi_bcm2835 is listed.

lsmod | grep spi

7. Install Python-dev and git with the command.

sudo apt-get install python-dev git

8. Start Node-Red to create a folder ~/.node in you home folder. Run the command

node-red-start

Access the user interface using your browser, point to the address http://<iprasberrpi>:1880. For example my Pi is on my home office network on address 192.168.0.17, so to access Node-RED I browse to http://192.168.0.17:1880/

Now stop the Node-Red Server with the command.

node-red-stop

9. Install the Daemon node and Serial node.

cd ~./node-red
npm i node-red-node-daemon
npm i node-red-node-serialport

10. Install SPI-Py.

cd ~
git clone https://github.com.lthiery/SPI-Py.git
cd SPI-Py/
sudo python setup.py install

11. Extract these files from the zip in your user's home directory – i.e. /home/pi.

12. Reboot the pi and Start Node-Red once again.

13. Test the card reader by running the script from commandline

cd ~
python rfidreader.py

When you wave a tag within ~1cm of the main area of the card reader, the script will print the unique UID of the card, and it's type. There are various types of card, the most common are called MIFARE 1KB, but there are others. Not all cards use the same RF communication protocol so don't be surprised if a random card that you pick up is not recognized. Remember the UID of your tags for later.

Step 5: Create a Node-RED Flow to Read Your RFID Tag and Color Sensor

Connect to Node-RED on your Pi using a browser – either from another computer or on the Pi itself.

1. Click on hamburger icon next to the red deploy button in the top right.

2. Go to Import and click on Clipboard.

3. Copy the entire contents of the text in the node-red-flow.txt file and paste it into the clipboard and press Import.

4. We have to make sure everything is configured correctly. Firstly, click on the orange serial node (top left). Click on the Pen icon and then the Viewfinder icon and select the serial port the Arduino is connected to. Then Click the red Update button followed by the red Done button.

5. Next we will configure the purple MQTT node (next to the serial node). Click on the Pen icon. Type the IP address of where you would like the broker to be. Once completed, click the red Update button and red Done button.

6. Finally, we configure the orange function node called JSON to Object next to the RFIDReader node. The function contains a switch statement. This will take the UID of tags and rename them. In our case, we had two tags which we renamed User 1 and User 2.

IMPORTANT NOTE: If you intend to use more than two tags you will have to change the scripts/flow.

Step 6: Deploy and Dashboard

Everything is setup and ready to go. Press the red Deploy button on the top right.

Click on the bar graph icon under it followed by the icon that appears to be a box with an arrow. A new window should appear with the RFID&SENSOR TABLE and RFID-RC522 widgets.

Press the Create button to start a new table and begin testing your tags. You should see the table showing the different tags the number of points/times it has been detected and the date/time. Meanwhile on the breadboard, the green led should light up whenever a tag is detected, if not the red light will be on. (To clear the table press Clear and to delete the table press Delete). The colour sensor should work in the same manner. If the cup is detected then the black dot will turn green.

Step 7: Future

  • Better looking UI
  • More Users
  • Camera/Photo database for more accurate coffee cup detection
  • Send point updates to twitter accounts