Chapter 1: Arduino Getting Started


Figure 1: The Arduino Uno R3

 

 

Chapter 1: Arduino Getting Started


Figure 1: The Arduino Uno R3

 

Why do you need the Arduino Classroom?

The official Arduino website is located at: www.arduino.cc (yes that is .cc and not a typo). With that website and reasonable Google skills you can find everything you could possible need or want to know about the Arduino - so why do you need the Arduino Classroom? Well if you've spent much time wading through the material available on the Internet you've seen that it is of varying quality and not well organized to promote stepwise learning. What you want is a logical progression for the presentation of information: a great reason to use a series of articles like these and websites like www.arduinoclassroom.com. Together they organize the materials for a sensible learning sequence and present labs with tested examples using a hardware projects kit. Having this structure helps you move quickly thorough what you need to know and provides you with a base of knowledge and skills that you use to pursue more advanced projects using what you find on the Internet. So let's get started!

What is a Microcontroller?

A microcontroller is a very small computer that has digital electronic devices (peripherals) built into it that helps it control things. These peripherals allow it to sense the world around it and drive the actions of external devices. An example of a use for a microcontroller is to sense a temperature and depending on the value sensed it could either turn on a fan if things were too warm or turn on a heater if things were too cool. You might already be aware that microcontrollers are in common devices like cell phones, microwave ovens, and alarm clocks that have buttons for you to input information and displays to tell you things. But there are even more microcontrollers embedded in things where you never see them. For example there are 30 or more microcontrollers in an automobile. These do everything from sensing the oxygen intake to setting the fuel air mixture to measuring the cabin temperature for controlling the air conditioning levels.

 What hardware you'll need

You'll need an Arduino board. I recommend the Arduino UNO R3 and you can get one from www.sparkfun.com or www.adafruit.com. Also you will need the Arduino 101 Projects Kit that you can get from the Nuts&Volts magazine and webstore and you can get it from www.smileymicros.com.

NOTE: 2/18/14 due to unforseen sales volume we are temporarily backordered on the Arduino 101 Projects Kit. You can do the exercises in the first two chapters with only the Arduino, but you'll need other parts to complete the exercises in later chapters. Sorry for the inconvenience.

What is an Arduino

Massimo Banzi, the Arduino team leader, begins his book, Getting Started with Arduino: "A few years ago I was given the very interesting challenge: teach designers the bare minimum in electronics so that they could build interactive prototypes of the objects they were designing." He summarizes his philosophy of "learning by tinkering" with a quote from www.exploratorium.edu/tinkering: "Tinkering is what happens when you try something you don't quite know how to do, guided by whim, imagination, and curiosity. When you tinker, there are no instructions - but there are also no failures, no right or wrong ways of doing things. It's about figuring out how things work and reworking them."

 

Arduino provides a great toolset for designers, tinkers, and anyone who sometimes just want to play with an idea that uses electronics. The genius of Arduino is that it provides just enough access to get specific tasks done without exposing the daunting underlying complexities.

 

What can you do with a microcontroller?

An Arduino looks like the board shown in Figure 1, but there are lots of varieties embedded in many interesting applications. Let's look at a few of these

 

 

Figure 2: Arduino based UAV

 

UAV

Chris Anderson, former editor of Wired magazine developed an Arduino derivative for remote control aircraft the ArduPilot for DIYDrones.com like the one shown in Figure 2. These are fully autonomous Unmanned Aerial Vehicles capable of performing a mission without a pilot in control.

 

Figure 3: Turn signal biking jacket

 

Electric Clothing

Leah Buechley, an associate professor at the MIT Media lab developed a variety of the Arduino, the Lilypad, shown in Figure 3 that can be used with fabrics. One clever application was to put it on the back of a jacket to provide turn signals for bikers

 

 

 

Figure 4: ArduSat

 

Arduinos in space?

See Figure 4 and why not?

 

 

 

Figure 5: Botanicalls

 

Tweet your plant

Botanicalls is an Arduino clone, shown in Figure 5, that lets a plant send you a Twitter message when it needs water.

Radiation Detectors on the Internet.

While getting a twitter message from your philodendron telling you that it is thirsty is pretty cool, how about having a bunch of concerned citizens who aren't confident that the government is telling them the truth about radiation exposure getting together and setting up an Internet based monitoring system themselves? Well, that is exactly what happened after the Tsunami at Fukushima, Japan causing the local nuclear power plant to fail. The folks at www.tokyohackerspace.org designed a Geiger counter (Figure 6) based on the Arduino and figured out how to get it to report radiation levels on the Internet (Figure 7).

 

