Project tutorial
Automated Lego Shooter

Automated Lego Shooter © GPL3+

A project that uses SG-90 Micro Servos to shoot Lego cannons, because why not.

  • 1,672 views
  • 5 comments
  • 6 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
This is the board I used for this project. However, an Arduino Uno would work as well.
×1
Sg90 servo motor 180 degrees sg90 micro
SG90 Micro-servo motor
These are the motors needed for the project.
×4
11026 02
Jumper wires (generic)
×1
Lego Star Wars Shootable Cannon
×4
12002 04
Breadboard (generic)
×1

Necessary tools and machines

08wx9696 40
Tape, Electrical
Any tape is perfectly fine. It most certainly does not have to be electrical tape.

Apps and online services

Ide web
Arduino IDE
A program that runs Arduino Code on Mac, Windows, and Linux Devices.

About this project

This project is about getting Lego Cannons to shoot automatically. I like playing with legos, and I like using Arduinos, so I decided to do this project in my free time. The Servo motors, with their horns attached, spin around hitting the lever and shooting the cannon. A great first, second, or third project for beginners!

This is a step by step guide on how to make it.

Step 1: The Hardware

Step 1 Part 1: Hardware Basics

Start with 5V going to the positively charged row and GRND going to the negatively charged row. It'll look like this -

Step 1 Part 2: Hooking up Servo #1

The Servo has three wires - the orange/yellow (signal) one will take up a slot in the DIGITAL (PWM~), the red one (Power) will go to VIN (which is any pin in the positive row), and the black/brown wire (GRND) will go to the negatively charged row connected to the GRND pin on the Uno. The Digital pin that I used for my first servo is 7. Here is how the schematic looks like when the first servo is hooked in -

Step 1 Part 3: Wiring up the rest of the Servos

Once you get the hang of it with the servos, the rest should be a breeze. One thing to note, however - you have to keep consistent with the Digital Pins you attached with your servo in the code.

For example, the code (which can be found at the bottom of the page), states-

secondCannon.attach(8);

Therefore, the second Servo must be connected to the digital pin 8.

In the same way, the third Servo must be attached to the digital pin 6, and the fourth should be attached to the ninth digital pin.

However, these can be changed so that they are in any order you want them to be. Just make sure you are changing both hardware and software for consistency, otherwise, the code will not work.

To run with the code provided at the bottom, this is how the schematic should look like-

Congratulations! You have officially completed the hardware portion of this project.

Step 2: The Software

Now, copy and paste the code at the bottom of the page onto an Arduino sketch, and upload it. Make sure you have the Servo library before uploading the code, otherwise it will not work! Below is a step-by-step guide explaining the code and what it means when running the program.

#include <Servo.h>  //Including the library for the Servos
Servo firstCannon;  //Defining the First Cannon as a Servo
Servo secondCannon;  //Defining the Second Cannon as a Servo
Servo thirdCannon;  //Defining the Third Cannon as a Servo
Servo fourthCannon;  //Defining the Fourth Cannon as a Servo

This code sets up the Servo.h library, which will play a fundamental role in the code. This can be shown by the following lines, using the library to define four cannons as Servos, or motors that can be rotated 180 degrees.

void setup() {  //Setup code, or code that is only run once
 Serial.begin(9600);  //Start the Serial communication
 Serial.println("Defend the Clones! Set up the defenses!")  //Prints something in the Serial Monitor
 firstCannon.attach(7);  //Attach Digital Pin 7 to First Cannon
 secondCannon.attach(8);  //Attach Digital Pin 8 to Second Cannon
 thirdCannon.attach(6);  //Attach Digital Pin 6 to Third Cannon
 fourthCannon.attach(9); //Attach Digital Pin 9 to Fourth Cannon
}

The void setup() code is used within almost any Arduino project, and it is code that is only run once. In this case, we are starting the newline Serial Communication at 9600 baud. Then, we are printing things in the Serial Monitor. In order to program the Servos, we have to connect their names the their respective digital pins. This is the purpose of the .attach() piece of code.

void loop() {  //Loop code, or code that is run an infinite amount of times
 Serial.println("Beginning firing sequence")  //Print something in the Serial Monitor
 firstCannon.write(180);  //Swerve the first servo 180 degrees, setting off the first cannon
 delay(100);  //Wait for 1/10 of a second
 firstCannon.write(0);  //Swerve the Servo back to original position in preparation for reload
 delay(1000);  //Wait for 1 second
 secondCannon.write(180);  //Swerve the second servo 180 degrees, setting off the second cannon
 delay(100);  //Wait for 1/10 of a second
 secondCannon.write(0);  //Swerve the Servo back to original position in preparation for reload
 delay(1000);  //Wait for 1 second
 thirdCannon.write(180);  //Swerve the third servo 180 degrees, setting off the third cannon
 delay(100);  //Wait for 1/10 of a second
 thirdCannon.write(0);  //Swerve the Servo back to original position in preparation for reload
 delay(1000);  //Wait for 1 second
 fourthCannon.write(180);  //Swerve the first fourth 180 degrees, setting off the fourth cannon
 delay(100);  //Wait for 1/10 of a second
 fourthCannon.write(0);  //Swerve the Servo back to original position in preparation for reload
 delay(1000);  //Wait for 1 second
 Serial.println("All cannons fired");
 Serial.println("Prepare for reload");
 delay(10000);
 Serial.println("Reloading Complete. Preparing to fire again")
}

