Distance Sensors Lab

Introduction: Distance Sensors Lab

In this lab you will learn to read distances using an ultrasonic sensor (HC-SR04) and display those distances with a 7 segment display.

As a note there is an error in the diagram! The 2 pins in the top right corner needs to be swapped!

Step 1: Add Ultrasonic Sensor

The ultrasonic sensor (HC-SR04) allows us to measure distances using ultrasonic pings. The sensor issues an ultrasonic ping, based on how long it takes for the ping to return the sensor can determine how far away an object is.

To connect the ultrasonic sensor, we will need to use the following pins on the Arduino*:

12 - Trigger Pin

11 - Echo Pin

VCC - +5v

GND - GND

*Make sure to use 4 wires from the ribbon cable which has male-female connectors

Step 2: Ultrasonic Sensor Code Example - NewPing Library

We will be using the NewPing Library to control the HC-SR04. Official Page: http://playground.arduino.cc/Code/NewPing

#include <NewPing.h>
 
#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
void setup() {
  Serial.begin(115200);
}
 
void loop() {
  delay(50);
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm());
  Serial.println("cm");
}

Step 3: Read Distances With 7 Segment Display

Instead of relying on the Serial monitor to read the distances we'll incorporate a 7 Segment display which can help us see if an object is 1 - 10 cm away. The steps for using the 7 Segment display are adapted from the following online tutorial : https://www.allaboutcircuits.com/projects/interface-a-seven-segment-display-to-an-arduino/

Step 4: How the 7 Segment Display Works

The seven segment display (with a decimal point), utilizes 8 LEDs to display the characters 0 - 9 and a decimal point. To display a specific character, the Arduino needs to on/off a specific set of LEDs. To help determine which LEDs need to be turned on/off use the following table:

Digit gfedcbaabcdefgabcdefg
0 0×3F0×7Eononononononoff
1 0×060×30offononoffoffoffoff
2 0×5B0×6Dononoffononoffon
3 0×4F0×79ononononoffoffon
4 0×660×33offononoffoffonon
5 0×6D0×5Bonoffononoffonon
6 0×7D0×5Fonoffononononon
7 0×070×70onononoffoffoffoff
8 0×7F0×7Fononononononon
9 0×6F0×7Bononononoffonon
A 0×770×77onononoffononon
B 0×7C0×1Foffoffononononon
C 0×390×4Eonoffoffonononoff
D 0×5E0×3Doffononononoffon
E 0×790×4Fonoffoffonononon
F 0×710×47onoffoffoffononon

Step 5: Add 7 Segment Display

1. Place the display in the breadboard. Notice the current orientation of the display, the decimal place is in the bottom right corner!

2. Connect the middle pin on each side to the - Ground rail (which in turn must be connect to the GND pin on the Arduino)

3. Connect the 7 Segment display to the Arduino using the following table:

   7 Segment Pin   
   Arduino Pin     
e6
d5
c4
b2*
a3*
f7
g8

Make sure to include a 220 Ohm resistor in your circuit!

*The b/a pins are incorrect in the diagram, please follow the table*

Step 6: First Code Example - Turn Each LED On/off

void setup()
{
  // define pin modes
  for(int i=2;i<9;i++)
  {
     pinMode(i,OUTPUT);
  }
 
}

void loop() 
{
  // loop to turn leds od seven seg ON
  
  for(int i=2;i<9;i++)
  {
    digitalWrite(i,HIGH);
    delay(600);
  }
  
  // loop to turn leds od seven seg OFF
  for(int i=2;i<9;i++)
  {
    digitalWrite(i,LOW);
    delay(600);
  }
  
  
  delay(1000);

}

Step 7: Second Code Example - Display Specific Characters

