Arduino Based Robotic Weeding Machine with Ultrasonic Obstacle Detection and L298N Motor Driver

Arduino Based Robotic Weeding Machine | Ultrasonic Obstacle Detection & L298N Motor Driver
🌿 Smart Agriculture · Arduino · Robotics

Arduino Based
Robotic Weeding Machine

A semi-autonomous weed-cutting robot combining ultrasonic obstacle avoidance, L298N motor control, and scalable AI integration for modern precision farming.

30-min build Beginner–Intermediate Obstacle Avoidance

01 / Project Overview

The Arduino Based Robotic Weeding Machine is an intelligent agricultural automation system designed to remove unwanted weeds efficiently while avoiding obstacles in the field. It promotes eco-friendly weed management without excessive chemical herbicides.

This smart farming project combines an Arduino UNO, HC-SR04 ultrasonic sensor, L298N motor driver, and DC motors to create a capable robot that navigates farmland with minimal human intervention.

The system uses a high-speed DC motor attached to a rotating blade mechanism to cut weeds at ground level. An ultrasonic sensor mounted at the front continuously measures distance to nearby objects. If an obstacle is detected within a predefined range, the Arduino automatically stops or redirects the robot to prevent collision and damage.

A Raspberry Pi board can be optionally integrated for advanced processing, monitoring, or future AI-based weed detection, making the design scalable for smart agriculture.

02 / Working Principle

  1. The ultrasonic sensor continuously emits sound waves and measures the echo return time to determine obstacle distance.
  2. The Arduino UNO calculates the distance to obstacles in real time using the speed of sound formula.
  3. If the path is clear (distance > 20 cm), the robot moves forward while the blade motor rotates to cut weeds.
  4. When an obstacle is detected within the 20 cm threshold, the robot stops immediately and can reverse or change direction based on programmed logic.
  5. The L298N motor driver manages both wheel motors and blade motor operation safely with H-Bridge current control.

03 / Key Components

🧠
Arduino UNO
Microcontroller — ATmega328P
📡
HC-SR04
Ultrasonic Sensor — 2–400 cm range
⚙️
L298N Module
Motor Driver — H-Bridge, 2A/ch
🔄
DC Motors (×2)
Drive wheels — left & right
Blade Motor
High-speed DC — weed cutting
🔋
12V Battery Pack
Rechargeable Li-ion / SLA
🖥️
Raspberry Pi
Optional — AI vision expansion

04 / Circuit Diagram

The interactive circuit diagram below shows all wiring connections. Scroll horizontally on mobile.

TRIG D9 · ECHO D8 IN1(D5) · IN2(D6) OUT1/OUT2 OUT3/OUT4 12V Power 5V ARDUINO UNO D5·D6·D8·D9·5V·GND ATmega328P @ 16MHz HC-SR04 VCC·TRIG·ECHO·GND L298N Motor Driver Module IN1·IN2 → OUT1·OUT2·OUT3·OUT4 BLADE MOTOR High-Speed DC DRIVE MOTORS ×2 Wheel DC Motors 12V BATTERY PACK Rechargeable Li-ion / SLA + + ⚠ Obstacle Threshold Distance > 20 cm → Motor ON Distance ≤ 20 cm → Motor STOP PWM Speed Control Pins D5 & D6 support PWM analogWrite() for speed ctrl Arduino Robotic Weeding Machine — Circuit Diagram HC-SR04 · Arduino UNO · L298N Motor Driver · DC Motors · 12V Battery
Sensor Signals
Motor Control
Blade Motor
Drive Motors
Power Lines

📋 Wiring Notes

  • HC-SR04 VCC → Arduino 5V; GND → common GND
  • L298N 12V → Battery+; GND → Battery–; 5V output can power Arduino
  • Pins D5 & D6 are hardware PWM — use analogWrite() for variable blade speed
  • Add 0.1 µF decoupling capacitors across motor terminals to reduce noise
  • Optional: Raspberry Pi UART (TX→RX) to Arduino for AI commands

Rendered from JSON — Components

Rendered from JSON — Connections

FromToLabelWire Colour

06 / Arduino Code

Upload the sketch below via Arduino IDE. The robot will automatically begin navigating and cutting weeds, stopping whenever an obstacle enters the 20 cm safety zone.

RoboticWeedingMachine.ino
/*
 * Robotic Weeding Machine
 * Controls a DC blade motor and drive motors via L298N.
 * HC-SR04 ultrasonic sensor provides obstacle detection.
 * Threshold: 20 cm — stops blade & drive motors on obstacle.
 */

// ── Pin Definitions ──────────────────────────────────────
const int trigPin  = 9;  // HC-SR04 Trigger pin
const int echoPin  = 8;  // HC-SR04 Echo pin
const int motorIn1 = 5;  // L298N IN1 (PWM capable)
const int motorIn2 = 6;  // L298N IN2 (PWM capable)

// ── Variables ────────────────────────────────────────────
long duration;
int  distance;

// ── Setup ────────────────────────────────────────────────
void setup() {
  Serial.begin(9600);

  pinMode(trigPin,  OUTPUT);
  pinMode(echoPin,  INPUT);
  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
}

// ── Main Loop ────────────────────────────────────────────
void loop() {
  distance = measureDistance();

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  if (distance > 20) {
    runMotor();    // Path clear — run blade
  } else {
    stopMotor();   // Obstacle detected — stop
  }

  delay(100);      // 10 Hz sensing loop
}

// ── Measure Distance (HC-SR04) ───────────────────────────
int measureDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

  return distance;
}

// ── Run Blade Motor (forward) ────────────────────────────
void runMotor() {
  digitalWrite(motorIn1, HIGH);
  digitalWrite(motorIn2, LOW);
}

// ── Stop Blade Motor ─────────────────────────────────────
void stopMotor() {
  digitalWrite(motorIn1, LOW);
  digitalWrite(motorIn2, LOW);
}

💡 Code Notes

  • Obstacle threshold: Change 20 on line if (distance > 20) to adjust sensitivity.
  • PWM speed: Replace digitalWrite(motorIn1, HIGH) with analogWrite(motorIn1, 180) (0–255) for variable speed.
  • Sensing rate: delay(100) gives 10 Hz — reduce to 50 for faster response.
  • Drive motors: Add separate IN3/IN4 pins and duplicate runMotor() logic for wheel control.

07 / Technical Features

📡

Real-Time Distance Sensing

HC-SR04 measures obstacles at 10 Hz with 2–400 cm range and ~3 mm accuracy.

🔀

Obstacle Avoidance

Programmable 20 cm threshold triggers instant motor stop and redirection logic.

PWM Speed Control

D5 and D6 are hardware PWM pins enabling precise blade and drive speed management.

🔧

H-Bridge Motor Control

L298N H-Bridge supports bidirectional control at up to 2A per channel, 12V supply.

🔋

Battery-Powered Mobile

12V rechargeable pack enables untethered field operation for extended sessions.

🤖

Scalable AI Integration

Optional Raspberry Pi UART bridge enables camera-based AI weed detection upgrades.

08 / Applications

Smart agriculture and precision farming
Automated weed removal systems for small–medium farms
Agricultural robotics research and prototyping
Engineering final-year capstone projects
Sustainable farming technology demonstrations
Reducing chemical herbicide dependence in organic farming

This robotic weeding machine reduces manual labour, improves efficiency, and serves as a practical example of combining embedded systems, robotics, and agricultural automation into a single innovative solution.