Machine Learning Powered Color Detector (On Any Microcontroller!)
by GreekGrapes in Circuits > Arduino
4177 Views, 68 Favorites, 0 Comments
Machine Learning Powered Color Detector (On Any Microcontroller!)
Today I'll be showing you how to create a color detector using cheap parts you (probably) already have! This project would be useful for automation in warehouses and factories where you simply need to know if something is red, yellow, or some other color.
As you've read from the title, we use machine learning to categorize what color the detector is looking at... but don't worry! We aren't using special boards, TensorFlow, or anything complicated. This project can be done with any microcontroller, and the machine learning part is only one function!
Supplies
You'll need these to make your own Color Detector
- 1" Conduit Body from a hardware store
- Arduino Nano (or any other microcontroller)
- A phototransistor
- Red, green, blue, and yellow LEDs
- Seven Segment Display
- A Button
- Linear Potentiometer (Any resistance)
- Toggle Switch (Make sure it's durable! I had to replace my rocker switch!)
- A micro SD card module
- A micro SD card (Any modern one will have more than enough space)
- Protoboards
- PCB standoffs (Make sure they fit your protoboards)
- LED Holders (We're not using them conventionally)
- Female header pins (Not required but nice if you want to reuse your microcontroller for another project)
You'll also need
- Jumper wires (I used the kind with male and female ends)
- Soldering iron
- Hot glue (Not necessary, but nice to keep the wires in place)
- A Drill
- File/Sandpaper
- Clamps/Vice
- A Permanent marker
- Cutting pliers
Modifying the Housing
Take your conduit body and decide on a layout for your button, switch, display, and potentiometer on the lid.
Secure the lid using a clamp or vice and drill out rough patterns for your components.
Once you've gotten the holes to the proper size, go in with a file or some sandpaper and smooth out the edges. We want the components to fit snug, but if you accidentally make the holes too large, don't be afraid to glue them into place.
We'll also need to create a circular protoboard for our LEDs. Using the conduit, draw a circle onto the protoboard with a marker. Take a pair of cutting pliers and cut a rough circle around your marking, smoothing out the edges with a file. We want the protoboard to fit inside the bottom hole of the detector, preferably without glue, so take it slow and check the fit often.
Making the Main Circuitry
For this portion, we use two identical protoboards.
Our first will be simple, with just the display and button.
For the display, wire the ground leads together. I only have the ground wire attached to the display. The other pins will be connected to directly with female jumper wires. For the button, just two wires are needed since we'll be using internal pullup resistors on the Arduino. To make attaching the two halves together easier, I used jumper wires. By cutting the wire in half (one male end and one female end) we can use this to color coordinate our connection points.
On the second board we need the female header pins and the micro SD card module.
I decided to have the micro SD card module and Nano USB connection face the same direction on the protoboard. The reason is so I can place them both facing the other hole in the conduit and have easy access. Make sure you have plenty of room for soldering wires, because there are a lot in this project.
The color coordination is really up to you, but I typically kept all positive voltage wires red and ground wires black/gray/dark purple. Use the wiring diagram to determine which wires need to go where. Ignore the LED and phototransistor part for now, since we'll cover that in the next step. One trick I used is for the potentiometer is simply using female jumper wires to connect right to the pins. You could also do this with the LEDs, but I don't recommend it. I didn't do this for the toggle switch, however. I simply soldered two wires to the leads and use another pullup resistor in the code.
Making the LED Circuitry
Placing the LEDs around the phototransistor, I soldered the cathodes (short pins) of the LEDs together to share one common ground (and reduce the amount of wires I'll need). I also color coordinated the LED wires so I could easily connect them to the proper pins of the Nano (I used brown for red).
For the phototransistor, I connected the resistors in series on a small piece of protoboard scrap, and wired the cathode pin to that. The other end of the resistors are wired to ground. The resistance values of these resistors can add up to whatever you want, but I used two 1M ohm resistors and one 470K ohm resistor. Feel free to play around with the total resistance to see what gives the best, wide range output for your specific microcontroller.
Referencing the wiring diagram again, you'll see that we need to wire the anode pin to 5 volts, and also wire the cathode pin to analog pin A4. Be sure to put this wire before the resistors as shown above, or you won't get any usable readings.
Once everything is soldered into place, place the circular protoboard into the hole of the conduit. If your board is loose, carefully glue it to the conduit, making sure none of the LEDs are covered.
Finally, place the LED holders onto the LEDs and phototransistor. They should just snap on, but if they don't, you can skip this step. They help direct the light of the LEDs but aren't necessary.
Begin Assembly
Using the standoffs, connect the two protoboards together. They will act as support for the boards, push the button and display closer to the lid, and prevent wires from moving around. Connect all jumper wires to their proper connections, and slide the boards into the conduit. Depending on the length of boards you used, you may have to slide them in at an angle first, with parts inside the second hole of the conduit.
Train Your Detector
We're now at the fun part of machine learning... training!
Using the code supplied below, we'll have to flip the switch and set the detector to Training Mode, and select which group we want to classify something as. For example, we will turn the potentiometer to A, place the end of the detector on a red object, and click the button. We can do this to multiple red objects, or just do this to the same red object 12 times. Then we'll turn the potentiometer again to B and point at something blue. Repeat this process for all letters and different colors.
You may want to write down what colors belong to what letter group so you don't forget!
Downloads
Detecting Colors!
Now that you've fully trained the detector on what colors you want to detect, flip the switch back to Sampling Mode, and pick a color you want to detect. If we point at our red object from earlier and hit the button, it will think for a second and display the letter A on the display, denoting that we just scanned a red object.
The beauty of using an SD card is that you can bring the data directly to your PC and see what color values the detector is "seeing." This means you can also hold onto certain color values to easily swap in and out files of the detector, skipping the training portion. You could also bring the data into Microsoft Excel to plot the values and see the overall similarities between certain training samples.
By the way, I left tons of comments explaining everything that happens in the code! Even if you don't want to create your own color detector, there are certainly neat tricks to learn from it's code, like how to save memory and create your own machine learning algorithm!