Arduino deneyi 13
Obstacle-Avoiding Robot
Intermediate · 90 min · Robot decisions
Combine a distance sensor and motor driver to make a simple robot avoid obstacles.
Malzemeler
- Arduino Uno
- HC-SR04
- L298N motor driver
- 2 DC gear motors and chassis
- Motor battery pack
- Jumper wires
Bağlantı
- Connect Trig to pin 7 and Echo to pin 6.
- Connect L298N IN1–IN4 to pins 4, 5, 8 and 9; keep ENA/ENB jumpers installed.
- Power motors from the battery through the driver. Join driver and Arduino grounds.
Kod
const int trigPin=7, echoPin=6;
const int in1=4, in2=5, in3=8, in4=9;
void drive(int a, int b, int c, int d) {
digitalWrite(in1,a); digitalWrite(in2,b); digitalWrite(in3,c); digitalWrite(in4,d);
}
float distanceCm() {
digitalWrite(trigPin,LOW); delayMicroseconds(2);
digitalWrite(trigPin,HIGH); delayMicroseconds(10); digitalWrite(trigPin,LOW);
return pulseIn(echoPin,HIGH,30000) * 0.0343 / 2.0;
}
void setup() {
pinMode(trigPin,OUTPUT); pinMode(echoPin,INPUT);
pinMode(in1,OUTPUT); pinMode(in2,OUTPUT); pinMode(in3,OUTPUT); pinMode(in4,OUTPUT);
}
void loop() {
float d=distanceCm();
if (d > 0 && d < 20) {
drive(LOW,HIGH,LOW,HIGH); delay(350);
drive(HIGH,LOW,LOW,HIGH); delay(450);
} else { drive(HIGH,LOW,HIGH,LOW); }
}
Ne olmalı?
The robot reverses and turns when it detects an object closer than 20 cm.
Meydan okuma
Test three thresholds and record which produces the most reliable movement.
Güvenlik ve sorun giderme
- Bağlantıyı değiştirmeden önce gücü kes.
- Her LED ile direnç kullan.
- Kart ve port seçimini Arduino IDE'de kontrol et.
- Her kablonun koddaki pin numarasıyla eşleştiğini doğrula.
Not: Ders metinleri İngilizcedir. Orijinal Türkçe çalışma kâğıdı PDF olarak indirilebilir.
