Acknowledgements

Special thanks to Hernando Barragán who created the development platform “Wiring” as a Master's thesis project that ultimately led to the Arduino. Without his efforts and the ongoing drive from people like Massimo Banzi, David Cuartielles, Tom Igoe, Gianluca Martino, and David Mellis none of this would have been possible. A great many individuals have carried the project to where it is today and I hope that this very small contribution will help maintain the forward progress and demonstrate my personal gratitude to everyone who has made a contribution to the ongoing Arduino project.

Also, massive thanks to the creators and maintainers of the Fritzing program, without which, the illustrations in this book would not have been remotely as useful.

The C language was created by Dennis Ritchie at Bell Labs. His creation still stands as an enduring monument to his foresight and skill.

Preface to first edition

A quotation from Douglas Adams’ “Dirk Gently’s Holistic Detective Agency” partly explains my personal love for the craft of programming.

“What I mean is that if you really want to understand something, the best way is to try and explain it to someone else. That forces you to sort it out in your own mind. And the more and slow dim-witted your pupil, the more you have to break things down into more and more simple ideas. And that’s really the essence of programming. By the time you’ve sorted out a complicated idea into little steps that even a stupid machine can deal with, you’ve certainly learned something about it yourself.”

I was first attracted to the Arduino platform when I decided it would be nice to try out some development project ideas that do not easily lend themselves to isolation within a PC. I wanted to use sensors and especially mobility to explore those ideas properly. The open source Arduino family represented something better than just a base level and I am currently working on projects combining multiple boards; a mix of Unos, Nanos and the latest 32 bit devices.

This book also allowed me to apply the Feynman technique to learning and understanding the Arduino microprocessor. The Feynman Technique is named after Richard Feynman, a Nobel Prize winning physicist. It is a technique to help you pretty much learn anything - to understand concepts you don't really get, remember details you have already learnt, or study more efficiently.

The Feynman technique can be boiled down to four steps. The first step is to study whatever it is you want to learn and to write down what you have learned in a notebook as you go. The second step is to pretend you are teaching someone else what you have learned. The third step is to go back to the available sources every time you hit something you don’t understand. The fourth and final component is to simplify what you have learned and to use analogies.

Given a “Feynman” excuse to write this book why then go all the way and publish (yet another) book on Arduino C? That phase started from an early browse through the on-line Arduino language reference pages trying to establish just what elements of C were available within the Arduino environment. One of the pages (early 2017 and still current) was the language reference for pointers. If I may, I will quote it in its entirety.

“Pointers are one of the more complicated subjects for beginners in learning C, and it is possible to write the vast majority of Arduino sketches without ever encountering pointers. However for manipulating certain data structures, the use of pointers can simplify the code, and knowledge of manipulating pointers is handy to have in one's toolkit.”

That was the complete reference entry. The reference pages probably reflect the emphases of their creators and many of the users. Now I know that pointers are a big deal in C and it is quite likely that an efficient C program is likely to need to make use of them. I then knew that if I was going to learn the Arduino from the perspective of a programmer I had better set about some detailed research. I realised during that research that there was a place for a book that both introduced newcomers to the C programming language but also one that introduced some of the more advanced programming techniques where they could be directly applied to maximise the efficiency of Arduino based projects.

While my experience with the Arduino C reference page on pointers was something of a trigger point for me it is only fair to point out that the reference materials are often very well written and can quickly introduce new program elements and even new programming techniques. They are a great resource, please do use them.

To clarify, just who is this book intended for? I hope that the answer is anyone who wants to apply Arduino microprocessors to real world tasks or even to just explore microprocessors and electronics. The pace of the book should be such that it will suit a newcomer to programming but I have tried also to maintain a focus on the final objective which is to equip the reader with the skills required to write program code that is a match for well-designed electronic projects. Just maybe also making up for deficiencies in prototype and experimental designs. In any case I want to ensure that C pointers will hold no fear for readers of this book.

Preface to the second edition

I had been following the development of new Arduino hardware devices a little unsure about the direction being followed by the project overall. There had been organizational splits and disputes that raised worries about the long term viability of the Arduino concept. Now in 2019 these issues seem to have been amicably resolved which is great news but added to that we have seen the launch of new devices equipped with Wi-Fi and intended to kick-start a new Arduino era for the “Internet of Things” (IOT).

This second edition has some revisions to the text coupled to the reworking of one of the extended program examples. Two additional data structures have been added to the relevant chapter and a new chapter written to bring Arduino development into the Internet age.

Despite all that is new, this book is still based upon the reliable and always available desktop IDE and remains focused on developing effective C skills. The underlying detailed exploration of Arduino features exposed to the programmer means that this book also represents a reference source to support the reader through future projects both large and small.

Introduction

Just a couple of notes on how program code is presented in this book.

Program code that you might like to enter and run on an Arduino is presented inside a ruled box and is formatted in a similar way to how code is formatted by the Arduino IDE. In fact, the formatting matches that used by the Notepad++ program when working with C or C++ content. Any program longer than a very few lines may be broken down into a sequence of ruled boxes as this simplified layout of the book pages while making it easier for the reader to digest and transcribe the code.

The greater part of the code samples and demonstration programs presented in this book can be found on the website practicalarduinoc.com but the serious reader is encouraged to type the code into the IDE rather than use copy/paste from a web page. Ask any teacher or lecturer in computer science or those new fun code schools. The advice will always be that learning is best achieved by the slower process that allows an opportunity to read and understand the code. Once you have digested the syntax and structure of the C programming language presented in chapter 3 you should take every opportunity to explore the language further by varying the code samples. This is a particularly effective strategy whenever you run into something you don’t quite understand. Add some more lines of code to break a given step down into stages or to explore data values before and after a given operation.

When specific code elements are quoted in the text they are most frequently presented in an alternate font and indented. A similar approach has been taken when presenting sample output.

All images in the book are reproduced in full colour on the book website together with additional notes, extra code samples and further reading on Arduino C and related topics.

I hope that you enjoy working through this book. Programming is a very pleasurable activity as well as a means to an end.