# Measuring Distance - قياس المسافات الصغيره باستخدام التراسونيك سنسور واردوينو

52

1

0

## Introduction: Measuring Distance - قياس المسافات الصغيره باستخدام التراسونيك سنسور واردوينو

This instructables provides basic guidelines on how to build advanced
electronic circuits and algorithms by using Arduino and fundamental
electronic hardware. Basically the goal is to:
• Create and Build electronic circuits on breadboard using a well
known components.
• Understand how to program the system to do the required task.

♧ What is the Ultrasonic PING Sensor?
Ultrasonic Sensor is a device you can use to measure how far away an object is. The range of this sensor is between 3 centimeters to 3.3 meters. It can be used in Robotics and automation projects.
♧ Definition of Measuring Distance Experiment
Definition Measuring Distance is an experiment based on how to sense the distance and display on fourteen segment display (Two digits).

### Supplies:

Basic components:
1- Arduino nano. (1 unit).
2- Fourteen segment display (2 digits).
3- Power supply 5V. (1 unit).
4- Wires (male-male, male-female).
5- Ultrasonic sensor PING (1 unit).
6- Ruler.

Basic tools:
1- Mini USB cable for Arduino (1unit).
2- Arduino IDE program on PC.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Test Ultrasonic Sensor.

♧ PING Sensor connections
• Connect the GND in PING sensor with GND in Arduino Nano.
• Connect the 5 Volt with 5 Volt in Arduino Nano.
• Connect the SIG with the D13 in Arduino Nano.

♧ How does the Ultrasonic Sensor work?
The Ultrasonic sensor uses a brief ultrasound wave making it possible for the micro-controller to measure the time it takes the echo to return to its ultrasonic microphone. The Ultrasonic sensor sends a pulse to start the measurement and waits long enough to start a PULSING command. When Ultrasonic sensor detects the echo with its microphone, it changes the high signal back to low. The microcontroller’s PULSING command stores how long the high signal lasted.
The time measurement is how long it takes the sound to travel to the object. With this measurement, you can then use the speed of sound in air to make your program calculate the object’s distance in centimeters, inches, feet, and show the distance on the fourteen segment display

## Step 2: Testing of Ultrasonic PING Sensor

Write the code in arduino IDE program
unsigned long echo = 0;
int ultraSoundSignal = 13; // Ultrasound signal pin
unsigned long ultrasoundValue = 0;

void setup()
{
Serial.begin(9600);
pinMode(ultraSoundSignal,OUTPUT);
}

unsigned long ping()
{
pinMode(ultraSoundSignal, OUTPUT); // Switch
signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low
pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high
pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch
signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on
pullup resistor
// please note that pulseIn has a 1sec timeout,
which may not be desirable. Depending on your
sensor specs, you can likely bound the time like
this -- marcmerlin, echo = pulseIn(
ultraSoundSignal, HIGH, 38000)
echo = pulseIn(ultraSoundSignal, HIGH); //Listen
for echo
ultrasoundValue = (echo / 58.138) * .39; //convert
to CM then to inches
return ultrasoundValue;
}

void loop()
{
int x = 0;
x = ping();
Serial.println(x);
delay(250); //delay 1/4 seconds.
}

■ Then open the SERIAL MONITOR in Arduino IDE to show the signals

## Step 3: Exercise: Measuring Distance From 3cm Yo 10cm.

¤ Built the circuit:
1- Connect the ultrasonic PING sensor with fourteen segment display.

## Step 4: Devide the LEDs Into Groups.

♧ To facilitate writing the code and lighting LEDs by connecting it with
Arduino you should divide the LEDs into groups as follows.

## Step 5: Measuring Distance Program Code.

