Advertisement

Responsive Advertisement

Arduino Robot Bug: A Fun and Easy DIY Project

Have you ever wanted to make your own robot that can avoid obstacles and explore its surroundings? If so, this project is for you! In this article, we will show you how to make an Arduino robot bug using simple and affordable components. This robot bug is a four-wheeled vehicle that uses two servo motors to control its movement and an ultrasonic sensor to detect obstacles. It also has a 3D printed shell that gives it a cute and bug-like appearance. You can customize the shell with different colors and shapes to make your own unique robot bug.

What You Will Need

To make this Arduino robot bug, you will need the following components:

  • An Arduino Nano board: This is the brain of the robot bug. It controls the servo motors and the ultrasonic sensor. You can use any other Arduino board, but the Nano is small and convenient for this project.
  • An HC-SR04 ultrasonic sensor: This is the eyes of the robot bug. It emits ultrasonic waves and measures the time it takes for them to bounce back from an object. This way, it can calculate the distance to the object and avoid collisions.
  • Two SG90 micro servo motors: These are the legs of the robot bug. They rotate the wheels and allow the robot to move forward, backward, left, and right.
  • A 9V battery or any battery ranging from 7V to 12V: This is the power source of the robot bug. It provides the necessary voltage and current for the Arduino board and the servo motors.
  • A breadboard and some jumper wires: These are the tools for connecting the components. They make the wiring easy and neat.
  • A 3D printer and some filament: These are the tools for making the shell of the robot bug. You can use any 3D printer and any filament that suits your preference. We will provide the STL files for the shell in the next section.
  • Some zip ties, screws, nuts, and glue: These are the tools for assembling the robot bug. They help to secure the components and the shell together.

How to Make the Arduino Robot Bug

To make the Arduino robot bug, you will need to follow these steps:

  1. Download and print the STL files for the shell of the robot bug. You can find them here. There are two parts: the bottom part that holds the battery and the breadboard, and the top part that holds the ultrasonic sensor. You can print them with any color and infill that you like. Make sure to print them with enough support and resolution for a good quality.
  2. Connect the components according to the circuit diagram below. You can use the breadboard and the jumper wires to make the connections. Make sure to follow the polarity and the pin numbers correctly. The circuit diagram is as follows:

Circuit diagram

The pin connections are as follows:

  • Arduino Nano pin D7 -> Ultrasonic sensor trigger pin
  • Arduino Nano pin D8 -> Ultrasonic sensor echo pin
  • Arduino Nano pin D5 -> Servo motor 1 signal pin
  • Arduino Nano pin D6 -> Servo motor 2 signal pin
  • Arduino Nano 5V pin -> Servo motors and ultrasonic sensor VCC pins
  • Arduino Nano GND pin -> Servo motors and ultrasonic sensor GND pins
  • Arduino Nano VIN pin -> Battery positive terminal
  • Arduino Nano GND pin -> Battery negative terminal

  • Upload the code to the Arduino board. You can use the Arduino IDE or any other software that supports Arduino programming. The code is as follows:

// Include the servo library
#include <Servo.h>

// Define the pins for the ultrasonic sensor
#define TRIG_PIN 7
#define ECHO_PIN 8

// Define the pins for the servo motors
#define SERVO1_PIN 5
#define SERVO2_PIN 6

// Create servo objects
Servo servo1;
Servo servo2;

// Define the speed and direction of the servo motors
#define FORWARD 90
#define BACKWARD 0
#define STOP 180
#define LEFT 45
#define RIGHT 135

// Define the threshold distance for obstacle detection
#define DISTANCE_THRESHOLD 15

// Define a variable to store the distance
int distance = 0;

// Define a variable to store the duration
long duration = 0;

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

  // Initialize the ultrasonic sensor pins
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  // Attach the servo motors to the pins
  servo1.attach(SERVO1_PIN);
  servo2.attach(SERVO2_PIN);

  // Set the initial speed and direction of the servo motors
  servo1.write(FORWARD);
  servo2.write(FORWARD);
}

void loop() {
  // Measure the distance to the obstacle
  distance = measureDistance();

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

  // Check if the distance is less than the threshold
  if (distance < DISTANCE_THRESHOLD) {
    // Stop the servo motors
    servo1.write(STOP);
    servo2.write(STOP);

    // Wait for a second
    delay(1000);

    // Move backward for a second
    servo1.write(BACKWARD);
    servo2.write(BACKWARD);
    delay(1000);

    // Stop the servo motors
    servo1.write(STOP);
    servo2.write(STOP);

    // Wait for a second
    delay(1000);

    // Turn left or right randomly
    if (random(2) == 0) {
      servo1.write(LEFT);
      servo2.write(LEFT);
    } else {
      servo1.write(RIGHT);
      servo2.write(RIGHT);
    }

    // Wait for a second
    delay(1000);

    // Move forward
    servo1.write(FORWARD);
    servo2.write(FORWARD);
  }
}

// A function to measure the distance to the obstacle
int measureDistance() {
  // Send a 10 microsecond pulse to the trigger pin
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Read the echo pin and calculate the duration
  duration = pulseIn(ECHO_PIN, HIGH);

  // Calculate the distance in centimeters
  distance = duration * 0.034 / 2;

  // Return the distance
  return distance;
}
  1. Assemble the robot bug. You can use the zip ties, screws, nuts, and glue to attach the components and the shell together. The assembly steps are as follows:

  2. Attach the servo motors to the bottom part of the shell using zip ties. Make sure to align the wheels with the holes on the shell.

  3. Attach the battery and the breadboard to the bottom part of the shell using glue or tape. Make sure to leave some space for the wires and the Arduino board.
  4. Attach the ultrasonic sensor to the top part of the shell using glue or screws. Make sure to align the sensor with the hole on the shell.
  5. Attach the Arduino board to the top part of the shell using glue or tape. Make sure to leave some space for the wires and the USB port.
  6. Connect the top and the bottom parts of the shell using screws and nuts. Make sure to align the holes on the shell.

  7. Test the robot bug. You can use the USB cable to connect the Arduino board to your computer and monitor the serial output. You can also use a battery clip to connect the battery to the Arduino board and power the robot bug. You can place the robot bug on a flat surface and watch it move and avoid obstacles. You can also adjust the code and the shell to change the behavior and the appearance of the robot bug.

Congratulations! You have successfully made your own Arduino robot bug. Have fun and enjoy your creation!

(1) Otto: The Ultrasonic-Servo-Robot | Arduino Project Hub. https://projecthub.arduino.cc/fix-22/otto-the-ultrasonic-servo-robot-f6da14. (2) Obstacle avoiding Robot using Arduino and Ultrasonic Sensor (Type 2 .... https://electronicsprojects.in/obstacle-avoiding-robot-using-arduino-and-ultrasonic-sensor-type-2/. (3) Arduino Nano bot - using ultrasonic sensor - element14 Community. https://community.element14.com/challenges-projects/project14/nano-rama/b/blog/posts/arduino-nano-bot---using-ultrasonic-sensor. (4) A DIY Quadruped Robot With Arduino, 3D Printed, and Lego-compatible Parts. https://www.instructables.com/A-DIY-Quadruped-Robot-With-Arduino-3D-Printed-and-/. (5) Obstacle Avoiding Robot Using Arduino Nano - Instructables. https://www.instructables.com/Obstacle-Avoiding-Robot-Using-Arduino-Nano/. (6) undefined. https://github.com/Fix-22/Otto.

Post a Comment

0 Comments