Project showcase

Windows 10 IoT Core: UltraSonic Distance Mapper © GPL3+

Scan and draw ruff sketch of distances ahead just like RADAR using UltraSonic Distance Sensor.

  • 51,776 views
  • 23 comments
  • 138 respects

Components and supplies

Apps and online services

About this project

Ever wonder that we can make a device just like RADAR and LiDAR using these sensors! Yes it is possible; but with some limitation. RADAR are the most precise but are much expensive. So let's make our own distance mapper device which acts like RADAR using UltraSonic Distance Sensor (HC-SR04) and a Servo. 

UltraSonic and IrfraRed are basic fundamental sensor for measurement of distance. These sensor measures the distance in the visible sight of them with the range of 80cm to 500cm. Both sensor have their pros and cons. IR are susceptible to sun-lite and are not good in open field.

Concept

Mapping distance like RADAR requires a rotating sensor that picks up distance at different level and maps into 2D view. Just like RADAR, to rotate UltraSonic Distance sensor, we need a servo that accurately rotates it. Once RPi2 rotates servo at specific angle, RPi2 then measures distance and plot it into 2D view as shown in title image of the article. But we can't directly operate servo from RPi2 because, Windows IoT buil 10531 provides access to ADC and PWM via external chips. It means we can't directly control servo from RPi2 and requires extra components. Thus I found a way to operate servo via Arduino. Arduino will communicate with RPi2 via I2C bus and operates servo:

Software

It is clear that we need to develop two firmware. One for Arduino and another for RPi2 (UWP). You don't have to bother with the complexity of the communication as I have already provided library that will provide easy way for communication between Arduino and RPi2.

I am developing gateway firmware for Arduino. With the gateway firmware, you will be able to operate Arduino directly from Windows IoT code just like you wrote in Arduino sketch. It means that you will be able to manipulate Arduino's digital, analog, PWM and Serial pins directly from RPi2 with minimal coding. I have already developed gateway firmware for Arduino but right now there are lots of stability issues with it. Thus I'm improving the firmware and going to write PowerTip article for that. Will update here when it will be ready.

Gateway Sketch

Here, gateway is an I2C slave device. Which will serves the task requested by RPi2. There is no special requirement to understand for gateway sketch as it is already provided at the end of the article. It is simple and well commented.

Universal Windows App

Universal Windows App will communicate with gateway via I2C and requests gateway to move servo at specified position. After successful move of servo, UWApp will measures distance and plot it into 2D view.

Do not take immediate distance reading after moving servo because it will take some time (in milliseconds) to set servo at an angle. Take multiple sample of a specific direction to get accurate distance reading. Higher the sampling, more accuracy but with less resolution (it means it will take some time to process samples). So we need to balance sampling and performance.

Schematic

An issue has been found with Arduino Nano. RPi2 won't be able to communicate via I2C with Arduino Nano if you have implemented voltage divider circuit (Green rectangle in above image) for I2C. Try direct connection for I2C between Arduino Nano and RPi2. It not good for RPi2 (because RPi2 works at 3.3v while Arduino works on 5v)  but it worked for me and there is no any side effect has been seen on RPi2.

Essential

Install assembly 'WinRT Xaml Toolkit'

'WinRT Xaml Toolkit' provides various pre-developed components like PieSlice, charts, etc. You can find complete list of components on its website. This toolkit provide a key component for this project that is PieSlice. You can see PieSlice in the title image of the article. RADAR like 2D map screen is developed using multiple PieSlice component which is explained later in the article.

After successful installation of assembly, you need to specify alias of the namespace into XAML code to use components of the assembly.

For example:xmlns:Shape="using:WinRTXamlToolkit.Controls"  <Shape:PieSlice ... /> 

How to show relation between origin and objects?

Most of have seen RADAR screen on television. RADAR is object detection system that will determine range and angle of an object. By considering RADAR idea, we are gonna make such view that will be able to map object distance related to an angle.

I have provided 2D map for up-to 300cm ranging between -70 to +70 degrees ahead of the sensor. You can implement 2D map of your own choice of angle and distances. Even full circular just like RADAR. Just a little bit of coding will be necessary.

Basics of Working

The overall concept is really easy. Let's understand by an algorithm:

How do I register Windows App as Startup?

If you want to startup your universal windows app just after Pi boots, read this ProTip.

Known Issues

Arduino Nano Random Crash

Servo seems to be stop suddenly and did't move at all. I don't know what was happens with Arduino Nano. I wrote a watchdog reset but some Arduino Nano comes with older bootloader and they don't supports watchdog.

Solution: Press Arduino Nano's reset. RPi2 can also reset Arduino Nano for you. You just have to make appropriate logic so that RPi2 will know about the Nano's state and resets it whenever it is necessary.

UltraSonic Distance Sensor Issues

It is not possible to get more accurate reading from ultrasonic sensor. Sometimes, they might give false reading. There is range limitation with ultrasonic which ranges between 3 - 5 meter (may differ from sensor to sensor). Different surfaces have different property. Thus measurement might vary for surface to surface.

Code

Gateway (Arduino Nano)C/C++
/*
  Gateway sketch for UltraSonic Distance Mapper (RPi2 + WinIoT)
  https://www.hackster.io/AnuragVasanwala/windows-10-iot-core-ultrasonic-distance-mapper-d94d63

  Objectives:
    + Initialize Servo
    + Initialize I2C Slave in address 0x40 (64 DEC)
    + Move servo to specified position when master (WinIoT) sends position byte
*/
#include <Wire.h>
#include <Servo.h> 

#define MyAddress 0x40
#define Pin_Servo 3

/* Create object for servo motor */
Servo MyServo;

/* Initial servo position */
byte Position = 70;

void setup()
{
  /* Initialize servo and move to initial position */
  MyServo.attach(Pin_Servo);
  MyServo.write(Position);

  /* Initialize I2C Slave & assign call-back function 'onReceive' */
  Wire.begin(MyAddress);
  Wire.onReceive(I2CReceived);
}

void loop(){ /* Do nothing. Just wait for call-back 'onReceive' */ }

/* This function will automatically be called when RPi2 sends data to this I2C slave */
void I2CReceived(int NumberOfBytes)
{
  /* WinIoT have sent position byte; read it */
  Position = Wire.read();

  /* Move servo to specified the position */
  MyServo.write(Position);
}
Improved Code (by Dave Glover)
Profile Ref: https://www.hackster.io/glovebox

Schematics

Schematic (Fritzing)
Windwows%20IoT%20-%20UltraSonic%20Distance%20Mapper-Anurag-PC.fzz
Schematic
Windwows%20iot%20 %20ultrasonic%20distance%20mapper bb

Comments

Similar projects you might like

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 297,288 views
  • 98 comments
  • 706 respects

Windows 10 IoT Core : Hydroflyer

Project showcase by Anurag S. Vasanwala

  • 17,657 views
  • 2 comments
  • 54 respects

OH HAI! on Windows 10 IoT Core

Project in progress by BuddyC

  • 12,530 views
  • 3 comments
  • 48 respects

GPS Datalogger, Spatial Analysis, and Azure IoT Hub.

Project tutorial by Shawn Cruise

  • 17,828 views
  • 4 comments
  • 66 respects

Solar Tracker with Live Data Feed - Windows IoT

Project tutorial by Jed Hodson

  • 10,563 views
  • 1 comment
  • 19 respects

Windows IOT - Automate your power outlets

Project tutorial by Syed Sanoor

  • 22,136 views
  • 13 comments
  • 48 respects
Add projectSign up / Login