├── .gitattributes ├── .gitignore ├── Firmware └── HC-SR04_UltrasonicSensorExample │ └── HC-SR04_UltrasonicSensorExample.ino ├── LICENSE.md └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## SparkFun Useful stuff 3 | ################# 4 | 5 | ## AVR Development 6 | *.eep 7 | *.elf 8 | *.lst 9 | *.lss 10 | *.sym 11 | *.d 12 | *.o 13 | *.srec 14 | *.map 15 | 16 | ## Notepad++ backup files 17 | *.bak 18 | 19 | ## BOM files 20 | *bom* 21 | 22 | ################# 23 | ## Eclipse 24 | ################# 25 | 26 | *.pydevproject 27 | .project 28 | .metadata 29 | bin/ 30 | tmp/ 31 | *.tmp 32 | *.bak 33 | *.swp 34 | *~.nib 35 | local.properties 36 | .classpath 37 | .settings/ 38 | .loadpath 39 | 40 | # External tool builders 41 | .externalToolBuilders/ 42 | 43 | # Locally stored "Eclipse launch configurations" 44 | *.launch 45 | 46 | # CDT-specific 47 | .cproject 48 | 49 | # PDT-specific 50 | .buildpath 51 | 52 | 53 | ############# 54 | ## Eagle 55 | ############# 56 | 57 | # Ignore the board and schematic backup files and lock files 58 | *.b#? 59 | *.s#? 60 | *.l#? 61 | *.lck 62 | 63 | 64 | ################# 65 | ## Visual Studio 66 | ################# 67 | 68 | ## Ignore Visual Studio temporary files, build results, and 69 | ## files generated by popular Visual Studio add-ons. 70 | 71 | # User-specific files 72 | *.suo 73 | *.user 74 | *.sln.docstates 75 | 76 | # Build results 77 | [Dd]ebug/ 78 | [Rr]elease/ 79 | *_i.c 80 | *_p.c 81 | *.ilk 82 | *.meta 83 | *.obj 84 | *.pch 85 | *.pdb 86 | *.pgc 87 | *.pgd 88 | *.rsp 89 | *.sbr 90 | *.tlb 91 | *.tli 92 | *.tlh 93 | *.tmp 94 | *.vspscc 95 | .builds 96 | *.dotCover 97 | 98 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 99 | #packages/ 100 | 101 | # Visual C++ cache files 102 | ipch/ 103 | *.aps 104 | *.ncb 105 | *.opensdf 106 | *.sdf 107 | 108 | # Visual Studio profiler 109 | *.psess 110 | *.vsp 111 | 112 | # ReSharper is a .NET coding add-in 113 | _ReSharper* 114 | 115 | # Installshield output folder 116 | [Ee]xpress 117 | 118 | # DocProject is a documentation generator add-in 119 | DocProject/buildhelp/ 120 | DocProject/Help/*.HxT 121 | DocProject/Help/*.HxC 122 | DocProject/Help/*.hhc 123 | DocProject/Help/*.hhk 124 | DocProject/Help/*.hhp 125 | DocProject/Help/Html2 126 | DocProject/Help/html 127 | 128 | # Click-Once directory 129 | publish 130 | 131 | # Others 132 | [Bb]in 133 | [Oo]bj 134 | sql 135 | TestResults 136 | *.Cache 137 | ClientBin 138 | stylecop.* 139 | ~$* 140 | *.dbmdl 141 | Generated_Code #added for RIA/Silverlight projects 142 | 143 | # Backup & report files from converting an old project file to a newer 144 | # Visual Studio version. Backup files are not needed, because we have git ;-) 145 | _UpgradeReport_Files/ 146 | Backup*/ 147 | UpgradeLog*.XML 148 | 149 | 150 | ############ 151 | ## Windows 152 | ############ 153 | 154 | # Windows image file caches 155 | Thumbs.db 156 | 157 | # Folder config file 158 | Desktop.ini 159 | 160 | 161 | ############# 162 | ## Mac OS 163 | ############# 164 | 165 | .DS_Store 166 | 167 | 168 | ############# 169 | ## Linux 170 | ############# 171 | 172 | # backup files (*.bak on Win) 173 | *~ 174 | 175 | 176 | ############# 177 | ## Python 178 | ############# 179 | 180 | *.py[co] 181 | 182 | # Packages 183 | *.egg 184 | *.egg-info 185 | dist 186 | build 187 | eggs 188 | parts 189 | bin 190 | var 191 | sdist 192 | develop-eggs 193 | .installed.cfg 194 | 195 | # Installer logs 196 | pip-log.txt 197 | 198 | # Unit test / coverage reports 199 | .coverage 200 | .tox 201 | 202 | #Translations 203 | *.mo 204 | 205 | #Mr Developer 206 | .mr.developer.cfg 207 | -------------------------------------------------------------------------------- /Firmware/HC-SR04_UltrasonicSensorExample/HC-SR04_UltrasonicSensorExample.ino: -------------------------------------------------------------------------------- 1 | /** 2 | HC-SR04 Demo 3 | Demonstration of the HC-SR04 Ultrasonic Sensor 4 | Date: August 3, 2016 5 | 6 | Description: 7 | Connect the ultrasonic sensor to the Arduino as per the 8 | hardware connections below. Run the sketch and open a serial 9 | monitor. The distance read from the sensor will be displayed 10 | in centimeters and inches. 11 | 12 | Hardware Connections: 13 | Arduino | HC-SR04 14 | ------------------- 15 | 5V | VCC 16 | 7 | Trig 17 | 8 | Echo 18 | GND | GND 19 | 20 | License: 21 | Public Domain 22 | */ 23 | 24 | // Pins 25 | const int TRIG_PIN = 7; 26 | const int ECHO_PIN = 8; 27 | 28 | // Anything over 400 cm (23200 us pulse) is "out of range" 29 | const unsigned int MAX_DIST = 23200; 30 | 31 | void setup() { 32 | 33 | // The Trigger pin will tell the sensor to range find 34 | pinMode(TRIG_PIN, OUTPUT); 35 | digitalWrite(TRIG_PIN, LOW); 36 | 37 | //Set Echo pin as input to measure the duration of 38 | //pulses coming back from the distance sensor 39 | pinMode(ECHO_PIN, INPUT); 40 | 41 | // We'll use the serial monitor to view the sensor output 42 | Serial.begin(9600); 43 | } 44 | 45 | void loop() { 46 | 47 | unsigned long t1; 48 | unsigned long t2; 49 | unsigned long pulse_width; 50 | float cm; 51 | float inches; 52 | 53 | // Hold the trigger pin high for at least 10 us 54 | digitalWrite(TRIG_PIN, HIGH); 55 | delayMicroseconds(10); 56 | digitalWrite(TRIG_PIN, LOW); 57 | 58 | // Wait for pulse on echo pin 59 | while ( digitalRead(ECHO_PIN) == 0 ); 60 | 61 | // Measure how long the echo pin was held high (pulse width) 62 | // Note: the micros() counter will overflow after ~70 min 63 | t1 = micros(); 64 | while ( digitalRead(ECHO_PIN) == 1); 65 | t2 = micros(); 66 | pulse_width = t2 - t1; 67 | 68 | // Calculate distance in centimeters and inches. The constants 69 | // are found in the datasheet, and calculated from the assumed speed 70 | //of sound in air at sea level (~340 m/s). 71 | cm = pulse_width / 58.0; 72 | inches = pulse_width / 148.0; 73 | 74 | // Print out results 75 | if ( pulse_width > MAX_DIST ) { 76 | Serial.println("Out of range"); 77 | } else { 78 | Serial.print(cm); 79 | Serial.print(" cm \t"); 80 | Serial.print(inches); 81 | Serial.println(" in"); 82 | } 83 | 84 | // Wait at least 60ms before next measurement 85 | delay(60); 86 | } 87 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | SparkFun License Information 2 | ============================ 3 | 4 | SparkFun uses two different licenses for our files — one for hardware and one for code. 5 | 6 | Hardware 7 | --------- 8 | 9 | **SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).** 10 | 11 | Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode). 12 | 13 | You are free to: 14 | 15 | Share — copy and redistribute the material in any medium or format 16 | Adapt — remix, transform, and build upon the material 17 | for any purpose, even commercially. 18 | The licensor cannot revoke these freedoms as long as you follow the license terms. 19 | Under the following terms: 20 | 21 | Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. 22 | ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. 23 | No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. 24 | Notices: 25 | 26 | You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. 27 | No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. 28 | 29 | 30 | Code 31 | -------- 32 | 33 | **SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).** 34 | 35 | The MIT License (MIT) 36 | 37 | Copyright (c) 2016 SparkFun Electronics 38 | 39 | Permission is hereby granted, free of charge, to any person obtaining a copy 40 | of this software and associated documentation files (the "Software"), to deal 41 | in the Software without restriction, including without limitation the rights 42 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 43 | copies of the Software, and to permit persons to whom the Software is 44 | furnished to do so, subject to the following conditions: 45 | 46 | The above copyright notice and this permission notice shall be included in all 47 | copies or substantial portions of the Software. 48 | 49 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 50 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 51 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 52 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 53 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 54 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 55 | SOFTWARE. 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Ultrasonic Sensor - HC-SR04 2 | ======================================== 3 | 4 | [![Ultrasonic Sensor - HC-SR04](https://cdn.sparkfun.com//assets/parts/1/3/5/0/8/15569-Ultrasonic_Distance_Sensor_-_HC-SR04-01a.jpg)](https://www.sparkfun.com/products/15569)) 5 | 6 | [*Ultrasonic Sensor HC-SR04 (SEN-15569)*](https://www.sparkfun.com/products/15569) 7 | 8 | This is the HC-SR04 ultrasonic ranging sensor. This economical sensor provides 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. 9 | 10 | There are only four pins that you need to worry about on the HC-SR04: VCC (Power), Trig (Trigger), Echo (Receive), and GND (Ground). 11 | 12 | [*HC-SR04 data sheet*](https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf) 13 | 14 | 15 | 16 | Repository Contents 17 | ------------------- 18 | 19 | * **/Firmware** - Arduino example code. Make sure to check the pin definitions and what you are connecting to. 20 | 21 | Documentation 22 | -------------- 23 | 24 | * **[Hookup Guide](https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v40/circuit-3b-distance-sensor)** - Basic hookup and project example using the ultrasonic sensor. _(Note: The example code used in this tutorial is slightly different than the basic example used in this GitHub repo. Make sure to check your pin definitions if you are just using just the ultrasonic sensor.)_ 25 | 26 | Product Versions 27 | ---------------- 28 | * [SEN-15569](https://www.sparkfun.com/products/15569)- Blue ultrasonic sensor w/ crystal 29 | * [SEN-13959](https://www.sparkfun.com/products/13959)- Red ultrasonic sensor w/out crystal 30 | 31 | License Information 32 | ------------------- 33 | 34 | This product is _**open source**_! 35 | 36 | Please review the LICENSE.md file for license information. 37 | 38 | If you have any questions or concerns on licensing, please contact techsupport@sparkfun.com. 39 | 40 | Distributed as-is; no warranty is given. 41 | 42 | - Your friends at SparkFun. 43 | 44 | __ 45 | --------------------------------------------------------------------------------