Tuesday, 6 August 2019

TinkerCad Tutorial #3 Arduino Distance Sensor

TinkerCad Tutorial #3

Arduino Distance Sensor

     

          Hi...today we shall see how we can interface Arduino with the ultrasonic distance sensor. In the first part we shall see on the theoretical part of the ultrasonic sensor. 








               First lets look at the theory of the ultrasonic distance sensor. The ultrasonic sensor works on the priciple of SONAR and RADAR system which is used to determine the distance to an object. The image below is the illustration of how does it work.












There are few version of ultrasonic module available in the market. Below are the image of 3 different type of ultrasonic module.


Figure 1.     3 Pin Ultrasonic Sensor


Figure 2.     4 Pin Ultrasonic Sensor


Figure 3.     5 Pin Ultrasonic Sensor

And below is the timing diagram for each the module.











           As you can see, for each module it has it's own timing diagram. In our tutorial today, we will use the 3 pin ultrasonic sensor. 






         The image below shows pin description of the ultrasonic sensor. Below is the pin connection which we have done in the tinkercad. 




       We have connected the GND and VCC pin of the ultrasonic sensor respectively to the Arduino GND and 5V. And the signal pin is connected to the digital PIN 7. Once we are ok this, we shall move on to the coding part.



For the coding, i have used the sample code, which was available in the arduino forum. Below is the code which we have used for this tutorial.



 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
const int PingPin = 7;
int C;

void setup()
{
 Serial.begin(9600); //to initialize the serial communication         
}

void loop()
{
 long duration;
        long inches;
   long cm;
  
   pinMode(PingPin,OUTPUT);
   digitalWrite(PingPin,LOW);
        delayMicroseconds(2);
   digitalWrite(PingPin,HIGH);
   delayMicroseconds(5);
   digitalWrite(PingPin,LOW);
  
  
   pinMode(PingPin,INPUT);
   duration = pulseIn(PingPin,HIGH);
   C = digitalRead(duration);
   
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
  
   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm, ");
   Serial.println();
   delay(100);
}  

long microsecondsToInches(long microseconds)
    {
     return microseconds / 74 / 2;
    }
  
long microsecondsToCentimeters(long microseconds)
    {
     return microseconds / 29 / 2; 
    }
   


       


Below is the result of this tutorial.






Below is the complete tutorial video






Thank you.


No comments:

Post a Comment