Distance Meter With Arduino

Introduction: Distance Meter With Arduino

About: My name is Simao and I love eletronics ,glass art,metal working,casting,paint ,draw...

Hello guys !Ready for a new project?Good,let's make it...

Today I gonna show you how to make a distance meter like that ones that are sold on hardware stores or construction stores ,I gonna use a ultra sonic sensor in my case Vellman VMA306 or other marks:HC-SR05 ,infortunly you can't find many thing on internet about this ultra sonic sensor because the most popular is the HC-SR04,I use a LCD screen 16*2 for display the distance.Let's make.


  1. Arduino UNO R3 (you can use NANO or MEGA).
  2. jumpers.
  3. Breadborde.
  4. LCD 16*2.
  5. HC-SR05 ultra-sonic sensor.
  6. 10K variable resistor.

Step 1: Schematic:

Note : the IC is not a IC is the ultra sonic sensor .

Step 2: Code:


LiquidCrystal lcd(2, 4, 7, 8, 9, 10);

// definiçao de constantes para pinos #define TRIG_PIN 13 #define ECHO_PIN 12

#define RED 6 #define GREEN 5 #define BLUE 3

// definiçao de constantes para facilitar a manutençao das distancias #define BLUE_LONG 60 #define BLUE_SHORT 30 #define GREEN_LONG 45 #define GREEN_SHORT 15 #define RED_LONG 30 #define RED_SHORT 0 void setup() { // inicia o display LCD lcd.begin(16,2); // seta o modo dos pinos do LED pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT); // seta o modo dos pinos do HC-SR05 pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT);

// apaga o LED (anodo comum, apaga com HIGH) digitalWrite(RED, HIGH); digitalWrite(GREEN, HIGH); digitalWrite(BLUE, HIGH); } void loop() { // declaracao de variaveis long duration, distance; // le a distancia e converte para cm digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); duration = pulseIn(ECHO_PIN, HIGH); distance = (duration/2) / 29.1; // de acordo com a distancia, mapeia a cor do led usando pwm // azul: de 60cm a 30cm // verde: de 45cm a 15cm // vermelho: de 30cm a 0cm if (distance > RED_LONG) digitalWrite(RED, HIGH); else analogWrite(RED, map(distance, RED_LONG, RED_SHORT, 255, 0)); if ((distance > GREEN_LONG) || (distance < GREEN_SHORT)) digitalWrite(GREEN, HIGH); else analogWrite(GREEN, map(distance, GREEN_LONG, GREEN_SHORT, 255, 0)); if ((distance > BLUE_LONG) || (distance < BLUE_SHORT)) digitalWrite(BLUE, HIGH); else analogWrite(BLUE, map(distance, BLUE_LONG, BLUE_SHORT, 255, 0)); // escreve a distancia no display lcd.clear(); lcd.setCursor(0,0); lcd.print("simaopintocorreia"); lcd.setCursor(0,1); lcd.print("Distancia: "); if (distance > 450) lcd.print("> 50"); else lcd.print(distance); lcd.print(" cm"); // aguarda para poder ler o sensor novamente delay(400); }

Step 3: Obs:

This distance meter can mesure distances between 0cm and 450cm.

This can be used every where.

I expect you enjoy this project .If you have trouble on this project ,find new ways to use the sensor live in the coments new ideas for projects, photos from your project.

See you soon .

simaopintocorreia .

Sensors Contest

Participated in the
Sensors Contest

Be the First to Share


    • Mason Jar Speed Challenge

      Mason Jar Speed Challenge
    • Bikes Challenge

      Bikes Challenge
    • Remix Contest

      Remix Contest