Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-Based Color Detection and Servo Control System with TCS3200 and PWM Servo Driver

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Color Detection and Servo Control System with TCS3200 and PWM Servo Driver

Circuit Documentation

Summary

This circuit is designed to control multiple servos and LEDs using an Arduino UNO microcontroller. It also includes a TCS3200 color sensor for color detection. The servos are controlled via a 16-Channel PWM Servo Driver, and the LEDs are used to indicate the detected color. The circuit is powered by a combination of 7.4V and 5V batteries.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. 16-Channel PWM Servo Driver

    • Description: PWM driver for controlling up to 16 servos.
    • Pins: V+, VCC, SDA, SCL, OE, GND, Vin+, Vin-, PWM1, PWM2, PWM3, PWM4, PWM5, PWM6, PWM7, PWM8, PWM9, PWM10, PWM11, PWM12, PWM13, PWM14, PWM15, PWM16
  3. Micro servo 9G

    • Description: Small servo motor.
    • Pins: GND, +5V, PWM
  4. 7.4V Battery

    • Description: Power source.
    • Pins: +, -
  5. 5V Battery

    • Description: Power source.
    • Pins: positive, negative
  6. TCS3200

    • Description: Color sensor.
    • Pins: GND, OUT, S2, S3, VCC, LED, S0, S1
  7. Resistor (220 Ohms)

    • Description: Resistor with 220 Ohms resistance.
    • Pins: pin1, pin2
  8. LED: Two Pin (red)

    • Description: Red LED.
    • Pins: cathode, anode
  9. LED: Two Pin (green)

    • Description: Green LED.
    • Pins: cathode, anode
  10. LED: Two Pin (blue)

    • Description: Blue LED.
    • Pins: cathode, anode

Wiring Details

Arduino UNO

16-Channel PWM Servo Driver

Micro servo 9G

7.4V Battery

5V Battery

TCS3200

Resistor (220 Ohms)

LED: Two Pin (red)

LED: Two Pin (green)

LED: Two Pin (blue)

Code Documentation

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// Create a PWM driver object
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Servo pulse length values (adjust as needed for specific servo motor)
int minPulse = 150;  // Pulse length for 0 degrees
int maxPulse = 700;  // Pulse length for 180 degrees

// TCS3200 color sensor pins
int S0 = 4; // Frequency scaling pin S0
int S1 = 5; // Frequency scaling pin S1
int S2 = 6; // Color filter pin S2
int S3 = 7; // Color filter pin S3
int OUT = 8; // Output pin for frequency

// LED pins
int redLED = 9;
int greenLED = 10;
int blueLED = 11;

// Helper function to convert degrees to pulse length
int angleToPulse(int angle) {
  return map(angle, 0, 180, minPulse, maxPulse);
}

// Function to rotate the servo to a specific angle
void rotateServo(int channel, int angle) {
  int targetPulse = angleToPulse(angle);
  pwm.setPWM(channel, 0, targetPulse);
}

// Function to initialize TCS3200 sensor
void initColorSensor() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(OUT, INPUT);
  
  // Set frequency scaling
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
}

// Function to read the frequency of each color
int readColor(int S2_state, int S3_state) {
  digitalWrite(S2, S2_state);
  digitalWrite(S3, S3_state);
  return pulseIn(OUT, digitalRead(OUT) == HIGH ? LOW : HIGH); // Frequency count for the current color
}

void setup() {
  Serial.begin(9600);
  Serial.println("PWM Servo Control with TCS3200 Color Detection");

  pwm.begin();               // Initialize the PWM board
  pwm.setPWMFreq(60);        // Set frequency to 60 Hz (standard for servos)
  delay(10);                 // Short delay for setup

  initColorSensor();         // Initialize TCS3200 sensor
  
  // Initialize LED pins
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(blueLED, OUTPUT);
}

// Function to detect color based on TCS3200 readings
String detectColor() {
  int redFrequency = readColor(LOW, LOW);
  int greenFrequency = readColor(HIGH, HIGH);
  int blueFrequency = readColor(LOW, HIGH);

  Serial.print("Red Frequency: ");
  Serial.println(redFrequency);
  Serial.print("Green Frequency: ");
  Serial.println(greenFrequency);
  Serial.print("Blue Frequency: ");
  Serial.println(blueFrequency);

  // Detect color logic
  if (redFrequency > 200