Figure 6: Internet connected radiation detector.

 

 

Figure 7: Internet radiation reports

Pebble: Really Smart Wristwatch Project

Pebble is a wristwatch that uses an e-paper display and a wireless connection to coordinate its activities with either an Android phone or iPhone. This device does a lot more than just tell time; it can control your music, display email alerts, monitor time and distance when you run, and lots of other things you can see at www.getpebble.com. An Arduino and cell phone parts were used to build the prototype shown in Figure 8. The designers started a Kickstarter project to get funding to go into production and though they requested $100,000 they got a record breaking $10,266,846. Figure 9 shows the production Pebble.

 

 

Figure 8: Pebble prototype

 

 

Figure 9: Pebble product

 

Robots

Lots of robots are made with the Arduino, just Google "Arduino Robot" and see for yourself. Figure 10 shows an inexpensive tracked robot mainly made of foamcore board, hot glue, and tape. It uses an Arduino and motor drivers on a breadboard and line/edge sensors taped to the front. It is simple, cheap, and quite effective for demonstrating basic robotic principles.

 

Figure 10: Experimental Robot

 

These are just a few of the many things you can do with a microcontroller such as the Arduino. Spend some time on the Internet researching the Arduino and you will see the original question "What can you do with a microcontroller" might be better as: "What can't you do with a microcontroller"?

Lab 1: Visit the Arduino Website

 

Required tools:

A computer with access to the Internet.

 

Estimated time for this lab: 30 minutes

 

Check off when complete:

 

The Arduino website is divided into six main sections. First visit the Main Site arduino.cc/en/Main.

 

Visit the Getting Started section and get acquainted with want they have to say about themselves.

 

Visit the Learning section and note the extensive list of links to projects. This will come in handy later when you want to do things not discussed in this book.

 

Visit the Reference section. This provides links to the documentation for the Arduino functions library. We will learn about much of what is shown here.

 

Visit the Blog at arduino.cc/blog. This is where the team members post interesting things about the Arduino.

 

Visit the Playground. This is a wiki where Arduino users can contribute. This is another huge resource that you will want to use later as you gain more experience with the Arduino.

 

Visit the forum. This is a great place to get good answers to your questions. However, if you aren't familiar with typical forum etiquette I strongly recommend you first visits: How to ask questions that have a better chance of getting an answer: http://www.catb.org/~esr/faqs/smart-questions.html

Lab 2: Download the Arduino Software

You can get the Arduino software free from the Arduino website.

 

Required tools:

A computer with access to the Internet.

 

Estimated time for this lab: 30 minutes

 

Check off when complete:

 

Go to arduino.cc/en/Main/Software and download the latest software for your particular computer system. The Arduino software is often upgraded so you may see some differences in what is shown here and what actually happens, but the Arduino folks have been very good at keeping things simple and backward compatible so the differences shouldn't be major.

 

The following is for a Windows system as shown in Figure 11. The download for the Mac and Linux will be a bit different.

 

Click on your operating system:

 

Figure 11: Select Your Operating System

 

 

Save the file: Figure 12 shows the Windows dialog box that lets you save the file by clicking the Okay button.

 

Figure 12: Save the File

 

 

The file is placed in the Windows Download Directory as shown in Figure 13.

 

 

Figure 13: Window Downloads Directory

 

 

Click on the File menu and select the Extract All item as shown in Figure 14;

 

 

Figure 14 Extract All Step 1.

 

 

In Figure 15 you see the Dialog that shows where the zip file is currently located. You will want to unzip the file into a directory on your C: drive, so click the Browse button.

 

 

Figure 15: Extract All Step 2.

 

 

Browse to the C: drive as shown in Figure 16. Note that in this figure there are several earlier versions of Arduino installed, just ignore that and click on the "Make New Folder".

 

Figure 16: Extract All Step 3.

 

 

Name the new folder for the version of Arduino you are using, in the Figure it is Arduino-1.0.1 shown in Figure 17.

 

 

Figure 17: Extract All Step 4.

 

 

Click the "Extract" button and settle down for a bit of a wait since as shown in Figure 18, there are a lot of files that must be unzipped.

 

 

Figure 18: Extract All Step 5.

 

 

Open the Arduino directory and right click on the Arduino.exe file and select "Create Shortcut" as shown in Figure 19.

 

 

Figure 19: Create Shortcut

 

 

