Interface Ultrasonic Sensor and Servo to Photon
by callmeappytechie in Circuits > Sensors
1136 Views, 1 Favorites, 0 Comments
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
Components Gathering




Components required to perform this ibles are:
- Photon
- ultrasonic sensor
- Servo
- breadboard
- jumper wires
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
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
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.
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);
}
Output

