Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Particle Photon/Core/P1/Electron Ping PulseIn Example (HC-SR04)
/*
******************************************************************************
* Copyright (c) 2015 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************
*/
/* HC-SR04 Ping / Range finder wiring:
* -----------------------------------
* Particle - HC-SR04
* GND - GND
* VIN - VCC
* D2 - TRIG
* D6 - ECHO
*/
#include "application.h"
void setup() {
Serial.begin(115200);
}
void loop() {
// Trigger pin, Echo pin, delay (ms), visual=true|info=false
ping(D2, D6, 20, true);
}
void ping(pin_t trig_pin, pin_t echo_pin, uint32_t wait, bool info)
{
uint32_t duration, inches, cm;
static bool init = false;
if (!init) {
pinMode(trig_pin, OUTPUT);
digitalWriteFast(trig_pin, LOW);
pinMode(echo_pin, INPUT);
delay(50);
init = true;
}
/* Trigger the sensor by sending a HIGH pulse of 10 or more microseconds */
digitalWriteFast(trig_pin, HIGH);
delayMicroseconds(10);
digitalWriteFast(trig_pin, LOW);
duration = pulseIn(echo_pin, HIGH);
/* Convert the time into a distance */
// Sound travels at 1130 ft/s (73.746 us/inch)
// or 340 m/s (29 us/cm), out and back so divide by 2
// Ref: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
inches = duration / 74 / 2;
cm = duration / 29 / 2;
if (info) { /* Visual Output */
Serial.printf("%2d:", inches);
for(int x=0;x<inches;x++) Serial.print("#");
Serial.println();
} else { /* Informational Output */
Serial.printlnf("%6d in / %6d cm / %6d us", inches, cm, duration);
}
delay(wait); // slow down the output
}
@technobly

This comment has been minimized.

Copy link
Owner Author

@technobly technobly commented Oct 21, 2015

Visual Output

 2:##
 3:###
 3:###
 3:###
 3:###
 4:####
 5:#####
 5:#####
 6:######
 7:#######
 8:########
 8:########
10:##########
10:##########
11:###########
12:############
12:############
12:############
12:############
12:############
11:###########
11:###########
10:##########
 9:#########
 9:#########
 8:########
 7:#######
 6:######
 5:#####
 5:#####
 4:####
 3:###
 3:###
 3:###
 2:##
 2:##
 3:###
 3:###
 3:###
 4:####
 5:#####
 5:#####
 6:######
 7:#######
 8:########
 8:########
 9:#########
10:##########
11:###########
12:############
12:############
12:############
12:############
12:############
12:############
11:###########
10:##########
10:##########
 9:#########
 8:########
 7:#######
 6:######
 6:######
 5:#####
 4:####
 4:####
 3:###
 3:###
 2:##
 2:##
 2:##
 3:###
 3:###
 3:###
 4:####
 4:####
 5:#####
 6:######
 7:#######
 8:########
 9:#########
 9:#########
10:##########
11:###########
11:###########
12:############
12:############
12:############
12:############
11:###########
10:##########
10:##########
 9:#########

Informational Output

     3 in /      8 cm /    478 us
     3 in /      7 cm /    460 us
     2 in /      7 cm /    424 us
     3 in /      8 cm /    472 us
     3 in /      8 cm /    502 us
     4 in /     10 cm /    602 us
     5 in /     12 cm /    746 us
    20 in /     52 cm /   3025 us
    20 in /     51 cm /   2974 us
    19 in /     50 cm /   2949 us
    20 in /     52 cm /   3019 us
     6 in /     17 cm /   1011 us
     7 in /     19 cm /   1128 us
     2 in /      6 cm /    364 us
     1 in /      4 cm /    279 us
     2 in /      5 cm /    331 us
     2 in /      7 cm /    430 us
    18 in /     48 cm /   2809 us
    19 in /     50 cm /   2902 us
     2 in /      7 cm /    412 us
     2 in /      7 cm /    436 us
     2 in /      7 cm /    421 us
     2 in /      7 cm /    433 us
     2 in /      5 cm /    340 us
     3 in /      9 cm /    556 us
    19 in /     48 cm /   2823 us
    19 in /     49 cm /   2895 us
    19 in /     50 cm /   2923 us
    19 in /     50 cm /   2943 us
    19 in /     49 cm /   2847 us
    19 in /     50 cm /   2920 us
     2 in /      5 cm /    316 us
     1 in /      4 cm /    282 us
     1 in /      5 cm /    295 us
     2 in /      6 cm /    376 us
    19 in /     48 cm /   2820 us
    19 in /     50 cm /   2920 us
     2 in /      5 cm /    331 us
     1 in /      4 cm /    237 us
     2 in /      6 cm /    364 us
     1 in /      4 cm /    267 us
     2 in /      5 cm /    304 us
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.