Project 2: Rocket Launch Initiator
Posted by Rhiannon L on
Table of Contents
Introduction
Have you ever heard the phrase “it’s not rocket science”? Rocket science is a difficult field of study that involves math, physics, and other complex tools. In addition to planning, launching a rocket into space needs astronauts and builders, but it always needs someone to push the “lift-off” button.
That’s where you come in. While you won’t have an actual rocket to launch, in this project you’ll make a program that turns on a light when the button is pushed, just like a rocket launch initiator that illuminates a series of lights in preparation for liftoff.
In this project, we’ll be using one of the most common inputs – a push button. Buttons are a simple function that you use all the time in your everyday life, but this project will also help you to understand the basics of input and output, which are the foundation of any technology.
So how does a button work? When the button is pushed, it closes the circuit, providing the necessary voltage for your LED light to turn on (for more information on LEDs in Arduino, read Project 1 - Airplane Strobe Light, or try it if you haven't already). To do this, your code will be running a loop that will be constantly checking for voltage so that it knows when to turn on the LED.
Here is the circuit diagram that represents this project:
In this circuit, you will also use a pull-up/down resistor (explained below in more detail), which helps clean up the voltage and prevent false readings from the button.
02 | Rocket Launch Initiator |
Parts List
This series of posts will guide you through your journey with Arduino with simple, easy-to-follow projects to help you get the basics down. Today’s post will teach you to use buttons and give an example of an “if” statement, which is an essential part of coding.
For this project, you’ll need the following items:
- Arduino Uno Rev3 (x1)
- 400-Tie Breadboard (x1)
- Arduino & Breadboard Holder (x1)
- USB Cable A-to-B (x1)
- Premium Splittable Jumper Wire (40 Pins M/M 10cm) (x5)
- Green LED 5mm (x1)
- 330Ω Resistor (x1)
- 10kΩ Resistor (x1)
- Push Button / Tactile Switch (x1)
For a full list of all the parts you'll need for this series of projects, visit our introductory blog, An Introduction to Arduino.
5V Current. Your Arduino runs on five volts. This is the power that will be supplied from your computer via USB and will be the driving force behind any components you use in your circuits. By plugging your Arduino board into your computer, you are supplying it with just the right voltage it needs to thrive! 5V can’t hurt you, so don’t be afraid to touch anything in your circuit.
There are three main steps to any Arduino project: | ||
ASSEMBLE the circuit |
WRITE the code |
UPLOAD & RUN See it work! |
Assemble the Circuit
Set up your Arduino and Breadboard. For help with this, you can refer to An Introduction to Arduino.
Before assembling, always unplug your Arduino from your computer so that there’s no power in the circuit. 5V, which is what the Arduino uses, is not enough to hurt you, although if you accidentally plug wires into the wrong sockets while the power is on, you could fry your Arduino.
Build the circuit while referring to the diagrams below. Double check to make sure that all your wires and parts are plugged into the right terminals.
Write the Code
Now that we’ve finished building, it’s on to the code. Copy the code down below, and pay special attention to the syntax (i.e. the coding grammar and punctuation). Syntax errors will prevent your program from running. Don’t worry about the colours of the words, your Arduino IDE (Integrated Development-Environment) will colour them automatically for you.
Later, you can try the exercises listed at the bottom of this page to further your understanding of the code.
void setup() {
pinMode(11, OUTPUT);
pinMode(2, INPUT);
}
void loop() {
if (digitalRead(2) == HIGH) {
digitalWrite(11, HIGH);
} else {
digitalWrite(11, LOW);
}
}
Explanation of the Code
We explained how void setup(), pinMode(11, OUTPUT);, void loop(), and digitalWrite(11, HIGH); work in Project 1: Airplane Strobe Light.
if (condition) { } else { } - The “if” statement is a fundamental piece of code that you will use in almost any program you write. It is part of a category of conditional statements (which execute code if a condition is met). If the test condition (digitalRead(2) == HIGH in this case) is true, the first part of the code (the part inside the first pair of squiggly { } brackets) will be executed, if it’s false, the second code (inside the second squiggly { } brackets) will be executed.
digitalRead(2); - this code reads the input (the information sent to the Arduino) of a pin. Since we are using pin #2 as an INPUT from the push button, we can see if the button was pushed or not. A response of HIGH means it was pushed, while LOW means it was not pushed.
Upload & Run
*Make sure the correct COM port (Tools > Port) is selected! For help with this, see An Introduction to Arduino.
When you’re finished copying the code above, press the following buttons:
1 | Verify | This compiles your code. The IDE changes it from text into instructions the computer can understand. | |
2 | Upload | This sends the instructions via the USB cable to the computer chip on the Arduino board. The Arduino will then begin running your code automatically |
What You Should See
Once upload is completed, “Done” will show up in the Arduino IDE message board. When you push the button, you should see your LED turn on, and when you let go, the light should turn off. If you want, you can even try saying “3! 2! 1! Liftoff!” before you push the button. If it’s not doing what I just described, try making sure you have assembled the circuit and written the code correctly. Also make sure you’ve clicked verify and upload. If it’s still not working like it’s supposed to, see the troubleshooting tips below.
Pull-Up/Down Resistor to Stabilize Input
Let’s say you have a microcontroller unit (MCU) with one pin configured as an input. If there is nothing connected to the pin and your program reads the state of the pin, will it be high (Vcc) or low (0V)? It is difficult to tell. This phenomena is referred to as floating. To prevent this unknown state, a pull-up or pull-down resistor will ensure that the pin is in either a high or low state, while also using a low amount of current.
With a pull-up resistor, the input pin will read a high state when the switch is open. In other words, a small amount of current is flowing between Vcc and the input pin (not to ground), so the input pin reads close to Vcc. When the switch is closed, it connects the input pin directly to ground. The current flows through the resistor to ground, thus the input pin reads a low state. With a pull-down resistor, everything is the same, just reversed. Keep in mind, if the resistor wasn’t there, your switch would connect VCC directly to ground. This is also known as a short and is very bad.
Troubleshooting
Light not turning on?
The pushbutton is square, and because of this it is easy to put it in the wrong way. Take it out and turn it 90 degrees before putting it in again, then see if it starts working.
Light always turns on?
This may be happening because you connected the 10kΩ resistor to the 5V rail instead of the GND rail, and the other leg of the push button to the GND instead of the 5V rail. Try checking to make sure everything is plugged into the right place and in the right way.
Other components
Check Project 1 for LED troubleshooting tips.
Still no success?
Remove everything from the breadboard and build the circuit again. If you are still having problems, send us an e-mail at support@carobot.ca and we will get back to you as soon as we can.
Exercises to Try
- Modify the code so that the LED is normally ON and pushing the button will turn the LED off instead.
- Apply your knowledge from Project 1 - Airplane Strobe Light, and modify the code so that the LED is normally OFF and pushing the button will cause the LED to blink.
Did you have any success with the exercises? The exercises are important because they help you think outside of the box and learn how to combine the different functions that you’ve learned in previous projects.
If you have any questions, leave a comment down below. Today you learned about push buttons and the basics of input and output, and as this series progresses, you’ll learn about more essential Arduino functions, so check back often!