Anth's Computer Cave Tutorials

Run a distance sensor from your Arduino

A distance sensor connected to an Arduino. Picture: Anthony Hartup

Today I'll connect and run a distance sensor and have the Arduino react to proximity.

This is great for robotics and it can also be useful for contact-less switch applications like hands-free taps, or proximity-related safety cutouts for machinery.

I have been using these sensors from the Raspberry Pi on my robots and they have been consistently accurate to within about 2mm.

I decided to adapt my Python code to test them with an Arduino.

For best results, mount the sensor squarely and securely, and leave about six inches clearance underneath.


These sensors have four pins. There is a VCC, a GND, a Trig and an Echo.

The Trig pin, or trigger, sends a short pulse to the sensor. The sensor then sends an ultrasonic pulse and routes the return pulse through the Echo pin and into the Arduino. The Arduino times how long the return signal takes to arrive and calculates the distance accordingly.

A connection diagram for an Arduino and distance-sensor. Picture: Anthony Hartup.

The VCC pin connects to the 5V pin on the Arduino. The GND pin connects to one of the Arduino's GND pins. I have connected the Echo pin to pin 2 on the Arduino and the Trig pin to pin 3. These pins match the code below.

Arduino sketch

Paste the code into a new Arduino file.

int echo = 2;
int trigger = 3;
int warningLed = 13;

void setup() {
  // Start the serial connection:
  // set echo pin as input and trigger pin as output
  pinMode(echo, INPUT);
  pinMode(trigger, OUTPUT);
  pinMode(warningLed, OUTPUT);

void loop() {
  float rawTime;
  //prepare trigger pin
  digitalWrite(trigger, LOW);
  // Send a 5us signal to trigger
  digitalWrite(trigger, HIGH);
  digitalWrite(trigger, LOW);
  //monitor the time signal takes to return
  rawTime = pulseIn(echo, HIGH); 
  // Convert rawTime to centimeters
  // To time only one-way divide by two
  float distance = (rawTime / 2) / 29.4;
  // print the distance in centimeters
  // Turn on LED if distance is less than 30cm
  if(distance < 30) {
   digitalWrite(warningLed, HIGH);
  else {
   digitalWrite(warningLed, LOW);


I have taken some liberties with the conversion to centimeters. Any examples I have seen divides the raw time by 29.1, but I found this would produce consistently high measurements. I did my own calculations based on what is working on my Raspberry Pi distance sensors.

With the Python code for the Pi I take the time in seconds, rather than microseconds, then multiply it by 34000. This works perfectly. When I calculate this to suit the microsecond increments the Arduino uses I get / 29.38, so that is the figure I am using.

With a tape measure I can see that my measurements are definitely within 2mm. If your measurements are low you can change the 29.4 to 29.1 and try again.

Running the code

Upload the sketch to the Arduino then go to Tools and select Serial Monitor.

The measurement should print to the screen every two seconds. Move your hand in front of the sensor. When you get within 30cm, the LED should light on the Arduino. It will stay lit until you move your hand further than 30cm away.

That's all for this tutorial. Leave a comment below if you need to know more, and stay tuned for more Arduino articles.





Leave a comment on this article

Leave a comment on this article