Introduction

This article will help those who are interested in learning the Internet of Things. It gives brief details of Arduino and Ultrasonic sensors which are used to detect a collision.
Requirements
Steps to follow.
Step 1
First of all, connect the Arduino board with a laptop or computer.
IoT
Step 2
Install the Arduino software on the computer.
IoT
Step 3
Open the Arduino software. The following screen will appear.
IoT
Step 4
You can see the Main Page of the software.
IoT
Step 5
Then, open the new page on the software.
Then select the File→ New.
IoT
A new page opens on the Arduino software.
IoT
Step 6
I have given a source code that you can use or you can use your own source code.
Then, select File → open → select written program →open the program.
IoT
Open the program folder.
IoT
Select the program and open it.
IoT
Source code
  1. #include <Servo.h>
  2. #include <NewPing.h>
  3. const int LeftMotorForward = 7;
  4. const int LeftMotorBackward = 6;
  5. const int RightMotorForward = 4;
  6. const int RightMotorBackward = 5;
  7. #define trig_pin A1 //analog input 1
  8. #define echo_pin A2 //analog input 2
  9. #define maximum_distance 200
  10. boolean goesForward= false;
  11. int distance = 100;
  12. NewPing sonar(trig_pin,echo_pin, maximum_distance); //sensor function
  13. Servo servo_motor;//our servo name
  14. void setup(){
  15. Serial.begin(9600);
  16. pinMode(RightMotorForward,OUTPUT);
  17. pinMode(LeftMotorForward,OUTPUT);
  18. pinMode(LeftMotorBackward,OUTPUT);
  19. pinMode(RightMotorBackward,OUTPUT);
  20. servo_motor. attach(10);//our servo pin
  21. servo_motor.write(115);
  22. delay(2000);
  23. distance =readPing();
  24. delay(100);
  25. distance = readPing();
  26. delay(100);
  27. distance =readPing();
  28. delay(100);
  29. distance = readPing();
  30. delay(100);
  31. }
  32. void loop(){
  33. int distanceRight = 0;
  34. int distanceLeft = 0;
  35. delay(50);
  36. if(distance<=40){
  37. //Serial.println(distance);
  38. moveStop();
  39. delay(400);
  40. moveBackward();
  41. delay(400);
  42. moveStop();
  43. delay(400);
  44. distanceRight = lookRight();
  45. delay(400);
  46. distanceLeft = lookLeft();
  47. delay(400);
  48. if(distance >= distanceLeft){
  49. turnRight();
  50. moveStop();
  51. }
  52. else{
  53. turnLeft();
  54. moveStop();
  55. }
  56. }
  57. else{
  58. moveForward();
  59. }
  60. distance = readPing();
  61. }
  62. int lookRight(){
  63. servo_motor.write(50);
  64. delay(500);
  65. int distance=readPing();
  66. delay(100);
  67. servo_motor.write(115);
  68. return distance;
  69. }
  70. int lookLeft(){
  71. servo_motor.write(170);
  72. delay(500);
  73. int distance = readPing();
  74. delay(100);
  75. servo_motor.write(115);
  76. return distance;
  77. delay(100);
  78. }
  79. int readPing(){
  80. delay(70);
  81. int cm = sonar.ping_cm();
  82. if (cm==0){
  83. cm=250;
  84. }
  85. return cm;
  86. }
  87. void moveStop(){
  88. digitalWrite(RightMotorForward,LOW);
  89. digitalWrite(LeftMotorForward,LOW);
  90. digitalWrite(RightMotorBackward,LOW);
  91. digitalWrite(LeftMotorBackward,LOW);
  92. }
  93. void moveForward(){
  94. if(!goesForward){
  95. goesForward=true;
  96. digitalWrite(LeftMotorForward,HIGH);
  97. digitalWrite(RightMotorForward,HIGH);
  98. digitalWrite(LeftMotorBackward,LOW);
  99. digitalWrite(RightMotorBackward,LOW);
  100. }
  101. }
  102. void moveBackward(){
  103. goesForward=false;
  104. digitalWrite(LeftMotorBackward,HIGH);
  105. digitalWrite(RightMotorBackward,HIGH);
  106. digitalWrite(LeftMotorForward,LOW);
  107. digitalWrite(RightMotorForward,LOW);
  108. }
  109. void turnRight(){
  110. digitalWrite(LeftMotorForward,HIGH);
  111. digitalWrite(RightMotorBackward,HIGH);
  112. digitalWrite(LeftMotorBackward,LOW);
  113. digitalWrite(RightMotorForward,LOW);
  114. delay(500);
  115. digitalWrite(LeftMotorForward,HIGH);
  116. digitalWrite(RightMotorForward,HIGH);
  117. digitalWrite(LeftMotorBackward,LOW);
  118. digitalWrite(RightMotorBackward,LOW);
  119. }
  120. void turnLeft(){
  121. digitalWrite(LeftMotorBackward,HIGH);
  122. digitalWrite(RightMotorForward,HIGH);
  123. digitalWrite(LeftMotorForward,LOW);
  124. digitalWrite(RightMotorBackward,LOW);
  125. delay(500);
  126. digitalWrite(LeftMotorForward,HIGH);
  127. digitalWrite(RightMotorForward,HIGH);
  128. digitalWrite(LeftMotorBackward,LOW);
  129. digitalWrite(RightMotorBackward,LOW);
  130. }
Step 9
The obstacle avoiding robot program coding will be there already. The value of distance for the ultrasonic sensors to detect the collision is set in the program as 30 cm.
IoT
Step 10
Click TOOL- Select BOARD - then press ARDUINO/GENUINO UNO R3.
IoT
Again Select TOOL >> Select a PORT >> COM3 ARDUINO UNO
IoT
Step 11
Verify the program or compile the program.
IoT
The compilation is done.
Step 12
Connect the hardware as per the connection diagram given for the Obstacle avoiding robot.
IoT
Complete the connection as per the diagram.
Step 13
Then connect the Arduino to the laptop and upload the code in Arduino.
IoT
Step 14
Then the obstacle avoiding robot will start working.
IoT
The ultrasonic sensor detects the collision using Arduino.

Summary

Hence, we have created and executed an Arduino project which prevents collisions with an obstacle avoiding robot.
More exciting articles are coming up next.