Arduino for the Terrified – Part 1

by

Jack Purdum, Ph.D.

W8TEE

Strange title, isn’t it? Yet, that’s often the impression I get when I mention using an Arduino microcontroller (c from now on) in a ham radio project. Distilled to its essence, the most common reason I hear for not investigating and using an Arduino is: “But I don’t know how to program!”

Yeah…so what?

You’ve already proved that you are smart enough to get a ham license. Couple that with the ability to fog a mirror and you pretty much have the qualifications necessary for using cs in your projects. When you started preparing for your license exam, did you know how to draw the schematic for a Colpitts oscillator? Could you calculate the length of a half wave dipole on 40M? For many of us, probably not, but you took the time and made the effort to learn what you needed to know to pass the exam. What you need to know about an Arduino is probably less difficult than what you went through to get your license. How so?

First, the Arduino family of cs is build upon the concept of Open Source resources. Open Source is a movement/philosophy which means that virtually everything from the software applications you use to write your own programs to the details of the Arduino chip design is openly available to anyone. As a result, there’s about a bazillion lines of program source code out there just waiting for you to use it. Program source code is a collection of English-like program statements, often written in the C programming language, that cause the controller to do your bidding. In addition, there are hundred of task-specific mini applications, called libraries, that are the basic building blocks of program development. There are libraries to use LCD displays, touch screen displays, stepper motors, finger print readers, motion-heat-fire sensors, GPS coordinate libraries, proximity sensors, laser controllers…you name it and there’s probably a support library for it. As a result, program development is like building an object with Legos. Find the right Lego, snap it into place, and repeat until done.

Second, mistakes are normally non-destructive. Unlike hardware mistakes where, once the white smoke gets out, that chunk of hardware is done, software mistakes manifest themselves as simply producing a result that doesn’t match what you want. Fixing mistakes simply means tracking the error, or program bug, down and correcting it. Even if you’d make a really BIG flat-forehead mistake (you know, the kind where, once understanding the mistake, you slam the heel of your hand into your forehead and ask “How could I make such a mistake!”) and that mistake does “brick” (i.e., destroy) an Arduino board, the Arduino that we’ll be learning on can be replaced for about $3.

