A Hat for Keeping Physical Distance, NeoPixels and Sound
Introduction: A Hat for Keeping Physical Distance, NeoPixels and Sound
In this tutorial, for a distance sensor hat, I’ll be using an ultrasonic sensor to measure the distance of others approaching. This will trigger a neopixel ring with colours, a buzzer for sound, and a vibration sensor to get some haptic feedback.
Supplies
* Sharp ultrasonic distance sensor. This item comes with a lot of kits and is pretty common so you will be able to find it easily.
* Lilypad USBboard (choose a board to suit your project)
* NeoPixel ring, or string of NeoPixels or another visual way to communicate
* Vibration Motor
* Sound module / piezo
* LiPoly battery, small so you can keep it in the hat,
* Croc clips are great for the prototyping phase
* Cable to upload your code
Step 1: Hooking Up!
For this circuit, I’m using the following pins, but I recommend setting each item up one by one, we’ll start with the distance sensor:
* Ultrasonic distance sensor: ECHO – A2
* Ultrasonic distance sensor: TRIG – A3
* NeoPixels data wire – pin 3
* Vibration – pin 9
* Buzzer – pin 10
All Ground to Ground and Power to Power
Step 2: Starting With Ultrasonic Distance Sensor
Hooking up the basics to check what works, step by step. First, add the ultrasonic distance sensor.
on the sensor you’ll see 4 pins out, hook them up:
VCC – to your ‘+’
TRIG – choose a pin and change it in the code, I’m using A3
ECHO – again, choose a pin, I’m using A2
GND – connect to your GND pin ‘-‘
Do you have the Library for HCSR04 installed?
Not too sure how to install a library?
You’ll need to have the HCSR04 installed to get this examples folder with sketches. For a quick tutorial on how to add an item to the library, here is a quick guide to installing Libraries with Arduino to get you up and running. [Beta Arduino 2.0.0 guide]
If you know how to then just go ahead and add the library (library from <PERSON>).
I found that this library worked the first time without errors, and the code was just clean and simple to check your sensor works.
#include <HCSR04.h>
UltraSonicDistanceSensor distanceSensor(13, 12); // Initialize sensor that uses digital pins 13 and 12.
void setup () {
Serial.begin(9600); // We initialize serial connection so that we could print values from sensor.
}
void loop () {
// Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.
Serial.println(distanceSensor.measureDistanceCm());
delay(500);
}
Change the code to reflect the pins you are using. I’m using A3 and A2 so I’ve modified the code. 939
After you have uploaded the code, open the Serial monitor to check it’s working.
You should have something similar to the output in my screenshot
Step 3: Testing the NeoPixels for Your Distance Sensor Hat
My next step is to connect and test the NeoPixels. Again, make sure you have installed the NeoPixel library through the Library manager.
I’m connecting the ‘data‘ wire to pin 3 on my board and I’ll change this in the code. You’ll also change the number of Neos you’re using. Connect your Ground and Power wires too.
Once you have installed the library and connected up to your circuit, Go to the NeoPixel examples folder. Then in the Examples folder, you’ll want to load the simple example (or just try any example you want).
Step 4: Add a Buzzer and Vibration Sensor If You Are Using Them…
I am going to add a vibration sensor and a buzzer for sound. these values will change as the distance changes. I’m hooking up the buzzer to pin 10, and then the vibration to pin 9. 939