Introduction to Arduino Programming
A series of based on New York University's
Introduction to Computer Programming class from the Courant Institute of Mathematical Sciences.
This is intended as a "gentle" introduction to computer programming and Arduino. The only prerequisite is that you've completed our Arduino Basics guide!
Push my Button(s)
We're going to read the value of an input bit (in this case a button) and display it on the serial monitor of the Arduino IDE and that value will tell us if we're pressing a button or not.
The serial monitor will let us display our values just like how we displayed "hello littleWorld" in the previous tutorial (click-through to see animation):
We'll also be picking up the following skills:
- learning about the basics of
- setting pin
Equipping your coding utility belt.
As we start writing more complex programs with our Arduino we'll need to expand our coding vocabulary. Here are some terms you'll need in your
programming utility belt:
type of person doesn't like pizza?" 🍕
We've seen them, but haven't really talked about
types. It's the
keyword that's in front of our
void is a kind of type, think of types like different states of matter. You have a liquid types (milkshakes) and solid types (pizza 🍕), in computer programming you have things like
strings. The number types are, well, numbers. The most common numbers we will deal with are
integers, whole numbers that can be negative or positive (up to a point).
strings are a "sequence of characters", for example: "abc" is a string of three characters. Strung together like this!
They are usually surrounded by double quotes (bunny ears if you will):
"I LOVE PIZZA";
"Chill out, it's just an
We've already been using these left and right! Expressions are evaluated by the Arduino and can be
numbers, or mathematical operations like
2 + 2. We've seen it in our own code like the:
(Note the quotation marks!)
"Let's not get into an argument"
We've also been doing this all day long and it's not as antagonistic as it sounds. Arguments are just
expressions that are
functions. Say what?! Let's look at our good old buddy
The expression 9600 is being passed into Serial.begin(). In this case that number set's how fast we're going to communicate to the Arduino. In the case of Serial.println() the expression is "hello littleWorld". Some functions like the one we're going to cover in this tutorial require at least some amount of
arguments, usually greater than 1.
- data types are just different kinds of data. The Arduino needs to know if we're using numbers or a string of characters
- expressions are data that can be
evaluated to numbers, strings, or functions
- arguments is data that is
passed into functions, values like numbers or **strings.
On to the code!
int button = A0;
And the circuit!
P1 power bit to a
button and into the
You'll notice that there are some lines of code you haven't seen before. Let's check them out!
Variable: Buckets and buckets of data
Variables! Our first line of code is a
variable in this case a number or
integer that is assigned or given the name
button. You can name it whatever you want but just don't start the name with numbers or have spaces. If you need spaces in your name use underscores for now.
int button = A0;
Few things are going on here:
int is the
type of this variable, and is a number.
= is an
assignment operator. What you need to know about the assignment operator is that you're giving a
name (in this case the name is "button") to whatever is on the right side of the
=. Since button is an
int anything on the right side of the
assignment statement will be a
You'll notice that we're using the
number A0, if you check out the circuit we just built, there is a bit snap labeled
a0. That's the bitsnap we'll be using for our
src: khan academy
In it's most primitive sense Variables are labeled buckets of data that let us "hold" on to
strings or other kinds of
expresssions. Meaning that if you wanted to reuse the number
9600 but call it something like
speed_limit so it's more "human readable" then you would use a variable. For example:
String message = "I'd love a slice of pizza today";
assigned our pizza loving string to a
variable called message.
State your case!
One more detail to cover as we move on to the to even more tasty concepts. Each time we
assign a variable it's called a
statement. Cause they
state of a program, we'll talk about them in detail in the future.
New functions / commands for the Arduino (OH MY!)
Let's check out pinMode()
We know it's a function because it has
() at the end of it and we know it takes two arguments, the first being
button that is a
variable (which we assigned to A0) and a mode that's either INPUT (like a button or slider) or OUTPUT like a bargraph.
On to analogRead()
Now you'll notice we're encountering another function, this one is especially important.
It let's the Arduino tell us if a button has been pressed or not.
Let's flash our Arduino! (no, not that kind of flashing)
Once you've typed up the sample code, hit the upload (make sure your circuit is turned on before load the Arduino IDE)
button found in the tool bar. It looks like this:
flash the memory in our Arduino so it can run our program!
The Expected Outcome!
(click-through to see animation)
In your serial monitor, you should be able to see lots of 0s when you're not pushing a button and 1023s when you are.
Think of the button as a dam that is holding back the electricity from your power bit from flowing to your Arduino. When the button is pressed the current flows openly at full force (1023). The Arduino has the ability to read the flow of the current coming through.
1023? Yeah you know me!
1023 is an 10 bit binary value all that means is your analogRead input will be either 0 and 1023 when using a button. The Arduino assigns a value to the amount of current that is coming in through it's circuit. Try replacing the the button with a slider or a dimmer and see what happens!
You made it!!!
Congrats we're one step closer to being an Arduino pro!
Here's an emoji for all your hard work: 🎉
On to the next tutorial: you light up my life.