The Arduino team did a great job in making the IDE modular so that they could easily add new boards to the system. This means it is possible to add software to support non-Arduino boards to the system and then program them with the Arduino IDE using the same commands.
Not only can you use ready-made boards such as those shown in Figure 3-1, but you can also use the Arduino IDE to program bare microcontroller chips (that are different models than those used on Arduino boards) for your projects.
Figure 3-1 From left to right: Trinket m0, Circuit Playground Express, Wemos D1 Mini, LOLIN32, and NodeMCU.
The Arduino IDE—in fact, the whole Arduino project—is open source. That is, all the code used to create the IDE and the code that runs on the Arduino board is available to be examined and copied for your own use. Although purists would argue that the Arduino hardware designs do not quite conform to open source hardware standards, the fact is, that the schematic diagrams and all the information you might need to make your own Arduino are in the public domain.
The Arduino IDE is also designed to be flexible without the need to actually modify the source code of the program. One key aspect of this is to allow other platforms or “cores” to be “plugged-in” to the Arduino IDE.
The easiest way to add support for other boards to the Arduino IDE is to use the Boards Manager (Figure 3-2), which is accessed by selecting the “Tools” menu, then selecting the “Board” option and then “Boards Manager…”.
Figure 3-2 The Boards Manager.
By default, this will not include all board options, especially those from “less official” sources. But you can inform the Arduino IDE about other boards by specifying URLs in the “Additional Boards Manager URLs” field that you will find in “Preferences” from the “File” menu (Figure 3-3).
Figure 3-3 Adding URLs to the Boards Manager.
In the next section we will add support for the popular Adafruit Circuit Playground.
The Adafruit Circuit Playground (Figure 3-4) can be programmed using several different programming languages and environments including Arduino. The board is intended in part for use in education and so includes a number of built-in peripherals, including 10 RBG “Neopixel” LEDs, a buzzer, two push switches, an accelerometer, and a microphone.
Figure 3-4 The Adafruit Circuit Playground Express.
This board does not use a processor from the mega family used by the Arduino Uno, but rather uses an ARM-based microcontroller. We will now use this board to illustrate the use of the Boards Manager.
Open the Boards Manager and then enter “Circuit Playground Express” in the search window (Figure 3-5). Then select the only entry in the list. You will notice that this adds support to a whole load of boards, including the Circuit Playground Express. Click on the “Install” button next to the version.
Figure 3-5 Adding a board using the Boards Manager.
Once you have installed the board support, you should find it in the Boards List (Figure 3-6).
Figure 3-6 The Circuit Playground Express in the Boards List.
To make good use of all the built-in peripherals on the Circuit Express, you will need to also install the Arduino library for it. To do this, use the Library Manager and search for “Playground Express”. Try out one of the examples provided with the library from the “File” menu under “Examples,” such as “HelloNeoPixels” under the category “Hello Circuit Playground.” Browse the other example sketches for the Circuit Playground library to see some of the other things you can do with the board, programming it as you would a “regular” Arduino board.
Note that if you are a Windows user, before you can use the board you will need to install a Windows driver for it (see https://learn.adafruit.com/introducing-circuit-playground/windows-driver-installation). Mac and Linux users don’t need to do so.
You can find out all about Adafruit’s Playground Express board at https://learn.adafruit.com/introducing-circuit-playground/.
Adafruit also has a wide range of Arduino-compatible boards. Go to their website and search for “feather,” “trinket,” and “flora” to see some of their other types of Arduino, some of which have special features such as RF (radio frequency), WiFi, or LiPo battery charging built in.
The NodeMCU and its relative, the Wemos D1 Mini (Figures 3-7 and 3-8), are extraordinarily low-cost Arduino-compatible boards with built-in WiFi connectivity. They are both based on the ESP8266 WiFi microcontroller module. It is possible to program these ESP8266-based devices with a range of programming languages including Arduino.
Figure 3-7 The NodeMCU.
Figure 3-8 The Wemos D1 Mini.
Although the NodeMCU looks like it has all the GPIO pins of, say, an Arduino Pro Mini, in actual fact it has a much-reduced set of pins, with only 1 analog input and 9 digital inputs/outputs pins. The Wemos D1 Mini takes advantage of this reduction in the number of useful pins available in the ESP8266 module, resulting in a physically much smaller board.
These pins must be referenced with D or A in front of the pin name—for example, A0, D4, etc. This differs from a regular Arduino where the “D” can be omitted. Note that the NodeMCU board shown in Figure 3-7 actually has an error in the pin labelling on the PCB—D2 is marked twice.
If you are a MAC or Windows user, you may have to install drivers for the CP2102 (NodeMCU) or CH340 USB interface chip (Wemos D1 Mini) before you can use them (see www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers or www.arduined.eu/ch340-windows-8-driver-download, respectively).
To add support for these boards to your Arduino IDE, open the Boards Manager and search for “ESP8266” and install the Board “esp8266 by ESP8266 Community.” If you load up the example “sketch_03_01_node_mcu_blink,” you can blink the built-in LED attached (normally to pin D0, but sometimes D1 or D2) on a NodeMCU or D4 on a Wemos D1 Mini. You will find this and all the other sketches used in the book at the book’s web page (http://simonmonk.org/nextsteps2/).
Note that these boards’ inputs and outputs operate at 3.3V, so do not connect 5V outputs to an input on this board, or you will break it. Each digital output can supply 12 mA. This is less than the 40 mA of an Arduino Uno, but still enough to light an LED brightly.
For examples of using the built-in WiFi capabilities of based boards like these and the ESP32 in the next section, see Chapter 14.
Often, the limited number of GPIO pins of the NodeMCU and other ESP8266-based Arduino-compatible boards is not a problem. However, sometimes you need more GPIO pins or might want to use Bluetooth. In which case, look for a board that uses the ESP32.
SparkFun and Adafruit both make high-quality and well-documented ESP32-based boards (the ESP32 Thing and Huzzah32 Feather Board, respectively). Both boards have built-in LiPo battery charging and power supply hardware making them great for mobile projects. If you want to spend less, then eBay will find you several low-cost alternatives such as the LOLIN32 featured in Figure 3-1.
To add ESP32 boards using the boards manager, you have to add the URL https://dl.espressif.com/dl/package_esp32_index.json into the Additional Boards URL field in Preferences, in the same way as yoiu added the URL for the Circuit Playground Express (see Figure 3-3). You will then find a board named “esp32 by Espresif Systems” in the board manager.
For more information on the ESP32 Arduino project see: https://github.com/espressif/arduino-esp32. This page will also tell you the current status of the project as at the time of writing some features, such as PWM output (“analogWrite”), have not been implemented.
The ESP32 is a remarkable device with dual processors, lots of memory, and really good low-power capabilities, making it a great choice for IoT projects. We will return to it in Chapter 14.
Some projects actually require very little in the way of digital inputs and outputs and also barely make a dent on the 32 kB of flash available for your programs on the Arduino Uno’s ATmega328 microcontroller. For such projects, you might like to consider a board based on the ATmega’s relative, the ATtiny family of microcontrollers. These chips share many of the characteristics of the ATmega range, but, as the name suggests, generally have less of everything, including pins and memory of all types. The advantages, of course, are that they don’t cost as much or take up as much room as an ATmega and offer an excellent next step for your Arduino Uno where you can dispense with most of the Arduino board and just base your project around a single microcontroller.
By way of illustration, let’s take a look at using the ATtiny44 microcontroller. This chip sits in the middle ground between the mega microcontrollers and a truly tiny device like the 8-pin ATtiny85.
The ATtiny44 has just 4 kB of flash memory and 256 bytes of RAM. If you run low on room for your program, you can always upgrade to the pin-compatible ATtiny84 that has 8 kB of flash memory.
Figure 3-9 shows an ATtiny44 on solderless breadboard along with an LED (that we are going to blink) and Series resistor. The chip has 14 pins: two required for power, one for reset, leaving the remaining 11 available as GPIO pins (see Figure 3-10).
Figure 3-9 The ATtiny44 microcontroller on breadboard.
Figure 3-10 The ATtiny44 pinout.
You may remember how back in Chapter 2 an Arduino Uno was used as a “programmer” to flash the Arduino bootloader onto a second Arduino. We can also use an Arduino Uno to program an ATtiny microcontroller, but rather than use it to burn the bootloader onto the ATtiny, we are going to use the Arduino to put the entire sketch (in this case, a Blink program) onto the ATtiny.
Before you do anything else, you need to install the “ArduinoISP” sketch onto your Arduino Uno. You will find it under the “File” menu in “Examples.” After this is done, you can connect the Arduino to the ATtiny as described below. It is important to install the ArduinoISP sketch first, because once you attach the 10 µF capacitor to the Reset pin of your Arduino you won’t be able to upload another sketch onto your Arduino until the capacitor is removed.
You can use male-to-male jumper wires to make the connections shown in Table 3-1 between the Arduino Uno and the ATtiny. These are also shown in Figure 3-11.
Table 3-1 Connecting an Arduino Uno to an ATtiny44 for Programming
Figure 3-11 Connecting an Arduino Uno to an ATtiny44 for programming.
You can tell which way the chip goes because it has a small notch at the top end near pin 1 (see Figure 3-10). To get the chip to fit into the breadboard holes, you may need to carefully bend all the pins on each side just a tiny amount. Pressing them gently against your table-top usually does the trick.
The capacitor will have one longer lead. This is the positive lead and should go to the Reset pin on the Arduino. Similarly, the longer lead of the LED is the positive lead.
There are several Cores available for ATtiny that you can add to your Arduino IDE. The one used in this book is called ATtinyCore and you can find its documentation here: https://github.com/SpenceKonde/ATtinyCore.
This page also contains a list of boards that the Core supports, along with useful pin-out diagrams for the ATtiny ICs supported.
The Core is available from the Boards Manager; however, the URL (http://drazzy.com/package_drazzy.com_index.json) must be added to the Boards Manager’s list of URLs as specified in Preferences (see Figure 3-4).
After adding the URL, open the Boards Manager, search for “attinycore,” and install the latest version.
Now, we have our Arduino IDE set up for ATtiny and we have our Arduino Uno ready to act as a programmer for the ATtiny and all the wires in place. We are almost ready to upload a sketch onto the ATtiny, but first we need to do a little configuration in the Arduino IDE.
Start by selecting a board type of “ATtiny24/44/84” from the section “ATtinyCore” (Figure 3-12).
Figure 3-12 Selecting a board type of ATtiny24/44/84.
This will add some extra options to the “Tools” menu relating to the ATtiny. You can leave most of these as their defaults. But you should change the “Chip” to ATtiny44 (see Figure 3-12). You should also check that “Clock” is set to “8 MHz (Internal)” and set “Programmer” (at the end of the “Tools” menu) to “Arduino as ISP.” If you have other programming hardware, you can select this here.
Some of these menu options affect the chip’s “fuses” that determine how the chip will behave, even configuring what certain pins will do. So, for example, by setting the “Clock” option to “8 MHz (Internal),” we are saying that pins 2 and 3 of the ATtiny44 are not needed for a crystal oscillator and can therefore be used as GPIO pins. We need to be a little careful here, because if, for example, we set the “Clock” fuse to “16 MHz (External),” then when we program the ATtiny, it would not be possible without adding a crystal oscillator. We would have “bricked” our chip. Fortunately, the changes you make in these settings do not take effect until you click on the “Burn Bootloader” option at the end of the menu. This does not actually burn the bootloader onto the ATtiny, it just sets the fuses.
Finally, you can upload the sketch onto the ATtiny. You can use the blink sketch provided as a download called “sketch_03_02_attiny44_blink”. Just click on the “Upload” button as if you were uploading to a regular Arduino. When the upload finishes, the LED should start blinking.
You may have noticed that we seem to have replaced most of an Arduino Uno with a chip on some solderless breadboard. So, how have we managed to dispense with so much, and what have we sacrificed in doing so?
One thing we have lost is the power supply. We no longer have a voltage regulator. The ATtiny is, at the moment, making use of the Arduino Uno’s power supply. However, the ATtiny is happy with any power supply between 2.7 and 5.5V. So once we have finished programming it, we could just power it using 3V from a pair of AA batteries.
By using the Uno as a programmer, we have also dispensed with the USB interface chip and related components. We have also lost the 16 MHz crystal oscillator that you will find as a small silver-colored component on an Arduino Uno. This is possible because the ATtiny is using a built-in oscillator. This oscillator runs at half the speed of the Arduino Uno’s 16 MHz. The software compensates for this, so, for example, the “delay” function still delays for the right amount of time. The internal oscillator is not as accurate as the external crystal. The datasheet for the ATtiny44 only guarantees an accuracy of ±2 percent for the internal oscillator versus the accuracy of a typical external crystal oscillator of ±0.003 percent. However, in practice, the oscillator accuracy is usually much better than ±2 percent.
In this chapter, we have explored some alternatives to official Arduino boards including the option of making our own Arduino with a chip and some breadboard. We will meet some of these devices again in later chapters.
In the next chapter we will look at interrupts and timers that give a means of allowing an Arduino to respond to external and timed events.