Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
TinyML Cookbook - Second Edition

You're reading from  TinyML Cookbook - Second Edition

Product type Book
Published in Nov 2023
Publisher Packt
ISBN-13 9781837637362
Pages 664 pages
Edition 2nd Edition
Languages
Author (1):
Gian Marco Iodice Gian Marco Iodice
Profile icon Gian Marco Iodice

Table of Contents (16) Chapters

Preface 1. Getting Ready to Unlock ML on Microcontrollers 2. Unleashing Your Creativity with Microcontrollers 3. Building a Weather Station with TensorFlow Lite for Microcontrollers 4. Using Edge Impulse and the Arduino Nano to Control LEDs with Voice Commands 5. Recognizing Music Genres with TensorFlow and the Raspberry Pi Pico – Part 1 6. Recognizing Music Genres with TensorFlow and the Raspberry Pi Pico – Part 2 7. Detecting Objects with Edge Impulse Using FOMO on the Raspberry Pi Pico 8. Classifying Desk Objects with TensorFlow and the Arduino Nano 9. Building a Gesture-Based Interface for YouTube Playback with Edge Impulse and the Raspberry Pi Pico 10. Deploying a CIFAR-10 Model for Memory-Constrained Devices with the Zephyr OS on QEMU 11. Running ML Models on Arduino and the Arm Ethos-U55 microNPU Using Apache TVM 12. Enabling Compelling tinyML Solutions with On-Device Learning and scikit-learn on the Arduino Nano and Raspberry Pi Pico 13. Conclusion
14. Other Books You May Enjoy
15. Index

Deploying a sketch on microcontrollers

Following the introductory section, we will delve into our first recipe to familiarize ourselves with the Arduino IDE and better understand how to compile and upload a sketch on an Arduino platform. To accomplish this objective, we will use a pre-built Arduino sketch to blink the LED on our microcontroller boards.

Getting ready

An Arduino sketch consists of two functions, setup() and loop(), as shown in the following code block:

void setup() {
}
void loop() {
}

The setup() function is the first function executed by the program when we press the reset button or power up the board. This function is executed only once and is generally responsible for initializing variables and peripherals.

After the setup() function, the program executes the loop() one, which runs iteratively and forever, as illustrated in the following diagram:

Text

Description automatically generated with medium confidence

Figure 1.23: The setup() function runs once

These two functions are required in all Arduino programs.

How to do it…

Open the Arduino IDE, and follow the steps to make the on-board LED of our microcontroller boards blink:

Step 1:

Connect either the Arduino Nano or Raspberry Pi Pico to a laptop/PC through the micro-USB data cable. Next, check that the Arduino IDE reports the board’s name and serial port in the device drop-down menu:

A close-up of a computer code

Description automatically generated
Figure 1.24: The device drop-down menu reporting the board’s name and serial port

If you have connected the Arduino Nano, the device drop-down menu in the Arduino IDE should report Arduino Nano 33 BLE as the board’s name, as shown in Figure 1.24.

Instead, if you connect the Raspberry Pi Pico, the Arduino IDE should report Raspberry Pi Pico as the board’s name.

Near the board’s name, you can also find the serial port. The serial port, which in Figure 1.24 is /dev/ttyACM0, depends on the operating system (OS) and the device driver. This serial port will be our bridge for communication between the microcontroller and the computer.

Step 2:

Open the prebuilt Blink example by clicking on Examples from the left-hand side menu, BUILT IN from the new menu, and then Blink, as shown in the following screenshot:

Graphical user interface, application

Description automatically generated

Figure 1.25: Built-in LED blink example

Once you have clicked on the Blink sketch, the code will be visible in the editor area.

Step 3:

Click on the arrow on the left of the board dropdown to compile and upload the program to the target device, as shown in Figure 1.26:

A picture containing graphical user interface

Description automatically generated

Figure 1.26: The arrow on the left of the board dropdown will compile and flash the program on the target device

In embedded programming, we generally use the term flashing when referring to the uploading of the program to the microcontroller.

The console output should return Done at the bottom of the page, and the on-board LED should start blinking, which means the sketch has been successfully compiled and uploaded to the microcontroller!

There’s more…with the SparkFun Artemis Nano!

The LED blinking sketch we just uploaded on the Arduino Nano and Raspberry Pi Pico is also available for the SparkFun Artemis Nano microcontroller.

In the local Arduino IDE, the Blink example is in File -> Examples -> 01.Basics -> Blink:

A screenshot of a computer

Description automatically generated

Figure 1.27: Built-in LED blink example in the local Arduino IDE

Once you click the Blink example, a new window with the sketch will be displayed. Before compiling the program, connect the SparkFun Artemis Nano to a laptop/PC through the USB-C data cable and make sure the device drop-down menu shows RedBoard Artemis Nano as the board’s name:

A close-up of a logo

Description automatically generated

Figure 1.28: The device drop-down menu reporting the SparkFun Artemis Nano board

Then, click on the arrow on the left of the board dropdown to compile and upload the program to the target device. After a few seconds, the console output should return Upload complete, and the on-board LED of the SparkFun Artemis Nano should start blinking!

You have been reading a chapter from
TinyML Cookbook - Second Edition
Published in: Nov 2023 Publisher: Packt ISBN-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.
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 $15.99/month. Cancel anytime}