Firmware (Microcontroller)

Custom-designed Arduino Libraries

Download source code
Functions for the Ex2_08 (assigned to header file).

Download source code

Library for Ex3_13 of the BME280 built-in SPI driver.
Download source code
C++ library for Ex3_14 (applying to the SPI interface).
Download source code
Library for Ex3_17 of the BME280 built-in I2C driver.

Download source code

C++ library for Ex3_18 (applying to the I2C interface).
Download source code
Library for Ex3_19 of the BME280 software-implemented I2C driver.
Download source code
Revised driver of BME280 for the following example codes: Ex4_1, Ex4_1b, Ex4_1c, Ex4_2, Ex4_4.
Download source code
Library applying to the built-in I2C driver of BNO055 for the example codes: Ex4_5, Ex4_5b, Ex4_6, Ex4_6b.
Download source code
Library associating music notes to the semi-period of each note for the example codes: Ex5_5, Ex5_6.

Third-party Arduino Libraries (External Links for Download)

Download from GitHub

TinyCircuits Wireling Arduino Library. This library allows for easy use of the various Wireling adapter boards. Provides basic functions for using the PCA9544A I²C multiplexer, and power switching. (Applies to Ex4_8)

Download from GitHub

VL53L0X library for Arduino. This is a library for the Arduino IDE that helps interface with ST’s VL53L0X time-of-flight distance sensor. The library makes it simple to configure the sensor and read range data from it via I²C. (Applies to Ex4_8)

Download from GitHub

Adafruit TCS34725 Color Sensor Driver. This driver is for the Adafruit TCS34725 Breakout. (Applies to Ex4_10, Ex4_11, Ex4_11b)

Download from GitHub

Adafruit MicroBit library. Wrapper code and examples for using Microbit with Arduino IDE. (Applies to Ex4_11, Ex4_11b)

Download from GitHub

Adafruit NeoPixel Library. Arduino library for controlling single-wire-based LED pixels and strip. (Applies to Ex5_3, Ex5_4, Ex5_5, Ex5_6)

Download from GitHub

DFRobot_BMP280 Library for Arduino. Provides an Arduino library for reading and interpreting Bosch BMP280 data over I2C. Used to read current temperature, air pressure and calculate altitude. (Applies to Ex5_4, Ex5_6)

Download from GitHub

SlowSoftWire. A wrapper for SlowSoftI2CMaster emulating the functionality of the Wire library, for master clients. (Applies to Ex5_4, Ex5_6)

Examples of Chapter 2

Download source code

Data output from the μC to a host PC. The reader explores: a) printable and non-printable ASCII characters and strings; b) storage of stings in either Data Memory or Program Memory of Arduino Uno.

Download source code

Datatypes and typecasting tasks. The reader explores: a) signed/unsigned data, datatypes of bool, char, short, int, long, and float; b) typecasting conversions between datatypes; c) number of bytes reserved in Arduino Uno memory by each particular datatype (sizeof() operator).

Download source code

Program flow of control. The reader explores: a) unconditional branching with while(); b) repetition of a code part with for(); c) the variable scope coding style in for() loops. Examples are applied to printable and non-printable ASCII and to typecasting, while the assignment (=), comparison (<), and compound (++) operators are used by the example.

Download source code

Boolean operators in program flow of control. The reader explores the Logical OR(II) and AND(&&) operators along with the if() and continue statements, so as to implement conditional branch.

Download source code

Data input to the μC from a host PC. The reader explores how to access the elements of an array via either subscripts when using loops of predefined iterations, or pointers when using loops of undefined iterations. In the latter case the reader explores the meaning of a null-terminated string, as well as the influence of a unary pre-increment/decrement or post-increment/decrement operation to a pointer. The Indirection (*), Address-of (&) and Logical NOT (!) operators are utilized by the example.

Download source code

Convert numeric string to binary value. The reader explores further the issues learned before (i.e., inputting data to the μC, accessing arrays with pointers, exploiting logical operations in the program flow of control, etc.) and, in addition, he/she applies arithmetic operations toward converting a numeric string to binary value. The Arithmetic operators of addition (+), subtraction (-), and multiplication (*) are utilized by the example.

Download source code

Convert binary value to numeric string. The reader explores further the arithmetic operations in the μC’s memory via the opposite operation than before. That is, by converting a binary value to numeric string. The conversion is performed in two steps: a) by applying BCD encoding to the binary value and b) by converting the BCD encoding outcome to ASCII character. The latter task is performed via a switch()…case statement. The Arithmetic operators of division quotient (/) and division remainder (%) are utilized by the example.

