|
|
Arduino Smart Car - Obstacle Avoidance Robot
PARTS LIST:
- Arduino Uno R3 x1
- L298N Motor Driver x1
- DC Gear Motors x2, Wheels x2, Caster Wheel x1
- Car Chassis x1, Battery Holder (4x AA)
- HC-SR04 Ultrasonic Sensor x1
- Jumper Wires, Breadboard
Total cost: about $15-20
WIRING:
Motor Driver L298N:
IN1 - Arduino D5, IN2 - Arduino D6
IN3 - Arduino D9, IN4 - Arduino D10
ENA - Arduino D3 (PWM, left speed)
ENB - Arduino D11 (PWM, right speed)
Motor A - Left Motor, Motor B - Right Motor
12V - Battery+, GND - Battery- and Arduino GND
Ultrasonic HC-SR04:
VCC - Arduino 5V, GND - Arduino GND
TRIG - Arduino A0, ECHO - Arduino A1
CODE - Basic Movement:
#define ENA 3
#define IN1 5
#define IN2 6
#define ENB 11
#define IN3 9
#define IN4 10
void setup() {
pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT); pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
}
void forward() {
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
analogWrite(ENA, 200); analogWrite(ENB, 200);
}
void backward() {
digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
analogWrite(ENA, 200); analogWrite(ENB, 200);
}
void left() {
digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}
void right() {
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
}
void stopCar() {
digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);
}
void loop() {
forward(); delay(2000);
stopCar(); delay(500);
right(); delay(1000);
stopCar(); delay(500);
}
CODE - Obstacle Avoidance:
#define TRIG A0
#define ECHO A1
long getDistance() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long duration = pulseIn(ECHO, HIGH);
return duration * 0.034 / 2;
}
void loop() {
long dist = getDistance();
if (dist < 20) {
stopCar(); delay(300);
backward(); delay(500);
right(); delay(600);
} else {
forward();
}
delay(50);
}
UPGRADE IDEAS:
1. Bluetooth (HC-05) for phone control
2. Line tracking sensors (TCRT5000)
3. OLED display for status
4. WiFi (ESP8266) remote control
5. Camera module for FPV
BUILD TIPS:
- Test each motor before assembly
- Use cable ties for wiring
- Keep center of gravity low
- Calibrate speeds with PWM
Have fun building! |
|