Target Robot Tutorial

by cooperrwasinger in Circuits > Arduino

62 Views, 1 Favorites, 0 Comments

Target Robot Tutorial

Robot.png

This is a tutorial for a robot that used Arduino. It rolls around and avoids hitting walls. It also has a target on top of it that ping pong balls can be thrown at.

Supplies

Screenshot 2024-04-16 093349.png
  1. Arduino IDE 
  2. Arduino board
  3. Breadboard
  4. 2 DC motors
  5. Driver board
  6. 4 Wheels
  7. 2 Ultrasonic distance sensors
  8. Battery and battery holder
  9. Switch
  10. Wires
  11. Cardboard
  12. Hot glue
  13. Duct Tape
  14. Soldering Iron
  15. Solder
  16. Arduino and breadboard holder(not required)

Declaring Pins and Variables

Screenshot 2024-04-16 093412.png
  1. Create a pin for the switch
  2. Create a variable to contain switch value
  3. Create first direction pin
  4. Create second direction pin
  5. Create PWNA pin for motor(must connect to a PWM pin)
  6. Create trig pin for ultrasonic sensor(call it “trigPin”)
  7. Create echo pin for ultrasonic sensor(call it “echoPin”)
  8. Create trig pin for second ultrasonic sensor(call it “trigPin2”)
  9. Create echopin for second ultrasonic sensor(call it “echoPin2”)
  10. Create a float variable named distance
  11. Create a float variable named distance2


Code Setup

Screenshot 2024-04-16 093435.png
  1. Declare every pin as output besides “a” and “b”. They are inputs
  2. The switch pin
  3. The echo pins for both ultrasonic sensors
  4. Begin serial monitor(not required but helps find errors)


Functions

Copy both of the functions that appear on the following 2 steps. These are for each of the ultrasonic sensor. The first function corresponds to the first ultrasonic sensor and the second function correspond to the second ultrasonic sensor. Make sure the variable and pin names match the ones that were specified in the “Declaring Pins and Variables” slide. Paste them after the loop function.

GetDistance Function

float getDistance()

{

float echoTime; 

float calculatedDistance; 

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

echoTime = pulseIn(echoPin, HIGH); 

calculatedDistance = echoTime / 148.0; 

return calculatedDistance; 

}



GetDistance2 Function

float getDistance2()

{

float echoTime2; 

float calculatedDistance2; 

digitalWrite(trigPin2, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin2, LOW);

echoTime2 = pulseIn(echoPin2, HIGH); 

calculatedDistance2 = echoTime2 / 148.0; 

return calculatedDistance2; 

}



Looping Code Pt1

Screenshot 2024-04-16 093502.png
  1. Have distance get its value from getDistance function
  2. Have distance2 get its value from getDistance2 function
  3. Have the switch variable get its value from reading the switchpin


Looping Code Pt2

Screenshot 2024-04-16 094037.png
  1. Create an if statement that happens if the switch variable is 1
  2. Inside that if statement, create an inner if statement that happens if distance2 is less then 4
  3. In the inner if statement, make the motor spin counterclockwise at 150 speed
  4. End the inner if statement


Looping Code Pt3

Screenshot 2024-04-16 094052.png
  1. Create a second inner if statement that happens if distance is less than 4
  2. In the second inner if statement, make the motor spin clockwise at 150 speed
  3. End the second inner if statement
  4. End the switch variable if statement


Looping Code Pt4

Screenshot 2024-04-16 094110.png
  1. Create an if statement that happens if the switch variable is 0
  2. Inside that if statement, turn both directions to LOW and the speed to 0
  3. End that if statement
  4. Serial print distance, distance2, and the switch variable on their own line(not required but helps find errors)
  5. Delay a second


Wiring Pt1, Arduino and Switch

  1. A finished wiring diagram is found at the end of the wiring section of the tutorial
  2. Wire 5V and ground from the Arduino to the breadboard
  3. Wire 1 side of the switch to ground and the other side of the switch to 5V. The switch will be on when it is on the 5V side and off when it is on the ground side
  4. The middle pin of the switch should be wired to whichever the switch pin is declared to in “Declaring Pins and Variables”


