Interfacing an Ultrasonic sensor
Hardware Required
- ARDUINO UNO
- Ultrasonic Sensor
- Connecting wires
About ARDUINO
Arduino is a popular open-source development board for engineers and makers to develop electronics projects in an easy way. It consists of both a physical programmable development board (based on AVR series of microcontrollers) and a piece of software or IDE which runs on your computer and used to write and upload the code to the microcontroller board.
About Ultrasonic Sensor
As the name indicates, ultrasonic sensors measure distance by using ultrasonic waves.The sensor head emits an ultrasonic wave and receives the wave reflected back from the target. Ultrasonic Sensors measure the distance to the target by measuring the time between the emission and reception.
#include <NewPing.h>
int x = 0 ;
int x = 0 ;
#define ECHO_PIN 12
#define TRIGGER_PIN 11
#define MAX_DISTANCE 500// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define TRIGGER_PIN 11
#define MAX_DISTANCE 500// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup()
void setup()
{
Serial.begin(9600);
x = sonar.ping_cm() ;
}
void loop()
}
void loop()
{
x = sonar.ping_cm() ;
Serial.println(x);
}
Code explanation
#define MAX_DISTANCE 500 - This command is used used for declaring the max distance of the ultrasonic sensor.
x = sonar.ping_cm() ; This command is used for defining that the variable x holds the value that ultrasonic sensor inputs to the ARDUINO.
Serial.println(x); In our code we'll serial print x to serial print the distance of the obstacle detected.
No comments:
Post a Comment