Cover

Chapter 1 Introduction and Getting Started

Chapter 1  Introduction and Getting Started

Nowadays, microcontrollers are everywhere around us. For many years, doing something with them took a lot of knowledge that had to be accumulated over a fairly long period of time. Using microcontrollers was also relatively expensive, so most of the time they were used by trained professionals in various industries.

This changed a lot back in the 2005 when the Arduino board came out. The whole idea behind Arduino is it provides hobbyists around the world with a relatively inexpensive means for building their electronics projects. A lot could be said about the Arduino, and the microcontrollers in general, but this is a succinct book so we’ll keep it short and to the point. Keep in mind that there are a lot of varieties of Arduino boards out there. You can look them up on the Arduino website here.

Describing every component is outside of the scope of this book. All of the examples in this book are going to be made with the Arduino Uno board. For more details on the Arduino Uno board, see this page on the Arduino website. This board is best suited for people starting out because it is relatively inexpensive and doesn’t require any soldering or special power source to get you started. Basically, all you need is an Arduino Uno board, USB cable, and a computer.

Note: You have to acquire an Arduino Uno board to follow along.

As we start using the electronic components, I will present you with a parts list at the beginning of every section where needed. The examples will be described with a sketch and a table showing how a component should be wired. In the final section, we will talk about networking. In that section, we will use two Arduino boards. Once again, one Arduino is perfectly fine and you will be able to follow along for most of the book. But at the end of the book, you will need two of them.

The examples in this book do not have any special preferences when it comes to the electronic components with which we will be using. You are free to get them from whatever source is best suited to you, financially or otherwise. The circuits that we’ll be making won’t use any external power source other than the Arduino itself. For someone who is just getting started with the Arduino, using strong currents might be a bit dangerous, might ruin the equipment, and might harm you. So we’ll just stick with the power coming from your computer and the Arduino Uno board.

To some of you, the electronic components that we’ll use might be unfamiliar because you’ve never heard about them, but don’t worry, we’ll give a simple explanation of the basic working principles with every one of those components when we mention them for the first time. If some of the concepts still aren’t clear to you, please look at the Arduino website before reading further.

The purpose of having the Arduino board is to program it to do something, so it is desirable that you have at least basic programming knowledge. It might be a bit hard for you to follow along without basic programming knowledge. Knowledge of the C programming language or any other similar language is also desirable.

We’ll also try to keep the programs simple and will make many comments in the code so that you can follow along. Getting started with Arduino programming is relatively easy. There is a free Arduino integrated development environment (IDE) available for download on all of today’s most popular operating systems.

We will use the IDE because it will make the upload of our programs to the Arduino board easy. Basically, there will be just a couple of clicks needed and then we’ll see the magic happen as our code will actually start to control real-world objects such as lights and buzzers.

Before doing anything with the board, we have to install some software that will enable us to program it. In the next couple of sections, we’ll go through the installation steps for the most popular operating systems. We’ll start by using Microsoft Windows.

Installing Arduino IDE on Windows

Installing Arduino IDE on Windows

At the moment of writing this book, Microsoft Windows 7 still seems to be the most popular Windows operating system (OS) on the market. The next most popular Windows OS is Windows XP. So we’ll concentrate on how to install Arduino IDE for Windows 7. There is an installer for Windows available on the Arduino website here. Download the installer and run it. There will be a couple of steps to take in order to install it on the Windows platform:

Figure 1: Allow the application to make changes to the computer by clicking Yes

Figure 2: Agree with the License Agreement by clicking “I Agree”

Figure 3: Select Installation Options and click Next

Figure 4: Select Installation Folder and click Install

 

Figure 5: Allow the installation of the Arduino USB Driver by clicking Install

Figure 6: After the installation completes, click Close

If you followed the steps shown in previous figures, by now you should have a functional Arduino IDE on your Windows machine. Start the Arduino IDE by clicking on Arduino in the Start menu or by double-clicking the Arduino icon on the desktop. You should see something like the following:

Figure 7: Arduino IDE

When you plug the Arduino in for the first time, you might wait for some time until Windows recognizes and installs the drivers for the Arduino Uno board. This is pretty common; just wait for a while, and you should be up and running with your Arduino in no time. This is as far as we’ll go for now. At this stage, the most important thing is that we have an installed Arduino IDE. Later we will cover how to actually hook up the Arduino Uno board with the Arduino IDE and how to upload the code to Arduino.

Installing Arduino IDE on Linux

Installing Arduino IDE on Linux

The installation on the Linux platform is not as straightforward as it is on the Windows platform. There is a prerequisite needed before installing and running the Arduino IDE. This prerequisite involves having an installed Java runtime environment. There are a lot of Linux versions available. Around one-third of desktop Linux machines run on Ubuntu. We’ll cover how to install Arduino IDE on Linux Ubuntu in this section. The most basic Java installation for the Linux Ubuntu is to start the terminal and then run the following commands:

After you’ve checked if Java is present on your system (or after you installed it from scratch), it’s time to download the IDE from the Arduino website here. Click the download link for the Linux version.

Be careful to check if your system is 32- or 64-bits and download the appropriate version from the website. If the versions of Linux are not matching your system, you will not be able to upload the programs to Arduino, and you will get an exception when trying to do so.

The Arduino IDE download for Linux is compressed. Open it after downloading it and extract it to a folder where you usually install your applications. Make sure that you remember the folder where you extracted the Arduino IDE, and then navigate to the folder where you extracted the Arduino IDE. Run the arduino executable file by double-clicking it. The system will ask you the following question:

Figure 8: Click Run

