Arduino Sketches. Langbridge James A.. Читать онлайн. Newlib. NEWLIB.NET

Автор: Langbridge James A.
Издательство: Автор
Серия:
Жанр произведения: Зарубежная образовательная литература
Год издания: 0
isbn: 9781118919699
Скачать книгу
to get you started, as well as the sketch to get you up and running.

      To get the most out of your sketches, each library is introduced and the different functions are explained. Examples are provided for every library, going through the code line by line so you understand what the sketch does. My hope is that this book will serve as a reference for your new projects. Have fun!

      Part I

      Introduction to Arduino

      Chapter 1

      Introduction to Arduino

      Electronics enthusiasts have always been present. For decades, they have been creating interesting devices and products. Amateur radio enthusiasts typically made their own radio sets using schematics found in magazines or simply from their own design. How many of us built a radio system to discover electronics, only to be hooked? With a few dollars' worth of components, you could create your own radio and listen to glorious long-wave transmissions on a small low-quality speaker, and yet it was better than what could be bought in the shops because it was homemade. If you wanted better sound, you could buy a better speaker. More volume? There were kits for that, too. Audiophiles built their own amplifiers and accessories depending on their needs. Electronics shops proposed books for all levels, from beginner to expert. Kits were also available using the simplest of components all the way to entire computer systems. It was a time in which you could build just about anything, and everything. You could, quite literally, walk into an electronics shop, buy a DIY computer, and spend a few hours soldering memory chips onto a printed circuit board. That's how I started.

      In the 1990s, things changed slightly. Most hobbyists had a PC on their desk and could use them to create schematics, simulate parts of a system, and even print circuit board with transparent layouts, making the entire process much easier. However, something was missing. Almost all the devices that could be made were not programmable. Microprocessors were available but were either too expensive or too complicated. At the time, the 68000 microprocessor was one of the most reliable components available and was relatively cheap but complex. The microprocessor by itself was useless; it had to be hooked up to external memory. To run a program on every boot, it had to also have read-only memory. If you wanted interrupts, again, you had to add a chip into the design. The end result was complicated and out of the reach of some enthusiasts. To do without this complexity, enthusiasts that wanted programmable devices tended to use what was already on their desk: a personal computer.

Most PCs at the time used the ISA bus, as shown in Figure 1.1. ISA was a simple bus that allowed components to be added to the processor and general computer system. It was a simple system that allowed users to insert add-on cards into their computer, and it was extremely easy to use. It wasn't hard to create a circuit board that could be plugged into an ISA slot, and complete prototyping boards existed, enabling enthusiasts and engineers to test a solution before making their own board. Some of these boards even included breadboards, a simple system allowing users to place their components and wires without the need to solder. This sparked a small revolution, and many enthusiasts turned to this type of board to do what previously could not be done: create programmable systems. An ISA board could have digital inputs and outputs, analog inputs and outputs, radios, communication devices – just about anything was possible. All this would be controlled by the computer's CPU, using simple programming languages such as C or Pascal. My ISA card kept my student apartment nice and warm by reading data from a thermometer and turning on electric heaters, acting like a thermostat. It also served as an alarm clock, programmed depending on my classes the next day. Although I did manage to miss a few morning classes, in all fairness it was usually my fault; the ISA card worked perfectly on a tight budget.

Figure 1.1 ISA prototyping board

      Computers became faster, and systems evolved. The industry changed, and so did the expansion ports. Just as enthusiasts became experts on the ISA bus, the industry invented a new system: the VESA Local Bus (VLB). The VLB bus was an extension to ISA, only adding a second connector for memory-mapped I/O and Direct Memory Access (DMA), but it announced a change. Computers were indeed getting faster, and some computer bus systems couldn't keep up. Even VLB couldn't keep up, and after only a year, PCI became the reference. The PCI bus is an advanced bus but requires components and logic to identify itself. It suddenly became increasingly difficult to create homemade boards. Some users decided to use other industry-standard ports, such as the parallel port or RS-232, but most stopped creating such systems. Those that did continue mainly used analog systems or nonprogrammable digital systems. Instead of having a programmable microcontroller, the system was designed using logic gates. For example, a bulb could turn on if both inputs A and B were true, or if input C was false. These tasks became more and more complicated as the number of inputs increased.

      Analog systems, such as radios and amplifiers, did not have a form of programming. They were designed with a specific task in mind. Configuration was analog; with a small screwdriver, the designer could “tweak” values with potentiometers, variable resistances. It wasn't possible to program the device to multiply an input signal by a specific value; instead, potentiometers were added to counter the effect of tolerances in the components. Designs therefore added an additional phase, calibration. Specific input signals were fed into devices, and a specific output was expected.

      Processors did exist that could be used, and some projects did use them, but integrating a processor into a design generally meant that several components needed to be used. Memory chips, I/O controllers, or bus controllers had to be used, even after a decade of technological advancements, and circuits became more and more complicated. Even when designs worked, programming them proved to be a challenge. Most programming was done via EEPROM devices, short for Electronically Erasable Programmable Read-Only Memory. These devices could contain a computer program and could be programmed using an external programmer attached to a computer. They were called erasable read-only because the contents could indeed be wiped and replaced, but doing so required removal of the circuit and subjecting it to ultra-violet light for 20 minutes. One small error in a program could often take 30 minutes or more to correct.

      Atmel AVR

      Atmel is an American semi-conductor company, founded in 1984, and the name Atmel is an acronym for Advanced Technology for Memory and Logic. Right from the start, Atmel designed memory chips that used less power than competing designs, but it soon decided to create programmable devices. In 1994, Atmel entered the microprocessor market, creating an extremely fast 8051-based microcontroller. In 1995, Atmel was one of the first companies to license the ARM architecture, giving it access to advanced processor technology.

Atmel didn't use only ARM technology, it also created its own processor, the AVR, in 1996 (see Figure 1.2). What does AVR stand for? Well, that is one of the many mysteries of Atmel. Designed by Alf-Egil Bogen and Vegard Wollan, some say it stands for Alf and Vegard's RISC processor. We will never know, and at the time, people were not interested in understanding the name, but rather getting their hands on this advanced piece of technology. Today, more and more people are curious as to the origin of this curious processor, Atmel continues to tease the public with videos of the inventors explaining the name, only to have the big reveal scrambled by mobile telephone interference.

Figure 1.2 Atmel AVR Microprocessor

      Previously, programming the read-only memory of a device required some tedious tasks, like subjecting the chip to UV light, or complicated erase techniques. This all changed with Atmel's 8-bit AVR. The AVR was the first microcontroller family to use on-chip flash memory for program storage. It also included Random Access Memory (RAM) directly on the chip, essentially containing everything needed to run a microcontroller on a single chip. Suddenly, all the complicated design could be replaced with a single component. Even better, programming the chip could be done in minutes, using minimal hardware. Some Atmel designs allowed users to plug the microcontroller directly into a USB port and to program it using Atmel's software. From compilation to program execution took less