Introduction: Interface Ultrasonic Sensor and Servo to Photon

Picture of Interface Ultrasonic Sensor and Servo to Photon

Hey !! I'm Sridhar Janardhan back with another ibles today I am going to show how to interface ultrasonic sensor and servo to Photon.This ibles teach you to control servo motors depending upon the values thrown by the ultrasonic sensor to the photon

Before we begin to have a look at the official website of the particle, click here

Let's now gather the component

Step 1: Components Gathering

Picture of Components Gathering

Components required to perform this ibles are:

  • Photon
  • ultrasonic sensor
  • Servo
  • breadboard
  • jumper wires

Step 2: Setting Up Photon

Picture of Setting Up Photon

To setup, a photon makes sure you achieve the blinking blue on the board led by holding the mode button for few seconds.

then download the official particle Android app from the play store and log in with your account credentials you had submitted in the particle website

Press the plus button in the bottom right of the screen and then select photon and continue the on screen instruction of the app and finally, you will be able to connect the photon to the wifi

Step 3: Connecting Ultrasonic Sensor

Picture of Connecting Ultrasonic Sensor

Ultrasonic sensor will work on the transmitting and reflection of the wave.the echo pin of the sensor will emit the wave form and travel through space and reflects back as soon as it obstructs an object.the time taken for the emission and reflection is calculated thus predicting the distance of the object.

The connection of the ultrasonic sensor is as follows:

VCC pin: to the positive or 3.3v pin of the photon

GND pin: to the GND pin of the photon

TRIG pin: to the digital pin 2

ECHO pin: to the digital pin 3

Step 4: Connecting Servo Motor:

Picture of Connecting Servo Motor:

Servo motor is a specially designed motor for the control of the acceleration of the shaft and its angular rotation in both directions.

The connection of the servo is as follows:

  • Red wire: this is the positive power supply of the servo and is connected to the 3.3 v pin of the photon
  • Black wire: this is the ground supply of servo and is connected to the gnd pin of the photon
  • orange wire: this is the signal wire which is connected to the digital pin 3 of the photon.

Step 5: Coding

Picture of Coding

#include "Servo.h"
const int trigPin = 15;

const int echoPin = 13;

long duration;

int distance;

int safetyDistance;

Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

myservo.attach(5);

Serial.begin(9600);

}

void loop() {

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

safetyDistance = distance;

if (safetyDistance <= 5){

// goes from 0 degrees to 180 degrees // in steps of 1 degree

myservo.write(90); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

}

else{ // goes from 180 degrees to 0 degrees

myservo.write(0); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position }

Serial.print("Distance: ");

Serial.println(distance);

}

Step 6: Output

Picture of Output

Comments

About This Instructable

104views

2favorites

License:

Bio: I am an electronic hobbyist on Arduino, Photon, Raspberry Pi and common electronics.A passionate cook.Any assistance in electronics and web development feel free ... More »
More by callmeappytechie:PIR Controlled Buzzer Using ArduinoDIY LampshadeTasty Egg Bonda
Add instructable to: