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();
}
//=========================================================
3 Comments
Question 2 years ago
You mention here the connection between the board and the 7 digit display like this: 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.
Notice that from the display you list only 7 pins and on the board side you list 8 pins. Pin 3 from the display is not listed.
With this setup I could not make the whole thing work.
Could you please help?
Thank you.
Reply 2 years ago
I have updated the code and included a circuit diagram. Please be advised that the pins of the display may vary from display to display, please find the data sheet of your specific display to ensure you are connecting to the correct data and segment pins.
I hope this helps.
Answer 2 years ago
Let me draw up the circuit diagram and ill add it to this project. I hope i can clear up any questions you have. Please allow some time for me to do this.
Thank you!