Arduino Ultrasonic Distance Meter With 7 Segment Display.

Introduction: Arduino Ultrasonic Distance Meter With 7 Segment Display.

This tutorial will show you how to use an Arduino and ultrasonic sensor to display the value in centimeters to a 4-digit 7 segment display.


· Microcontroller

· 4-digit 7 segment display

· x8 330Ω resistors

· Jumper leads

· HC-SR04 (Ultrasonic Sensor)

· Breadboard

Step 1: Set Up Hardware.

Connect pins 12,9,8,6 from the display to pins 13,12,11,10 (respectively)of the Arduino.

Connect pins 11,7,4,2,1,10,5 of the display to pins 2,3,4,5,6,7,8,9 (respectively) of the Arduino with one 330Ω resistor in line.

On the ultrasonic sensor connect to VCC to 5V power pin, GND to the ground pin, the Trigger pin to A1 and Echo pin to A0.

Step 2: Upload the Code


#include "SevSeg.h" // library for 7 segment display

SevSeg sevseg; // initate 7 segment display

//ultrasonic sensor pins globar variables int trigPin = A1; int echoPin = A0;

int cm; // variable to be read by display int interval; //value from the trig and echo pins

void setup() {

Serial.begin (9600); // serial monotor comm rate

pinMode(trigPin, OUTPUT); // set to output pinMode(echoPin, INPUT); // set to input

// set up SevSeg library parmeters byte numDigits = 4; // number of digits on the display byte digitPins[] = {13,12,11,10}; // pin numbers for each digit byte segmentPins[] = {2,3,4,5,6,7,8,9}; // pins for each part of the 7 segment display

bool resistorsOnSegments = true; // true if resistors are being used bool updateWithDelaysIn = true; // delays used byte hardwareConfig = COMMON_ANODE; // display type sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments); sevseg.setBrightness(100); // brightness level of the display


void loop() {

digitalWrite(trigPin, LOW); // send short signal on the trigpin


digitalWrite(trigPin, HIGH); // send signal for 10ms


digitalWrite(trigPin, LOW); // turn off signal after 10ms

interval = pulseIn(echoPin, HIGH); // read signal from trigpin

// determine distance by using the time from the trig and echo pins // divide interval by 2 then divide again by 29 cm = (interval/2) / 29;

// print to serial monitor




//print to 7 segment display sevseg.setNumber(cm); // print the value of cm to the display sevseg.refreshDisplay();



Be the First to Share


    • Puzzles Speed Challenge

      Puzzles Speed Challenge
    • "Can't Touch This" Family Contest

      "Can't Touch This" Family Contest
    • CNC Contest 2020

      CNC Contest 2020