Write code using Arduino IDE program.
int ledA = 10;
int ledB = 9;
int ledC = 8;
int ledD = 7;
int ledE = 6;
int ledF = 5;
int ledG = 4;
int ledAA = A1;
int ledBB = A2;
int ledCC = A3;
int ledDD = A4;
int ledEE = A5;
int ledFF = 3;
int ledGG = 2;
int distance;
int signal=13;
unsigned long pulseduration=0;
void setup()
{
pinMode(signal, OUTPUT);
Serial.begin(9600);
pinMode(ledA, OUTPUT);
pinMode(ledB, OUTPUT);
pinMode(ledC, OUTPUT);
pinMode(ledD, OUTPUT);
pinMode(ledE, OUTPUT);
pinMode(ledF, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(ledAA, OUTPUT);
pinMode(ledBB, OUTPUT);
pinMode(ledCC, OUTPUT);
pinMode(ledDD, OUTPUT);
pinMode(ledEE, OUTPUT);
pinMode(ledFF, OUTPUT);
pinMode(ledGG, OUTPUT);
}
void measureDistance(){
// set pin as output so we can send a pulse
pinMode(signal, OUTPUT);
// set output to LOW
digitalWrite(signal, LOW);
delayMicroseconds(5);

// now send the 5uS pulse out to activate Ping)))
digitalWrite(signal, HIGH);
delayMicroseconds(5);
digitalWrite(signal, LOW);
// now we need to change the digital pin to input to
pinMode(signal, INPUT);
// finally, measure the length of the incoming pulse
pulseduration=pulseIn(signal, HIGH);
}

void loop(){
off();
// get the raw measurement data from Ping)))
measureDistance();
// divide the pulse length by half
pulseduration=pulseduration/2;

// convert to centimetres.
distance = int(pulseduration/29);
// Display on serial monitor
Serial.print("Distance - ");
Serial.print(distance);
Serial.println(" cm");
show(distance);
delay(500);
}
void show(int n) {
switch (n) {
case 1:
number1(); break;
case 2:
number2(); break;
case 3:
number3(); break;
case 4:
number4(); break;
case 5:
number5(); break;
case 6:
number6(); break;
case 7:
number7(); break;
case 8:
number8(); break;
case 9:
number9(); break;
case 10:
number10(); break;
}
void off() {
digitalWrite(ledA, LOW);
digitalWrite(ledB, LOW);
digitalWrite(ledC, LOW);
digitalWrite(ledD, LOW);
digitalWrite(ledE, LOW);
digitalWrite(ledF, LOW);
digitalWrite(ledG, LOW);
digitalWrite(ledAA, LOW);
digitalWrite(ledBB, LOW);
digitalWrite(ledCC, LOW);
digitalWrite(ledDD, LOW);
digitalWrite(ledEE, LOW);
digitalWrite(ledFF, LOW);
digitalWrite(ledGG, LOW);

}
void zero() {
digitalWrite(ledA, HIGH);
digitalWrite(ledB, HIGH);
digitalWrite(ledD, HIGH);
digitalWrite(ledE, HIGH);
digitalWrite(ledF, HIGH);
digitalWrite(ledG, HIGH);
digitalWrite(ledAA, HIGH);
digitalWrite(ledBB, HIGH);
digitalWrite(ledDD, HIGH);
digitalWrite(ledEE, HIGH);
digitalWrite(ledFF, HIGH);
digitalWrite(ledGG, HIGH);

}
void number1() {
digitalWrite(ledA, HIGH);
digitalWrite(ledB, HIGH);
digitalWrite(ledD, HIGH);
digitalWrite(ledE, HIGH);
digitalWrite(ledF, HIGH);
digitalWrite(ledG, HIGH);
digitalWrite(ledBB, HIGH);
digitalWrite(ledEE, HIGH);

}
void number2() {
digitalWrite(ledA, HIGH);
digitalWrite(ledB, HIGH);
digitalWrite(ledD, HIGH);
digitalWrite(ledE, HIGH);
digitalWrite(ledF, HIGH);
digitalWrite(ledG, HIGH);
digitalWrite(ledAA, HIGH);
digitalWrite(ledBB, HIGH);
digitalWrite(ledCC, HIGH);
digitalWrite(ledFF, HIGH);
digitalWrite(ledGG, HIGH); }
void number3() {
digitalWrite(ledA, HIGH);
digitalWrite(ledB, HIGH);
digitalWrite(ledD, HIGH);
digitalWrite(ledE, HIGH);
digitalWrite(ledF, HIGH);
digitalWrite(ledG, HIGH);
digitalWrite(ledAA, HIGH);
digitalWrite(ledBB, HIGH);
digitalWrite(ledCC, HIGH);
digitalWrite(ledEE, HIGH);
digitalWrite(ledFF, HIGH);

}
void number4() {
digitalWrite(ledA, HIGH);
digitalWrite(ledB, HIGH);
digitalWrite(ledD, HIGH);
digitalWrite(ledE, HIGH);
digitalWrite(ledF, HIGH);
digitalWrite(ledG, HIGH);
digitalWrite(ledCC, HIGH);
digitalWrite(ledBB, HIGH);
digitalWrite(ledDD, HIGH);
digitalWrite(ledEE, HIGH);
}
void number5() {
digitalWrite(ledA, HIGH);
digitalWrite(ledB, HIGH);
digitalWrite(ledD, HIGH);
digitalWrite(ledE, HIGH);
digitalWrite(ledF, HIGH);
digitalWrite(ledG, HIGH);
digitalWrite(ledAA, HIGH);
digitalWrite(ledCC, HIGH);
digitalWrite(ledDD, HIGH);
digitalWrite(ledEE, HIGH);
digitalWrite(ledFF, HIGH);
}
void number6() {
digitalWrite(ledA, HIGH);
digitalWrite(ledB, HIGH);
digitalWrite(ledD, HIGH);
digitalWrite(ledE, HIGH);
digitalWrite(ledF, HIGH);
digitalWrite(ledG, HIGH);
digitalWrite(ledAA, HIGH);
digitalWrite(ledCC, HIGH);
digitalWrite(ledDD, HIGH);
digitalWrite(ledEE, HIGH);
digitalWrite(ledFF, HIGH);
digitalWrite(ledGG, HIGH);
}
void number7() {
digitalWrite(ledA, HIGH);
digitalWrite(ledB, HIGH);
digitalWrite(ledD, HIGH);
digitalWrite(ledE, HIGH);
digitalWrite(ledF, HIGH);
digitalWrite(ledG, HIGH);
digitalWrite(ledAA, HIGH);
digitalWrite(ledBB, HIGH);
digitalWrite(ledEE, HIGH);
}
void number8() {
digitalWrite(ledA, HIGH);
digitalWrite(ledB, HIGH);
digitalWrite(ledD, HIGH);
digitalWrite(ledE, HIGH);
digitalWrite(ledF, HIGH);
digitalWrite(ledG, HIGH);
digitalWrite(ledAA, HIGH);
digitalWrite(ledBB, HIGH);
digitalWrite(ledCC, HIGH);
digitalWrite(ledDD, HIGH);
digitalWrite(ledEE, HIGH);
digitalWrite(ledFF, HIGH);
digitalWrite(ledGG, HIGH);
}
void number9() {
digitalWrite(ledA, HIGH);
digitalWrite(ledB, HIGH);
digitalWrite(ledD, HIGH);
digitalWrite(ledE, HIGH);
digitalWrite(ledF, HIGH);
digitalWrite(ledG, HIGH);
digitalWrite(ledAA, HIGH);
digitalWrite(ledBB, HIGH);
digitalWrite(ledCC, HIGH);
digitalWrite(ledDD, HIGH);
digitalWrite(ledEE, HIGH);
digitalWrite(ledFF, HIGH);
}
void number10() {
digitalWrite(ledB, HIGH);
digitalWrite(ledE, HIGH);
digitalWrite(ledAA, HIGH);
digitalWrite(ledBB, HIGH);
digitalWrite(ledDD, HIGH);
digitalWrite(ledEE, HIGH);
digitalWrite(ledFF, HIGH);
digitalWrite(ledGG, HIGH);
}

## Recommendations

26 3.6K
92 18K
31 8.5K
Photography Class

17,832 Enrolled