"Try N' Block Me" Toy Car
by srinivasadityavarmau in Circuits > Arduino
376 Views, 8 Favorites, 0 Comments
"Try N' Block Me" Toy Car
Hi, readers!
Hope all of you are well. Stay home, stay safe.
This 'Obstacle Avoiding Car', as the name suggests, avoids obstacles.
For everyone who loves robotics, but has no clue what it is, this project is the best to start off with.You just have to know the basics of programming, if you want to tinker around with the final product...But if you just want a tiny car roaming around your room, and scaring your cat, you just have to follow the instructions.
The car uses an Ultrasonic Sensor to measure distance , a Motor Driver to control the direction and speed of the motors, and the Arduino Uno as it's brain.
Whenever it detects an obstacle--that is when a distance below than 15 cm is detected, it takes a U-turn.
I designed a chassis myself for this car, to make the car more efficient. You will also see how to make it in this instructable.
Once the project is completed , you can keep improvising it with other movements, modules and sensors.
It is an educational project, you can play around with, while you also get a tight grip on programming !
Have fun learning!
Supplies
1.l293d Motor driver module( a motor driver shield is recommended when using Uno)
https://www.amazon.in/Driver-Module-Bridge-Stepper...
2. Gear motors with wheels
https://www.amazon.in/ERH-India-Electric-Magnetic-...
3.Arduino Uno
https://www.amazon.in/Robotly-ATmega328P-compatibl...
4. 2 Lithium ion batteries (22000 mAh)
https://www.roboelements.com/product/irc18650-3-7v...
5. A lithium ion battery charger
https://www.amazon.in/THINK3-Universal-Charger-Rec...
6. Ultrasonic Sensor
https://www.amazon.in/REES52-Ultrasonic-Distance-M...
7. Switch
https://www.amazon.in/rocker-switch2A-Terminal-Swi...
8. Battery holder
https://www.amazon.in/SunRobotics-Battery-Holder-L...
9.Jumper wires
https://www.amazon.in/Electrobot-Jumper-Wires-120-...
10. Double sided tape
https://www.amazon.in/ETI-Double-Side-Foam-24mmX5M...
All of these products can be found in 'Roboelements' with low prices or in 'Amazon'. All the links given (except for the lithium ion batteries, as amazon doesn't have it in stock) are of amazon products.
Making the Chassis
For this chasis, I used a circular cardboard.
Use the given picture to get the brief idea of how my chassis is.
The diameter of my chassis is 24.8 cm.
Cut out the shaded portion from the circle. This space is perfect for your wheels to fit in. It is very important for the wheels to be within the body. If the wheels are out of the body, then some object might end up between the wheel and the body, making it very difficult for the car to move any further.
Be precise with the measurements while cutting out the shaded portion.
With this design the wheels would be parallel to each other, and are on the diameter of the body. This way, the car can turn left or right, while staying on the same spot.
The Circuit
The circuit involves the Arduino Uno which receives the information from the Ultrasonic sensor and sends information to the motor driver to control the motor.
Ultrasonic Sensor to Arduino Uno connections:
1. gnd of sensor to GND of Uno
2.Echo of sensor to pin A0 of Uno
3. Trig of sensor to pin A1 of Uno
4.Vcc of sensor to 5V pin of Uno
Motor Driver to Arduino Uno connections:
a) power pins:
1. Gnd of driver to GND of Uno
2. 5V of sensor to VIN pin of Uno
b) control pins:
1. ENA of driver to Digital pin 9 of UNO
2. IN1 of driver to Digital pin 8 of UNO
3. IN2 of driver to Digital pin 7 of UNO
4. IN3 of driver to Digital pin 5 of UNO
5. IN4 of driver to Digital pin 4 of UNO
6. ENB of driver to Digital pin 3 of UNO
Battery to Motor Driver connections:
1. gnd of battery to gnd of driver
2. 7.4V of battery to 12+ of driver(through a switch)
Motors to Motor Driver connections (keeping the control pins towards you):
1. MotorA to Left socket
2.MotorB to Right socket
The Whole Body
I stuck my parts to the board using the double sided tape.
You can arrange the parts and keep them intact with any way you are comfortable with. You can use a glue gun, screws etc.
The car is ready! Now upload the code...
Code
Here is my code:
//Motor A connections
int enA = 9; int L_Forwards = 8;
int L_Backwards = 7;
// Motor B connections
int enB = 3;
int R_Forwards = 5;
int R_Backwards = 4;
//us sensor
int trigpin = A1;
int echopin = A0;
long duration;
long distance;
//Speed controls
int MxSpeed = 155;
int MnSpeed = 90;
//Returns distance from the Ultrasonic sensor to the board
long getDistance() {
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = duration * 0.034 / 2; return distance;
}
void setup() {
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(L_Forwards, OUTPUT);
pinMode(L_Backwards, OUTPUT);
pinMode(R_Forwards, OUTPUT);
pinMode(R_Backwards, OUTPUT);
//Setup/Initialize Ultrasonic sensor
pinMode(trigpin, OUTPUT);
// Sets the trigPin as an OUTPUT
pinMode(echopin, INPUT);
// Sets the echoPin as an INPUT
Serial.begin(9600);
// Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test");
// print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
}
void loop() {
distance = getDistance();
Serial.print("Distance: ");
Serial.print(distance); Serial.println(" cm");
if (distance <= 15) {
Stop();
turnLeft();
Break();
MoveAbit();
Break();
turnLeft();
Break();
moveForwards(); }
else {
moveForwards();
}
}
//Car controls:
void moveForwards() {
//Pick up speed
for (int i = MnSpeed; i <= MxSpeed; i += 10) {
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
//Set direction of motors to move forwards
digitalWrite(L_Forwards, HIGH);
digitalWrite(L_Backwards, LOW);
digitalWrite(R_Forwards, HIGH);
digitalWrite(R_Backwards, LOW);
}
void moveBackwards() {
//Pick up speed slowly
for (int i = MnSpeed; i <= MxSpeed; ++i) {
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
//Set direction of the motors
digitalWrite(L_Forwards, LOW);
digitalWrite(L_Backwards, HIGH);
digitalWrite(R_Forwards, LOW);
digitalWrite(R_Backwards, HIGH);
}
void turnRight() {
analogWrite(enB, MxSpeed);
analogWrite(enA, MxSpeed);
digitalWrite(L_Forwards, HIGH);
digitalWrite(L_Backwards, LOW);
digitalWrite(R_Forwards, LOW);
digitalWrite(R_Backwards, HIGH);
delay(350);
}
void turnLeft() {
analogWrite(enB, MxSpeed);
analogWrite(enA, MxSpeed);
digitalWrite(L_Forwards, LOW);
digitalWrite(L_Backwards, HIGH);
digitalWrite(R_Forwards, HIGH);
digitalWrite(R_Backwards, LOW);
delay(350);
}
void Stop() {
//gradually comes to a halt
for (int i = MxSpeed; i >= MnSpeed; i -= 10) {
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
// Now turn off motors
digitalWrite(L_Forwards, LOW);
digitalWrite(L_Backwards, LOW);
digitalWrite(R_Forwards, LOW);
digitalWrite(R_Backwards, LOW);
delay(500);
}
void MoveAbit() {
analogWrite(enB, MxSpeed);
analogWrite(enA, MxSpeed);
//Set direction of motors to move forwards
digitalWrite(L_Forwards, HIGH);
digitalWrite(L_Backwards, LOW);
digitalWrite(R_Forwards, HIGH);
digitalWrite(R_Backwards, LOW);
delay(200);
}
void Break() {
//A sudden break
digitalWrite(L_Forwards, LOW);
digitalWrite(L_Backwards, LOW);
digitalWrite(R_Forwards, LOW);
digitalWrite(R_Backwards, LOW);
delay(500);
}
//once done with the car, you can play around
The Final Product
This is how the final product might work like...
You can play around with the code, by changing the directions and speeds of rotating wheels, until you are satisfied with it's appearance and movement.
Hope you will have fun playing with.