Things used in this project

Hardware components:
Ard nano
Arduino Nano R3
×1
R8326274 01
Raspberry Pi 2 Model B
×1
Ultrasonic Distance Sensor (HC-SR04)
×1
Servo (generic)
×1
Resistor (1K)
×3
Resistor (2K)
×3
11026 02
Jumper wires (generic)
×1
12002 04
Breadboard (generic)
×1
Software apps and online services:
Ide web
Arduino IDE
Vs2015logo
Microsoft Visual Studio 2015
10
Microsoft Windows 10 IoT Core

Schematics

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

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

Issues

RPi2 works on 3.3v while Arduino works on 5v. Thus voltage level conversion is needed. Schematic I've provided uses voltage divider to convert from 5v to 3.3v. It is better to use bi-directional logic level converter than voltage divider. It is more safer with the bi-directional logic level converter than voltage divider.
See all issues

Credits

Asv2
Anurag S. Vasanwala
8 projects • 337 followers
Contact
Thanks to Jerry Nixon.

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog