What is an Arduino microcontroller board?
The Arduino ecosystem is a set of microcontroller hardware, software tools, and libraries that make programming microcontrollers much more accessible than it has been traditionally. In this section, we will discuss what microcontrollers are, and how Arduino helps us use them easily and effectively.
What is a microcontroller?
A microcontroller is an integrated circuit (IC) that contains all the necessary components to make it a little standalone computer. An IC is a complex piece of electrical circuitry that is made from a single, tiny piece of silicon (a chip). A microcontroller has a processor (CPU), memory and storage, and usually a host of other hardware peripherals that implement standardized low-level interfaces to other electrical circuits. These interfaces can be used to communicate with other pieces of hardware, such as sensors and motor drivers. To make them easier to work with, manufacturers put the microcontroller IC in a housing that is much larger than the IC itself. The housing exposes metal pins that are connected to the electrical contacts of the IC inside. The terms IC and chip are often used interchangeably to describe the combination of the silicon chip and its housing.
The simplest interface is digital input/output (I/O). Most of the pins of a microcontroller can be configured to be either digital inputs or output, and our program can either set their voltage level low (0V) or high (typically 3.3V or 5V) or read their voltage level (as digital LOW or HIGH). We will use this functionality, for example, to control LEDs that are connected to a digital output and to read the status of switches that are connected to a digital input. While digital I/O is extremely useful to transmit the state of a single bit, it is not a good interface for the transmission of a lot of data. For this task, slightly more complicated interfaces (often called buses) are used. A well-known example of a hardware interface for data transmission is the Universal Serial Bus (USB). This is a very powerful, flexible, but complicated-to-implement interface. It is usually not directly used in DIY robotics. The interfaces we use for our robots are simpler and easier to understand and use. Interfaces commonly used in Arduino robots include Universal Asynchronous Receiver/Transmitter (UART), Serial Peripheral Interface (SPI), the Inter-Integrated Circuit (I2C) bus, Pulse Width Modulation (PWM), and analog data transmission.
A microcontroller will have dedicated hardware for some or even all of these interfaces integrated on the same chip. That means the CPU does not need to spend time handling the transmission or reception of data (and we do not need to spend time implementing any of the algorithms for these interfaces). Our program will simply tell the CPU to pass data to these peripherals for transmission, or ask the peripheral for any received data. Easy access to these hardware interfaces is a major reason why microcontrollers are so great for DIY robotics. Most sensors, motor drivers, or any other component we might want to use in our robot will have one of these interfaces, and as long as we know how to program a microcontroller, we can easily talk to these components. More generally, access to these interfaces makes microcontrollers great for any kind of hardware hacking. Most chips that you find in appliances, toys, tools, printers, and game console controllers (everywhere, really) likely communicate over one of these interfaces. You can use a microcontroller to talk to them directly and gain lower-level access to their capabilities, rather than having to use the interfaces that the manufacturer exposes. An example of this (that works with very little hacking) is the popular use of the Nintendo Wii Nunchuck controller, a nice and inexpensive in-hand joystick, in DIY robotics projects. It uses the I2C interface.
Programming microcontrollers
Writing programs for microcontrollers is different from more typical programming on PCs. One of the main differences is that, in general, there is no operating system (OS) on a microcontroller. Your program is the only thing running on the CPU, and there is no OS to help manage resources or prevent the system from crashing. Your program also has direct access to hardware functionalities, such as sending power to any of the digital output pins. This is good in many ways: it makes it easy to understand what is going on during program execution, it means that there is almost no computational overhead from anything that is not your program, and it makes hardware access very transparent. However, it also means that we need to be a little more aware of what can go wrong if our program does not behave as intended since it is easy to make the program stall (for example, if we wait for a condition that never occurs), crash, or even damage attached hardware if we sent incorrect output signals. This direct access to hardware functionalities without the safeguards and layers of abstraction that an OS provides is why we sometimes call this type of programming hardware-near programming or, more commonly, embedded programming. Some languages lend themselves better to embedded programming than others. Most commonly, microcontrollers are programmed using the C language since it offers powerful and elegant ways to write highly efficient code that lets the programmers squeeze out every bit of performance, even from the smallest microcontroller. Even though C is sometimes regarded as a low-level language, is still much more user-friendly than going one level closer to machine language and programming in assembly.
The Arduino IDE supports not only C but also C++ (often abbreviated together as C/C++), and we rely on C++ to enable object-oriented programming (OOP), which allows us to easily use many third-party software libraries and make our own code reusable across different projects. We will take a much closer look at this in a later chapter. All examples in this book are written in C/C++.
The Arduino IDE does not support any other programming language, such as Python, for example. However, the Arduino project officially supports MicroPython, and you can use MicroPython and the OpenMV IDE to program Arduino boards using the popular Python programming language.
Pain points of working with microcontrollers
The lack of hardware abstraction provided by an OS means that the exact commands for doing a certain task (for example, setting a certain output pin high or sending data over a certain interface) can be very different from microcontroller to microcontroller, and you, the programmer, often have to dig through hundreds of pages of datasheets to understand how exactly to perform a certain operation, such as sending data over I2C on a new microcontroller. Even translating your final program to the machine code that can run on the microcontroller (compiling) can be complicated and extremely specific to the exact microcontroller you are using. And finally, transmitting the compiled program to the microcontroller often requires specialized hardware that can be complicated to use. For these reasons, the barriers to working with microcontrollers have traditionally been high, and they were mostly used by expert engineers rather than hobbyists, artists, students, or makers. To summarize, the traditional pain points preventing more people from using microcontrollers are as follows:
- The fact that programs are highly hardware specific
- The need for specialized compilers
- The need for specialized hardware to transmit the compiled program
The creators of Arduino had the goal to get rid of these barriers and enable non-experts to use microcontrollers in their projects. Arduino is not the only project with this goal, but it has been the most successful, and it has had the largest impact on the maker scene by enabling many more people to take advantage of the power of microcontrollers.
What exactly is Arduino?
Arduino is a company that makes and distributes open source hardware and software. However, when we talk about an Arduino that controls a robot, we usually mean a circuit board with an Arduino-compatible microcontroller on it. But, as we already mentioned, Arduino is more than the physical hardware: it is an entire ecosystem of software, tools, and hardware that makes using microcontrollers accessible for anyone, with Arduino boards and the Arduino IDE at its center. Together, the IDE and the supported boards address the pain points we identified earlier. They simplify and streamline writing programs, compile them, and transmit the compiled programs to the board. There are also extension boards (called Arduino shields) that add additional functionality to standard Arduino boards without requiring specialized electronics skills.
There is a variety of different supported Arduino boards, and we will look at their differences and distinctions in more detail in a later chapter. One great aspect of Arduino is that the workflow of writing programs and transmitting them to the physical board is the same across all the different boards. In fact, in many cases, the exact same code can be used across different boards with different microcontrollers. No need to comb through datasheets for specific register names or to use specialized hardware for programming. When everything is set up, all you need to do is to connect your Arduino board to your computer with a standard USB cable, click one button, and the program will be compiled, transmitted, and starts running on your board automatically. For the remainder of this chapter, we will assume that our Arduino board is an Arduino Uno Rev3, but the steps to program it would not be different from any other official Arduino board that is supported by the IDE. Arduino boards also contain all the components needed to support the microcontroller and provide easy access to all of its pins. So, to answer the question in the title of this section: an Arduino microcontroller board is an easy-to-use circuit board with a microcontroller and all the electronics to support it, and can be programmed using the Arduino IDE.
Because of the popularity of Arduino, the fact that both hardware and software are open source, and the use of licenses that allow reusing the hardware design in commercial projects, a huge number of third-party projects that expand the Arduino ecosystem have emerged. There are numerous manufacturers of Arduino shields or entire (robot) starter kits, and even original equipment manufacturers (OEMs) of electronics components make development boards intended to be integrated into Arduino-based projects. There is also an incredible number of online tutorials, YouTube videos, and How-To guides that show you how to build a certain project with Arduino. As exciting as all of this is, experience shows that it can be overwhelming for anyone who is just entering the field of DIY electronics and robotics. And without a solid understanding of the basics, much of this content can feel more like noise rather than educational. The goal of this book is to equip you with a solid understanding of robotics with Arduino, the possibilities and limitations, as well as the pros and cons of many different design choices you have to make when building your own robot. After you have read this book, you will know where to start any Arduino robotics project that you want to build, and you will be able to understand and make use of the large amount of information and resources that are available from and for the Arduino robotics community.