Download source code

2’s complement representation of signed numbers. The reader exploits the entry of a numeric string from the host PC, which represents a single-byte decimal value (i.e., from 0 to 255). Through particular bitwise and arithmetic operations performed by the μC, the reader explores the signed number representation in 2’s complement arrangement. The method of extracting bit information from a variable using bitmasks, along with bitwise tasks, is also explored.

Download source code

Multiply/Divide binary numbers to a power of 2. The reader explores further the bitwise  operations, and in particular the bit-shift left (<<) and right (>>) operators, toward multiplying a binary number with a power of 2 (e.g. 23, 27, and so on).

Download source code

Code decomposition with functions. The reader explores further how to decompose the tasks performed by the previous example codes, with functions.

Download source code

Code decomposition with macros. The reader explores further the code decomposition techniques with the use of macros as well as the difference between macros and functions.

Examples of Chapter 3

Download source code

Blinking LED. The reader explores how provide, from the μC device, a digital pin interface with the outside world. Through the simplest example of a blinking LED, the reader realizes the rapid instruction execution by the μC’s processor, as well as the need to slowdown particular processes, through delay() routines, so as to support the user interface.

Download source code

Blinking LED. The reader explores further how to generate an easily upgradable code with the use of global variables.

Download source code

Blinking LED. The reader explores further how to generate an easily upgradable code with the use of object-like macros as well as the space in memory reserved by macros compared to the global variables.

Download source code

Blinking LED. The reader explores further how to generate a more readable code with the use of functions.

Download source code

Blinking LED. The reader explores further how to generate a more readable code with the use of function-like macros.

Download source code

Digital PORT interface. The reader explores how the digital IO pins of Arduino Uno are grouped together and how to concurrently access all pins of a PORT without using Arduino-specific functions.

Download source code

Digital PORT interface. The reader explores further how to concurrently access some, but not all pins of a PORT without using the Arduino-specific functions. To this end, the reader exploits bitwise operators along with the proper bitmasks.

Download source code

Digital PORT interface. The reader explores further the direct manipulation of a PORT via bitwise operators and bitmasks, which are now incorporated within custom function-like macros. The reader explores the delay generated by the coding differences in the examples Ex3_2, Ex3_2b, Ex3_2c, when removing the long delay of 1s.

Download source code

Digital PORT interface. The reader explores further the latency generated when controlling two digital pins of the same PORT sequentially, compared to the instant response of the pins when they are controlled concurrently, via the PORT registers.

Download source code

Digital data input to the μC with a push-button. The reader explores the data input process from a digital pin, using a common push-button as input unit. The connection of the push-button to one of the μC pins via a pull-up resistor, is also analyzed. (Data output using a LED is also carried out.)

Download source code

Debounce delay. The reader explores the switch-bounce phenomenon and how it can affect the regular functionality of a system, and address a debounce delay to resolve that issue.

Download source code

Digital data input via direct port manipulation. The reader explores the data input process from a digital pin, via direct manipulation of the PORT’s registers using proper bitwise operations & bitmasks.

Download source code

Analog data input. The reader explores how to read analog data from the μC when the latter encompasses an ADC.

Download source code

Analog data output via PWM. The reader explores how to indirectly generate an analog signal, via the mean value occurred by a pulse width modulation.

Download source code

Interrupt. The reader explores how to enable external interrupt to a special-function digital pin and how the processor invokes an interrupt service routine (ISR), as well as resumes the control flow after servicing the ISR.

Download source code

Built-in UART Serial Interface. The reader explores, in detail, the functionality of the UART interface through a simple example which repeatedly transmits the ASCII character ‘A’ to the host PC. An analysis of the quite known 8N1 type of communication, through the observation of the serial data acquired by an oscillator, is performed.

Download source code

External UART module. The reader explores how to implement an UART serial interface using an external module. (That particular task is useful when incorporating a μC of no buil-in UART.)

Download source code

Software-implemented UART. The reader explores how to implement a software-based UART serial interface via regular IO pins (particularly useful when no built-in or external UART is available).

Download source code

Built-in SPI Serial Interface (Read). The reader is introduced, (through a simplified example) to the operating principles of the SPI serial interface. The example performs an SPI read to the chip id of BME280 environmental sensor and sends data to the host PC.

Download source code

Built-in SPI Serial Interface (Write). The reader explores further the SPI operating principles, by applying an SPI write to one of the registers of BME280 sensor, used for the configuration of the device.

