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 gfedcba | abcdefg | a | b | c | d | e | f | g |
---|---|---|---|---|---|---|---|---|
0 0×3F | 0×7E | on | on | on | on | on | on | off |
1 0×06 | 0×30 | off | on | on | off | off | off | off |
2 0×5B | 0×6D | on | on | off | on | on | off | on |
3 0×4F | 0×79 | on | on | on | on | off | off | on |
4 0×66 | 0×33 | off | on | on | off | off | on | on |
5 0×6D | 0×5B | on | off | on | on | off | on | on |
6 0×7D | 0×5F | on | off | on | on | on | on | on |
7 0×07 | 0×70 | on | on | on | off | off | off | off |
8 0×7F | 0×7F | on | on | on | on | on | on | on |
9 0×6F | 0×7B | on | on | on | on | off | on | on |
A 0×77 | 0×77 | on | on | on | off | on | on | on |
B 0×7C | 0×1F | off | off | on | on | on | on | on |
C 0×39 | 0×4E | on | off | off | on | on | on | off |
D 0×5E | 0×3D | off | on | on | on | on | off | on |
E 0×79 | 0×4F | on | off | off | on | on | on | on |
F 0×71 | 0×47 | on | off | off | off | on | on | on |
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 |
---|---|
e | 6 |
d | 5 |
c | 4 |
b | 2* |
a | 3* |
f | 7 |
g | 8 |
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++; } }