Drag the shortcut to the desktop and you'll have the handy shortcut shown in Figure 20.

 

 

Figure 20: The Arduino Shortcut

Lab 3: The Arduino IDE

The Arduino IDE (Integrated Development Environment) lets you write sketches (also known as programs) that provide instructions telling the Arduino board what to do and how to do it. The IDE has many features that let you select the particular type of Arduino you'll be using. This book uses the Arduino Uno R3, but one of the great things about Arduino is that the code you write for one particular Arduino variant may be easily changed to use with another variant. There are also many useful example sketches included in the IDE and looking at these examples is one of the best ways to learn how to use the Arduino. So let's get started with the IDE.

 

Required tools:

A computer with access to the Internet.

The Arduino IDE on your computer.

 

Estimated time for this lab: 15 minutes

 

Check off when complete:

 

You can open the Arduino IDE (Integrated Development Environment) by clicking on the shortcut (or the arduino.exe in the Arduino directory.) and you'll see the Security Warning shown in Figure 21. Just ignore it and click "Run" since the Arduino IDE is quite secure.

 

 

Figure 21: Ignore the security warning.

 

 

You will first see the Arduino wait panel shown in Figure 22.

 

 

Figure 22: Arduino wait panel

 

 

After a brief wait for the application to load you'll see the IDE when first opened as shown in Figure 23. (Note that it may vary slightly if you are using another version or seeing it on a Mac or Linux machine).

 

 

Figure 23: Arduino IDE

 

 