Wiring Pt2, Distance Sensors

  1. Wire the VCC pin of each distance sensor to 5V
  2. Wire the GND pin of each distance sensor to ground
  3. Wire trigPin, echoPin, trigPin2, and echoPin2 to whichever pin is declared to each in “Declaring Pins and Variables”
  4. Keep track of which trig pin and echo pin each distance sensor is connected to. The first distance sensor should only be wired to the first trig pin and first echo pin while the second distance sensor should only be wired to the second trig pin and second echo pin. Which distance sensor is which will matter during assembly


Wiring Pt3, Motors and Motor Driver

Connect the following pins on the motor driver to their assigned positions

  1. Connect Vm to VIN on Arduino
  2. Connect Vcc and STBY to 5V
  3. Connect GND to ground
  4. Connect PWNA to PWNA pin specified in “Declaring Pins and Variables”
  5. Connect AIN1 to first direction pin
  6. Connect AIN2 to second direction pin
  7. The red wire on the motors will be connected to A01

  8. The black wire on the motors will be connected to A02


Wiring Picture

Screenshot 2024-04-16 094140.png

The motor driver in the picture is different then the motor driver used in this robot. Follow the wiring the was specified in the last slide for the motor driver. The distance sensors will not fit as shown in the image. They will not be on the breadboard during assembly.

Construction Pt1

Screenshot 2024-04-16 094157.png
  1. Connect wheels to each side of both of the motors
  2. Cut out 2 12 inch by 5 inch pieces of cardboard
  3. Cut 2 holes in both of the cardboard, one on each side. The top of the motor and the wires should be able to fit through it, not the whole motor. 
  4. Stack the 2 pieces of cardboard and use hot glue to glue them together. This is the bottom plate


Construction Pt2

Screenshot 2024-04-16 094211.png
  1. Cut out 2 5 inch by 4 inch pieces of cardboard
  2. Cut 2 holes in the middle of both of the cardboard. The circular part of the ultrasonic sensors should fit in the holes. These are the small plates


Construction Pt3

Screenshot 2024-04-16 094224.png
  1. Cut out a 3rd piece of cardboard the same size as the cardboard cut out in”Construction Pt1”
  2. Cut a small hole somewhere in the cardboard. The pins of the switch should be able to fit through it. This is the top plate


Construction Pt4

Screenshot 2024-04-16 094240.png
  1. Cut out 2 more pieces of cardboard that are a little more than 12 inches by 4 inches. These are the long plates
  2. Glue the small plates and long plates to the bottom plates. It should look like a box with the top off


Construction Pt5

Screenshot 2024-04-16 094254.png

Complete the wiring explained in the “Wiring” portion of the tutorial

Construction Pt6

Screenshot 2024-04-16 094313.png
  1. Glue the breadboard and Arduino in between the 2 holes in the bottom plate. Make sure they both touch the same long plate. Also make sure the Arduino’s connector the the battery holder faces toward the other plate
  2. Put the batteries in the battery holder
  3. Put the battery holder in between the 2 holes in the bottom plate. Make sure they touch the other plate that the Arduino and breadboard are not touching. Also make sure the connector of the battery holder can connect to the Arduino.


Construction Pt7

Screenshot 2024-04-16 094327.png

Solder a wire to each of the pins on both of the distance sensors. One of the distance sensors will need another 4 wires soldered onto each of the 4 wires already on it

Construction Pt8

Screenshot 2024-04-16 094341.png
  1. Place 1 distance sensor into each of the 2 holes in the small plate. Make sure the distance sensor that does not have extra wires soldered to it is closer to the breadboard
  2. Place 1 motor into each of the holes in the bottom plate. Make sure the wires can reach the breadboard
  3. Connect the distance sensors and the motors to the breadboard.


Construction Pt9

Screenshot 2024-04-16 094400.png
  1. Place the switch in the hole in the top plate with the pins facing towards the breadboard
  2. Solder a wire onto each of the pins on the switch
  3. Wire the switch to the breadboard. 
  4. Use duct tape to attach the top plate to the rest of the robot
  5. Create a target and use hot glue to attach it to the top of the robot. The look of the target does not matter

Completed Code

Completed code for the project

Downloads

Constuction Video

Constructing Robot Video

A video showing the construction of the project