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

Arduino and Raspberry Pi-Based Robotic Weeding Machine with Ultrasonic Obstacle Detection

View and Edit Project In Cirkit DesignerImage of Arduino and Raspberry Pi-Based Robotic Weeding Machine with Ultrasonic Obstacle Detection

Circuit Documentation

Summary

This document provides a detailed overview of a robotic weeding machine circuit. The circuit includes an Arduino UNO, an HC-SR04 Ultrasonic Sensor, an OV7725 camera module, a Raspberry Pi 4B, an L298N DC motor driver, a DC motor, and a 12V battery. The Arduino UNO controls the motor and ultrasonic sensor, while the Raspberry Pi 4B interfaces with the camera module. The L298N motor driver is used to control the DC motor, and the 12V battery powers the motor driver.

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. HC-SR04 Ultrasonic Sensor

    • Description: An ultrasonic sensor used for distance measurement.
    • Pins: VCC, TRIG, ECHO, GND
  3. OV7725

    • Description: A camera module used for capturing images.
    • Pins: 3V3, GND, SIOC, SIOD, VSYNC, HREF, PCLK, XCLK, D9, D8, D7, D6, D5, D4, D3, D2, RESET, D1, D0, PWDN
  4. Raspberry Pi 4B

    • Description: A single-board computer used for processing and interfacing with the camera module.
    • Pins: 3V3, 5V, GPIO2, GPIO3, GND, GPIO4, GPIO14, GPIO15, GPIO17, GPIO18, GPIO27, GPIO22, GPIO23, GPIO24, GPIO10, GPIO9, GPIO25, GPIO11, GPIO8, GPIO7, ID_SD, GPIO5, GPIO6, GPIO12, GPIO13, GPIO19, GPIO16, GPIO26, GPIO20, GPIO21, ID_SC, TR01_TAP, TR00_TAP, TR03_TAP, TR02_TAP, RUN, GLOBAL_EN, ETH, HDMI 0, HDMI 1, CAMERA, AV
  5. L298N DC Motor Driver

    • Description: A motor driver used to control the DC motor.
    • 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
  6. DC Motor

    • Description: A motor used for rotating a blade to cut weeds.
    • Pins: pin 1, pin 2
  7. Battery 12V

    • Description: A 12V battery used to power the motor driver.
    • Pins: +, -

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

OV7725

Raspberry Pi 4B

L298N DC Motor Driver

DC Motor

Battery 12V

Documented Code

Arduino UNO Code

/*
 * Robotic Weeding Machine
 * This Arduino sketch controls a robotic weeding machine that uses a DC motor
 * to rotate a blade for cutting weeds. The machine uses an ultrasonic sensor
 * to detect obstacles and avoid collisions. The motor is controlled via an
 * L298N motor driver.
 */

// Pin definitions
const int trigPin = 9; // Trigger pin for HC-SR04
const int echoPin = 8; // Echo pin for HC-SR04
const int motorIn1 = 5; // IN1 pin for L298N
const int motorIn2 = 6; // IN2 pin for L298N

// Variables for ultrasonic sensor
long duration;
int distance;

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

  // Set pin modes
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
}

void loop() {
  // Measure distance using ultrasonic sensor
  distance = measureDistance();

  // Print distance to serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Control motor based on distance
  if (distance > 20) {
    // If no obstacle, run motor
    runMotor();
  } else {
    // If obstacle detected, stop motor
    stopMotor();
  }

  // Small delay before next loop
  delay(100);
}

// Function to measure distance using HC-SR04
int measureDistance() {
  // Clear the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Set the trigPin HIGH for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance