Pew Pew! How To Build A Laser Turret With An Arduino

James Bruce 20-11-2013

Are you bored? Might as well build a laser turret. You know – one that goes pew pew, shoots a red beam in lots of different directions, and maybe even throw in a smoke machine? Yep, one of them.


What You Will Need

  • Arduino
  • 2 servos
  • Laser module, such as one from this sensor kit
  • Piezo buzzer or other small output device
  • Metal wire and cable ties for fixing
  • Long female->male jump cables, plus regular jump cables

Optionally, a smoke machine is needed – the laser is pretty low wattage, so you won’t be able to see the beam without smoke even in a dark room.


Build Plan

The basic idea of the turret is to put the laser module on top of one servo to provide horizontal rotation; then mount that package onto another servo placed at a 90 degree angle to provide vertical movement. We’ve got a piezo to provide the pew pew sound effects, and I’m throwing in a smoke machine for good measure.

Servo Testing

Depending on your servo, the wires may be coloured differently, but in general:

  • Red is the positive wire, and on both my servos it was the centre of three – to be connected to +5v rail.
  • Brown or black is the negative, to be connected to GND on the Arduino.
  • White or orange is the signal wire, to be connected to a PWM capable digital I/O pin (9 and 10 in the demo below).

Once you’ve wired up your two servos, upload the following sample code. I’ve named one servo “hori” to control the horizontal movement, and the other “vert”. Each should perform a full range of motion sweep (about 60 degrees, in my case).

#include <servo.h>
Servo vert,hori;  // create servo object to control a servo
                // a maximum of eight servo objects can be created 
int pos = 0;    // variable to store the servo position
void setup()
  vert.attach(10);  // attaches the servo on pin 9,10 to the servo objects

