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

Arduino UNO-Based Robotic System with Ultrasonic Sensor and Motor Control

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Robotic System with Ultrasonic Sensor and Motor Control

Circuit Documentation

Summary

This circuit involves an Arduino UNO microcontroller controlling two DC motors, a micro servo motor, and an HC-SR04 ultrasonic sensor. The DC motors are driven by an L298N motor driver. The ultrasonic sensor measures distance, and based on the distance detected, an LED will blink for a specified duration. The servo motor is controlled directly by the Arduino and moves to various angles as specified in the code.

Component List

  1. Arduino UNO

    • Description: A 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. Micro servo 9G

    • Description: A small servo motor used for precise control of angular position.
    • Pins: GND, +5V, PWM
  3. DC Motor

    • Description: A motor that converts electrical energy into mechanical motion.
    • Pins: pin 1, pin 2
  4. L298N DC motor driver

    • Description: A dual H-bridge motor driver that allows control of two DC motors.
    • Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
  5. HC-SR04 Ultrasonic Sensor

    • Description: A sensor that measures distance by using ultrasonic waves.
    • Pins: VCC, TRIG, ECHO, GND

Wiring Details

Arduino UNO

Micro servo 9G

DC Motor 1

DC Motor 2

L298N DC motor driver

HC-SR04 Ultrasonic Sensor

Code Documentation

/*
 * This Arduino sketch controls two DC motors, a servo motor, and an ultrasonic
 * sensor. The DC motors are driven by an L298N motor driver, and the servo
 * motor is controlled directly by the Arduino. The ultrasonic sensor measures
 * distance, and if an object is detected within 6-8 cm, an LED will blink for 3
 * seconds. If an object is detected within 8-12 cm, the LED will blink for 1
 * second. The code performs the following sequence:
 * 1. Both motors move forward for 2 seconds.
 * 2. Both motors stop for 0.5 seconds.
 * 3. Both motors move in reverse for 2 seconds.
 * 4. Both motors stop for 0.5 seconds.
 * 5. Motor 1 moves forward and Motor 2 moves in reverse for 2 seconds.
 * 6. Both motors stop, and the sequence repeats.
 * 7. The servo motor moves to 25 degrees, then back to 0 degrees.
 * 8. The servo motor moves to 90 degrees, then back to 0 degrees.
 * 9. The servo motor moves to 155 degrees, then back to 0 degrees.
 */

#include <Servo.h>

// Pin definitions
const int motor1Pin1 = 7; // IN1 on L298N
const int motor1Pin2 = 8; // IN2 on L298N
const int motor2Pin1 = 9; // IN3 on L298N
const int motor2Pin2 = 10; // IN4 on L298N
const int enablePinA = 5; // ENA on L298N
const int enablePinB = 6; // ENB on L298N
const int servoPin = 9; // Servo PWM pin
const int trigPin = 3; // TRIG pin on HC-SR04
const int echoPin = 2; // ECHO pin on HC-SR04
const int ledPin = 13; // LED pin

Servo myServo;

void setup() {
  // Set motor pins as outputs
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(enablePinA, OUTPUT);
  pinMode(enablePinB, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // Initialize servo
  myServo.attach(servoPin);

  // Enable motors
  digitalWrite(enablePinA, HIGH);
  digitalWrite(enablePinB, HIGH);
}

void loop() {
  // Move both motors forward
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
  delay(2000);

  // Stop both motors
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
  delay(500);

  // Move both motors in reverse
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, HIGH);
  delay(2000);

  // Stop both motors
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
  delay(500);

  // Motor 1 forward, Motor 2 reverse
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, HIGH);
  delay(2000);

  // Stop both motors
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
  delay(500);

  // Measure distance
  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;

  // If distance is less than 6-8 cm, blink LED for 3 seconds
  if (distance >= 6 && distance < 8) {
    for (int i = 0; i < 6; i++) {
      digitalWrite(ledPin, HIGH);
      delay(