Skip to content

Instantly share code, notes, and snippets.

@mandyRae
Created February 14, 2016 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandyRae/9b08cc6896fcd89db1fa to your computer and use it in GitHub Desktop.
Save mandyRae/9b08cc6896fcd89db1fa to your computer and use it in GitHub Desktop.
Sample sketch for using ultrasonic sensors with Arduino
//This sketch demonstrates how to interface with ultrasonic sensors using the Arduino and NewPing library
#include <NewPing.h>
int TRIGGER = 12;
int ECHO = 11;
//Declare a sonar object specifying the trigger and echo pins
NewPing sonar(TRIGGER, ECHO);
void setup(){
//Start serial at 115200 baud rate
Serial.begin(115200);
}
void loop(){
//get current measurement from sensor in centimeters
int distance = sonar.ping_cm();
//print it to the serial monitor
Serial.print(distance);
Serial.println("cm");
//pause slightly before rereading the sensor again, don't make this value less than 30
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment