This is the 14core design Obstacle Avoidance BOT with L293D Shield and HC-SR04 and Servo, which is capable of avoiding obstacles using an Ultrasonic Ranging Sensor. The HC-SR04 is economical sensor provides 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. Each HC-SR04 module includes an ultrasonic transmitter, a receiver and a control circuit. There are only four pins that you need to worry about on the HC-SR04: VCC (Power), Trig (Trigger), Echo (Receive), and GND (Ground). If you’re new to H-BRIDGE / Motor controller just follow also this link L293D / L298N.
Components Required
- Arduino Microcontroller
- L298N / l293D UNO Shield
- Smart Car Kit / Build your own Kit
- HC-SR04 Ultrasonic Raging Sensor
- 2 or 4 Gearhead Motors
- Servo
- Jumper Wire / DuPont Wire (Optional)
- Bread-board (Optional)
Wiring Diagram
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
/* 14CORE OBSTACLE AVOINDACE ROBOT ======================TEST CODE */ #include <NewPing.h> // #include <AFMotor.h> // You can download the code library below #include <Servo.h> // // Ultranic Pin Configuration #define TRIG_PIN A5 #define ECHO_PIN A4 #define MAX_DISTANCE 400 #define MAX_SPEED 255 #define MAX_SPEED_OFFSET -8 #define COLL_DIST 20 #define TURN_DIST COLL_DIST+10 #define ACT_TIME 250 NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); AF_DCMotor motorR(1, MOTOR12_1KHZ); // Set motor #1, 1kHz PWM AF_DCMotor motorL(4, MOTOR12_1KHZ); // Set motor #2, 1kHz PWM Servo myservo; // Set servo object to control a servo String motorSet = ""; int curDist = 0, pos, speedSet = 0; //int pos; //int speedSet = 0; void setup() { myservo.attach(9); // Set to attach the servo on pin 9 myservo.write(90); // Write 90 to face servo forward delay(2000); motorSet = "FORWARD"; moveForward(); } void loop() { checkPath(); } void checkPath() { int curLeft = 0; int curRight = 0; int curFront = 0; curDist = 0; checkForward(); myservo.write(135); delay(100); for (pos = 135; pos >= 45; pos -= 45) { myservo.write(pos); delay(170); curDist = readPing(); if (curDist < COLL_DIST) { checkCourse(); break; } if (curDist < TURN_DIST) { changePath(); } } } int readPing() { int cm = 0; while (cm < 2) {int uS = sonar.ping(); cm = uS/US_ROUNDTRIP_CM;} return cm; } void checkForward() { if (motorSet=="FORWARD") { motorR.run(FORWARD); motorL.run(FORWARD); } } void changePath() { if (pos < 90) { veerLeft(); } if (pos > 90) { veerRight(); } } void veerRight() { motorR.run(BACKWARD); motorL.run(FORWARD); delay(ACT_TIME); motorR.run(FORWARD); motorL.run(FORWARD); motorSet = "FORWARD"; } void veerLeft() { motorL.run(BACKWARD); motorR.run(FORWARD); delay(ACT_TIME); motorL.run(FORWARD); motorR.run(FORWARD); motorSet = "FORWARD"; } void checkCourse() { moveBackward(); delay(ACT_TIME); moveStop(); setCourse(); } void setCourse() { if (pos < 90) { turnRight(); } if (pos > 90) { turnLeft(); } } void moveBackward() { motorSet = "BACKWARD"; motorR.run(BACKWARD); // Turn right motor backward motorL.run(BACKWARD); // Turn left motor backward for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) { motorL.setSpeed(speedSet); motorR.setSpeed(speedSet+MAX_SPEED_OFFSET); delay(5); } } void moveForward() { motorSet = "FORWARD"; checkForward(); for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) { motorL.setSpeed(speedSet); motorR.setSpeed(speedSet+MAX_SPEED_OFFSET); delay(4); } } void moveStop() { motorR.run(RELEASE); motorL.run(RELEASE); } void turnRight() { motorSet = "RIGHT"; motorR.run(FORWARD); // Turn right motor forward motorL.run(BACKWARD); // Turn left motor backward delay(ACT_TIME); motorSet = "FORWARD"; checkForward(); } void turnLeft() { motorSet = "LEFT"; motorR.run(BACKWARD); // Turn right motor backward motorL.run(FORWARD); // Turn left motor forward delay(ACT_TIME); motorSet = "FORWARD"; checkForward(); } |
Downloads
14CORE Obstacle Avoidance Bot with HC-SR04, L293D Shield, SERVO & Arduino Microcontroller
Where do u connect the arduino?
Note: it is actually an L2983D Arduino UNO Shield. see above image :) Regards
Hi, can you send me a circuit and code for same but 2 servo’s and 2 ultrasonic sensor’s? Thanks