User Tools

Site Tools


measuring_distance_with_arduino_and_ultrasound

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
measuring_distance_with_arduino_and_ultrasound [2017/08/29 03:12]
glassgiant [The Completed Circuit]
measuring_distance_with_arduino_and_ultrasound [2017/09/23 21:16] (current)
glassgiant [Coding]
Line 10: Line 10:
 {{ :workshops:ultrasound:ultrasound_light_bb.png?300|}} {{ :workshops:ultrasound:ultrasound_light_bb.png?300|}}
 ===== About the SR-04 ===== ===== About the SR-04 =====
-There are four pins: //Vcc// and //GND//, which supply power to the module, //trigger//, which is used to send an ultrasonic pulse when brought high by the Arduino, and //echo//, which is used to receive the echo of the pulse.  The time it takes for the emitted pulse to echo back to the module is what's used to calculate the distance to the object or person.  The formula is //distance// = //echo time// x //speed of sound// / 2 The SR-04 can measure distances from 2cm to about 400cm.+There are four pins: //Vcc// and //GND//, which supply power to the module, //trigger//, which is used to send an ultrasonic pulse when brought high by the Arduino, and //echo//, which is used to receive the echo of the pulse.  The time it takes for the emitted pulse to echo back to the module is what's used to calculate the distance to the object or person. The SR-04 can measure distances from 2cm to about 400cm optimally, but in most cases, using the 5V supply from the Arduino, you can measure from about 4cm to 120cm. 
 + 
 +The pulse is a 40kHz audio tone, which is too high a frequency for humans to hear.  We'll be sending a short pulse of 20ms (one fiftieth of a second) every quarter of a second or so. When the receiver hears a 40kHz tone, it will set the echo pin high. 
 + 
 +The formula for calculating distance is //time// x //speed of sound//And because that measures the distance from the emitter to the object and back, we will divide it by two to get the distance to the object We are using 29.1 as the speed of sound, though obviously this is varies depending on air pressure and temperature. 
 + 
 +So, if we have an object in front of our sensor, and it takes 5ms for our pulse to bounce off it and be detected by our sensor, we get the following formula, where distance is in cm, time in ms and speed of sound is cm per ms: 
 + 
 +''distance = time x speed of sound / 2'' 
 + 
 +''distance = 5 x 29.1 / 2'' 
 + 
 +''distance = 73 cm''
  
 ===== Wiring the SR-04 ===== ===== Wiring the SR-04 =====
Line 23: Line 35:
  
 ===== Wiring the Common Anode RGB LED ===== ===== Wiring the Common Anode RGB LED =====
-  * Plug the LED into the breadboard so the pins are on different numbered rows+  * Plug the LED into the breadboard so the pins are on different numbered rows.  It's helpful to plug the longest pin, the common anode, into a different lettered column (not shown on the diagram).  This alleviates the issue of the long lead making it difficult to seat the shorter leads in the breadboard, and gives you a visual indication of which way the LED is oriented.
   * Use a red jumper between the common anode pin (the longest pin) and the power bus   * Use a red jumper between the common anode pin (the longest pin) and the power bus
   * For each of the remaining pins, plug a resistor to connect the row the pin is in to the same row, but the other side of the breadboard, as shown in the diagram.   * For each of the remaining pins, plug a resistor to connect the row the pin is in to the same row, but the other side of the breadboard, as shown in the diagram.
-  * Use a red, green, and blue jumper to connect the end of resistors opposite the LED to Arduino pins 910 and 11.  It doesn't really matter which is which for now.+  * Use a red, green, and blue jumper to connect the end of resistors opposite the LED to Arduino pins 6, 5, and 3.  It doesn't really matter which is which for now.
  
 ===== Coding ===== ===== Coding =====
-The code for this workshop is available [[https://pastebin.com/TDSHFUZB|here]].+The code for this lesson is available {{:workshops:ultrasound:arduinodistance.txt|here}}.
  
 ===== The Completed Circuit ===== ===== The Completed Circuit =====
 +This example shows an LED changing from white to red when someone gets within about 60cm of it, but the outputs could be easily changed to be more LEDS, motors, servos, sound circuits, etc.
 +
 {{:workshops:ultrasound:2017-08-28_23.43.12.jpg?300|}} {{:workshops:ultrasound:2017-08-28_23.43.12.jpg?300|}}
 {{:workshops:ultrasound:2017-08-28_23.43.17.jpg?300|}} {{:workshops:ultrasound:2017-08-28_23.43.17.jpg?300|}}
measuring_distance_with_arduino_and_ultrasound.1503976351.txt.gz · Last modified: 2017/08/29 03:12 by glassgiant