Getting familiar with the sensor

It's now time to write the code for the sensor. Let's output the values of the sensor to Arduinos Serial Monitor and play around with the sensor a little. We are going to use the ping_cm() function from the NewPing library. Other functions of the library are listed at http://playground.arduino.cc/Code/NewPing.

According to the web page the function is explained as follows:

sonar.ping_cm(); Sends a ping, returns the distance in centimeters or 0 (zero) if no ping echo within set distance limit. There are other functions (also called Methods in some cases) that return the distance in inches, or the time it took for the eco to return, mentioned on the webpage.

Let's get coding for our sensor!

#include <NewPing.h> 
 
NewPing sonar(8, 9); 
 
void setup() { 
 Serial.begin(115200); 
} 
 
void loop() { 
  delay(50); 
  int dist = sonar.ping_cm(); 
  Serial.print("Distance: "); 
  Serial.print(dist); 
  Serial.println("cm"); 
} 

After connecting the sensor to the Arduino as shown in the following screenshot, and uploading you sketch, start your Arduino IDE's Serial Monitor to see a stream of information:

Getting familiar with the sensor

Image source: http://fritzing.org/media/fritzing-repo/projects/h/hc-sr04-project/images/HC-SR04-3.png

This is the stream of information from the sensor! Move your hand towards and away from the sensor and see the values change!

Now let's walk through this code:

#include <NewPing.h> 

This includes the library in our sketch.

NewPing sonar(8, 9); 

This initializes an object of the NewPing library and call it sonar, and tells it that the sensor's trigger pin, which is responsible to emit the ultrasonic wave is connected to pin 8, and the sensors echo pin, that gives an output when a reflected wave is detected is connected to pin 9.

void setup() { 
 Serial.begin(115200); 
} 

Here we initialized the serial communication required for the Serial communication between the Arduino and the Arduinio IDE's Serial Monitor.

void loop() { 
  delay(50); 

We start our main loop by setting a time delay. The delay function halts the code for a specified amount of time in milliseconds as its parameter. This is required here because the main loop can cycle upto 16 000 000 times a second, which is VERY fast, and in our use case, unnecessary.

That is why, we introduce a small delay that stops the code for a small amount of time before proceeding. Here, we initialize an integer called dist that will hold the value of our distance read from the sensor We then assign it a value using the ping_cm() function we talked about earlier. Note how the function is used along with the sonar object we initialized earlier:

int dist = sonar.ping_cm(); 

Finally, we print the value to our Serial Monitor:

  Serial.print("Distance: "); 
  Serial.print(dist); 
  Serial.println("cm"); 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.203.68