Obstacle Avoider Robot Using Relay Board
by robotuck_98 in Circuits > Robots
3394 Views, 17 Favorites, 0 Comments
Obstacle Avoider Robot Using Relay Board
All right, so i decided to make a robot with a relay board. I have never used a relay board in my life and this was the first time i was working with it. First i thought it would be really complicated but it turned out not to be. I used fairly simple tools and if you follow this, i'm sure that you'll be able to make this robot in less then an hour.
tools
1 relay board
2 arduino uno
3 two dc motors
4 jumper cable
5 hc-sro4 sensor
http://www.amazon.com/Development-Microcontroller-MEGA328P-ATMEGA16U2-Arduino/dp/B00G46X31W/ref=sr_1_8?s=electronics&ie=UTF8&qid=1391729459&sr=1-8&keywords=arduino+uno
http://www.amazon.com/SunFounder-Channel-Shield-Module-Arduino/dp/B00E0NSORY/ref=sr_1_14?s=electronics&ie=UTF8&qid=1391729511&sr=1-14&keywords=relay
tools
1 relay board
2 arduino uno
3 two dc motors
4 jumper cable
5 hc-sro4 sensor
http://www.amazon.com/Development-Microcontroller-MEGA328P-ATMEGA16U2-Arduino/dp/B00G46X31W/ref=sr_1_8?s=electronics&ie=UTF8&qid=1391729459&sr=1-8&keywords=arduino+uno
http://www.amazon.com/SunFounder-Channel-Shield-Module-Arduino/dp/B00E0NSORY/ref=sr_1_14?s=electronics&ie=UTF8&qid=1391729511&sr=1-14&keywords=relay
First, you use a box or something that you could use as the body of the robot. In this case, i have used a simple white box to make it appealing. It could be any box, but remember it should be able to carry the weight of the materials.
Then, fix the motors in a proper place, you could also put a clamp to hold the motor as i did, or you could use something else to support the motor and gears.
After fixing the motors, i placed the relay board and the arduino uno board on top of the robot, but you may place them in a location of your choice.
Then, fix the motors in a proper place, you could also put a clamp to hold the motor as i did, or you could use something else to support the motor and gears.
After fixing the motors, i placed the relay board and the arduino uno board on top of the robot, but you may place them in a location of your choice.
After placing the arduino uno board, i placed the hc-sro4 sensor.
Once you have everything in the place of your choice, you need to start connecting the wires.
First, connect the arduino uno board to the relay board with wires.
Gnd goes to the gnd and vcc goes to the vcc pin of the arduino. Pin2 of the relay goes to 7 and pin4 of the relay goes to pin9.
For hc-sro4 sensor, gnd pin goes to gnd, vcc pin goes to vcc, eco pin goes to pin2, and trig pin goes to pin3.
Now you can connect the battery wire and motor wires to the relay board, and also use the 9v battery power to power the arduino.
These are the codes below for the arduino board:
#include <Ultrasonic.h>
const int numOfReadings = 10; // number of readings to take/ items in the array
int readings[numOfReadings]; // stores the distance readings in an array
int arrayIndex = 0; // arrayIndex of the current item in the array
int total = 0; // stores the cumlative total
int averageDistance = 0; // stores the average value
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm)
int motor1=9;
int motor2=7;
int eco=2;
int trig=3;
int dist=0;
void setup()
{
pinMode (motor1, OUTPUT);
pinMode (motor2, OUTPUT);
pinMode (eco, INPUT);
pinMode (trig, OUTPUT);
float dist = pulseIn(eco, HIGH);
}
void loop() {
digitalWrite(trig, HIGH); // send 10 microsecond pulse
delayMicroseconds(10); // wait 10 microseconds before turning off
digitalWrite(trig, LOW); // stop sending the pulse
pulseTime = pulseIn(eco, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.
total= total - readings[arrayIndex]; // subtract the last distance
readings[arrayIndex] = distance; // add distance reading to array
total= total + readings[arrayIndex]; // add the reading to the total
arrayIndex = arrayIndex + 1; // go to the next item in the array
// At the end of the array (10 items) then start again
if (arrayIndex >= numOfReadings) {
arrayIndex = 0;
}
averageDistance = total / numOfReadings; // calculate the average distance
delay(10);
digitalWrite(motor1, HIGH);
digitalWrite(motor2, HIGH);
if (averageDistance <= 10){
// go backwards
digitalWrite(motor1,LOW);
digitalWrite(motor2,LOW);
digitalWrite(motor1,HIGH);
digitalWrite(motor2,LOW);
}
else
digitalWrite(motor1,LOW);
digitalWrite(motor2,LOW);
}
Once you have everything in the place of your choice, you need to start connecting the wires.
First, connect the arduino uno board to the relay board with wires.
Gnd goes to the gnd and vcc goes to the vcc pin of the arduino. Pin2 of the relay goes to 7 and pin4 of the relay goes to pin9.
For hc-sro4 sensor, gnd pin goes to gnd, vcc pin goes to vcc, eco pin goes to pin2, and trig pin goes to pin3.
Now you can connect the battery wire and motor wires to the relay board, and also use the 9v battery power to power the arduino.
These are the codes below for the arduino board:
#include <Ultrasonic.h>
const int numOfReadings = 10; // number of readings to take/ items in the array
int readings[numOfReadings]; // stores the distance readings in an array
int arrayIndex = 0; // arrayIndex of the current item in the array
int total = 0; // stores the cumlative total
int averageDistance = 0; // stores the average value
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm)
int motor1=9;
int motor2=7;
int eco=2;
int trig=3;
int dist=0;
void setup()
{
pinMode (motor1, OUTPUT);
pinMode (motor2, OUTPUT);
pinMode (eco, INPUT);
pinMode (trig, OUTPUT);
float dist = pulseIn(eco, HIGH);
}
void loop() {
digitalWrite(trig, HIGH); // send 10 microsecond pulse
delayMicroseconds(10); // wait 10 microseconds before turning off
digitalWrite(trig, LOW); // stop sending the pulse
pulseTime = pulseIn(eco, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.
total= total - readings[arrayIndex]; // subtract the last distance
readings[arrayIndex] = distance; // add distance reading to array
total= total + readings[arrayIndex]; // add the reading to the total
arrayIndex = arrayIndex + 1; // go to the next item in the array
// At the end of the array (10 items) then start again
if (arrayIndex >= numOfReadings) {
arrayIndex = 0;
}
averageDistance = total / numOfReadings; // calculate the average distance
delay(10);
digitalWrite(motor1, HIGH);
digitalWrite(motor2, HIGH);
if (averageDistance <= 10){
// go backwards
digitalWrite(motor1,LOW);
digitalWrite(motor2,LOW);
digitalWrite(motor1,HIGH);
digitalWrite(motor2,LOW);
}
else
digitalWrite(motor1,LOW);
digitalWrite(motor2,LOW);
}