This is the final piece of code, using the void loop() function that allows the following code to be run an infinite amount of times. Then, we print in the Serial Monitor that the cannons are about to fire. The Servos for each cannon should spin 180 degrees, then spin back to the original position, one at a time. After that, there should be 10 seconds (reloading time) until the process repeats itself again.

If you have any other issues when uploading the code, you can comment in the comments section and I will respond ASAP. If it works, then move on to the next step.

Step 3: Installation

The last step requires the lego cannons and some old-fashioned regular tape. The lego cannon looks like this -

Run the code. See which way the servo turns. Tape the servo onto the lego cannon so that its trajectory sets it on a collision course with the lever on top of the cannon in the picture above.

Good job! You have officially finished this project. Like I said, comment in the section below if you have any questions.

A video of the Lego Cannons being shot at some battle droids.

If you liked this project, check out these links -

https://create.arduino.cc/projecthub/342600/basketball-hoop-with-buzzer-using-an-arduino-693bad?ref=user&ref_id=1536504&offset=1

https://create.arduino.cc/projecthub/ShreyanR/soundbox-826a56

Code

Lego Gun Shooter CodeC/C++
Paste this into Arduino IDE
#include <Servo.h>  //Including the library for the Servos

Servo firstCannon;  //Defining the First Cannon as a Servo
Servo secondCannon;  //Defining the Second Cannon as a Servo
Servo thirdCannon;  //Defining the Third Cannon as a Servo
Servo fourthCannon;  //Defining the Fourth Cannon as a Servo

void setup() {  //Setup code, or code that is only run once

  Serial.begin(9600);  //Start the Serial communication

  Serial.println("Defend the Clones! Set up the defenses!")  //Print something in the Serial Monitor

  firstCannon.attach(7);  //Attach Digital Pin 7 to First Cannon
  secondCannon.attach(8);  //Attach Digital Pin 8 to Second Cannon
  thirdCannon.attach(6);  //Attach Digital Pin 6 to Third Cannon
  fourthCannon.attach(9); //Attach Digital Pin 9 to Fourth Cannon

}

void loop() {  //Loop code, or code that is run an infinite amount of times
    
  Serial.println("Beginning firing sequence")  //Print something in the Serial Monitor

  firstCannon.write(180);  //Swerve the first servo 180 degrees, setting off the first cannon
  delay(100);  //Wait for 1/10 of a second
  firstCannon.write(0);  //Swerve the Servo back to original position in preparation for reload
  delay(1000);  //Wait for 1 second

  secondCannon.write(180);  //Swerve the second servo 180 degrees, setting off the second cannon
  delay(100);  //Wait for 1/10 of a second
  secondCannon.write(0);  //Swerve the Servo back to original position in preparation for reload
  delay(1000);  //Wait for 1 second

  thirdCannon.write(180);  //Swerve the third servo 180 degrees, setting off the third cannon
  delay(100);  //Wait for 1/10 of a second
  thirdCannon.write(0);  //Swerve the Servo back to original position in preparation for reload
  delay(1000);  //Wait for 1 second

  fourthCannon.write(180);  //Swerve the first fourth 180 degrees, setting off the fourth cannon
  delay(100);  //Wait for 1/10 of a second
  fourthCannon.write(0);  //Swerve the Servo back to original position in preparation for reload
  delay(1000);  //Wait for 1 second

  Serial.println("All cannons fired");
  Serial.println("Prepare for reload");
  
  delay(10000);
  
  Serial.println("Reloading Complete. Preparing to fire again")

}

Schematics

Lego Shootable Cannon Servo Diagram
Screenshot 2020 05 12 at 2 51 21 pm irlecnyqhw

Comments

Similar projects you might like

Cockroach Laser Killer

Project showcase by NerdFatherRJ

  • 11,649 views
  • 4 comments
  • 14 respects

MeArm Controlled by Arduino Uno and TTP229-BSF Touchpad

Project tutorial by Alan Wang

  • 4,236 views
  • 2 comments
  • 14 respects

Simple Arduino Uno - ESP 8266 Integration

Project tutorial by circuito.io team

  • 120,834 views
  • 25 comments
  • 100 respects

Control Arduino Uno Using ESP8266 WiFi Module and Blynk App

Project tutorial by Adithya TG

  • 61,938 views
  • 16 comments
  • 59 respects

Multicolor Knight Rider with RGB PL9823 LED + Arduino UNO

Project tutorial by Giovanni Gentile

  • 8,360 views
  • 1 comment
  • 16 respects

Magnet Levitation with Arduino

Project tutorial by jsirgado

  • 79,770 views
  • 119 comments
  • 234 respects
Add projectSign up / Login