This repository contains the code and setup instructions for a Color Detection Robot, designed by Logic Laboratories. The robot detects a specific color (such as a tennis ball) using a camera and HSV color calibration. Once detected, it moves toward the object by controlling motors through an Arduino microcontroller, connected via serial communication.
- Hardware Requirements
- Software Requirements
- File Structure
- Setup Instructions
- Usage
- Troubleshooting
- License
- Arduino Uno: To control the robot motors.
- Motor Driver (we used TB6612FNG): To drive the robot’s motors.
- Raspberry Pi: To run the color detection code.
- Webcam: For real-time color detection.
- Robot Chassis: Motorized chassis for movement.
- Tennis ball (or other color object): The object the robot will track.
- Python: For running the main detection code.
- OpenCV: For image processing (
cv2
library). - Numpy: For numerical processing.
- PySerial: For serial communication between the Raspberry Pi/Laptop and the Arduino.
Install the required libraries by running:
pip install opencv-python numpy pyserial
|-- main.py # Main color detection script
|-- threshold.py # HSV color thresholding and calibration
|-- arduino_code.ino # Arduino sketch for motor control
|-- color_thresholds.txt # Exported HSV values (generated after calibration)
- Upload the
arduino_code.ino
to your Arduino board using the Arduino IDE. - Connect the Arduino to your motor driver and chassis according to the pin definitions in the code.
- The Arduino reads movement commands from serial input (e.g.,
F
for forward,B
for backward). - Motors are driven using PWM values received from the main Python script.
- The Arduino reads movement commands from serial input (e.g.,
- Run
threshold.py
to calibrate the HSV color detection for the object (e.g., a tennis ball):python3 threshold.py
- Adjust the HSV sliders in the UI until the mask correctly detects the object.
- The final calibrated values are automatically saved to
color_thresholds.txt
.
- Connect the Arduino to your Raspberry Pi or computer.
- Run
main.py
to begin the detection and control process:python3 main.py
- The robot will begin tracking the color and adjusting its position accordingly.
- You can stop the script anytime by pressing
q
.
Ensure that the correct serial port is used when initializing the camera detector:
detector = CameraDetector('/dev/ttyACM0', 9600)
Adjust /dev/ttyACM0
to match your Arduino's serial port if needed.
- Forward Movement (
F + PWM
): Moves the robot forward with the specified PWM. - Backward Movement (
B + PWM
): Moves the robot backward. - Left Turn (
L + PWM
): Turns the robot left by reducing the left motor's speed. - Right Turn (
R + PWM
): Turns the robot right by reducing the right motor's speed. - Stop (
S
): Stops the robot.
- Press
q
to quit both the color detection script (main.py
) and the calibration tool (threshold.py
).
- No Camera Detected: Ensure that your camera is properly connected and that
cv2.VideoCapture(0)
works. - Serial Communication Issues: Verify that the correct serial port is being used and that the Arduino is properly connected.
- Robot Not Moving: Check the motor wiring and make sure the motor driver is properly connected to the Arduino.