© Norman Dunbar 2020
N. DunbarArduino Software Internalshttps://doi.org/10.1007/978-1-4842-5790-6_1

1. Introduction

Norman Dunbar1 
(1)
Rawdon, West Yorkshire, UK
 

The Arduino is a great system for getting people into making with electronics and microcontrollers. I was reintroduced to a long-lost hobby when I was gifted an Arduino Duemilanove (aka 2009) by my wife’s late grandmother, and since then, I’ve had lots of fun learning and attempting to build things. I’ve even built a number of Arduino clones based on just the AVR microcontroller and a few passive components – it’s cheaper than fitting a new Arduino into a project!

Much has changed over the intervening years. LEDs used to cost about £10 each and came in one color, red. These days I can get a pack of 100 LEDs for about £2 in various different colors. Even better, my old faithful Antex 15W soldering iron still worked, even after 35 years.

The Arduino, and I’m concentrating on either the Uno version 3 or the Duemilanove here, as those are two of the ones I’ve actually purchased (or been given), is based on an Atmel ATmega328 microcontroller. On the Uno it’s the Atmel ATmega328P-AU, while the Duemilanove uses the ATmega328P-PU.

Roughly, the only difference between the two is the UNO’s AU version is a surface mount, while the PU version is a 28-pin through-hole device. They are/should be identical to program, although the AU version does have two additional analogue pins that are not present on the ATmega328P-PU.

Occasionally though, I may mention in passing the Mega 2560 R3 – as I have a cheap Chinese clone of one of these – which is based on the Atmel ATmega2560 microcontroller.

Some older Arduino boards had the ATmega168 microcontroller, which also was a 28-pin through-hole version, but it only had 16 Kb of flash memory as opposed to the 32 Kb in the later 328 chips. The EEPROM and RAM size is also half that of the ATmega328P devices.

The Arduino was designed for ease of use, and to this end, the software and the “Arduino Language” hides an awful lot from the maker and developer. Hopefully, by the time you have finished reading this book, you will understand more about what it does and why and, when necessary, how you can bypass the Arduino Language (it’s just C or C++ after all) and use the bare metal AVR-specific C or C++ code instead. Doing this can lead to more space for your code, faster execution, and lower power requirements – some projects can be run for months on a couple of batteries.

1.1 Arduino Installation Paths

The version of the Arduino software used in this book is 1.8.5.

I used an installation on Windows 7 and another on Linux while writing this book although Linux is my operating system of choice. Both versions were installed by downloading the zip file version, as opposed to the appropriate installer, and extracting it. The locations I used are as follows:
  • On Linux – /home/norman/arduino-1.8.5

  • On Windows 7 – c:users ormanarduino-1.8.5

Within this book, there are references to various files provided by the Arduino software. Because of the way I’ve installed my software and the fact that the installer versions of the download may install to a different location, all paths used in this book will be relative to the preceding locations.

Paths used will be as follows:
  • $ARDBASE is the preceding given location where I’ve extracted the zip file – /home/norman/arduino-1.8.5. This is where you will find the file arduino.exe on Windows or arduino on Linux which is the Arduino IDE.

  • $ARDINST is the location of the main Arduino files for AVR microcontrollers. This is $ARDBASE/hardware/arduino/avr and is where the various cores, bootloaders, and so on can be found, beneath this directory. On my Linux system, this is the path /home/norman/arduino- 1.8.5/hardware/arduino/avr.

  • $ARDINC is the location of many of the *.h header files and most of the *.c and *.cpp files that comprise the Arduino Language for AVR microcontrollers. This is $ARDINST/cores/arduino. The expanded path is /home/norman/arduino-1.8.5/hardware/arduino/avr/cores/arduino on my Linux system.

  • $AVRINC is where the header files for the version of the AVR Library provided by the Arduino IDE are located. The Arduino Language (eventually) compiles down to calling functions within the AVR Library (henceforth referred to as AVRLib), and the header files are to be found in location $ARDBASE/hardware/tools/avr/avr/include. The fully expanded path here is /home/norman/arduino-1.8.5/hardware/tools/avr/avr/include.

So, if you see $ARDINC/Arduino.h mentioned, you will know that this means the file
  • /home/norman/arduino-1.8.5/hardware/arduino/avr/cores/arduino/Arduino.h on Linux

  • c:users ormanarduino-1.8.5hardwarearduinoavrcoresarduinoArduino.h on Windows

You can see why I’m using abbreviations now, can’t you?

In addition, most of the file paths I refer to will be in Linux format with the “/” as a path separator, unless I’m specifically referring to a Windows file, in which case the file’s path will use the “” Windows path separator character.

If you wish to examine the files that I am discussing in the book on your system, see Appendix A for a couple of useful tips on how to avoid always having to type the full paths.

1.2 Coding Style

Code listings in the book will be displayed as follows:
#define ledPin LED_BUILTIN
#define relayPin 2
#define sensorPin 3
...
void loop()                         ①
{
    // Flash heartbeat LED.
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin LOW);       ②
    ...
}
  • ①    This is a callout and attempts to bring your attention to something in the code which will be described beneath the code listing in question.

  • ②    This is another callout – there can be more than one.

