Simple Photon Ping Sensor (HC-SR04)

Simple Ping Sensor (HC-SR04)

Uses new pulseIn() function

available in v0.4.7 firmware
https://docs.particle.io/reference/firmware/photon/#pulsein-

Code (Photon/Core/P1/Electron Compatible)

(see below)

Connections

Particle ~ HC-SR04
     GND - GND
     VIN - VCC
      D2 - TRIG
      D6 - ECHO

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

Connect it to the Internet using:

Particle.variable()

Particle.publish()

Webhooks

The end! :smile: :particle:

Wat… you want a video also?? ok… :grin:

Code: Particle Ping


Code: Particle-Ping-Sound


Code: Particle Internet Button Police Lights

And one more video because these lights are awexome

10 Likes

Using this for an 11th-hour* Halloween project. Thanks for this!

* (No, not the sequel to the 7th Guest, though the time of year is appropriate)

1 Like

Cool! :slight_smile: I was planning to hook this up to my smoke machine tomorrow morning… and maybe point it up so it only smokes up the front porch for taller trick or treaters :jack_o_lantern: :ghost:

I’ll be using this to interact with a string of NeoPixels, NeoPixels in pumpkins, and a servo. >=)

Is it possible that an extra cap across VCC and GND on the sensor might improve stability? I can also hear a slight buzz in the speakers. It’s sharing voltage with 100 NeoPixels. It’s a 2A USB power supply plugged into a barrel connector (on my custom PCB) with filter caps. The current draw on the NeoPixels at full red brightness (I only need that color for Halloween!) and Photon is 1.4A, so there’s plenty of room to spare. The voltage goes from 5.0v even down to 4.92v with all the pixels on.

I’m calculating a rotating average of 50 pings with about 10ms between pings (plus the delays already in the ping function you shared). In the span of 1 second (which is how often I’m publishing), the average distance can go from 40cm to 100cm+. That seems a little wild. The sensor is currently 1-2" from the floor, but it’s angled upwards. Maybe the floor is still within the 15° angle. I’ll try raising it up onto a table and leveling it off tomorrow.

It seems to stay pretty stable below 50cm, though. At least that’s a safe threshold that doesn’t “bounce” so much. It’s kind of cool to watch the NeoPixels follow my hand or foot as it moves closer and farther away! In the meantime, I’ll let it run all night and track the averaged ping distances every second, only because I can!

1 Like

Well, poop. When I was moving the sensor, it came unplugged. When I plugged it back in, I apparently swapped the trigger and echo pins. That made it get really hot, really fast. Now the max distance I can measure is ~40cm. :cry:

Bummer! The sensor likes hard flat surfaces… and typically distances beyond 100cm or so are pretty iffy. Soft round surfaces not so good, but do work. What are you using for graphs (google?), looks nice :smile:

It's StatsD + Graphite. I built my own Docker container that allows for quick and easy deployment. And then I have a Node.JS script that listens to my Particle event stream for "statsd" and pushes those metrics into StatsD. It takes maybe 5 whole minutes to spin up a new deployment of all 3 of those. MAGIC! :crystal_ball:

2 Likes

This may be a huge ask, but is it possible to time much shorter events in higher resolution? i want to make an underwater current sensor, measuring time of flight between 3 ultrasonic sensors at about 5cm apart (in a triangle) and interpolate the difference to determine the direction and speed of the current.

but from some quick calculations i need to measure about 33us but at a higher resolution.

some basic maths
speed of sound in seawater = 1500m/s
space between sensors =0.05m
0.05/150010001000=33.33333us

so say there was a 5m/s current running directly from sensor 1 to 2 (full scale example), speed would change to 1505, so 33.222 from the equation above. thats a difference of just 110ns at full scale, so to have any sort of resolution on the sensor i would need to be able to read in nanosecond increments…

am i dreaming? is my maths right? is there any ic to do this?

@Hootie81 if you take the pulsein function and replicate it in your user app, if you do not divide the returned result by SYSTEM_US_TICKS (120 on the Photon and 72 on the Core), you’ll get the highest timing resolution available on both devices.

Also you can try using interrupts for greater accuracy: