You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To get started with your first **TinyML project**, the **Arduino Nano RP2040 Connect** is a good option. Built by Arduino, it uses the **RP2040 microcontroller** and is fully supported by the Arduino core package. The board comes with built-in Wi-Fi, Bluetooth, and an onboard IMU—features that is useful for deploying machine learning models at the edge.
11
+
To get started with your first **TinyML project**, the **Arduino Nano RP2040 Connect** is a good option. Built by Arduino, it uses the **RP2040 microcontroller** and is fully supported by the Arduino core package.
12
+
13
+
14
+
The board comes with built-in Wi-Fi, Bluetooth, and an onboard IMU. These are features that are useful for deploying machine learning models at the edge.
Its compatibility with popular tools like Edge Impulse and the Arduino IDE makes it a suitable choice for TinyML applications. You can learn more about the Arduino Nano RP2040 Connect on the [official Arduino website](https://store.arduino.cc/products/arduino-nano-rp2040-connect-with-headers?_gl=1*1laabar*_up*MQ..*_ga*MTk1Nzk5OTUwMS4xNzQ2NTc2NTI4*_ga_NEXN8H46L5*czE3NDY1NzY1MjUkbzEkZzEkdDE3NDY1NzY5NTkkajAkbDAkaDE1MDk0MDg0ODc.).
16
19
17
-
## Put everything together
20
+
## Connect and set up the Arduino Nano RP2040 Connect
21
+
22
+
This guide will help you connect the Arduino Nano RP2040 Connect to your computer and set up the Arduino IDE for programming.
18
23
19
-
### Step 1: Connect the LED to the Arduino Nano RP2040
24
+
##Gather the components
20
25
21
-
To visualize the output of the voice command model, you will use a simple LED circuit.
26
+
To visualize the output of the voice command model, you'll use a simple LED circuit.
22
27
23
-
### Components Needed
28
+
You will need the following components:
24
29
25
-
- Arduino Nano RP2040 Connect
26
-
-1x LED
27
-
-1x 220Ω resistor
30
+
- Arduino Nano RP2040 Connect
31
+
-1× LED
32
+
-1× 220 Ω resistor
28
33
- Breadboard and jumper wires
29
34
30
-
#### Circuit Diagram
35
+
##Connect the LED to the Arduino Nano RP2040
31
36
32
-
-**Anode (long leg) of the LED** → Connect to **GPIO pin D2**via the 220Ω resistor
33
-
-**Cathode (short leg)** → Connect to **GND**
37
+
-**Anode (long leg) of the LED** → connect to **GPIO pin D2**through a 220 Ω resistor


38
43
39
-
### Step 2: Set Up the Arduino IDEs
44
+
##Set up the Arduino IDE
40
45
41
46
To program and deploy your trained model to the Arduino Nano RP2040, you first need to configure your development environment.
42
47
43
-
Follow the detailed setup instructions provided in the [Arduino Nano RP2040 Install Guide](https://learn.arm.com/install-guides/arduino-pico/)
48
+
Follow the steps in the [Arduino Nano RP2040 install guide](https://learn.arm.com/install-guides/arduino-pico/).
44
49
45
-
This guide will walk you through:
50
+
This guide walks you through:
46
51
47
-
- Installing the Arduino IDE
52
+
- Installing the Arduino IDE
48
53
- Adding the board support package for the Nano RP2040
49
54
50
-
{{% notice Note %}}
51
-
Follow every instruction in the guide *except*`How do I set up the Raspberry Pi Pico W?`, as it is not needed for this project.
52
-
{{% /notice %}}
55
+
{{< notice Note >}}
56
+
Follow all steps in the guide except*How do I set up the Raspberry Pi Pico W?*, which is not required for this project.
57
+
{{< /notice >}}
53
58
54
-
### Step 3: Select Your Board and Port in the Arduino IDE
59
+
##Select your board and port in the Arduino IDE
55
60
56
-
First, open the **Arduino IDE**.
61
+
Open the Arduino IDE. To select your board:
57
62
58
-
To select your board:
59
-
60
-
1. Go to **Tools** > **Board**.
61
-
2. From the list, choose **Arduino Nano RP2040 Connect**.
63
+
Go to **Tools** > **Board** then select **Arduino Nano RP2040 Connect**
62
64
63
65
To select your port:
64
66
65
-
1. Connect your Arduino board to your computer using a USB cable.
66
-
2. Go to **Tools** > **Port**.
67
-
3. Select the port labeled with your board’s name, e.g., `COM4 (Arduino Nano RP2040 Connect)` or `/dev/cu.usbmodem...` on macOS.
67
+
Connect your board to your computer using a USB cable, then follow these steps:
68
+
69
+
- Go to **Tools** > **Port**
70
+
- Select the port labeled with your board's name, such as `COM4 (Arduino Nano RP2040 Connect)` or `/dev/cu.usbmodem...` on macOS.
68
71
69
-
*Your Arduino IDE is now ready to upload code to the Arduino Nano RP2040.*
72
+
Your Arduino IDE is now ready to upload code to the Arduino Nano RP2040.
Copy file name to clipboardExpand all lines: content/learning-paths/embedded-and-microcontrollers/edge/program-and-deployment.md
+90-60Lines changed: 90 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,49 +1,79 @@
1
1
---
2
-
title: Program your first tinyML device
2
+
title: Program your first TinyML device
3
3
weight: 5
4
4
5
5
### FIXED, DO NOT MODIFY
6
6
layout: learningpathall
7
7
---
8
8
9
-
#Programming your first tinyML device
9
+
## Program and deploy your first TinyML device
10
10
11
-
This section helps you create a complete sketch that you can upload onto your Arduino Nano RP2040.
11
+
This section shows you how to create a complete sketch that you can upload onto your Arduino Nano RP2040.
12
12
13
-
## Step 1: Create a New Sketch
13
+
You’ll begin by setting up a new sketch in the Arduino IDE, then add the library generated by Edge Impulse. After reviewing a sample program, you’ll upload it to your board to run real-time audio inference and control an LED based on voice commands.
14
14
15
-
1. Open the **Arduino IDE**.
16
-
2. Go to **File** > **New**.
17
-
3. A new sketch (blank code window) will open, ready for you to start writing your code.
18
-
4. Save your sketch by going to **File** > **Save**. Choose a name and location for your file.
15
+
By the end of this section, your board will be able to respond to the words "on" and "off". It is a simple, but powerful, demonstration of TinyML in action.
19
16
20
-
## Step 2: Upload the Arduino Library from Edge Impulse
17
+
## Create a new sketch
21
18
22
-
1. After creating and saving your sketch, go to **Sketch** > **Include Library** > **Add .ZIP Library**.
23
-
2. In the file dialog that opens, navigate to the location of the **ZIP file** you exported from Edge Impulse in [Set up your environment](/learning-paths/embedded-and-microcontrollers/edge/software-edge-impulse/)
24
-
3. Select the **ZIP file** and click **Open**.
19
+
To create a new sketch in the Arduino IDE, follow these steps:
25
20
26
-
## Step 3: Include the Library in Your Sketch
21
+
* Open the **Arduino IDE**, and then go to **File** > **New**.
22
+
* A new sketch window will open, and you will see a blank sketch with the default structure, which includes two main functions: `setup()` and `loop()`. The `setup()` function runs once when the program starts, and the `loop()` function runs continuously after that.
23
+
* You will see a blank sketch with the default structure, which includes two main functions: `setup()` and `loop()`. The `setup()` function runs once when the program starts, and the `loop()` function runs continuously after that.
24
+
* Now save your sketch using **File** > **Save**, and give it a clear name. For example, you can name it **"TinyML_Voice_Control"** or something similar that describes your project.
27
25
28
-
Finally, to include the library and model in your sketch, go to **Sketch** > **Include Library** and select the newly installed library and model from the list.
26
+
{{< notice Note >}}
27
+
Make sure to save it in a location where you can easily find it later, such as your **Documents** folder or a dedicated **Arduino** folder.
28
+
{{< /notice >}}
29
29
30
-
{{% notice Note %}}
31
-
The libray should be of the form `Name_of_your_library_inferencing.h`
32
-
{{% /notice %}}
33
30
34
-
# Code walk-through
31
+
## Upload the Arduino library from Edge Impulse
32
+
33
+
* After creating and saving your sketch, go to **Sketch** > **Include Library** > **Add .ZIP Library**
34
+
* In the file dialog that opens, navigate to the location of the ZIP file you exported from Edge Impulse in [Set up your environment](/learning-paths/embedded-and-microcontrollers/edge/software-edge-impulse/)
35
+
* Select the ZIP file and click **Open**
36
+
* The Arduino IDE will automatically install the library and any dependencies it requires.
37
+
38
+
## Include the library in your sketch
39
+
40
+
To include the library and model in your sketch, go to **Sketch** > **Include Library** and select the newly installed Edge Impulse library from the list.
41
+
42
+
{{< notice Note >}}
43
+
The header file should be named something like `Name_of_your_library_inferencing.h`
44
+
{{< /notice >}}
35
45
36
-
In the example repository, you will find a code snippet `Code_Sample.ino`, which is used as a sketch for the project. Before running the code, it’s important to understand what each part does.
46
+
This will add the necessary include statement at the top of your sketch, allowing you to use the functions and classes provided by the Edge Impulse library.
47
+
48
+
## Code walk-through
49
+
50
+
In the example repository, you will find a code snippet named `Code_Sample.ino`, which is used as a sketch for this project. Before running the code, it’s important to understand what each part does.
37
51
38
52
Take a few minutes to read through the comments and logic in the sketch before uploading it to your board. The code is available in the example repository, and below is a walk-through of the steps.
39
53
40
-
## Include Necessary Libraries and Define Data Structure for Inference
54
+
## Include the library in your sketch
55
+
56
+
To include the library and model in your sketch, go to **Sketch** > **Include Library** and select the newly installed Edge Impulse library from the list.
57
+
58
+
{{< notice Note >}}
59
+
The header file should be named something like `Name_of_your_library_inferencing.h`
60
+
{{< /notice >}}
61
+
62
+
This will add the necessary include statement at the top of your sketch, allowing you to use the functions and classes provided by the Edge Impulse library.
41
63
42
-
This block sets up the core dependencies for running Edge Impulse inference on audio input. It includes necessary libraries and defines a structure `inference_t` that holds the audio buffer and relevant state needed to manage sampling and inferencing.
64
+
## Code walk-through
65
+
66
+
In the example repository, you will find a code snippet named `Code_Sample.ino`, which is used as a sketch for this project. Before running the code, it’s important to understand what each part does.
67
+
68
+
Take a few minutes to read through the comments and logic in the sketch before uploading it to your board. The code is available in the example repository, and below is a walk-through of the steps.
69
+
70
+
## Include libraries and define inference data structure
71
+
72
+
This block sets up the core dependencies for running Edge Impulse inference on audio input. It includes the necessary libraries and defines a structure `inference_t` that holds the audio buffer and relevant state needed to manage sampling and inferencing.
43
73
44
74
```c
45
75
#include<Name_of_your_library_inferencing.h>// Include the Edge Impulse inference SDK for running the model
46
-
#include<PDM.h>// Include the Pulse Density Modulation (PDM) library for audio input
76
+
#include<PDM.h>// Include the Pulse Density Modulation (PDM) library for audio input
47
77
48
78
// Define a structure to store inference-related audio data
49
79
typedefstruct {
@@ -54,16 +84,16 @@ typedef struct {
54
84
} inference_t;
55
85
```
56
86
57
-
## Code to Define Global Variables for Inference and Sample Buffer
87
+
## Define global variables for inference and sample buffer
58
88
59
-
Declares and initializes global variables used for storing inference data, raw audio samples, debug mode flag, and a recording readiness flag. These are essential for coordinating the data collection and inference process.
89
+
This block declares and initializes global variables used to store inference data, raw audio samples, a debug flag, and a recording readiness flag. These variables coordinate the data collection and inference process.
60
90
61
91
```c
62
92
staticinference_t inference; // Global instance of the inference structure to manage audio data
63
93
staticsignedshort sampleBuffer[2048]; // Buffer to temporarily store raw audio samples (16-bit signed)
64
94
staticbool debug_nn = false; // Flag to enable/disable detailed neural network debug output
65
95
staticvolatilebool record_ready = false; // Flag indicating when the system is ready to start recording (volatile due to use in ISR)
66
-
```
96
+
67
97
68
98
## Setup Function for Initializing the Serial and Microphone
69
99
@@ -98,9 +128,10 @@ void setup() {
98
128
The macro `#define LED_PIN 2` specifies the pin number to which the LED is connected. You can change this value to any available digital pin on your board.
99
129
{{% /notice %}}
100
130
101
-
## Main Loop to Handle Inference and Control LED
131
+
## Main loop to handle inference and control LED
132
+
133
+
The `loop()` function runs continuously after `setup()`. It performs the inference process, including recording audio, running the classifier, and controlling an LED based on the model's prediction.
102
134
103
-
Defines the `loop()` function, which runs continuously after `setup()`. It performs the inference process, including recording audio, running the classifier, and controlling an LED based on the inference result.
104
135
105
136
```c
106
137
void loop() {
@@ -152,9 +183,9 @@ In this example, the LED will turn on if the model predicts the "on" label with
152
183
You are free to modify these threshold values to better suit your application or improve model response.
153
184
{{% /notice %}}
154
185
155
-
## PDM Data Ready Callback for Buffer Management
186
+
## PDM data ready callback for buffer management
156
187
157
-
Defines the `pdm_data_ready_inference_callback()` function, which is triggered when the Pulse Density Modulation (PDM) buffer is full. It reads available audio data and stores it in the inference buffer for processing.
188
+
The `pdm_data_ready_inference_callback()` functionis triggered when the Pulse Density Modulation (PDM) buffer is full. It reads the available audio data and stores it in the inference buffer for processing.
Defines the `microphone_inference_record()` function, which waits for the microphone buffer to be filled with audio samples. Once the buffer is ready, it resets the buffer state and prepares for the next recording cycle.
Defines two functions: `microphone_audio_signal_get_data()`, which converts raw audio data from the buffer into a float format, and `microphone_inference_end()`, which cleans up by stopping the PDM and freeing memory allocated for the audio buffer.
Defines the `print_inference_result()` function, which outputs the results of the inference, including the timing of different stages — **DSP**, classification, anomaly detection — and the prediction values for each class.
The `ei_printf` command is a custom logging function from the Edge Impulse SDK, used for printing debug or inference-related information to the serial monitor, optimized for embedded systems. It works similarly to `printf` but is tailored for the Edge Impulse environment. You can find the complete `Code_Sample.ino` in the example repository and try it out yourself.
281
312
{{% /notice %}}
282
313
283
-
# Run Your Code
314
+
## Run your code
284
315
285
316
Now that you have a good understanding of the code, you should run it on your device. With your **Arduino Nano RP2040** plugged into your computer, and the correct [board and port](/learning-paths/embedded-and-microcontrollers/egde/connect-and-set-up-arduino/) selected in the Arduino IDE, follow these steps:
286
317
287
-
#### If you're using the **Upload Button**
318
+
If you're using the **Upload Button**:
288
319
289
-
1. Click the **right-facing arrow** at the top-left of the Arduino IDE window.
290
-
2. The IDE will compile your code and upload it to your board.
291
-
3. Wait for the message **“Done uploading.”** to appear at the bottom of the IDE.
320
+
- Click the **right-facing arrow** at the top-left of the Arduino IDE window.
321
+
- The IDE will compile your code and upload it to your board.
322
+
- Wait for the message **“Done uploading.”** to appear at the bottom of the IDE.
292
323
293
-
#### If you're using the **Sketch Menu**
324
+
If you're using the **Sketch Menu**:
294
325
295
-
1. Go to **Sketch** > **Upload**.
296
-
2. The IDE will compile and upload your sketch to the board.
297
-
3. Once the upload is complete, you’ll see **“Done uploading.”** at the bottom.
326
+
- Go to **Sketch** > **Upload**.
327
+
- The IDE will compile and upload your sketch to the board.
328
+
- Once the upload is complete, you’ll see **“Done uploading.”** at the bottom.
298
329
299
330
Your board should now start running the uploaded code automatically.
300
331
301
-
### Verify Your Code is Running
332
+
## Verify your code is running
302
333
303
-
To further confirm that your code is running properly:
334
+
To confirm that your code is running properly:
304
335
305
-
1. Go to **Tools** > **Serial Monitor** in the Arduino IDE.
306
-
2. Set the baud rate to **115200** (if it's not already).
307
-
3. Observe the output messages:
308
-
- Start and end of recording
309
-
- Inference process
310
-
- Predictions for each label
336
+
- Go to **Tools** > **Serial Monitor** in the Arduino IDE
337
+
- Set the baud rate to **115200** (if it's not already)
338
+
- Observe the output messages:
339
+
- Start and end of recording
340
+
- Inference process
341
+
- Predictions for each label
311
342
312
343
These messages indicate that your model is working and processing voice input as expected.
313
344
314
-
### Recording Your Voice to Toggle the LED
315
-
316
-
1. Wait for the **"Recording..."** message to appear on the Serial Monitor. This indicates that the system is ready to record your voice input.
317
-
318
-
2. Speak your command (e.g., "on" or "off") quickly, as the system only records for a brief 1-second window. Once that window closes, inference will take place, and the system will process the voice command.
345
+
## Record your voice to toggle the LED
319
346
320
-
3. The system will make a prediction based on the input, toggling the LED accordingly.
347
+
- Wait for the **Recording...** message to appear on the Serial Monitor. This means the system is ready to capture voice input.
348
+
- Say your command (for example, "on" or "off") clearly and promptly. The system records for only about one second.
349
+
- The model will make a prediction based on your voice input and toggle the LED accordingly.
350
+
- You can adjust the **threshold** values in the code to control how confident the prediction must be before the LED toggles. This helps fine-tune the system’s responsiveness.
351
+
- If the LED turns on when you say "on" and turns off when you say "off", your system is working correctly.
321
352
322
-
4. You can adjust the **threshold** for prediction accuracy in the code to fine-tune when the LED should toggle, based on the prediction confidence level. This helps control how sensitive the system is to voice commands.
323
353
324
-
### Serial Monitor Output
354
+
## Serial Monitor output
325
355
326
-
Your Serial Monitor should look like the image below.
356
+
Your Serial Monitor should look like the image below:
Congratulations, you’ve successfully programmed your first TinyML microcontroller! You've also built a functional, smart system to control an LED with your voice.
0 commit comments