In the book’s main text, where you see words formatted like USCR0A or PORTB, then these are examples of Arduino pin names, AVR microcontroller registers, bits within those registers, and/or flags within the ATmega328P itself, as well as references to something listed in the data sheet for the device. Where code listings are being explained, then variables from the code will be shown in this style too.

Arduino pin numbers will be named Dn or An as appropriate. This is slightly different from the normal usage of the digital pins, which normally just get a number. I prefer to be a little more formal and give the digital pins their full title. <grin>

../images/494109_1_En_1_Chapter/494109_1_En_1_Figa_HTML.gif Tips are exactly that. They give you a clue or information about something that may not be too well known in the Arduino world, but which might be incredibly useful (or, maybe, just slightly useful!).

../images/494109_1_En_1_Chapter/494109_1_En_1_Figb_HTML.gif This is a note. It brings your attention to something that may require a little more information. It could be useful to pay attention to these notes. Maybe!

../images/494109_1_En_1_Chapter/494109_1_En_1_Figc_HTML.gif Cautions are there to highlight potential problems with something in the software, or just something that the data sheet needs you to take extra care over. There may be a possibility of damage to your Arduino if you don’t pay particular attention. Occasionally, the data sheet warns against doing something – so it’s best not to do what it says not to do!

1.3 The Arduino Language

I should perhaps point out that there isn’t really such a thing as the Arduino Language. I may refer to it frequently within the pages of this book, but technically, it doesn’t exist. What it is is simply an abstraction of the C/C++ language, written in such a way as to make life easier for people learning to make stuff with their Arduino. Which of the following is easier to understand?
digitalWrite(13, HIGH);
or
PORTB |= (1 << PORTB5);

The first is definitely easier to understand; however, the latter is by far the quicker of the two as it just does what it says – it sets pin 5, on PORTB of the ATmega328P, to HIGH. The name, digitalWrite(), appears to be a different language, but it isn’t; it’s that abstraction away from plain AVR C/C++ which makes life easier for us all.

1.4 Coming Up

In Chapter 2, I explain how a sketch gets massaged into a proper C++ program and how the libraries used in the sketch are incorporated into it. Following the brief overview of how compiling a sketch operates, I then document the Arduino’s main() function, the various header files that it includes, and the initialization carried out by the init() function. These initializations are part of every sketch that you compile, so it helps if you know what the Arduino system is doing, in the background, for you.

In Chapter 3, I explain about the features and facilities of the Arduino Language. This will include all the commands such as pinMode(), digitalWrite(), and so on. I talk through all the functions that relate to the Arduino, with particular emphasis on the code that applies to the standard Arduino boards, those based on the ATmega328P family of AVR microcontrollers.

Chapter 4 looks into a number of the C++ classes (or objects) which are supplied with, and used by, the Arduino Language. The classes of main interest here are the HardwareSerial class which provides us with the serial interface and its commands like Serial.begin() or Serial.println(). However, the HardwareSerial class is not fully self-contained, so the other, lesser known, supporting classes are also explained in this chapter.

Chapter 5 takes a brief look at how to cast off the bonds of the Arduino Language and delves into the brazen world of AVR C++ itself, where you bypass the likes of pinMode() calls and talk to the AVR microcontroller in something akin to its own language. In here you will learn how you can set the pinMode() for up to eight pins with a single instruction or how to digitalWrite() those same eight pins, again with one instruction, and other efficient methods of communication with your board.

Chapter 6 demonstrates a couple of alternatives to the Arduino IDE. Some people don’t get on with it. I myself have a sort of love-hate relationship with it as I find the editor a little clumsy and slow for my liking.

In this chapter, I will show you how you can write code for Arduino boards in both the Arduino Language and plain AVR C/C++ code using the “PlatformIO” system and, also, give you a sneak preview of the forthcoming “Arduino Command-Line Interface (CLI).”

The Arduino Command-Line Interface is to be the basis for a forthcoming release of the Arduino IDE, but you can also use it in make files, the command-line environment, and so on. It does not require you to install Java as the current IDE does.

Chapter 7 is where I delve deeper into some features of the ATmega328P which, while not strictly software, are fundamental to configuring the ATmega328P how you might like it and not as the Arduino designers, however talented they may be, have decided. In this chapter, I’ll be looking at the ATmega’s fuses, power reduction modes, sleep modes, and similar features which determine how the ATmega328P works, but not necessarily what it does.

Chapters 8 and 9 are where I delve deeper into some features of the ATmega328P which, while not strictly software, are either important in understanding the Arduino Language or just useful to know about. Hardware features such as the Analogue Comparator, timer/counters, Analogue to Digital Converter (ADC), and Universal Synchronous/Asynchronous Receiver/Transmitter (USART) are covered in some detail.

Finally, in the appendices, there are a number of topics that may be of interest, or are kept together in one place for reference. In here you will find all the helpful reference material you might need such as pinout diagrams and potentially useful (or unusual) code to upload to your Arduino.

There’s even an index!

Without any further ado, let’s dive in to what happens when you want to compile a sketch in the Arduino IDE.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.252.201