Download source code

Built-in SPI Serial Interface (Multiple-byte Read). The reader explores further how to speed-up the SPI reading transaction, through the possibility of applying multiple-byte read to the adjacent registers of an SPI peripheral device.

Download source code

Built-in SPI (Environmental Measurements). The reader explores the first real-world project which obtains environmental measurements from BME280 sensor. Through this particular example, the reader explores how to build an SPI driver for the sensor device as well as perform advanced arithmetic and bitwise operation, which are needed to obtained the compensated Temperature, Pressure, and Humidity values. (The requisite operations are described, in detail, in the BME280 datasheet.)

Download source code

Software-implemented / custom-designed SPI. As before, the reader (through a simplified example) performs an SPI read to the chip id of BME280 environmental sensor. However, this time we address a custom-defined (i.e., software-implemented) SPI library. The reader explores how to build custom-designed Arduino (i.e., C++) libraries. This library is quite useful for μCs of no built-in SPI, as well for the case where pins other than the dedicated SPI pins are required (for a particular reason) to drive the SPI module.

Download source code

Software-implemented / custom-designed SPI. As before, the reader obtains environmental measurements from BME280 sensor, but using the custom-designed SPI library of the previous example.

Download source code

Built-in I2C Serial Interface (Read). The reader is introduced, (through a simplified example) to the operating principles of the I2C serial interface. The example performs an I2C read to the chip id of BME280 environmental sensor and sends data to the host PC.

Download source code

Built-in I2C Serial Interface (Write). The reader explores further the I2C operating principles, by applying an I2C write to one of the registers of BME280 sensor, used for the configuration of the device.

Download source code

Built-in I2C Serial Interface (Multiple-byte Read). The reader explores further how to speed-up the I2C reading transaction, through the possibility of applying multiple-byte read to the adjacent registers of an I2C peripheral device.

Download source code

Built-in I2C (Environmental Measurements). As in the SPI example, the reader explores how to build an I2C driver for the acquisition of environmental measurements by BME280 sensor.

Download source code

Software-implemented / custom-designed I2C. As in the SPI example, the reader (through a simplified example) performs an I2C read to the chip id of BME280 environmental sensor. However, this time we address a custom-defined (i.e., software-implemented) I2C library. The reader explores how to build the custom-designed Arduino (i.e., C++) library for the I2C control of a peripheral module. This library is quite useful for μCs of no built-in I2C, as well for the case where pins other than the dedicated I2c pins are required (for a particular reason) to drive the I2C module.

Download source code

Software-implemented / custom-designed I2C. As before, the reader obtains environmental measurements from BME280 sensor, but using the custom-designed I2C library of the previous example.

Examples of Chapter 4

Download source code

Real-time monitoring of atmospheric pressure. The reader exploits BME280 sensor (which has been used in the previous chapter) toward implementing a data acquisition system (DAQ). The reader explores, in depth, the critical timings that determine the hardware’s sampling interval (i.e., the dead time among adjacent air pressure samples). Then, he/she learns how to build a custom-designed C language code for acquiring the pressure samples that arrive (from the μC device) to the serial port (i.e. USB-to-UART) of a host PC. The samples are stored, through the C code, to a text (.txt) file for additional offline analysis. To build the executable (.exe) file from the C source code, the reader exploits the freeware Minimalist GNU for Windows (MinGW) programming tool, while the source code development is performed by the freeware Notepad++ editor.

Download source code

Data acquisition of atmospheric pressure. The reader implements the same example code but this time, the μC acquires 100 samples temporarily stored to its data memory and, then, the μC transmits the 100 samples to the host PC (non-real-time monitoring).

Download source code

Data acquisition of atmospheric pressure. The reader explores further how to provide a graphical monitoring feature to the C code running on the host PC. For that particular purpose, the reader exploits the freeware Gnuplot graphing utility, which can be invoked by C code.

Download source code

Data acquisition of atmospheric pressure. The reader upgrades Ex4_1 (firmware & software) so that air pressure measurements are concurrently obtained from two identical sensors. This particular setup can be used for calculating the absolute height in-between the two sensor devices (i.e., a regular implementation in indoor navigation systems, and other creative applications).

Download source code

Getting started with Teensy 3.2. This simple example, which blinks a LED and sends a string to the USB-to-Serial port of a host PC, attempts to help the reader in getting started with the 32-bit Teensy 3.2 board. To download the code into the Teensy board, the user needs to install (the available for free) Teensyduino add-on software.

