Reader small image

You're reading from  TinyML Cookbook - Second Edition

Product typeBook
Published inNov 2023
PublisherPackt
ISBN-139781837637362
Edition2nd Edition
Right arrow
Author (1)
Gian Marco Iodice
Gian Marco Iodice
author image
Gian Marco Iodice

Gian Marco Iodice is team and tech lead in the Machine Learning Group at Arm, who co-created the Arm Compute Library in 2017. The Arm Compute Library is currently the most performant library for ML on Arm, and it's deployed on billions of devices worldwide – from servers to smartphones. Gian Marco holds an MSc degree, with honors, in electronic engineering from the University of Pisa (Italy) and has several years of experience developing ML and computer vision algorithms on edge devices. Now, he's leading the ML performance optimization on Arm Mali GPUs. In 2020, Gian Marco cofounded the TinyML UK meetup group to encourage knowledge-sharing, educate, and inspire the next generation of ML developers on tiny and power-efficient devices.
Read more about Gian Marco Iodice

Right arrow

Unleashing Your Creativity with Microcontrollers

Bringing machine learning (ML) to life on microcontrollers is a thrilling adventure because our creations can go beyond our computers’ boundaries and make an impact in the real world. However, before diving into this fascinating world, let’s take a moment to explore how to craft basic applications on microcontrollers to get up to speed with the principles of embedded programming.

In this chapter, we will start our exploration by handling data transmission over the serial communication protocol, equipping ourselves with a foundation for basic code debugging. The transmitted data will be captured in a log file and uploaded to our cloud storage in Google Drive.

Afterward, we will delve into programming the GPIO peripheral using the Arm Mbed API and use a solderless breadboard to connect external components, such as LEDs and push-buttons.

The aim of this chapter is to delve into the basic principles of microcontroller...

Technical requirements

To complete all the practical recipes of this chapter, we will need the following:

  • An Arduino Nano 33 BLE Sense
  • A Raspberry Pi Pico
  • A SparkFun RedBoard Artemis Nano (optional)
  • A micro-USB data cable
  • A USB-C data cable (optional)
  • 1 x half-size solderless breadboard (30 rows and 10 columns)
  • 1 x red LED
  • 1 x 220 Ω resistor
  • 1 x push-button
  • 5 x jumper wires
  • Laptop/PC with either Linux, macOS, or Windows
  • Google Drive account

The source code and additional material are available in the Chapter02 folder of the GitHub repository: https://github.com/PacktPublishing/TinyML-Cookbook_2E/tree/main/Chapter02.

Transmitting data over serial communication

Code debugging is a fundamental process of software development to identify errors in code.

This recipe will demonstrate how to conduct print debugging on the Arduino Nano and Raspberry Pi Pico by transmitting the following strings to the serial terminal:

  • Initialization completed: once the serial port on the microcontroller has finished initializing
  • Executed: after every 2 seconds of program execution

Getting ready

All programs are prone to bugs, and print debugging is a basic process that displays statements on the output terminal, providing valuable insight into the program’s execution, as shown in the following example:

int func (int func_type, int a) {
  int ret_val = 0;
  switch(func_type){
    case 0:
      printf("FUNC0\n");
      ret_val = func0(a)
      break;
    default:
      printf("FUNC1\n");
      ret_val = func1(a);
  }
  return ret_val;
}

In the preceding...

Reading serial data and uploading files to Google Drive with Python

When developing tinyML projects, our microcontrollers can use serial communication to transfer data of any type to our computer.

In this recipe, we will showcase how to develop a local Python script to retrieve the transmitted data from the PC’s serial port. The program will record one minute of data transmission to a file, which will be uploaded to Google Drive.

Getting ready

Throughout the book, the microcontroller will use serial communication for various scopes, such as:

  • Tracking events when the program runs
  • Debugging sensor functionalities
  • Gathering relevant data for building the dataset used to train and test an ML model

The last point will likely be the most enjoyable. For instance, you will use the microphone to record your vocals or musical recordings and a camera to snap pictures. However, unlike our standard computers or laptops, which have an operating...

There’s more…

In this recipe, we learned how to use the PyDrive library to upload data captured with the microcontroller to the cloud automatically.

PyDrive is not restricted to uploading files to Google Drive only. In reality, PyDrive can carry out the usual tasks that are done in the web browser, such as:

  • Creating files
  • Downloading files
  • Searching for files
  • Delegating files

For more information, refer to the official PyDrive documentation at https://pythonhosted.org/PyDrive/.

For example, you may consider extending the Python script to create a directory in Google Drive to automate uploading files in the cloud fully.

Serial communication is undoubtedly an easy way to get information during the program execution. However, its relatively slow data transfer speed could make it unsuitable for some applications.

