Leeland Heins
Published © GPL3+

Esquilo Air & HC-SR04 Ultrasonic Sensor Tutorial

This is a very simple example setup to show how to hook an HC-SR04 Ultrasonic sensor up to an Esquilo Air.

EasyProtip1 hour197
Esquilo Air & HC-SR04 Ultrasonic Sensor Tutorial

Things used in this project

Hardware components

Esquilo Air
Esquilo Air
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Green
5 mm LED: Green
×1
Resistor 221 ohm
Resistor 221 ohm
×2
Breadboard (generic)
Breadboard (generic)
×1
Male/Male Jumper Wires
It is nice if you pick color coordinated ones. I used: 2 black, 2 red, 1 blue, 1 green, 1 yellow, 1 orange
×8

Story

Read more

Schematics

Esquilo HC-SR04 wiring diagram

Suggested wiring diagram
38afwbomcke8j3rqh8xk

Code

Esquilo HC-SR04 sample code

Plain text
Squirrel nut for using the HC-SR04 with an Esquilo
/*
 * 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.
 */

require("GPIO");
require("Capture");

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.
greenLed.output();

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

local duration;
local distance;

while (true) {
    trigPin.low();  // Pulse the trigger off.
    delay(2);
    trigPin.high();  // Pulse the trigger on.
    delay(10);
    trigPin.low();  // Pulse the trigger off.
    echoPin.arm(0, CAPTURE_EDGE_RISING);
    duration = echoPin.read(0);
    //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.
}

Credits

Leeland Heins

Leeland Heins

4 projects • 0 followers
Hardware & Software H4x0r

Comments