An archive of as of Saturday January 26, 2019.

Esquilo Air and HC-SR04 Ultrasonic Sensor example


Simple Esquilo Air and HC-SR04 Example

This is a very simple example setup to show how to hook this sensor up to an
Esquilo Air. If you aren't familiar with it, this part is very common and
uses Sonar (like a Bat) to find distance to an object. It has a sender and
a transmitter which sends out a small ultrasonic pulse which bounces off of
an object and back to the sensor. Because of how this works please take into
consideration that small objects and those that don't reflect sound (like
cloth or soft fuzzy things) may not work well with this sensor. It works
best with hard and reflective items like glass, metal and most plastics. If
you search around you can find that several vendors sell the HC-SR04 sensors
and if you price shop you can sometimes find them under $2 each, although
watch out for shipping times and charges as many times the very inexpensive
vendors ship from China instead of the US.

Step 1: Parts List

1 Esquilo Air (the one in the pics is in a holder I found online)
1 HC-SR04 Ultrasonic Sensor
1 Red LED
1 Green LED
2 200 Ohm resistors (actually anything between 200 to 800 Ohms should work).
1 Half width Breadboard
8 Male-Male hookup wires. It is nice if you pick color coordinated ones.
I used:
2 black
2 red
1 blue
1 green
1 yellow
1 orange

Step 2: Connect the components

Connect the components and wires as shown in the pictures.

Step 3: Load the Squirrel code into the IDE

 * This is a demo of the HC-SR04 Ping distance sensor
 * connected to the Esquilo Air.
 * VCC to Esquilo 5V  (red)
 * GND to Esquilo GND  (black)
 * Echo to Esquilo 2 PWM0 CH0  (green)
 * Trig to Esquilo pin 13  (blue)
 * Red LED+ to Esquilo pin 11  (orange)
 * Green LED+ to Esquilo pin 10  (yellow)
 * 200 Ohm resistor to both LED- and GND
 * This code and wiring hookup was inspired by an Arduino sketch I found
 * online.


trigPin <- GPIO(13);  // Use digital pin 13 for the trigger.
echoPin <- Capture(0);  // PWM0 input.

trigPin.output();  // Set the trigger to output.

redLed <- GPIO(11);  // Set the pins to use for the LEDs.
greenLed <- GPIO(10);

redLed.output();  // Set both LEDs to output.

redLed.low();  // Turn off both LEDs.

local duration;
local distance;

while (true) {
    trigPin.low();  // Pulse the trigger off.
    trigPin.high();  // Pulse the trigger on.
    trigPin.low();  // Pulse the trigger off.
    echoPin.arm(0, CAPTURE_EDGE_RISING);
    duration =;
    //print("duration=" + duration);
    distance = (duration / 2) / 29.1;  // This may need to be adjusted for accurate distances.
    //print("distance=" + distance);
    if (distance < 4) {
        redLed.high();  // Turn on the red LED.
        greenLed.low();  // Turn off the green LED.
    } else {
        redLed.low();  // Turn off the red LED.
        greenLed.high();  // Turn on the green LED.
    delay(500);  // Delay 1/2 second before checking again.

Step 4: Run the nut


I'm interested to know if anyone found this interesting and/or useful. I'm also open to feedback about formatting, etc. Would anyone like to see more tutorials like this one?


I think it's really interesting. Useful with nice clean code and a good wiring diagram. Thanks a lot for sharing it!

If you're up to it, this would port to our platform page pretty easily from what you have (


Thanks Patrick!

I did the wiring diagram in Fritzing and used the Esquilo part that I found here on the community. I had to search on the Internet to find the part for the HC-SR04 sensor as it is not in the default Fritzing part libraries either. I had a little learning curve with Fritzing but I've got it figured out enough to get some simple diagrams out of it.

I will try to get the tutorial pushed over to hackster as well, that's a good idea.

I just finished porting the Arduino LiquidCrystal library (for support of 1602 type 16x2 LCD displays) this morning. There was already a partial port on github but it only supported the versions of the 1602 LCDs that have the SPI add-on board. The one that i have does not have that and requires 6 GPIO pins to run parallel so I did a more complete port of the whole library. I've also ported a couple of the simple LCD demos and I will be combining this with the HC-SR04 to make it display the distance on the LCD instead of just using a couple of LEDs. I'm also thinking about writing a library for the HC-SR04 to simplify its use for other people.

After I get those tutorials done I am thinking I may do some more tutorials using other sensors, controls and output devices I have. I have a variety of temperature, humidity, barometric pressure type sensors, some of which I've seen code for the Esquilo on, others not. I've also got a bunch of other sensors, switches, push-buttons, multi segment LEDs, etc.


It is on hackster now...


I fixed a small bug in the code that I found while I was working on my next project which combines the HC-SR04 sensor code with the 1602A LCD to output the range finder distance on the LCD.


I have ported the Arduino Ultrasonic library to the Esquilo. You can find it here:

I will probably update the demo code here soon to use the library.

I am also working on porting the Arduino NewPing library which is a little more fancy library but that is a lower priority than getting the HC-SR04 & 1602A tutorial finished up so it may be a while before it is finished.


I finally got around to trying this out with the LCD and it worked great.

I noticed the number was in centimeters using a ruler.
In you calculation where did you get the 29.1?

distance = (duration / 2) / 29.1;




It has been quite a while since I did that code, but I believe the constant came from either some of the Arduino sample code or was arrived at by trial and error.


Thanks, no problem, I'll look at some example Arduino code and see what I can find. It seemed pretty close using a ruler.