Third, the tools to write programs are completely free. Most popular platforms are supported (e.g., Windows, Mac, and Linux). There are dozens of good books that provide more information if you need it, plus there are literally thousands of free programming tutorials on line. In this series, we will give you enough to get you started. If you have a particularly nettlesome question, there is an Arduino user group Forum that is designed to answer those questions. (See: http://forum.arduino.cc/index.php?board=4.0. Make sure you read the post titled How to Use This Forum before you post a question.) Simply stated, there’s a lot of help out there if you need it.

Finally, many of you are going to discover a new sense of accomplishment when your program does what you want it to do. It’s not too different from making your first QSO with a piece of homebrew equipment. I spend a lot of time on the above-mentioned Forum simply because I truly enjoy programming. You might discover that you enjoy it as well.

I guess what I’m saying is: Give it a shot. For an investment of a couple of dollars and a little of your time, you may discover a new venue for augmenting the enjoyment you get from ham radio. Like it or not, software is a growing element of our hobby…just look at the number of rigs that have a least some element of SDR (Software Defined Radio) in them. You may as well stick a toe in and test the waters.

Which Arduino to Buy?

Actually, there is a whole family of cs that find a home under the heading “Arduino”. Table 1 shows some of them, plus a number of key elements of each. All of the members of the Arduino family have

Micro controller

Flash memory (bytes)

SRAM(bytes)

EEPROM (bytes)

Clock speed

Digital I/O Pins

Analog input pins

Voltage

Price (Approx.)

Arduino Uno

32K

2K

1K

16Mhz

14

6

5V

$8.00

Arduino Nano

32K

2K

1K

16Mhz

14

8

5V

$4.00

Digispark

16K

2K

1K

16Mhz

14

10

5V

$2.00

RoboRED

32K

2K

1K

16Mhz

14

6

5 or 3.3V

$15.00

ATmega1280

128K

8K

4K

16Mhz

54

16

5V

$15.00

ATmega2560

256K

8K

4K

16Mhz

54

16

5V

$10.00

Arduino Leonardo

32K

2.5K

1K

16Mhz

20

12

5V

$8.00

Arduino Due

512K

96K

84Mhz

54

12/21

3.3V

$16.00

Table 1. Atmel cs commonly used in Arduino boards.

three types of memory, have a clock speed of 16Mhz (except the Due), and can operate from a 5V source. (Actually, they can tolerate voltages between 5V and 17V, but need 5V to function properly.) As you can see, you have quite a few choices, but none are prohibitively expensive. Also, they all use the same programming tools to write programs.

So, which one should you buy? It depends on what you want to do with it. Recently I’ve been working on an antenna analyzer and started out using a Nano, mainly because of its small size. (See Figure 1. The Nano is the thumb-sized board at the center-bottom of the figure while the Uno is in the top-left of the figure.) However, as I started adding more and more features, I exceed the amount of memory needed for the program, so I upgraded to the Atmega2560 (center-top of Figure 1). The great thing about the Arduino family is that most code is upward/downward compatible.

Typically, your choice is most influence by available memory and the number of Input/Output (I/O) pins available. Let’s look at the memory types first.

Figure 1. Some Arduino boards.

Flash Memory

Flash memory is the memory that is used to store your program code. If you look at Table 1, you’ll see that the most popular Arduino boards (Uno and Nano) only have 32K of memory for your program. In an age where PC’s have a mega-munch of memory, how can you do anything in 32K? You’d be surprised! My article in the March, 2016, issue of QST describes building a 40M, 3W, CW transceiver with VFO and LCD display and the code that drives the rig uses less than 9K of flash memory…plenty of room left over for adding more features (e.g., a keyer?).

Another feature of flash memory is that it is non-volatile. That is, when power is removed, the content of flash memory remains unchanged. When power is reapplied, the application restarts. (There are also some tricks you can use to stretch the flash memory a bit if you start to impinge on its upper limits.)

SRAM Memory

All of the program’s data resides in SRAM memory. Indeed, you are more likely to run out of SRAM memory than flash memory in most applications. When you compile a program, the compiler reports the amount of flash and SRAM your program is using so it’s pretty easy to see when things might be getting tight. (Look at the bottom of Figure 7 below.)

Unlike flash memory, SRAM is volatile memory, which means it goes stupid when power is lost. Any values that variables may have had when the power is removed are lost. When power is reapplied, those variables will assume whatever values you gave them at program start. If you didn’t assign the variables an initial value, they will have random values.

EEPROM Memory

Electrically Erasable Programmable Read Only Memory (EEPROM) is an area of nonvolatile memory where one often stores data that need to be retrievable each time the program is started. Like flash memory, data values stored in EEPROM survive power removal.

However, EEPROM has two drawbacks when compared to flash memory: 1) it is a little slower to access than flash memory, and 2) it has a finite number of write cycles (i.e., 100,000) before it becomes unreliable. Because of these factors, EEPROM memory is often used to store configuration or other types of information that are needed when the system powers up, but are not often changed. For example, you might have some sensors that need to have certain values sent to them before they can be used, or other devices that need to be initialized with specific data. EEPROM would be a likely candidate for storing such configuration data. I used the EEPROM memory to store the band edges for the CW transceiver mentioned earlier because they are unlikely to change that often.

Input/Output (I/O) Pins

As you might expect, a c with more memory and I/O pins costs a little more. Note that there are numerous clones available on the Internet for each member of the Arduino family. As a general rule, buy the “biggest” you can comfortably afford that is consistent with the project(s) you have in mind. If you buy a Nano (a good choice for this series), make sure you don’t get the Pro Mini, as it does not have the USB connector on it which makes it less convenient to program. Hardware projects are often subject to “feature creep” where more and more functionality is requested as the project goes forward. “Buying bigger than you need” is often a good idea if you can afford it.

