Categories
Making

ZX and Ultrasonic Sensing

February 16, 2018

After making some advances with audio board selection and programming, it was time to go through my parts supply and explore various sensors to see how they could be employed.

First up was the ZX Distance and Gesture Sensor made by Sparkfun. I have the original version which is now retired. Following the hookup guide, things were easy enough to get going.

This sensor uses infrared (IR) beams to place objects. As Sparkfun note, “The ZX Sensor is a touchless sensor that is capable of looking for simple gestures in the air above the sensor (e.g. swipe left or swipe right). Additionally, the sensor can also recognize the distance of an object away from the sensor at distances up to about 12 inches (30 cm), referred to as the “Z” axis, and the location of the object from side to side across the sensor in about a 6 inch (15 cm) span, referred to as the “X” axis.”

This has some very similar capability to the Sparkfun APDS-9960 RGB and Gesture Sensor, though a key difference is that it requires 3.3 –  5V power, but 5V gives better range according to Sparkfun. Oddly, there is no “down” gesture, which the APDS-9960 does have. I found the response a bit less immediate than I would like for some things, so haven’t worked this into a project yet.

HC-SR04 Ultrasonic Distance Sensor Module

I’ve had the common HC-SR04 sensor sitting around for years, exploring it early in my Arduino learnings. I probably got mine from Adafruit, but I see they no longer carry it. Sparkfun does, and these sensors are pretty easy to find elsewhere, being a staple in the robotics community.

I chose to use a Pro Mini as the main board here because it would mirror the size of the project I have in mind. Setup was fairly easy, though I did have some issues with my library. Eventually I landed on the one from Martinsos on GitHub, though there are others out there that may be more suitable depending on the application.

After basic tests, I wanted to add some LEDs and found a similar idea from Instructables, so integrated that code. From there, I wanted to incorporate my Adafruit FX Sound Board project and trigger it from the ultrasonic sensor. This was actually a dry run for a similar idea I have for a Particle Photon project, but I wanted to have a proof of concept first.

In the pictures there is a little red PCB on an orange breadboard between the two projects. This is because I thought a logic converter would be needed, but in the end it was not. For one thing, the Adafruit FX Sound Board has a built in regulator for the inputs. So there was no benefit to overbuilding with the converter.

I also couldn’t get it to work anyway – though this was probably user error. Everything worked with the LEDs, but as soon as I tried triggering the soundboard, I either saw repeated triggering (bad) or only got the intended results with what I saw as some backwards wiring. After telling my dad about this, he set me straight with a simple transistor approach that did the trick. “Transistor BASE goes to Mini OUTPUT. COLLECTOR goes to sound board input pin. EMITTER goes to ground. Probably both ground of Mini and ground of soundboard should connect together.”

So the projects are now connected, with an object triggering a sound as it passes the sensor. Final code below. In many ways this is still rather overbuilt (compared to Teensy), but could still be fun to use for Halloween 2018.

/*
JJD 2/16/18 HC-R04 with Pro Mini 5V
Uses Library from
https://github.com/Martinsos/arduino-lib-hc-sr04/blob/master/examples/simple/simple.ino


Sensor is initialized by creating instance of class UltraSonicDistanceSensor and providing trigger and echo pins:
UltraSonicDistanceSensor sensor(triggerPin, echoPin);.

Then, to measure the distance, you just call measureDistanceCm(), which will return distance in centimeters (double).
If distance is larger than 400cm, it will return negative value.

LED code inspired by
http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/

*/

#include <HCSR04.h>
#define LED_1 10
#define LED_2 11

// Initialize sensor that uses digital pins 13 and 12.
int triggerPin = 13;
int echoPin = 12;

//UltraSonicDistanceSensor distanceSensor(triggerPin, echoPin); // this line not really needed if declaring INTs above

void setup () {
 Serial.begin(9600); // We initialize serial connection so that we could print values from sensor.
 pinMode (LED_1, OUTPUT);
 pinMode (LED_2, OUTPUT);
}

void loop () {
 long duration, distance;
 digitalWrite(triggerPin, LOW);
 delayMicroseconds(2);
 digitalWrite(triggerPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(triggerPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 if (distance < 6) { // This is where the LED On/Off happens JJD X cm to act as switch for whatever
 digitalWrite(LED_1,HIGH); // When the LED_1 condition is met, the LED_2 LED should turn off
 digitalWrite(LED_2,LOW);
}
 else {
 digitalWrite(LED_1,LOW);
 digitalWrite(LED_2,HIGH);
 }
 if (distance >= 200 || distance <= 0){
 Serial.println("Out of range");
 }
 else {
 Serial.print(distance);
 Serial.println(" cm");
 }
 delay(500);
}
avatar

By jjdeprisco

Sonic explorer, sound artist, guitarist in Fricknadorable, software designer.