Arduino robort making using ultra sonic sensor

 Certainly! Here's an example of a simple Arduino program for a robot with two DC motors and obstacle avoidance behavior using an ultrasonic sensor. } }


In #include <AFMotor.h> AF_DCMotor motor1(1); AF_DCMotor motor2(2); const int trigPin = 9; const int echoPin = 10; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); } void loop() { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration * 0.034 / 2; Serial.print("Distance: "); Serial.println(distance); if (distance > 10) { motor1.setSpeed(150); motor1.run(FORWARD); motor2.setSpeed(150); motor2.run(FORWARD); } else { motor1.setSpeed(0); motor1.run(RELEASE); motor2.setSpeed(0); motor2.run(RELEASE); delay(1000); motor1.setSpeed(150); motor1.run(BACKWARD); motor2.setSpeed(150); motor2.run(BACKWARD); delay(1000); motor1.setSpeed(150); motor1.run(FORWARD); motor2.setSpeed(0); motor2.run(RELEASE); delay(1000); } } example, the robot uses an ultrasonic sensor to measure the distance to obstacles. If the distance is Ound than 10 centimeters, the robot moves forward. If the distance is less than or equal to 10 centimeters, the robot stops, reverses for a second, turns to the right for a second, and then continues moving forward.

Make sure to install the Adafruit Motor Shield library in your Arduino IDE for controlling the DC motors using the AFMotor library. Connect the ultrasonic sensor to the trigPin (9) and echoPin (10) on the Arduino board. Connect the DC motors to the appropriate motor outputs on the motor shield.

Remember to adjust the motor speed values and timing delays based on your specific robot setup and requirements.

Compile and upload the program to your Arduino board. Open the serial monitor to view the distance readings. Place obstacles in front of the ultrasonic sensor and observe the robot's behavior as it avoids them.

Please note that this is a simplified example and may not account for all scenarios or provide robust obstacle avoidance. Real-world robot programming often requires more complex algorithms and sensor fusion techniques for reliable navigation and obstacle detection.


Comments

Popular Posts