There is one advantage to the Uno and Mega boards: They support plug-in boards that can be directly plugged into the board. These plug-in boards, called shields, are hardware devices for everything from GPS sensing to finger print readers. The Nano and other smaller boards do not directly support these plug-in boards. You can still use such boards, but they are connected to the I/O pins via jumper wires. BTW, the RobRed board makes it easy to have off-board connections because it brings all of the I/O pins out to individual header pins.

My suggestion: Buy a Nano to start with. They can be purchased online for less than $3 and they have the same hardware resources as the Uno.

Arduino Software

A c without software is about as useful as a bicycle without wheels. Like any other computer, a c needs program instructions for it to do something useful. Arduino has provided all the software tools within their (free) Integrated Development Environment (IDE) that you need to write program code. The remainder of this article discusses downloading, installing, and testing the software you need.

Start your Internet browser and go to:

http://arduino.cc/en/Main/Software

There you will find the Arduino software download choices for Windows, Mac OS X, and Linux. Click on the link that applies to your development environment. The latest Arduino IDE available at the time this is being written is Release 1.6.9. You are asked to select the directory where you wish to extract the files. I named my directory Arduino1.6.9 and placed it off the root of the C drive (e.g., C:\Arduino1.6.9). Now download the file and run the installer.

Inside the Arduino directory you just created, double-click on the arduino.exe file. This should cause the IDE to start. After the IDE loads into memory, you screen should look similar to Figure 2. You should have your Arduino board connected to your PC via the appropriate USB cable for your board.

Figure 2. The IDE startup screen.

All that remains is to select the proper Arduino board and port. Figure 3 shows how to set the IDE to recognize your board. As you can see, the one IDE supports a large number of the Arduino family. Click on the board that you are using.

Figure 3. Setting the Arduino board.

Now set the port number for the USB port that is being used to communicate between your Arduino and the PC. This is shown in Figure 4. Sometimes the Windows environment does not find the proper port. This is usually because the device driver for the port isn’t found. If this happens, go to your installation directory and into the drivers subdirectory (e.g., C:\Arduino1.6.9\drivers) and run the driver installation program that’s appropriate for your system. (Figure 5 shows the drivers subdirectory contents.)

Figure 4. Setting the communications port for the Arduino.

Figure 5. The drivers subdirectory

Once the driver is installed, the program should now find your Arduino COM port.

The Integrated Development Environment (IDE)

The easiest way to test that everything installed correctly is to run a program. Your IDE has a program called Blink. You can find it using the menu sequence: File → Examples → o1. Basics → Blink. You can see this sequence in Figure 6. Once you click on Blink, the IDE loads it into the Source Code window.

Figure 6. Loading the Blink program.

Once the Blink program is loaded, the IDE looks similar to Figure 7. I’ve marked some of the more important elements of the IDE in the figure. Starting near the top is the Serial Monitor icon. You click on this to see any print statements you might be using in the program. We’ll show an example of such statements in a moment.

The large white space is where you will write your program source code. Program source code consists of English-like instructions that tell the compiler what code to generate. Because you already loaded the Blink program, you can see the Blink source code in Figure 7.

The Compile Icon is exactly that: It compiles your program. It does not, however, link all the parts of the program together to form an executable program. Using the Compile Icon is a quick way to see if your have the code syntax correct.

Figure 7. The IDE.

The Compile-Upload icon not only compiles your program, but it links it into an executable program and then transfers the program code to the Arduino via the USB cable. In other words, your PC is used to write, test, compile, and debug your program, but the code actually runs on the Arduino. The Arduino has a small program, called a bootloader, that manages all of the communications between your PC and the Arduino. You don’t need to worry about it. (If you compile a program on a Nano, it will tell you that you have about 30K of program space even though it’s a 32K memory bank. The “missing” 2K is the bootloader.)

