Distance sensor

Create new circuit and call it "Distance sensor".

Add components:

  • Arduino Uno R3 board
  • Diode
  • Resistor
  • Ultrasonic Distance sensor
  • Breadboard

What is an Ultrasonic Distance sensor?.

An ultrasonic sensor is an electronic device that measures the distance of a target object by emitting ultrasonic sound waves, and converts the reflected sound into an electrical signal. Ultrasonic waves travel faster than the speed of audible sound (i.e. the sound that humans can hear). Ultrasonic sensors have two main components: the transmitter (which emits the sound using piezoelectric crystals) and the receiver (which encounters the sound after it has travelled to and from the target).
In order to calculate the distance between the sensor and the object, the sensor measures the time it takes between the emission of the sound by the transmitter to its contact with the receiver. The formula for this calculation is D = ½ T x C (where D is the distance, T is the time, and C is the speed of sound ~ 343 meters/second).

Breadbord or protoboard, is a construction base for prototyping of electronics. Originally the word referred to a literal bread board, a polished piece of wood used when slicing bread. In the 1970s the solderless breadboard (a.k.a. plugboard, a terminal array board) became available and nowadays the term "breadboard" is commonly used to refer to these. 

The breadboard has strips of metal underneath the board and connect the holes on the top of the board. The metal strips are laid out as shown below. Note that the top and bottom rows of holes are connected horizontally and split in the middle while the remaining holes are connected vertically.

Connect components:

This time we will use breadboard 

  • Connect diode, resistor and Ultrasonic distance sensor to breadbord;
  • Connect diode cathode to breadboard "-";
  • Diode anode connect to resistor;
  • Resistor connect to digital pin 13;
  • Ultrasonic distance sensor GND connect to breadboard "-";
  • Ultrasonic distance sensor 5V connect to breadboard "+";
  • Ultrasonic distance sensor Signal connect to digital pin 7;
  • Breadboard "-" connect to Arduino Uno R3 GND;
  • Breadboard "+" connect to Aruino Uno R3 5V;


Code:
We'll go through code line by line:
1. Define variable for diode pin - digital pin 13;
3. Define variable centimeters;
5. Define function "readUltrasonicDistance(int triggerPin, int echoPin)";
6. Open function "readUltrasonicDistance(int triggerPin, int echoPin)";
8., 9., 10. Clear the trigger;
14., 15. Sets the trigger pin to HIGH state for 10 microseconds;
16. Sets the trigger pin LOW;
17. Define echoPin as INPUT;
20. Reads the echo pin, and returns the sound wave travel time in microseconds;
21. Close function "readUltrasonicDistance(int triggerPin, int echoPin)";
23. Define function "setup()";
24. Open function "setup()";
25. Define serial ;
26. Define diode pin as output;
27. Close function "setup()";
29. Define function "loop()";
30. Open function "loop()";
33. Measure the ping time in cm;
35. Send variable to serial monitor;
36. Send text to serial monitor;
38. Wait for 100 milliseconds;
40. If statement for diode (if ultrasonic distance sensor measure more than 100 cm, turn on diode) ;
41. Open if statement;
42. Tur diode on;
43. Close if statement;
44. Close function "loop()".



Simulation:
Start simulation, pay attention to diode.  Open Serial Monitor, what is the distance?