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

Arduino UNO-Based Distance Measuring Robot with Motor Control and Servo

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Distance Measuring Robot with Motor Control and Servo

Circuit Documentation

Summary

This circuit is designed to control a motor driver, an ultrasonic sensor, and a servo motor using an Arduino UNO. The ultrasonic sensor measures distance, and based on the distance, an LED blinks a specified number of times. The results are also printed to the serial monitor.

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. Motor with reducer

    • Description: A DC motor with a gear reducer.
    • Pins: 3 - 6 VCC, GND
  3. L298N DC motor driver

    • Description: A dual H-Bridge motor driver.
    • 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
  4. HC-SR04 Ultrasonic Sensor

    • Description: An ultrasonic distance sensor.
    • Pins: VCC, TRIG, ECHO, GND
  5. Micro servo 9G

    • Description: A small servo motor.
    • Pins: GND, +5V, PWM
  6. 7.4v Battery

    • Description: A 7.4V battery for powering the circuit.
    • Pins: +, -

Wiring Details

Arduino UNO

Motor with reducer (Instance 1)

Motor with reducer (Instance 2)

L298N DC motor driver

HC-SR04 Ultrasonic Sensor

Micro servo 9G

Code Documentation

/*
 * This Arduino sketch controls a motor driver, ultrasonic sensor, and a servo.
 * The ultrasonic sensor measures distance, and based on the distance, the LED
 * blinks a specified number of times. The results are also printed to the
 * serial monitor.
 */

#include <Servo.h>

// Pin definitions
const int trigPin = 8;
const int echoPin = 9;
const int ledPin = 13;
const int servoPin = 3;
const int motorIn1 = 7;
const int motorIn2 = 6;
const int motorIn3 = 5;
const int motorIn4 = 4;

Servo myServo;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  
  // Initialize pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
  pinMode(motorIn3, OUTPUT);
  pinMode(motorIn4, OUTPUT);
  
  // Attach the servo
  myServo.attach(servoPin);
}

void loop() {
  // 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;
  
  // Print distance to serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  // Blink LED based on distance
  if (distance >= 3 && distance <= 5) {
    blinkLED(5);
  } else if (distance >= 10 && distance <= 20) {
    blinkLED(10);
  }
  
  // Small delay before next measurement
  delay(500);
}

void blinkLED(int times) {
  for (int i = 0; i < times; i++) {
    digitalWrite(ledPin, HIGH);
    delay(250);
    digitalWrite(ledPin, LOW);
    delay(250);
  }
}

This code initializes the pins for the ultrasonic sensor, motor driver, and servo motor. It measures the distance using the ultrasonic sensor and blinks an LED based on the measured distance. The results are printed to the serial monitor.