If you are running the IDE from the terminal, you will not get the dialog shown in the earlier figure. There is one more step before you actually start working with the Arduino IDE. The IDE will also ask you where to store the newly created sketches. In short, sketches are source code files for programs that can be uploaded to Arduino boards:

Figure 9: Specify a folder where IDE is going to save sketch files

Installing Arduino IDE on Mac OS X

Installing Arduino IDE on Mac OS X

On Mac OS X, Java is a prerequisite for running Arduino IDE. Java is not available on the Mac OS X by default—at least not with the most recent version. There are multiple ways you can bring Java to your Mac. Some of the previous versions of Mac had it installed by default. The easiest way to get started with Java on Mac is to go to the Apple support page available here, and download and install the provided files. As with the Linux and Windows versions, the Arduino IDE is available on the Arduino website here.

The Mac OS X Arduino IDE is compressed by default. Extract the archive to a folder and remember where you extracted it. The rest of the installation is easy; just take the extracted application and copy it to Applications folder. Then, run the Arduino the same way as you would run the other applications installed on your Mac:

Figure 10: Drag the extracted application to the Applications folder

Once the Arduino IDE is started, it’s pretty much identical no matter what platform you are using. In the next section, we will cover the basic Hello World of Arduino programming.

Arduino Uno Hello World

Arduino Uno Hello World

This is the first section in which you will need the Arduino Uno board to follow along. Besides the board, you will also need an A to Mini-B USB cable. There are two approaches that you can go by at the moment. One approach is to hook the Arduino to the USB and see what’s going on when you plug it in to your computer.

A second approach is that you can get to know the parts of the board before you plug it in. Getting to know the components on the Arduino Uno board might not seem as important in the beginning, but as you progress, you will have to get to know the parts well. It might seem a bit complicated at first, but there are a couple of things of which you should be aware. Let’s take a look at the Arduino:

Figure 11: An Arduino Uno board

 

Take a moment to get acquainted with the board. Try to locate the elements located near the letters marked in the previous figure.

The following are Arduino Uno board elements that we will use:

  • A – USB cable socket. Through this cable we power Arduino and upload programs.
  • B – Reset button. We use this button to restart the uploaded program.
  • C – Arduino digital pins. How these work will be explained in later chapters.
  • D – Arduino power pins. These will be used to power electronic components.
  • E – Arduino analog pins. How to use them will be explained in later chapters.
  • F – TX and RX Light Emitting Diodes (LEDs) indicate Arduino communicating over USB. L is programmable.
  • G – Power-on light. Arduino should ignite this LED as soon as it has power.

It’s perfectly fine if you don’t remember all of these elements in the beginning. The more you work with the board, the more you will be acquainted with it. At the moment, the only important element is the USB cable socket. Plug the USB cable to your computer and then to the USB cable socket on Arduino. Once again, you should use the USB A to Mini-B cable. If you are not sure what kind of cable that is, ask somebody working at your nearby electronics store. The cable should look exactly like (or similar to) the following:

Figure 12: USB cable type A to Mini-B

Plug the cable into the Arduino Uno and connect it with your computer. If this is the first time ever that you plugged your Arduino to the computer, you should see a small, yellow LED marked with a letter “L” going on and off at regular intervals. It’s near the area marked with a letter F on the figure that explains the basic parts of the Arduino Uno board.

This is actually an Arduino Hello World program that is running. All of the official Arduino Uno boards come with this program pre-uploaded. And this program runs as soon as the Arduino Uno board is plugged in. That way, you can quickly determine if everything is fine with your Arduino board. If, however, the LED is not going on and off, don’t worry. Perhaps you or somebody else uploaded something to it already. Anyway, we will go through the steps needed for you to get this LED blinking.

The previous section explained how to install the Arduino IDE. Please run the IDE now if you didn’t already do it. There are two steps that you need to do every time you upload a program to the Arduino Board. First, make sure the board type is set up properly in the Arduino IDE. Go to the Tools menu and then to Board, and select Arduino Uno to properly set the type.

The second step is to select the Serial Port (it’s underneath the Board menu option). Depending upon the type of OS that you have installed, the serial port might have a different name. On Windows, it will be COM followed by a suffix. On Linux and Mac, it will be something more in the direction of /dev/tty.xxxxxx or something similar. If you plugged in the Arduino Uno on Windows when the Arduino IDE is already running, you probably won’t be able to select the Serial port. Restart the Arduino IDE if this happens. Now that we have the Arduino connected, let’s do some programming. Enter the following code into the Arduino IDE:

// setup runs just once

void setup() {

  // initialize digital pin 13 as an output.

  pinMode(13, OUTPUT);

}

 

// this loop runs for as long the Arduino has power

void loop() {

 

  // turn the LED on with HIGH voltage level

  digitalWrite(13, HIGH);

  

  // wait for a second

  delay(1000);

  

  // turn the LED off by making the voltage LOW

  digitalWrite(13, LOW);

  

  // wait for a second

  delay(1000);

}

We’ll explain the code a bit before we continue. The onboard LED is bound to the digital pin

Impressum

Verlag: BookRix GmbH & Co. KG

Tag der Veröffentlichung: 28.12.2015
ISBN: 978-3-7396-2953-7

Alle Rechte vorbehalten

Widmung:
Microcontrollers like Arduino provide a great introduction to physical computing, allowing you to design: environment sensors and controls; visual and auditory alerts based on input; and devices comprising the Internet of Things. In Arduino Succinctly, author Marko Švaljek explains the fundamentals of the Arduino Uno board and how it interacts with common components. Table of Contents Introduction and Getting Started Building Circuits with LED's Working with Buttons Using Buzzers Measuring Environment Conditions Detecting Objects Networking Conclusion

Nächste Seite
Seite 1 /