IoT has been a full blown affair as of 2016. When I heard about IoT the first time in 2014, I was not sure how it is different from all those embedded projects that developers have been doing as a hobby for years. Soon though, the Internet in the IoT became quite clear not only to me but to a lot of other people.
Although Internet aspect of IoT has changed a lot for those small embedded projects, core of IoT is still largely sensors and embedded components, and sooner we (the non embedded developer community) are familiar with all these sensors, better for the community, IoT, and us.
Today, I will talk about Ultrasonic sensors and using them with Raspberry Pi to build a small IoT device that can quite accurately measure distance. This could have a great set of applications such as in smart cars, in automation, in storage and logistics and so on.
I am using HC – SR04 ultrasonic sensor, that has accurate maximum distance measurement to 4 meters, Raspberry Pi 2 model B, Latest stable build of Windows 10 IoT Core, and my favorite IDE, Visual Studio 2015. I have also successfully managed to build this project on Raspberry Pi 3 Model B, with Windows 10 IoT Core insider preview. The tutorial is generic though and can be used with other IoT devices/other OS as well.
First, how does Ultrasonic sensor work? Its quite simple. You send a high to low pulse to the sensor’s trigger pin with 10 micro second’s interval, this invokes 8 ultrasonic pulses from the sensor. The echo of these pulses is received on echo pin. The echo pin is set to high for the time it took for the echo to return. So it is quite simple, trigger the sensor, and listen to the echo pulse. As soon as the echo is high, see how long it was set to high. Then, convert this time to distance with the help of speed of sound. (Ultrasonic waves travel with approximately speed of sound, and covers twice the distance, once from the sensor to object, and then the echo of the same. So you need to divide it by 2) You can do the calculation is you like, but I will save you some time, and give you the direct formula in a short while.
So, now we know how to calculate distance from UT sensor. However, there is a catch. Ultrasonic sensor needs 5V VCC. This 5V is returned on Echo pin to the Raspberry Pi. However, all the GPIOs on Pi are designed to receive 3.3V. Because of this, you should build a voltage divider circuit between echo pin and GPIO of Pi.
Here is a diagram of voltage divider circuit and a photo of how I built it on a breadboard.
Input voltage is by default 5V. Resistor R1 and R2 has to be chosen in such a way that Output voltage V2 should be 3.3V.
V2/V1 = R2/(R1+R2)
By selecting R1 = 2K ohms, we get R2 = 4K.
Here is a photo of my circuit
Now, let’s dive into code.
Create a new UWP application from Visual Studio. In solution explorer right click on references, add a reference, extensions, and add IoT extension for UWP. Once that is done, you can use all the GPIO functionality of the Raspberry Pi.
Once, that is done, do these following steps -
1. Create a GpioController object, and objects for GpioPin. You need Pins for trigger, echo, and alternatively for LED and button if you are using those.
2. Open the pins, and set the driver mode on them. Trigger is default output, while echo is input.
3. Send a high to low pulse on Trigger of 10 microseconds width.
This needs to be done on a new thread so that, you can continuously trigger the sensor on a specific interval.
4. Set a valueChanged event on Echo pin.
m_Echo.ValueChanged += m_GpioEchoValueChanged;
5. Every time you enter the value changed event, check if the transition is from low to high, if so, start the timer (Which is basically Stopwatch). If it is from high to low, stop the timer, and calculate the ticks.
6. Here is the formula to calculate distance in centimeters from timer ticks -
First you convert the ticks into microseconds, and then, multiply this by 0.01715 (One sided speed of light in cm/us), and that’s how you get the distance in CM.
Once, you have the distance, you can use it to calculate a whole lot of other stuff, such as predicting the storage capacity, finding the approximate velocity of moving object or vehicle, etc.