Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino Mega 2560 Controlled Robotic Vehicle with TCS230 Color Sensing and Ultrasonic Obstacle Avoidance

Image of Arduino Mega 2560 Controlled Robotic Vehicle with TCS230 Color Sensing and Ultrasonic Obstacle Avoidance

Circuit Documentation

Summary

This circuit is designed to control a vehicle with motorized wheels, using an Arduino Mega 2560 as the central processing unit. It features a TCS230 color sensor for detecting color changes, an HC-SR04 ultrasonic sensor for distance measurement, a Tower Pro SG90 servo for steering, and an L298N DC motor driver for controlling the motors. The circuit is powered by a 7.4V li-ion battery, and it includes a TP4056 charging module for battery management. A photocell (LDR) is used for light detection, and a buzzer is included for audio feedback. The circuit is switched on and off using a rocker switch.

Component List

  • L298N DC Motor Driver: A module used to control the direction and speed of DC motors.
  • Motor and Wheels: Four instances of this component represent the vehicle's motors and wheels.
  • TCS230 Color Sensor: A sensor that detects color and provides frequency output corresponding to the color detected.
  • Rocker Switch: A switch to control the power supply to the circuit.
  • Photocell (LDR): A light-dependent resistor used for detecting light levels.
  • HC-SR04 Ultrasonic Sensor: Two instances of this sensor are used for measuring distances by emitting ultrasonic waves.
  • Tower Pro SG90 Servo: A small servo motor used for precise control of angular motion.
  • Li-ion Battery 7.4V 3000mAh: The power source for the circuit.
  • TP4056 Charging Module: A module used for charging the li-ion battery.
  • Resistor: Two resistors with different resistance values for current limiting and voltage division.
  • BC547 Transistor: A general-purpose NPN transistor used for switching or amplification.
  • Buzzer: An audio signaling device.
  • Arduino Mega 2560: The microcontroller board that controls the entire circuit.

Wiring Details

L298N DC Motor Driver

  • ENA, ENB: Connected to Arduino Mega 2560 (PWM pins D2, D3) for speed control.
  • IN1, IN2, IN3, IN4: Connected to Arduino Mega 2560 (pins D22, D24, D26, D28) for direction control.
  • OUT1, OUT2: Connected to two motors and wheels.
  • OUT3, OUT4: Connected to the other two motors and wheels.
  • GND: Connected to the common ground.
  • 12V: Connected to the positive terminal of the li-ion battery.
  • 5V: Connected to the Rocker Switch.

Motor and Wheels

  • Four instances, each connected to the L298N DC Motor Driver's OUT1, OUT2, OUT3, and OUT4 for motor control.

TCS230 Color Sensor

  • VCC: Connected to the 5V output from the Arduino Mega 2560.
  • GND: Connected to the common ground.
  • Out: Connected to Arduino Mega 2560 (pin D4 PWM).
  • S0, S1, S2, S3: Connected to Arduino Mega 2560 (pins D36, D32, D30, D34) for frequency scaling and color selection.

Rocker Switch

  • Pin 1: Connected to the L298N DC Motor Driver (5V).
  • Pin 2: Connected to the Photocell (LDR).

Photocell (LDR)

  • Pin 0: Connected to Arduino Mega 2560 (pin D38).
  • Pin 1: Connected to the Rocker Switch.

HC-SR04 Ultrasonic Sensor

  • Two instances:
    • VCC: Connected to the 5V output from the Arduino Mega 2560.
    • GND: Connected to the common ground.
    • TRIG: Connected to Arduino Mega 2560 (pins D44, D40).
    • ECHO: Connected to Arduino Mega 2560 (pins D46, D42).

Tower Pro SG90 Servo

  • Signal: Connected to Arduino Mega 2560 (pin D48).
  • +5V: Connected to the 5V output from the Arduino Mega 2560.
  • GND: Connected to the common ground.

Li-ion Battery 7.4V 3000mAh

  • Positive: Connected to the TP4056 Charging Module and the L298N DC Motor Driver (12V).
  • Negative: Connected to the common ground.

TP4056 Charging Module

  • Connected to the li-ion battery for charging purposes.
  • Connected to the BC547 Transistor and the Buzzer for control.

Resistor

  • Two instances with different resistance values:
    • 200 Ohms: Connected between the TCS230 Color Sensor (GND) and the common ground.
    • 1500 Ohms: Connected between the TP4056 Charging Module and the BC547 Transistor (Base).

BC547 Transistor

  • Collector: Connected to the Buzzer (POSITIVE).
  • Base: Connected to the 1500 Ohms resistor.
  • Emitter: Connected to the common ground.

Buzzer

  • POSITIVE: Connected to the BC547 Transistor (Collector).
  • NEGATIVE: Connected to the TP4056 Charging Module.

Documented Code

#include <Servo.h>

Servo servo1;

// TCS Colour sensor pins
const int S0 = 36;
const int S1 = 32;
const int S2 = 30;
const int S3 = 34;
const int OUT = 4;

// Color frequency variables
int red_frequency;
int green_frequency;
int blue_frequency;

// L298N MOTOR DRIVER pins
const int IN1 = 22;
const int IN2 = 24;
const int IN3 = 26;
const int IN4 = 28;
const int ENA = 2;
const int ENB = 3;

// Ultrasonic sensor pins
const int trig_up = 40;
const int echo_up = 42;

// Ultrasonic sensor variables
long duration_up;
int distance_up;
int tolerance = 3;

// Servo pin
const int pin = 48;

void setup() {
  // Initialize TCS230 Color sensor
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(OUT, INPUT);

  // Initialize L298N MOTOR DRIVER
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

  // Initialize Ultrasonic sensor
  pinMode(trig_up, OUTPUT);
  pinMode(echo_up, INPUT);

  // Initialize Servo
  pinMode(pin, INPUT);
  servo1.attach(pin);

  // Start serial communication
  Serial.begin(9600);

  // Set frequency scaling to 20%
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);

  // Read initial color frequencies
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  red_frequency = pulseIn(OUT, LOW);
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  green_frequency = pulseIn(OUT, LOW);
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  blue_frequency = pulseIn(OUT, LOW);
}

void loop() {
  // Read color frequencies
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  red_frequency = pulseIn(OUT, LOW);
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  green_frequency = pulseIn(OUT, LOW);
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  blue_frequency = pulseIn(OUT, LOW);

  // Color detection logic
  if (red_frequency == 255 && green_frequency == 255 && blue_frequency == 255) {
    slow();
  } else {
    // Movement logic based on color detection
    if (red_frequency == red && green_frequency == green && blue_frequency == blue) {
      go();
    } else {
      right();
      delay(750);
      updateColorFrequencies();
      if (red_frequency != red || green_frequency != green || blue_frequency != blue) {
        left();
        delay(1500);
        updateColorFrequencies();
        if (red_frequency != red || green_frequency != green || blue_frequency != blue) {
          stop();
        } else {
          go();
        }
      } else {
        go();
      }
    }
  }

  // Ultrasonic sensor logic
  measureDistance();
  if (distance_up <= 6 + tolerance) {
    stop();
    delay(5000);
    servo1.write(45);
    measureDistance();
    if (distance_up >= 6 + tolerance) {
      right();
      delay(500);
      left();
      delay(500);
      go();
    }
  }
}

// Function definitions for motor control
void go() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 200);
  analogWrite(ENB, 200);
}

void back() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  analogWrite(ENA,