Interface Ultrasonic Sensor and Servo to Photon

Introduction: Interface Ultrasonic Sensor and Servo to Photon

About: 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 to contact me at

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

Components required to perform this ibles are:

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

Step 2: 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

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:

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

#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);




void loop() {

digitalWrite(trigPin, LOW);


digitalWrite(trigPin, HIGH);


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: ");



Step 6: Output

Makerspace Contest 2017

Participated in the
Makerspace Contest 2017

Be the First to Share


    • CNC Contest 2020

      CNC Contest 2020
    • Puzzles Speed Challenge

      Puzzles Speed Challenge
    • Colors of the Rainbow Contest

      Colors of the Rainbow Contest