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

Arduino UNO Controlled Robot with Ultrasonic Distance Sensing and Line Following Capabilities

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Robot with Ultrasonic Distance Sensing and Line Following Capabilities

Circuit Documentation

Summary

This circuit is designed to control two motors using an L298N DC motor driver, interfaced with an Arduino UNO microcontroller. The circuit also includes an MKE-S01 Ultrasonic Distance Sensor for measuring distances and an MKE-S10 CNY70 Line Follower Sensor for detecting lines. The motors' operations are influenced by the sensors' readings. One motor's activation is based on the line follower sensor, while the other motor's speed is adjusted according to the distance measured by the ultrasonic sensor. The circuit is powered by a 12V battery.

Component List

Arduino UNO

MKE-S10 CNY70 Line Follower Sensor

MKE-S01 Ultrasonic Distance Sensor

L298N DC Motor Driver

Motor Amarillo Motorreductor Hobby (x2)

Battery 12V

Wiring Details

Arduino UNO

MKE-S10 CNY70 Line Follower Sensor

MKE-S01 Ultrasonic Distance Sensor

L298N DC Motor Driver

Motor Amarillo Motorreductor Hobby

Battery 12V

Documented Code

/*
 * Arduino Sketch for controlling two motors using an L298 motor driver,
 * an ultrasonic sensor, and a line follower sensor. Motor 1 is activated
 * or stopped based on the line follower sensor, and motor 2's speed is
 * adjusted based on the distance measured by the ultrasonic sensor.
 */

// Pin definitions
const int lineFollowerPin = A0; // Line follower sensor pin
const int trigPin = 9;          // Ultrasonic sensor TRIG pin
const int echoPin = 10;         // Ultrasonic sensor ECHO pin
const int motor1Pin1 = 2;       // Motor 1 control pin 1
const int motor1Pin2 = 3;       // Motor 1 control pin 2
const int motor2Pin1 = 4;       // Motor 2 control pin 1
const int motor2Pin2 = 5;       // Motor 2 control pin 2
const int motor2SpeedPin = 6;   // Motor 2 speed control pin

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Initialize pins
  pinMode(lineFollowerPin, INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(motor2SpeedPin, OUTPUT);
}

void loop() {
  // Read line follower sensor
  int lineState = analogRead(lineFollowerPin);

  // Control motor 1 based on line follower sensor
  if (lineState < 500) { // Assuming black is low value
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
  } else { // Assuming white is high value
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);
  }

  // Measure distance using ultrasonic sensor
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;

  // Control motor 2 speed based on distance
  int motorSpeed = map(distance, 0, 100, 0, 255);
  motorSpeed = constrain(motorSpeed, 0, 255);
  analogWrite(motor2SpeedPin, motorSpeed);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);

  // Small delay to stabilize readings
  delay(100);
}

This code is responsible for reading sensor inputs and controlling the motors accordingly. The line follower sensor's signal is read through an analog pin and is used to control the first motor. The ultrasonic sensor's distance measurement is used to adjust the speed of the second motor. The code includes setup routines for pin configurations and a main loop that handles sensor readings and motor control logic.