In this post I’ll show the new circuit from the addition of the ultrasonic sensor, the code I uploaded to the Arduino to drive it, and a video to demonstrate its functioning.
Here is the circuit.
Nothing much going on here, the HC-SR04 needs 5V, so it's connected to the 5V pin, and obviously to ground. The Trig and Echo pins of the sensor are connected directly to two of the digital pins of the Arduino, D11 and D12.
An ultrasonic sensor works like this:
1 – You send a short pulse to trigger the vibration of the transducer. Like ringing a bell. 2 – The sound waves produced by the transducer travel, hit something and are reflected, travelling back to the sensor, and making the transducer vibrate again. For this sensor there are two separate transducers, one for production, one for reception. 3 – The transducer, because of the vibration, produces a current, which is a function of the distance travelled by the sound waves. 4 – Read out that current and, knowing the function f(current)=distance, you have your distance!
The sensor I’m using is quite smart, and instead of returning a continuous current to be read out and processed – the function f(current) = distance is easily nonlinear – it returns a digital signal, where the duration of the high value is linearly proportional to the distance of the sensed obstacle. Which makes things much easier.
Here’s a video demonstration of the project at this point.
And here’s the code I used, adapted from the basic example of the New Ping library.
The code is pretty straightforward, there isn’t much to explain I guess. The 1.6 value I used to adjust the amount of vibration of the motor was obtained experimentally, by trial and error. It is high enough so that the motor still vibrates when the distance is at the maximum. If a lower value were to be used, then the motor wouldn’t receive enough current to start vibrating at distances near the limit.I’m starting to think that maybe having a vibration motor is not that great an idea. Its expressive range is very limited, as you can listen from the video, the vibration doesn’t seem to change much from the nearest distance to the farthest. Also, it consumes a great deal power, which I could use to add more sensors to the glasses. To explore other options, I bought two piezoelectric transducers, which should need very very little power, and much more control over their vibration is possible.For the next post, I’ll experiment with the piezoelectric tranducers. I think it won’t be a circuit post, as a piezoelectric transducer only needs a resistor, and nothing more, as far as I understand. Instead, given that there’s more things you can do with a piezoelectric transducer, like controlling amplitude and frequency of vibration, more attention will be given to the code.I’ll keep you posted!