Here is the code for object avoiding robot that uses one HC-SR04 Ultrasonic Object Detector.

int trigPin = 2;   
int echoPin = 3;   
long duration, inches;
void setup() {
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(4,OUTPUT);
 pinMode(7,OUTPUT);
}
void loop()
{
 digitalWrite(trigPin, LOW);
 delayMicroseconds(5);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);
 inches = (duration/2) / 74;
 delay(100);
 if(inches<=10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,HIGH);
   digitalWrite(7,LOW);
 }
 if(inches>10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,HIGH);
   digitalWrite(7,HIGH);
 }
}

Learn how to set up a headless raspberry pi (needs to connected to the display for the one-time setup)
Check out other articles from learn2create.

LEAVE A REPLY

Please enter your comment!
Please enter your name here