Take a moment to familiarize yourself with this IDE. Note that the top line says "sketch_july24a|Arduino 1.0.1". This identifies the name of the sketch (the program you'll write momentarily) and the version of the Arduino you are using. The next line has the menu selections for File, Edit, Sketch, Tools, and Help. We'll go into great detail on these in later labs.

 

The next line has six icon buttons as shown labeled in Figures 24 to 29.

 

Figure 24 shows the "Verify" icon. After you write a sketch, you'll click on this icon to verify (convert the sketch into a form that can be understood by the microcontroller. This is also known as compiling. A compiler is software that converts the human readable source code you type into the Arduino IDE into code the computer can run.)

 

 

Figure 24: Verify Button

 

 

Figure 25 shows the "Upload" button that is used to transfer the verified sketch to the Arduino.

 

 

Figure 25: Upload Button

 

 

Figure 26 Shows the New button that is used to open a new instance of the Arduino IDE containing a new blank sketch.

 

 

Figure 26: New Button

 

 

Figure 27 Shows the Open button that you use to locate and open an existing sketch [lets you find an Arduino program on your computer].

 

 

Figure 27: Open Button

 

 

Figure 28 Shows the Save button that you use to save a sketch to your Arduino Sketch directory (or wherever else you want to save it.)

 

 

Figure 28: Save Button

 

 

Figure 29 shows the Serial Monitor button that opens the serial monitor, an application on your PC that lets your talk to the sketch you've uploaded to the Arduino.

 

 

Figure 29: Serial Monitor Button

 

 

Now refer back to Figure 23 and notice the large white area in the middle with the "sketch_jul24a" tab above it. This area is a text editor that you will use momentarily to write your first sketch.

 

Also note the smaller black area near the bottom of Figure 23. This area provides feedback from the verification and upload process. You need to know that the verification and upload processes are done by two other applications that the Arduino IDE calls. These applications are fairly complex and they provide error messages that may help, but these messages can be very obtuse. We'll look at this in more detail later.

 

Finally on the very bottom of Figure 23 you see a line that identifies the Arduino we are using (Arduino Uno) and the Communication port for the PC (COM4). The COM port will probably be different on your set up.

 

Lab 4: Compile a Sketch to Blink an LED

Figure 1 shows the Pin 13 LED. The Arduino senses and actuates through its pins. It has a built in LED on pin 13 that we can blink with a simple sketch.

 

Required tools:

A computer with access to the Internet.

The Arduino IDE on your computer.

An Arduino board, preferably the Uno R3

 

Estimated time for this lab: 20 minutes

 

Check off when complete:

 

Place your cursor in the sketch editor and type the following:

 

int led = 13;

void setup() {
    pinMode(led, OUTPUT);
}

void loop() {
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
}

 

 


 

This is shown in Figure 30.

 

Figure 30: Blink Sketch

 

 

Click the "Verify" Button and you'll get a progress bar while the verification process takes place as shown in Figure 31.

 

 

Figure 31: Compiling the Sketch

 

 

And if you typed the sketch in exactly as shown you'll get the Done Compiling message shown in Figure 32.

 

 

Figure 32: Done Compiling

 

 

But if you made a mistake by, say typing dela instead of delay in the last line of the code as shown you'll see the line with the error highlighted and an error message in the lower black box as shown in Figure 33. If you add back the missing 'y' it compiles just fine.

 

 

Figure 33: Error "dela" not declared

 

Lab 5: Upload the Blink LED Sketch

Now that we have the verified sketch we can upload it to the Arduino to blink the LED attached to pin 13.

 

Required tools:

A computer with access to the Internet.

The Arduino IDE on your computer.

An Arduino board, preferably the Uno R3

 

Estimated time for this lab: 15 minutes

 

Check off when complete:

 

Hook a USB cable between your PC and Arduino as shown in Figures 34 and 35

 

Figure 34: USB Cable attached to the PC

 

Figure 35: USB Cable attached to the Arduino

 

 

Click on the Tools menu item and select Serial Port, then click on the checkbox for the port as shown in Figure 36.

 

 

Figure 36: Tools Serial Port

 

 

If more than one Serial Port are indicated then you should close the Tools/Serial Port item, unplug your Arduino Then reopen the Serial Port to see which port has gone missing. Then you plug your Arduino back in and select that port as shown in Figure 37.

 

 

Figure 37: Multiple Serial Ports

 

 

Now Click on the Upload button in your Arduino IDE and watch the TX and RX lights on the Arduino board. They should rapidly blink for a few moments indicating that the PC and the Arduino are talking to each other in order to upload the program.

 

Now look at the Pin 13 LED as shown in Figure 38 and it should be blinking on and off once a second. Congratulations you've programmed an Arduino!

 

Open the File menu and click on Save As and save this sketch as MyBlinky.

 

Open the File menu and click on Sketchbook and note that your program has been saved.

 

 

Figure 38: Pin 13 LED blinking

Lab 6: Using an Example Program

The Blink sketch that you just typed into the Arduino editor is taken from an example that you can load from the Examples item under the File menu item. Yes, you didn't really need to type in that program, but the exercise gave you some insight into both how easy it is to write a program and how easy it is to make mistakes.

 

Required tools:

A computer with access to the Internet.

The Arduino IDE on your computer.

An Arduino board, preferably the Uno R3

 

Estimated time for this lab: 10 minutes

 

Check off when complete:

 

Click on the File menu item and select Examples\Basics\ Blink as shown in Figure 39.

 

 

Figure 39: Select Blink Example

 

 

Read the Blink program shown in Figure 40 and pay special attention to all the comment (Gray words following // or bracketed by /* and */). When you typed in the program you weren't asked to add these comments, but in the future you'll want to be very liberal with your comments since they will help others understand your code and may help you in six months when you've forgotten why you did something in your code.) Comments are a critical part of project documentation.

 

 

Figure 40: Example Program - Blink

 

Lab 7: Using the Reference

The Arduino IDE has a built-in reference book that provides explanations for most of Arduino's features. This is an extremely valuable tool to help you learn about the Arduino. Let's see how to use it.

 

Required tools:

A computer with access to the Internet.

The Arduino IDE on your computer.

An Arduino board, preferably the Uno R3

 

Estimated time for this lab: 10 minutes

 

 

 

Figure 41: Select Reference

 

Check off when complete:

 

Open the Help menu and click on Reference as shown in Figure 41.

 

The Arduino Reference opens in a browser as shown in Figure 42.

 

Take a few minutes to look at the various sections and click on a few links to see how to navigate among the various terms and their web pages.

.

 

Figure 42: Reference in Browser

 

 

Finding Terms in the Reference

 

Arduino has a very helpful feature that allows you to highlight a word and then see if you can find it automatically in the reference.

 

In the Blinky program, highlight the term pinMode as shown in Figure 43.

 

Open the Help menu and click on Find Reference as shown in Figure 43.

 

The Arduino Reference opens on the entry for the term in a browser as shown in Figure 44.

 

Take a few minutes to look at the various sections and click on a few links to see how to navigate among the various terms and their web pages.

 

Figure 43: Find in Reference

 

Figure 44: pinMode Found in Reference

 

Summary

In this chapter you were introduced to the following Arduino concepts:

  • Microcontrollers in general.
  • The Arduino hardware and software development system.
  • The Arduino web site.
  • Where to get the Arduino IDE and how to install it.
  • The Arduino IDE.
  • How to write a sketch to blink an LED.
  • How to upload a sketch to the Arduino hardware.
  • How to find and use the built-in Arduino reference.

Exercises

1. As you may have guessed, the Blink program function: delay(1000); caused the LED to be off and on for one second each. You can further guess that the 1000 is for milliseconds since 1000 millisecond is one second. Now modify the program so that the LED is on for 2 seconds and off for ? second.

2. Using the Examples menu, find a sketch that blinks the LED without using the delay function.

3. Use Google to find the delay() function documentation on the Arduino website.

4. Search the Arduino Forum for threads about the delay() function.

 

Questions

1. In your own words, what is a microcontroller?

2. Give three examples of things you can do with a microcontroller.

3. Where is the microcontroller on the Arduino board?

4. What are two good signs that a device has a microcontroller in it?

5. What do you use the Arduino pins for?

6. Where would you go on the Internet to get help with an Arduino problem?

7. What is the difference between the Arduino IDE and Board?

8. How will you know that your sketch is okay?

 

Solutions

Q1: A microcontroller is a tiny computer that can be used to sense and control things.

Q2:

  1. You can sense the temperature in a room and turn on a fan if it is too warm.
  2. You can read a button on a remote control and send a signal to change the channel on a television.
  3. You can control the direction and speed of a robot.

Q3: It is the large integrated circuit on the lower right of the Arduino board.

Q4: One is that it has buttons to accept user input and another is that it has a display to show information to the user.

Q5: To sense or control things.

Q6: www.arduino.cc\forum

Q7:The Arduino IDE is the software application that runs on a PC and is used to write and upload sketches to the Arduino Board. The Arduino Board is the hardware the contains the microcontroller that runs the sketch uploaded from the PC.

Q8: You will see a "Done compiling" message near the bottom of the IDE and no red error messages.

 

 

Supplementary Questions and Answers:

 

NOTE: these are here temporarily until I get a quiz module installed where I put these sorts of things. In the meantime if you think of some good quiz questions please post them on the forum.

What is a peripheral?

Digital electronic device built into the microcontroller to help it control things.

A microcomputer has is a tiny computer with what to help it control things?

Peripherals.

What does a microcontroller use to sense the world around it?

Peripherals

What does a microcontroller use to drive the actions of external devices?

Peripherals

Name three things that contain microcontrollers.

Automobiles, cell phones, microwave ovens, televisions, video games, alarm clock, ATMs, etc.

What is the Internet address for the Arduiono website?

www.arduino.cc

Where would you look on the Arduino website for information on the Arduino language?

The reference section.

What is the Arduino Playground.

It is where anybody can post Arduino projects

Where on the Internet would you go to ask a question about the Arduino?

The Arduino Forum.

What does IDE stand for?

Integrated Development Environment

What is an IDE?

It is a software application that is used for microcontroller development.

What does the IDE Verify button do?

It converts the sketch into a form that can be understood by the Arduino.

What is another word for "Verifying" in the context of microcontrollers?

Compiling.

What does the IDE Upload button do?

It verifies the sketch and if it is okay, it sends the sketch to the Arduino.

What does the IDE New button do?

It opens a new instance of the Arduino IDE application.

What does the IDE Open button do?

It lets you open an existing Arduino sketch.

What does the IDE Save button do?

It lets you save your sketch to your PC.

What does the IDE Serial Monitor button do?

It opens an instance of the Arduino Serial Monitor.

What is the large white area in the IDE used for?

It is used to write and edit a sketch.

What is the black area at the bottom of the IDE used for?

It is used by the verification process to display progress and/or errors.

Describe the steps for creating a sketch and sending it to an Arduino.

  1. Write the sketch in the editor.
  2. Click the Verify to verify that the sketch doesn?t have errors
  3. Click the Upload button to send the sketch to the Arduino.
  4. NOTE: the Upload button will "verify" before trying to upload so you can do steps 2 and 3 in one step.

How do you mark comments in your sketch?

You precede them with // or you bracket them with /* */

If you are curious about an element of the Arduino sketch such as the word pinMode, how would you find out more about it using the IDE?

  1. You could open Help\Reference and see if it is in the list.
  2. You could highlight the word and then click on Help\Find in Reference. 

 
Remember that all the components used in the Arduino 101 series are available from the Nuts&Volts magazine web site. And if you have any questions about this series, please don’t hesitate to visit the forum on www.arduinoclassroom.com and ask.