void loop()
  for(pos = 0; pos < 180; pos += 10)  // goes from 0 degrees to 180 degrees   
  {                                  // in steps of 10 degrees     
    hori.write(pos);    // tell servo to go to position in variable 'pos'     
    delay(100);                       // waits 100ms for the servo to reach the position   
  for(pos = 180; pos>=1; pos-=10)     // goes back from 180 degrees to 0 degrees
    vert.write(pos);              // tell servo to go to position in variable 'pos'
    delay(100);                       // waits 100ms for the servo to reach the position

All good? Moving on then.

Testing the Laser and Pew Pew Sound

The laser module is just like an LED, but it has a resistor built into the module so we can hook it up directly to a digital I/O – very simple. If you’re using the same laser module as me, the “” goes to GND, the S goes to pin 12. Modify the sample code above to make pin 12 an output:

int laser = 12;

Then blink the pin on and off each loop using standard digitalWrite() method.

We’ll just use PWM to drive the piezo buzzer at a comfortable sound level – you could experiment with using the tone library if you wanted, but a simple noise is all I need. Connect the black wire to ground and the red wire to pin 11. Define your buzzer on the relevant pin, set to output mode, and activate using analogWrite(buzzer, 100) (or any number you want up to 254); and analogWrite(buzzer,0) to turn off.


The full sample code modified to sweep two servo, activate a laser, and play the annoying sound, can be found here.

All your components should be working – now we need to tie it all together.

Creating the Turret

Using cable ties, attach one servo to the other; it doesn’t really matter which, just make sure one will move on the horizontal and the other will move the vertical. You can pull off the rotor blade and re-seat during testing if the angle isn’t right.



Use some stiff modelling wire to fix the laser module to the blade of the other servo, like so:


Finally, I attached the whole thing to a desk leg with yet more cable-ties and a bit of scrap wood.



Programming the Turret

I don’t know about you, but my idea of a laser turret comes from countless numbers of sci-fi films and star trek episodes. Invariably someone will fly past a turret and little pew-pew shots will come flying out in a sweeping pattern, always milliseconds too slow so our protagonist doesn’t actually get hit. That’s what I’m trying to replicate, though feel free to tweak the main routine to suit your idea of what a turret should do.

Here’s the pseudo code I ended up using for the main loop:

  • Randomize time between bursts, and time between each individual shot.
  • Randomize the start and end position for each servo, vert and hori.
  • Randomize the number of shots to take.
  • Work out the number of degrees of change after each shot as the difference between start and end positions divided by number of shots.
  • Move the servos to the starting positions, and wait a little for them to get there (100ms)
  • Loop until all shots have been taken, each time moving the servos a little as previously calculated; move and shoot, move and shoot.
  • Repeat.

I also added a separate fire() method to structure the code a little better. Adjust the ranges of all random() functions to speed up or slow down each parameter; or increase the number of shots for a more dance club vibe. Scroll down for a video of the code in action!

#include <servo.h>

Servo vert,hori;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position
int laser = 12;
int buzzer = 11;

void setup()
     vert.attach(10);  // attaches the servo on pin 9 to the servo object

void loop()
     int timeBetweenBursts = random(200,1000);
     int timeBetweenShots = random(50,200);
     int vertStart = random(1,180);
     int vertEnd = random(1,180);
     int horiStart = random(1,180);
     int horiEnd = random(1,180);
     int numShots = random(5,20);

     int vertChange = (vertEnd - vertStart) / numShots; //how much to move vertical axis by each shot
     int horiChange = (horiEnd - horiStart) / numShots;

     vert.write(vertStart);//let it get to start position first, wait a little

     for(int shot = 0; shot<numShots; shot++){

          vertStart += vertChange;//increment the vert value for next time
          horiStart += horiChange;

          delay(timeBetweenShots); //add a bit of variety to the speed of shots

void fire(){
     delay(20);//adjust this to change length of turret shot
     analogWrite(buzzer, 0); 

In Action

I don’t think there’s a practical use for this little toy, but it’s an awful lot of fun and there are a lot of variables you can tweak to get the desired effect. Perhaps it’ll come in handy for a homemade LEGO movie?

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Ryan Morgan
    February 27, 2018 at 7:51 pm

    How do I make it remote controllable?

    • Shariq
      March 30, 2018 at 3:51 am

      I'm pretty sure that in the bundle there is a joystick, you could try that...

  2. JohnnyFRX
    December 17, 2016 at 1:44 am cats REALLY hate me now. Soooo...THANK YOU! Lord knows they had this coming! HAAAAAAAAAA

  3. Michael
    December 1, 2016 at 3:54 pm

    How would I incorporate a PIR Sensor so the turret turns on when it detects motion? I have tried a few different ideas but none are working

    • James Bruce
      December 1, 2016 at 4:05 pm

      I would wrap everything in the loop() function with an IF statement that checks the status of the PIR sensor pin, and only goes on if ==1. Wire up your PIR and output the status to the serial log first to check it's working. PIRs can be tricky due to their timer and delay adjustments, so you'll want to figure that out on its own first.

  4. msa
    August 18, 2016 at 2:38 am

    How would you arrange the servos in order to align the x and y axis with the center point of the laser? Right now a degree change in up or down for the laser has a varying degree of change depending on where the laser is because of the offset in how they are positioned.

  5. Sagar
    July 14, 2016 at 7:02 am

    What is the wattage for the laser module? I have a 50mW module. Can I plug that directly in to the arduino? If not how can I provide an external power supply?

    • James Bruce
      July 14, 2016 at 7:04 am

      No, you absolutely cannot plug something that powerful directly into the Arduino, and using that for this project would be fairly insane given it can actually burn things. That said, you'd just hook up a relay and power the laser externally (so the relay goes on the positive line, and the trigger goes to an arduino pin).

      • James Bruce
        July 14, 2016 at 7:05 am

        Wait, wait. MILLIWATTS. Sorry, I read 50 WATTS. Yes, 50mW should be fine!

  6. Realname
    June 2, 2016 at 1:20 am

    Is there any way to make it aim at any moving targets?

    • James Bruce
      June 2, 2016 at 7:06 am

      You'd need to add code to calibrate the positions, and provide some kind of visual processing through a Rasberry Pi

  7. evan
    December 18, 2015 at 11:01 pm

    it keeps telling me that theres a fatal error with servo.h how can i fix this

    • James Bruce
      December 19, 2015 at 8:28 am

      Looks like they might have changed the servo library naming - follow this link to download latest and see sample code on how to include it:

      • Andrew
        January 7, 2019 at 2:58 am

        I uploaded that directory and it is still not working. I am real rusty with Arduino. Sorry.

        Arduino: 1.8.8 (Windows Store (Windows 10), Board: "Arduino/Genuino Uno"

        In file included from C:\Users\awkno\Documents\Arduino\sketch_jan06a\sketch_jan06a.ino:1:0:

        C:\Users\awkno\Documents\Arduino\libraries\SoftwareServo/SoftwareServo.h:4:22: fatal error: WProgram.h: No such file or directory

        compilation terminated.

        exit status 1
        Error compiling for board Arduino/Genuino Uno.

        This report would have more information with
        "Show verbose output during compilation"
        option enabled in File -> Preferences.

  8. Anonymous
    October 6, 2015 at 11:11 pm

    Which arduino dose this use?

    • James Bruce
      October 7, 2015 at 7:39 am

      Just a regular old Uno, but there's no reason it won't work on other boards with a level shifter.

      • Shariq
        March 30, 2018 at 3:54 am

        How would you connect the laser to a MKR1000?

  9. Robotguy
    November 21, 2013 at 4:03 am

    Another awesome application: Cat Toy

  10. Joe
    November 21, 2013 at 2:02 am

    Set this up with a home intrusion system and I'd bet no self-respecting robber would hang around long enough to find out if they were going to be fried by a laser.

  11. plasbot
    November 21, 2013 at 1:44 am

    use an xbox kinect with facial recognition, and replace laser with a homeade green laser.
    area denial system complete

  12. Tristan
    November 21, 2013 at 1:24 am

    It is a step towards making your own life size portal turrent.

  13. Justin P
    November 20, 2013 at 10:24 pm

    You completely had me after the first paragraph. Brilliant.