The Program stats window tells you how much flash and SRAM memory your program is using. The window is also used to display error messages if the IDE detects something wrong with your program.

Your First Program

Let’s look at the Blink program and make a couple of simple changes to it. The code is reproduced in Listing 1, with all of the comments stripped away. Look at the element called setup(). The element is

Listing 1. Blink source code.

void setup() {

// initialize digital pin 13 as an output.

pinMode(13, OUTPUT);

}

// the loop function runs over and over again forever

void loop() {

digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(13, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}

actually called a function. A function is a group of one or more program statements designed to perform a specific task. The purpose of the setup() function is to define the environment in which the program is to operate. Without getting too deep right now, pinMode() is also a function which has been predefined for you to establish how a given Arduino I/O pin is to operate. In this case, we are saying that we want to use pin 13 to output data. However, every Arduino happens to have a small LED available for use on pin 13. In this program, therefore, we are going to use pin 13 to output data. As it turns out, the output data consists of blinking the LED.

One thing that it unique about setup() is that it is only called once, and that is when you first apply power to the Arduino or you press its Reset button. Once it has defined its operating environment, its task is complete and it is not recalled.

The loop() function, however, is called continuously. If you look at the code, the first statement is a call to the digitalWrite() function. It has two function arguments, 13 and HIGH. These two pieces of information (i.e., the pin number and its state) are needed by the digitalWrite() function to perform its task. The function’s task is to set the state of pin 13 to HIGH. This has the effect of turning the LED on. The next program statement, the delay() function call, has a number between its parentheses. This number (i.e., 1000) is called a function argument which is information that is passed to the delay() function code which it needs to complete its task. In this case, we are telling the program to delay executing the next program statement for 1000 milliseconds, or one second.

After one second, another digitalWrite() function call is made, only this time it sets the state of pin 13 to LOW. This turns off the LED tied to pin 13. Once the LED is turned off, the program again calls delay() with the same 1000 millisecond delay. So, collectively, the four program statements in loop() are designed to turn the LED on and off with a one second interval. That almost sounds like blinking the LED, right?

Now here’s the cool part. After the last delay() is finished, the program loops back up to the top of the loop() function statements and re-executes the first program statement, digitalWrite(13, HIGH). (The semicolon at the end of the line marks the end of a program statement.) Once the LED is turned on, delay() keeps it one for once second and the third program statement is again executed.

Now press the Compile-Upload icon and after a few seconds you will see a message saying the upload is done. If you look at your Arduino, it’s now sitting there blinking its LED for you.

Your program repeats this sequence until: 1) you turn off the power, 2) you press the reset button (which simply restarts the program), or 3) there is a component failure. If your program is performing as described here, you have successfully installed the IDE and compiled your first program.

A Simple Modification

Let’s add two lines to make this “your” program. Move the arrow cursor into the Source Code window. The cursor will change from an arrow to an I-bar cursor. Now type in the new lines shown in Listing 2.

Listing 2. Blink modifications.

void setup() {

// initialize digital pin 13 as an output.

Serial.begin(9600);

Serial.print(“This is Jack, W8TEE”);

pinMode(13, OUTPUT);

}

Obviously, you should type in your name and call. The first line says we want to use the IDE’s Serial object to talk to your PC using a 9600 baud rate. The Serial object has a function imbedded within itself named begin(), which expects you to supply the correct baud rate as its function argument. The second line simply sends a message to your PC at 9600 baud over the Serial communication link (i.e., your USB cable). If you click on the Serial Monitor icon (see Figure 7), you will see the message displayed in the Serial monitor dialog box. At the bottom of the box are other baud rates that are supported by the IDE. The begin() baud rate and the rate shown at the bottom of the box must match. If they don’t, your PC will blow up! Naw…just kidding. Actually, you may not see anything at all or you may get characters that look a lot like Mandarin.

Conclusion

That’s enough for now. Try adding some more print() function calls using the Serial object. Put one inside loop(). What happens? What happens if you use println() instead of print()? Experiment and enjoy!