Download source code

DAQ of atmospheric pressure with Teensy 3.2. The reader learns how to obtain air pressure samples from BMP280 sensor found on the Pesky’s BNO055+BMP280 module. The reader exploits the compatibility between BMP280 and BME280 sensors and addresses the same driver, used earlier for the BME280 device. Thereby, the reader stays focused on the differentiations found in the application code between Teensy and Arduino Uno boards.

Download source code

Orientation detection. The reader continuous the involvement with contemporary sensor devices and in this example, he/she exploits BNO055 system in package (SiP) toward the orientation detection expressed with Euler angles (i.e., Pitch, Roll, Yaw).

Download source code

Orientation detection. The previous firmware has been updated to work with the custom-designed DAQ software (i.e., BNO055-gnuplot.c) running on the host PC, as well as with the OpenGL-based software explained at the end of this subchapter (i.e., BNO055-openGL.c). The latter software requires installing GLUT toolkit and generates a real-time rotation of 3D box on host PC, where rotation is in agreement with the Euler angels obtained from the hardware board.

Download source code

Gesture recognition & motion detection. The reader exploits BNO055 SiP toward the Gravity vector as well as Linear acceleration detection. Then, the reader explores further how to utilize Gravity vector toward applying gesture recognition, and how to utilize Linear acceleration toward motion detection.

Download source code

Gesture recognition & Motion detection. The previous firmware has been updated to work with the custom-designed DAQ software (i.e., BNO055-gnuplot.c) running on the host PC.

Download source code

Getting started with TinyZero. This simple example, which blinks a LED and sends a string to the USB-to-Serial port of a host PC, attempts to help the reader in getting started with the 32-bit TinyZero board. To download the code into the TinyZero board, the user should follow the directions provided by the book.

Download source code

Distance detection and 1D gesture recognition. The reader is introduced to the distance (ToF) sensor VL53L0X and develops a firmware which applies either to distance detection (when using a single sensor) or to 1 dimension (1D) gesture recognition (when using two identical sensors). The accompanying DAQ software VL53L0X.c applies to distance detection.

Download source code

Getting started with Micro:bit. This simple example, which read two push-buttons, blinks a LED and sends a string to the USB-to-Serial port of a host PC, attempts to help the reader in getting started with the 32-bit Micro:bit board. To download the code into the Micro:Bit board, the user should follow the instructions provided by the book.

Download source code

RGB color sensing. The reader is introduced to color sensing techniques and develops firmware as well as software for the host PC, which recognizes red (R), green (G) and blue (B) colors (via the Adafruit’s board employing TCS3472 sensor). It is here noted that the software code for the PC constitutes OpenGL code and, hence, GLUT toolkit is required for this particular example.

Download source code

RGB color sensing via Bluetooth. The reader upgrades the previous example code so that data are forwarded to a mobile phone, as well, via Bluetooth low energy (BLE). To obtain the data to the mobile device (of Bluetooth version 4.2 or higher) the user should install the Adafruit Bluefruit Connect app.

Download source code

RGB color sensing via Bluetooth. The reader upgrades the previous example code so that data are forwarded to the mobile phone, as well, via as soon as the user transmits any character to the Micro:bit board.

Examples of Chapter 5

Download source code

Tinkering an RC car. The user explores a trial code so as to test the Bluetooth connectivity between the Teensy 3.2 board and a smartphone.

Download source code

Tinkering an RC car. The user explores how to decode the commands available at the Control Pad user interface of Bluefruit Connect app.

Download source code

Tinkering an RC car. The user develops the final firmware code for the control of an RC car through a Smartphone device.

Download source code
Prototyping an interactive game for sensory play. The reader starts the bottom-up design method of the prototype system via the control of a LED ring (found on the Kitronik’s Zip Halo board), while also implementing a random number generator. (This code part is meant for triggering the children’s sight sense.)

Download source code

Prototyping an interactive game for sensory play. The reader continues the the bottom-up design method of the prototype system and explores how to generate a virtual button with an air pressure sensor and a DC fan. (This code part is meant for triggering the children’s proprioception sense.)

Download source code

Prototyping an interactive game for sensory play. The reader continues the bottom-up design method of the prototype system and this time, he/she explores how to reproduce sound from a μC and a speaker. (This code part is meant for triggering the children’s hearing sense.)

Download source code
Prototyping an interactive game for sensory play. The reader concludes the bottom-up design method with the implementation of the overall firmware of the proposed game, which engages the children’s sight, hearing, and proprioception senses.