Things used in this project

Hardware components:
Esquilo%20top%20small
Esquilo Air
×1
51gzz5eu9pl. sx425
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Kingbright wp7113srd d image
5 mm LED: Red
×1
Kingbright wp7113sgd
5 mm LED: Green
×1
Mfr 25fbf52 221r sml
Resistor 221 ohm
×2
12002 04
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

Schematics

Esquilo HC-SR04 wiring diagram
Suggested wiring diagram
38afwbomcke8j3rqh8xk

Code

Esquilo HC-SR04 sample codePlain 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

27eae0443d051e9e246159707c1f2203
Leeland Heins
4 projects • 0 followers
Hardware & Software H4x0r
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog