Learn
Arduino code is written in Processing, a language based loosely on Java. If you know Java, it’s not that hard to get the hang of; there are just a few Arduino-specific quirks you need to learn. We won’t cover programming basics on this page, but we will go into the quirks. Hope this helps!
In the Arduino world, each separate class is saved in a sketch. When you download the Arduino software, it comes with a bunch of example sketches – check under File >> Examples. To test your Arduino with no hardware, open the Blink sketch. You can click the check mark in the upper left corner to make sure all the syntax looks good, then press the sideways arrow to upload the sketch to your Arduino. A loading bar will appear in the bottom of the sketch screen. If something goes wrong, it’ll give you an error message at the bottom. Once your sketch is uploaded, it’s saved in the Arduino’s memory and will run whenever your Arduino has power. Easy, right?
Arduino
Arduino
When you’re working with Arduino, it’s hard to remember which component is plugged into which pin. That’s why we use class variables to store the information up front. At the top of every program, you’ll want to declare variables to store the components plugged into each pin. Digital pins are represented by integers – for example, if you have an LED in digital pin 13, you’d say int ledPin = 13. For analog pins, it’s still stored as an int, but you’ll need to put an A in front of the pin number. If you have a potentiometer in analog pin 2, for example, you’d say int potPin = A2. You can make these variables constant so they won’t change halfway through your program, and then whenever you want to send a command to that pin, you can send it to ledPin or potPin instead of having to remember what’s plugged into pin 13.
Every Arduino program must contain at least two methods: setup() and loop(). The setup() method is called only once, when the program first begins. This is a good place to initialize any extra variables you might have. You’ll also need to tell the Arduino which pins are input and which are output. You do this by calling pinMode(pinName, INPUT/OUTPUT). If you have a declared ledPin variable and you want it to be output, for example, you’d say pinMode(ledPin, OUTPUT). This is also where you initialize the Serial monitor, which is where you can print things out to your computer screen. The command for that is Serial.begin(9600), where 9600 is the rate at which it’ll check for new input. If there are any other methods you need to call to set up your program, do that in this method as well.
Arduino
Arduino
Loop() is also necessary for every Arduino program. It’s called repeatedly as long as your Arduino is powered on. If you have an operation that’s repeated over and over, you put it in the loop() method. However, if you’re trying to do something like blink an LED, you don’t want the Arduino to continually call run, or it’ll turn the LED on and off so fast that you won’t even be able to see it changing. Instead, you use the delay method to pause the program momentarily. Delay takes as its arguments the number of milliseconds to pause. If you wanted to turn an LED off for 1 second, for example, you’d say delay(1000). If you know your program is supposed to be doing something but it doesn’t look like it is, check if you missed a delay and it ran too quickly for you to see!

The other thing to remember in the loop() method is to make sure you cover all your cases. If you’re checking if one of two buttons is pressed, you’ll have to write an if statement to check if the first is on, if the second is on, or if both are off, and write commands for all of them. All your variables have to be in some state at all times. Basically, you’re building a state machine, where the Arduino’s behavior is perfectly defined for each state. If you don’t give part of the Arduino instructions for one of your states, it’ll get confused and do something you don’t expect. Always give all your variables a value, and you’ll be fine!
Digital input/output is binary valued – on or off. The Arduino language writes this as HIGH or LOW. The methods to get or set digital pins are digitalRead and digitalWrite, respectively. To explain these methods, let’s pretend we have two LED variables, one stored in ledPin1, and one in ledPin2. If we wanted to check if our first LED is on, we’d write int state = digitalRead(ledPin1). That would store either 0 for LOW or 1 for HIGH in the variable state. If we wanted to turn the second LED on, we’d say digitalWrite(ledPin2, HIGH). Digital is pretty straightforward!
Arduino
Unlike digital values, analog values come in a range. The methods you’ll use to store and output these values, though, are still similar: analogRead and analogWrite. The only difference is that you’ll pass in a number instead of HIGH or LOW. The range of input given by analogRead is 0-1023, and the range of output you can write to a PWM pin is 0-255. Worried about the math to convert from one to the other? Never fear; you can use the map function to do it for you! The prototype for the math function is map(value, fromLow, fromHigh, toLow, toHigh). For instance, if you read in an analog value stored in int val and wanted to output it to another analog pin, you’d say int newVal = map(val, 0, 1023, 0, 255), and it would give you the necessary number to output. It’s a little annoying, but you’ll get used to it!
Arduino
Arduino
In most programming languages, you can print output to the console. With Arduino, you can print to the Serial monitor. You initialize it in the setup() method by including Serial.begin(9600), which tells the Serial pins to start communicating at a rate of 9600 bits per second. Then, whenever you want to print something out, you can say Serial.println(“Hello World!”), and your output will show up onscreen. You open the Serial monitor by pressing the button in the top right corner of the sketch that looks like a magnifying glass. It won’t open automatically; you’ll have to do it after you upload your sketch to the Arduino. The Serial monitor is great for debugging; you can print your input or output to make sure you’re getting the right values, or use keyboard input to control different parts of the Arduino. Experiment with printing to see what you can create!
When using a speaker, you’ll need to use the tone and noTone methods to control what note is playing. The prototype for tone is either tone(pin, frequency) or tone(pin, frequency, duration) if you only want the tone to be playing for a certain amount of time. As you can imagine, it’s a little annoying to have to constantly remember which frequency corresponds to which note. To fix this, we generally define constants to keep track of different frequencies. For example, if you wanted to play a 4th octave C, you could write #define NOTE_C4 262, and then whenever you want to play C4, you just have to say tone(speakerPin, NOTE_C4) and it’ll work. To stop a speaker from playing, use noTone(pin).
Arduino
The default Arduino methods are great, but sometimes you might want to do something a little more complicated. That’s what libraries are for. A library is a collection of methods used to do something specific, like talk to a certain sensor. For instance, if you use a Max7219 to control several 7-segment displays, you’ll need to use the LedControl library. You can download libraries off the Arduino website. They should come as .zip files – don’t unzip them yet. Instead, go to Sketch >> Import Library >> Add Library, navigate to your zip file, and press choose. Whenever you want to use methods from that library in one of your sketches, make sure you import the library at the top of your sketch. To import the LedControl library, for instance, you’d type #include “LedControl.h.” Then you can use any of the LedControl methods you want.
Arduino



<-- Previous: Hardware Start Projects!