Gaurav Mishra
Published © CC0

Death Star Turret Using Particle Photon

Death Star turret controlled using servo and Particle Photon.

IntermediateFull instructions provided10 hours1,158
Death Star Turret Using Particle Photon

Things used in this project

Hardware components

Particle Photon
Servos (Tower Pro MG996R)

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)


Read more

Custom parts and enclosures

3D model

Printed it using the following:

Printer: Rigidbot Big
Rafts: No
Supports: Yes
Resolution: 0.1 mm
Infill: 20% - 30%


Servo and Particle connection

Connect 3V Output to red/orange wire of servo
Connect Black/brown wire to GND
Connect white/yellow to D0


The Code: Spark Particle Servo Control

This program needs to be uploaded to the Particle via the Web IDE, provided at the spark build page.

Note that the loop() function has no code because that is all been taken care for us and all we have to do is call the spark.variable() function and the spark.function().
Servo myservo;  // create servo object to control a servo<br>
int pos = 0;    // variable to store the servo position</p><p>void setup()
  myservo.attach(A0);  // attaches the servo on the A0 pin to the servo object
  Spark.function("setpos", setPosition);
  Spark.variable("getpos", &pos, INT);

void loop()

int setPosition(String posValue) {
    pos = posValue.toInt();
    return 0;

Web Side

Time for a bit of JavaScript, this is the tough part of the tutorial and here we get the controller side of the code ready.If you are new to JavaScript, you can just enter your access token and Particle id and run this file in a web browser, this is a program that performs Jason requests and communicates to the Particle via the spark cloud.

And using AJAX to get things to happen live time.

Note- Do not share your .html file with anyone as it contains your Access Token and Particle ID, make sure you erase it before you share it.
<<span class="hljs-title">html></span>
  <<span class="hljs-title">script src="" type="text/javascript" charset="utf-8"></span></<span class="hljs-title">script></span>
<<span class="hljs-title">body></span>
    <<span class="hljs-title">P></span>Set Servo Position:<<span class="hljs-title">br></span><<span class="hljs-title">br></span>
    <<span class="hljs-title">input type="range" name="degBox" id="degBoxId" min="0" max="180" step="1" value="90" list="myData" onchange="setValue(this)"></span>
    <!-- This adds the tick marks to the range but does not in Safari -->
    <<span class="hljs-title">datalist id="myData"></span>
       <<span class="hljs-title">option value="0"></span>
       <<span class="hljs-title">option value="30"></span>
       <<span class="hljs-title">option value="60"></span>
       <<span class="hljs-title">option value="90"></span>
       <<span class="hljs-title">option value="120"></span>
       <<span class="hljs-title">option value="150"></span>
       <<span class="hljs-title">option value="180"></span>
    </<span class="hljs-title">datalist></span>
    <<span class="hljs-title">br></span><<span class="hljs-title">br></span>
    <<span class="hljs-title">button id="minusbutton" onclick="fineAdjust(-5)"></span>⇐ -5 °</<span class="hljs-title">button></span>
    <<span class="hljs-title">button id="plusbutton"  onclick="fineAdjust(+5)"></span>+5 ° ⇒</<span class="hljs-title">button></span>
    <<span class="hljs-title">br></span><<span class="hljs-title">br></span>
    <<span class="hljs-title">P></span>Current Position: <<span class="hljs-title">span id="curPos"></span></<span class="hljs-title">span></span><<span class="hljs-title">br></span>

    <<span class="hljs-title">script type="text/javascript"></span>
      <span class="hljs-keyword" style="font-weight: bold;">var deviceID    = "<< device id >>";
      var accessToken = "<< access token >>";
      var setFunc = "setpos";
      var getFunc = "getpos";

      window.setInterval(<span class="hljs-keyword" style="font-weight: bold;">function() </span>{
        requestURL = "" + deviceID + "/" + getFunc + "/?access_token=" + accessToken;
        $.getJSON(requestURL, <span class="hljs-keyword" style="font-weight: bold;">function(json) </span>{
                 document.getElementById("curPos").innerHTML = json.result + "°";
                 document.getElementById("curPos").style.fontSize = "28px";
                 document.getElementById("degBoxId").value = parseInt(json.result);
      }, 1000);

      <span class="hljs-keyword" style="font-weight: bold;">function setValue(obj) </span>{
        var newValue = document.getElementById('degBoxId').value;

      <span class="hljs-keyword" style="font-weight: bold;">function fineAdjust(value) </span>{
        var currentValue = parseInt(document.getElementById('curPos').innerHTML);
        var setValue = value + currentValue;
        document.getElementById("degBoxId").value = setValue;

      <span class="hljs-keyword" style="font-weight: bold;">function sparkSetPos(newValue) </span>{
	var requestURL = "" +deviceID + "/" + setFunc + "/";
        $.post( requestURL, { params: newValue, access_token: accessToken });

    </span></<span class="hljs-title">script></span>
</<span class="hljs-title">body></span>
</<span class="hljs-title">html></span><br>


Gaurav Mishra

Gaurav Mishra

2 projects • 20 followers
Electronics professional working in Advanced Engineering of an automotive OEM.


Add projectSign up / Login