Build a color-based object sorting system using the TCS34725 sensor and a Raspberry Pi
Let’s build a color-based object sorting system using the TCS34725 sensor and a Raspberry Pi. This project can classify objects (e.g., Skittles, LEGO bricks) by color and trigger actions (e.g., servo motors, LEDs, or even a robotic arm). Here’s how to do it:
Project Overview
Hardware Setup
Raspberry Pi + TCS34725 sensor
Servo motor (to push objects into bins)
LED feedback (optional)
Conveyor belt (optional, for automation)
Software Logic
Calibrate sensor for target colors (e.g., red, green, blue).
Classify objects based on RGB values.
Trigger servo/LED when a color is detected.
Step 1: Wiring (Add a Servo Motor)
Component | Raspberry Pi Pin |
---|---|
TCS34725 (VCC) | 3.3V (Pin 1) |
TCS34725 (GND) | GND (Pin 6) |
TCS34725 (SDA) | GPIO2 (Pin 3) |
TCS34725 (SCL) | GPIO3 (Pin 5) |
Servo PWM | GPIO17 (Pin 11) |
(Use a separate 5V power supply for the servo if needed.)
Step 2: Install Servo Control Library
sudo pip3 install gpiozero
Step 3: Python Code (Color Sorting Logic)
import time import Adafruit_TCS34725 from gpiozero import Servo, LED # Initialize hardware tcs = Adafruit_TCS34725.TCS34725() servo = Servo(17) # GPIO17 led_red = LED(22) # GPIO22 (optional) # Color thresholds (calibrate these for your objects!) COLOR_THRESHOLDS = { "red": ((150, 50, 50), (255, 100, 100)), # Min/Max (R, G, B) "green": ((50, 150, 50), (100, 255, 100)), "blue": ((50, 50, 150), (100, 100, 255)), } def classify_color(r, g, b): for color, ((r_min, g_min, b_min), (r_max, g_max, b_max)) in COLOR_THRESHOLDS.items(): if (r_min <= r <= r_max) and (g_min <= g <= g_max) and (b_min <= b <= b_max): return color return "unknown" try: while True: r, g, b, _ = tcs.get_raw_data() r_norm = int((r / (r + g + b)) * 255) # Normalize g_norm = int((g / (r + g + b)) * 255) b_norm = int((b / (r + g + b)) * 255) color = classify_color(r_norm, g_norm, b_norm) print(f"Detected: {color} (R: {r_norm}, G: {g_norm}, B: {b_norm})") # Trigger actions if color == "red": servo.max() # Move servo to 90° led_red.on() elif color == "green": servo.mid() # Move servo to 45° led_red.off() elif color == "blue": servo.min() # Move servo to 0° led_red.off() time.sleep(1) except KeyboardInterrupt: servo.close() led_red.off()
Step 4: Calibration
Place a red object under the sensor and note the
(R, G, B)
values.Repeat for green/blue objects.
Adjust
COLOR_THRESHOLDS
in the code to match your readings.
(Tip: Use print(r_norm, g_norm, b_norm)
to debug values.)
Step 5: Mechanical Sorting
Attach a cardboard arm to the servo to push objects into bins.
Use a sloped track to guide objects after sorting.
For a conveyor belt, add a motor controlled via GPIO.
https://i.imgur.com/JQ8WzVl.png
(Example: Servo arm pushes objects left/right based on color.)
Troubleshooting
Issue | Solution |
---|---|
Servo jitters | Use a capacitor or external power. |
Wrong colors detected | Re-calibrate thresholds. |
Sensor noise | Add a diffuser (e.g., white paper). |
Extensions
Add a Database
Log sorted colors to a CSV/SQLite file:import csv with open('colors.csv', 'a') as f: csv.writer(f).writerow([time.time(), color])
Web Interface
UseFlask
to display sorting stats in real-time:from flask import Flask app = Flask(__name__) @app.route("/") def stats(): return f"Last sorted: {color}"
Machine Learning
Train a K-NN classifier for more colors (e.g., purple, yellow).
Demo Video Idea
Show Skittles being sorted into cups by color.
Use LEDs to light up the bin where the object lands.
评论
发表评论