The upcoming recipe will show an alternative approach that can only display simple information to the user...

Implementing an LED status indicator on the breadboard

Microcontrollers enable us to interact with the world around us by using sensors and performing physical actions, such as turning an LED on and off or moving an actuator.

In this recipe, we will learn how to connect external components with the microcontroller by building the following electronic circuit on the breadboard:

Figure 2.20: The LED power status indicator circuit

The electronic circuit illustrated in Figure 2.20 uses the red LED to indicate whether the microcontroller is connected to the power source.

Getting ready

Connecting external components to the microcontroller means physically joining two or more metal connectors. Although we could solder these connectors, it is not usual for prototyping because it is not quick and straightforward.

Therefore, this recipe presents a solderless alternative to connect our components effortlessly.

Making contact directly with the microcontroller...

Controlling an external LED with the GPIO

Nowadays, LEDs are everywhere, particularly in our houses, because they use less energy than older lights for the same luminous intensity. However, the LEDs considered for our experiments are not light bulbs but through-hole LEDs for rapid prototyping on the breadboard.

In this recipe, we will discover how to build a basic circuit with an external LED and program the GPIO peripheral to control its light.

Getting ready

To implement this recipe, we need to know how the LED can emit light and how to program the microcontroller GPIO peripheral to turn the light on and off.

Let’s start by explaining what an LED is and how it works.

Understanding the LED functionality

LED stands for Light Emitting Diode and is a semiconductor component that emits light when a current flows through it.

In this book, we will use through-hole LEDs, which are made of the following:

  • A head of transparent material from where...

Turning an LED on and off with a push-button

In contrast to a PC, where the keyboard, mouse, or touchscreen facilitates human interactions with software applications, the physical button represents the most common way to interact with a microcontroller.

In this recipe, we will learn how to integrate a push-button into the electronic circuit built in the previous recipe. Then, we will employ the GPIO peripheral to detect whether the button is pushed or released and use this information to control the LED light.

Getting ready

Before diving into the practical part of this recipe, let’s start by introducing the operating principles of the push-button.

The operating principles of the push-button

From an electronics point of view, a push-button is a device that makes (a.k.a. shorts) or breaks (a.k.a. opens) the connection between two wires. When we press the button, we connect the wires through a mechanical system, allowing the current to flow. However, unlike...

Using interrupts to read the push-button state

The previous recipe showed how to read digital signals with the GPIO peripheral. However, the proposed solution is inefficient because the CPU wastes clock cycles waiting for the button to be pressed while it could perform other tasks in the meantime. Furthermore, this could be a situation where we would keep the CPU in low-power mode when there are no other tasks to run.

Therefore, this recipe will show you how to change the sketch developed in the previous recipe to read the push-button state efficiently using interrupts.

Getting ready

Let’s prepare this recipe by learning what an interrupt is and which Mbed OS API we can use to read the push-button efficiently.

Working with interrupts using the Mbed OS API

An interrupt is a signal that temporarily pauses the main program to address an event through a dedicated function known as an interrupt handler or interrupt service routine (ISR). When the ISR ends the...

Summary

The recipes presented in this chapter covered the basic principles of microcontroller programming, a prerequisite for the projects developed in the rest of this book.

In the first part, we learned how to use the microcontroller to transmit data serially to the computer for generating files to upload to Google Drive.

Then, our focus shifted to the principles of controlling the LED light through the GPIO peripheral. These recipes taught us how to build electronic circuits on the breadboard, determine the appropriate resistor based on the LED emitting light color, and program the GPIO peripheral to output digital signals.

Finally, we discovered how to attach a push-button to the microcontroller and program the GPIO peripheral to read its state. Upon completing this chapter, you should be well prepared to delve into developing your first tinyML project.

In the next chapter, we will implement a basic weather station to predict the occurrence of snowfall using the...

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://packt.link/tiny

lock icon
The rest of the chapter is locked
You have been reading a chapter from
TinyML Cookbook - Second Edition
Published in: Nov 2023Publisher: PacktISBN-13: 9781837637362
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Gian Marco Iodice

Gian Marco Iodice is team and tech lead in the Machine Learning Group at Arm, who co-created the Arm Compute Library in 2017. The Arm Compute Library is currently the most performant library for ML on Arm, and it's deployed on billions of devices worldwide – from servers to smartphones. Gian Marco holds an MSc degree, with honors, in electronic engineering from the University of Pisa (Italy) and has several years of experience developing ML and computer vision algorithms on edge devices. Now, he's leading the ML performance optimization on Arm Mali GPUs. In 2020, Gian Marco cofounded the TinyML UK meetup group to encourage knowledge-sharing, educate, and inspire the next generation of ML developers on tiny and power-efficient devices.
Read more about Gian Marco Iodice