Introduction: Candy Locker

Someone stealing your candy? Keep it safe from greedy hands with this color recognition lock and dispenser. Using a set of 5 distinct color images, you’ll be able to set up a pattern that will activate and dispense candy. In this tutorial, you will learn how to use the Intel® Edison and a webcam for color recognition. After assembling the electronics and housing, you’ll train the color recognition software using the image cards.

Here is a video of the completed project.

This tutorial was originally published on Intel Communities here.

Step 1: Before You Start

Before you begin, make sure you’ve followed through Intel® Edison Getting Started guide, and our tutorial, the Intel® Edison mini-breakout Getting Started Guide. This means that your Intel® Edison:

  1. has updated firmware
  2. is connected to the local wireless network
  3. can connect to ssh/scp through microUSB or WiFi

Step 2: Needed Components

  1. Intel® Edison and Arduino expansion board
  2. Arduino Protoshield
  3. 1 Webcam UVC compatible (Logitech HD Webcam C615)
  4. 1 Intel® Edison power supply
  5. Wire
  6. 1 Standard micro servo
  7. 1 220 Ohm resistor
  8. 1 1k Ohm resistor
  9. 0.2 mm Enamel wire
  10. 8 SMD white LED
  11. 1 10 mm red LED
  12. 1 10 mm green LED

Step 3: Preparing the Intel® Edison

This example requires you to have a webcam recognized by Intel® Edison. Fortunately, with the latest system installed (ww05-15), most webcams should be recognized by default.

Other than that, we need to install a few python libraries: OpenCV, Numpy, and updated mraa.

Get the Extra Repositories:

The python libraries needed are not in the standard repository so you must add a few new repositories. The following commands will add the new repositories, and update the existing ones. After that you’ll be able to install the necessary libraries with ease.

SSH to your Intel® Edison and type the following command in the terminal:

echo -e “src iot-devkit-i586 http://iotdk.intel.com/repos/1.1/iotdk/i586\nsrc mraa-upm http://iotdk.intel.com/repos/1.1/iotdk/i586\nsrc > /etc/opkg/intel-iotdk.conf
opkg update
opkg upgrade

Step 4: Install Python Libraries

Next, it’s time to install Numpy and OpenCV

Numpy is a python library for working with data. To install it, type the following command in the terminal:

opkg install python-numpy

The newest version of system actually has OpenCV installed, but only with C++. Here we need a Python binding to get it working. To do this, type the following command in the terminal:

opkg install python-opencv

Step 5: Move Script to Intel® Edison

On your computer, download “CandyLock.zip” from the attachment of this tutorial and unzip it. Go into the parent folder of the unzipped folder. Copy it over by typing the following command in the terminal:

scp -r ./CandyLock root@your-edison-ip:/home/root/

Adjust focus for the camera

Unfortunately the webcam doesn’t handle auto-focus very well when it comes to OpenCV. Based on this tutorial and we developed our own method of calibration.

Open the camera casing and place it within the construction. After following the instructions, this is already done. It may be a bit inconvenient to reopen some panels and work in a confined space. We do the calibration within the construction so the focal length is fixed.

Run the script TestCamera.py by typing the following into the terminal:

python /home/root/CandyLock/TestCamera.py

The script will take a picture and save it to a local file whenever you hit enter. Take one picture by hitting key once.

Copy the picture over to the current directory by typing the following into the terminal:

scp root@your-edison-ip:/home/root/CandyLock/camTest.jpg ./

If the image is not sharp, adjust the webcam lens manually by twisting the lens a bit to change its focal length. Repeat this process until the image is clear and sharp.

Step 6: Run the Candy Locker

First, you need to save a 4-token sequence for the locker. To start the training sequence, type the following command in the terminal:

python /home/root/CandyLock/combiLock_train.py

Decide on your sequence and then place the first token inside the machine. You’ll see the green LED blink a few times. Shortly after, the picture is taken. Now take the token out, and place the next one in. Repeat until all 4 tokens are trained. The Python script will end when the sequence is recorded.

Next, it’s time to get your candies out. To start the dispensing sequence, type the following command in the terminal:

python /home/root/CandyLock/combiLock.py

Place the first token of the sequence inside the machine. Wait until the green LED finishes blinking and then place the next one. Do this until all 4 tokens are registered.

If the sequence is correct,, the green LED will blink quickly a few times and a candy will drop out. If the sequence is incorrect, the red LED will turn on for a few seconds and nothing is dispensed. Wait until the red LED turns off before making another try.

Step 7: Sort the Lasercut Pieces for Easier Construction

Sort all the piecies according to the image.

Step 8: Solder the Electronics

Step 9: Assemble the Candy Locker Pt1

  1. Make the camera LED ring
  2. Apply white glue to Part C1
  3. Place the SMD LEDs into the slots and wait for the glue to dry
  4. Place a small bead of solder on each pad of the SMD LEDs
  5. Cut about 40 cm of enamel wire and cover about 5 cm of the ends with solder
  6. Solder the wires to the pad of the SMD LEDs. Clip off any excess
  7. Test connections with a coin battery
  8. Cover with hot glue to secure SMD LEDs to plexiglas ring
  9. Note: Enamel wire has a thin plastic coating which needs to be removed. To do this, get a scrap piece of MDF and place the end of the wire on it. Get a bead of solder and press the iron on the enamel wire, about 5 cm from the end. Slowly pull the wire, drawing it through the solder bead
  10. This coats only the top surface of the wire, so flip it over and repeat
  11. Bend and glue dispenser plate.
  12. Heat Part B3 along dashed line and bend slightly
  13. Glue Parts B2 and B4 to B3
  14. Cut a piece of plastic and place some copper tape on it
  15. Insert through the slit and tape it on the underside
  16. Assemble dispenser servo
  17. Use contact glue or spray adhesive to attach Part D1 to D2
  18. Attach to servo arm and secure with screws
  19. Insert into Part A5
  20. Secure with Part A12

Step 10: Assemble the Candy Locker Pt2

  1. Disassemble the body of webcam
  2. Attach to Part A11, with the engraved recesses facing the camera
  3. Hot glue the LED ring to the underside of Part A11
  4. Assemble main body frame
  5. Attach Part A4, A5, A6, and B3 to Part A10. Put the servo cable through hole on Part B3
  6. Attach Part A1 to assembly
  7. Attach Part A7 to assembly
  8. Secure wires along notched edge
  9. Attach electronics and make token switch
  10. Mount the Intel® Edison onto Part A3
  11. Attach the prototype shield
  12. Insert wire into Part B3 and tape down with copper tape
  13. Attach switch wires to the nodes using copper tape
  14. Plug in the webcam, microUSB, and power cables
  15. Push the LEDs through the holes on Part A2

Step 11: Assemble the Candy Locker Pt3

  1. Close bottom, back, and front
  2. Attach Parts A3 to the bottom, A8 to the back, and Part A9 to the front
  3. Attach funnel, LEDs, fill with candy, and decorate
  4. Attach Part E1 to assembly using double sided tape or glue
  5. Push red and green LED through openings in Part A2
  6. Fill the container with candy
  7. Secure lid and decorate front and tokens with images