AIR: Getting started with Arduinos

Table of Contents

1 Install the Software

Your first step: install onto your laptop the software that you'll need to program Arduinos. You'll find instructions on how to do so from a link called “Software Installs” on the course Moodle site.

The installs page also contains instructions on how to install some additional packages that we won't need until later in the term. Skip those for now.

Before moving on to the next section, you should have the Arduino development environment installed and it should start up successfully. Ask for help if you need it.

2 Look at a sample program and test the software

Connect your Arduino's power cable and plug it in to a regular power outlet. After you see some lights turn on, plug your Arduino into into your computer via a USB cable. ALWAYS make sure to give your Arduino external power first, and make sure that the physical switch is said to "on," before plugging it into your computer.

Start up the Arduino development environment. First, get it configured:

  1. In the Tools menu, go to Board, and choose "Arduino/Genuino Mega or Mega 2560." That's the choice to make that's consistent with the particular Arduino that we're using.
  2. In the Tools menu, go to Port, and choose the serial port that matches your Arduino. This can vary a bit from computer to computer, and is sometimes a little tricky to identify. On a Mac, it will be the one that looks like either /dev/cu.SLAB_USBtoUART or /dev/cu.usbserial-xxxx (where xxxx is a bunch of numbers and letters). On a Windows computer, it will be one of the higher numbered COM ports. A great trick for both types of computers is to unplug your Arduino USB cable, wait a minute, then open up the Serial Port menu to see what's there; then close the menu, plug your Arduino back in, and then look at the menu again. Whatever is new in the menu after you've plugged your Arduino in is likely it. Ask for help if you need it.

The Arduino software has a number of built-in sample programs to help get started. To test to see if your software is set up properly, we'll try installing and running the first one. In the File menu, go to Examples, then Basics, and then choose Blink. Read through the program that you see, and try to make some sense of it.

See if you can get this program to run on your Arduino. To do so, click on the "Upload" button in the Arduino environment: it's the second button from the left, which looks like an arrow pointing to the right. This should take a few seconds, and you should see some lights blinking on your Arduino while your program is transferred. If all goes well, you should see “Done uploading.” in the Arduino development environment status bar, and the pin 13 LED light on your Arduino should start blinking on and off. Close the Blink sketch window when done.

Before moving on to the next section, you should be able to successfully transfer code to your Arduino, and get the LED light blinking. Ask for help if you need it.

3 Modify the sample program

The program that you saw earlier for making the Arduino LED light blink is written in a programming language. The particular programming language that Arduinos used is generally uncreatively referred to online as “the Arduino programming language.” It turns out that this language is just a subset of the well-known general purpose language C++. We'll be doing most of our programming this term directly in this language. We'll start doing some brief coding here. The Blink program that you pulled up from the built-in examples is read-only, so we're going to copy so you can modify it

  1. In the Arduino app, create a new sketch by choosing "File, New" from the menu bar. Then copy into it all of the code from the Blink sketch that you found in the previous section.
  2. Save your sketch by choosing "File, Save" from the menu bar. Choose a name for your sketch, and a location where you'd like to save it.
  3. Download your sketch to the Arduino just as you did in the previous section.
  4. Now see if you can modify your code so the Arduino can blink twice as fast. Download your code to the Arduino and see if it works. Ask for help if you need it.

4 Make music

Let's get the Arduino singing! Find a multi-frequency "beeper" brick in our supply. It looks almost exactly like the single-frequency "buzzer," which we're not using, but they work differently internally. Find a "3s to 3s" cable. These are three wire cables, consisting of red, white, and black wires, that have 3-prong connectors at each end..

Plug in one end of the connector into the beeper brick. Orient the connector so that the pin labeled S ("signal") is connected to the white wire, and the pin labeled with a minus sign ("ground") is connected to the black wire. Plug the other end of the connector into the three pins labelled as D12. Orient the connector so that the white wire is towards the center of the board, and the black wire is near the edge of the board. Ask for help if you're having trouble finding D12, or unsure how to orient the wire.

Create a new Arduino sketch. Then copy and paste in the following program:

int ourPin = 12;
void setup()
{
  pinMode(ourPin, OUTPUT);
}


void loop()
{
  tone(ourPin, 440);
  delay(1000);
  tone(ourPin, 523);
  delay(1000);
}

Upload your program to the Arduino. If all goes as planned, you should hear the beeper emitting some tones. Try modifying this code to create your own song.