int numArray[10][7] = {  { 1,1,1,1,1,1,0 },    // 0
                          { 0,1,1,0,0,0,0 },    // 1
                          { 1,1,0,1,1,0,1 },    // 2
                          { 1,1,1,1,0,0,1 },    // 3
                          { 0,1,1,0,0,1,1 },    // 4
                          { 1,0,1,1,0,1,1 },    // 5
                          { 1,0,1,1,1,1,1 },    // 6
                          { 1,1,1,0,0,0,0 },    // 7
                          { 1,1,1,1,1,1,1 },    // 8
                          { 1,1,1,0,0,1,1 }};   // 9
                                       
//function header
void numWrite(int);

void setup() 
{ 
  // set pin modes
for(int i=2;i<9;i++)
  {
     pinMode(i,OUTPUT);
  }
}

void loop() 
{
  
  //counter loop
  
  for (int i = 10; i > 0; --i) 
  {
   delay(1000);
   numWrite(i-1); 
  }
  delay(3000);
}

// this functions writes values to the sev seg pins  
void numWrite(int number) 
{
  int pin= 2;
  for (int j=0; j < 7; j++) {
   digitalWrite(pin, numArray[number][j]);
   pin++;
  }
}

Step 8: Combine the 7 Segment Display and Ultrasonic Sensor

Nothing new needs to be added to the Arduino. The above diagram simply shows the 7 Segment Display and the Ultrasonic Sensor.

Step 9: Code to Display Ultrasonic Distances With 7 Segment Display

#include < NewPing.h >
 
#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 90 //With only 9 digits, we'll limit our distance to be at most 90 decimeters
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);


int num_array[10][7] = {  { 1,1,1,1,1,1,0 },    // 0
                          { 0,1,1,0,0,0,0 },    // 1
                          { 1,1,0,1,1,0,1 },    // 2
                          { 1,1,1,1,0,0,1 },    // 3
                          { 0,1,1,0,0,1,1 },    // 4
                          { 1,0,1,1,0,1,1 },    // 5
                          { 1,0,1,1,1,1,1 },    // 6
                          { 1,1,1,0,0,0,0 },    // 7
                          { 1,1,1,1,1,1,1 },    // 8
                          { 1,1,1,0,0,1,1 }};   // 9
//function header
void numWrite(int);
						  
void setup() {
  Serial.begin(9600);
  for(int i = 2; i < 9; i++)
  {
     pinMode(i,OUTPUT);
  }
}
 
void loop() {
  delay(50);
  int distance = sonar.ping_cm();
  Serial.print("Ping: ");
  Serial.print(distance);
  Serial.println("cm");
  numWrite(distance / 10);//Convert 0 - 90 centimeters to 0 - 9 decimeters
}

// this functions writes values to the sev seg pins  
void numWrite(int number) 
{
  int pin= 2;
  for (int j=0; j < 7; j++) {
   digitalWrite(pin, num_array[number][j]);
   pin++;
  }
}

1 Person Made This Project!

Recommendations

  • Puzzles Speed Challenge

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

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

    CNC Contest 2020

2 Discussions

0
chantelmillersong
chantelmillersong

4 months ago

I made this project it took a long time to make but it was worth it, stuff had to be modified in order to work because the diagram is missing a few details.
In the first photo, it shows how we tidied up the wires and resistors so it wouldn't be a mess - if you make this project I recommend you do it like this .as well in the last 2 photos, for your distance Sensors Lab to work you will need to add 2 wires, one containing the ground to the negative rail on your breadboard and the other one connecting the 5volt to the positive rail on your breadboard. last of all check whether your 7 Segment Display is common anode or common-cathode if the one you are using is common anode you will need to supply 5VDC to the center pins on the 7 Segment Display. You will also need to change the code for the display characters. basically it's the opposite. For example, to display a "zero" you need to use '0,0,0,0,0,0,1' instead of '1,1,1,1,1,1,0'. Maybe there's another way for this to work but this is how I got it to work. good luck

20200330_143802.jpg20200330_143558.jpg20200330_143626.jpg
0
ReymarkD2
ReymarkD2

Question 1 year ago on Step 9

Hey, I just wanna ask if the program will run on ATMEGA 2560? Thanks