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.

Supplies

· 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 (D1- D4) from the display to pins 13,12,11,10 (respectively)of the Arduino.

Connect pins 11,7,4,2,1,10,5 (a,b,c,d,e,f,g) of the display to pins 2,3,4,5,6,7,8 (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.

***Pin Numbers are respective to the specific hardware/ IC being used. Please consult data sheets for more detailed explanations ***

Step 2: Upload the Code

***Use this reference, please download the file linked and load this code into the IDE. ***

//=========================================================

#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

delayMicroseconds(5);

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

delayMicroseconds(10);

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

Serial.print(cm);

Serial.print("cm");

Serial.println();

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

}

//=========================================================