Skip to content

Commit 5228e89

Browse files
Merge pull request #535 from arduino/benjamindannegard/display-shield-microphone-update
[Display Shield] microphone update
2 parents c1fa899 + 0efe18a commit 5228e89

File tree

1 file changed

+55
-58
lines changed
  • content/hardware/10.mega/shields/giga-display-shield/tutorials/07.microphone-tutorial

1 file changed

+55
-58
lines changed

content/hardware/10.mega/shields/giga-display-shield/tutorials/07.microphone-tutorial/content.md

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,105 +5,99 @@ author: Benjamin Dannegård
55
tags: [Display, microphone, LVGL]
66
---
77

8-
The GIGA Display Shield comes equipped with on-board microphone and with the help of LVGL the microphones readings can be visualized on the screen. This tutorial will show you how to create a sketch that ties together the microphones readings with visual elements on the screen.
8+
9+
The GIGA Display Shield comes equipped with on-board microphone that when combined with the visual element of the GIGA Display Screen can be used in a number of ways. For example, using the LVGL framework the display can be used to show an animated volume indicator. Using the [Arduino_Graphics]() or [Arduino_GigaDisplay_GFX](https://github.com/arduino-libraries/Arduino_GigaDisplay_GFX) libraries we can detect when a loud noise, like a clap is made and change a visual element on the screen. This tutorial will take a closer look at the [PDM library](https://docs.arduino.cc/learn/built-in-libraries/pdm) to see how this can be used. And then re-create the two sketches mentioned.
910

1011
## Hardware & Software Needed
1112

1213
- [Arduino GIGA R1 WiFi](/hardware/giga-r1)
1314
- [Arduino GIGA Display Shield](/hardware/giga-display-shield)
1415
- [Arduino IDE](https://www.arduino.cc/en/software)
15-
- [LVGL library](https://reference.arduino.cc/reference/en/libraries/lvgl/)
1616

1717
## Downloading the Library and Core
1818

19-
Make sure the latest GIGA core is installed in the Arduino IDE. **Tools > Board > Board Manager...**. Here you need to look for the **Arduino Mbed OS Giga Boards** and install it, the [Arduino_H7_Video library](https://github.com/arduino/ArduinoCore-mbed/tree/main/libraries/Arduino_H7_Video) and [PDM library](https://docs.arduino.cc/learn/built-in-libraries/pdm) are included in the core. Now you have to install the library needed for handling the visual component. Go to **Tools > Manage libraries..**, search for **LVGL**, and install it.
19+
Make sure the latest GIGA core is installed in the Arduino IDE. **Tools > Board > Board Manager...**. Here you need to look for the **Arduino Mbed OS Giga Boards** and install it, the [Arduino_H7_Video library](https://github.com/arduino/ArduinoCore-mbed/tree/main/libraries/Arduino_H7_Video) and [PDM library](https://docs.arduino.cc/learn/built-in-libraries/pdm) are included in the core.
2020

2121
## Microphone Readings With The Shield
2222

23-
### Test Microphone
23+
### Testing The Microphone
2424

2525
To test the microphone we can use the **PDMSerialPlotter** example sketch. This sketch can be found in **File > Examples > PDM** in the Arduino IDE. This sketch will print readings in the serial monitor. Upload the sketch and check so that readings are appearing in the serial monitor.
2626

2727
![Example sketch printing values in the serial monitor](assets/pdm-test-sketch.png)
2828

2929
### Using the Microphone Readings
3030

31-
Now let's create a sketch that combines the microphone readings with a visual component. First, the libraries we need are, `PDM.h` which will handle the microphone readings and `lvgl.h` will handle the visual elements on the screen.
31+
Now let's take a look at the PDM sketch and how we can use the microphone readings.
3232

33-
```arduino
34-
#include <PDM.h>
35-
#include "Arduino_H7_Video.h"
36-
#include "lvgl.h"
37-
```
38-
39-
In the `setup()` we can start the display with `Display.begin();` and the microphone with `PDM.onReceive(onPDMdata);`. This will call the `void onPDMdata()` function at the bottom of the sketch. This function handles collecting and storing the readings from the microphone.
33+
First we need to define the number of output channels, output frequency, a variable for counting when reading from the buffer and creating the buffer which the readings will be put into. This is done with the following lines:
4034

4135
```arduino
42-
void setup() {
43-
Display.begin();
36+
// default number of output channels
37+
static const char channels = 1;
4438
45-
PDM.onReceive(onPDMdata);
39+
// default PCM output frequency
40+
static const int frequency = 16000;
4641
47-
if (!PDM.begin(channels, frequency)) {
48-
Serial.println("Failed to start PDM!");
49-
while (1);
50-
}
51-
}
42+
// Buffer to read samples into, each sample is 16-bits
43+
short sampleBuffer[512];
44+
45+
// Number of audio samples read
46+
volatile int samplesRead;
5247
```
5348

54-
In the `setup()` we will also create our LVGL bar and animation for that bar. Make sure you also define the `lv_obj_t * obj;` and `lv_anim_t a;` variables outside of the `setup()` function so they can be used in the `loop()` function later. Now `obj` will be the name of bar object. These next lines will set the bar's position, size and values. After that we can initialize the animation and set the time for the animation. Then we set the animation to our bar with `lv_anim_set_var(&a, obj);`.
49+
A callback function needs to be set, which is called when new PDM data is ready to be read. We do this in the `setup()` function using:
5550

5651
```arduino
57-
// Create the bar
58-
obj = lv_bar_create(lv_scr_act());
59-
lv_obj_set_size(obj, 600, 50);
60-
lv_obj_center(obj);
61-
lv_bar_set_value(obj, 500, LV_ANIM_OFF);
62-
63-
// Create the animation for the bar
64-
lv_anim_init(&a);
65-
lv_anim_set_exec_cb(&a, set_slider_val);
66-
lv_anim_set_time(&a, 300);
67-
lv_anim_set_playback_time(&a, 300);
68-
lv_anim_set_var(&a, obj);
52+
PDM.onReceive(onPDMdata);
6953
```
7054

71-
The microphone readings will be put into a buffer, in the `loop()` we will need to go through all the samples in the buffer. We can do this with a simple `for()` loop. If you ran the previous mic test sketch you will know that the readings can get pretty high in value, to make it fit our LVGL component reduce that value. By assigning the value to our `micValue` variable we can make it fit the visual element and easily use it to set the end value of the animation with `lv_anim_set_values(&a, 0, micValue)`.
55+
`onPDMdata` is the callback function that we will have to create at the end of the sketch.
7256

73-
```arduino
74-
void loop() {
75-
76-
// Wait for samples to be read
77-
if (samplesRead) {
57+
Now when we want to print or use the readings lets do it with a `for` loop since they are inside a buffer, which we need to step through. But lets first check so that there are readings to be printed with a simple `if` statement. These lines will step through the buffer until all the readings inside are printed and then start over:
7858

79-
// Print samples to the serial monitor or plotter
59+
```arduino
60+
if (samplesRead) {
8061
for (int i = 0; i < samplesRead; i++) {
8162
Serial.println(sampleBuffer[i]);
82-
micValue = sampleBuffer[i];
83-
micValue = micValue / 100;
84-
if (micValue > 500)
85-
{
86-
micValue = 500;
87-
}
88-
lv_anim_set_values(&a, 0, micValue);
89-
lv_anim_start(&a);
9063
}
64+
}
65+
```
9166

92-
// Clear the read count
93-
samplesRead = 0;
94-
delay(10);
95-
}
96-
lv_timer_handler();
67+
Its inside this `for` loop where we can get readings that will then change visual elements on the screen. This is where you would put any code that needs to react to the microphone readings.
68+
69+
And the last important part is the callback function that we used in the `setup()` function. This will take care of reading the values into the buffer and setting the value of the `samplesRead` variable which we used in the previous step.
70+
71+
72+
```arduino
73+
/**
74+
* Callback function to process the data from the PDM microphone.
75+
* NOTE: This callback is executed as part of an ISR.
76+
* Therefore using `Serial` to print messages inside this function isn't supported.
77+
* */
78+
void onPDMdata() {
79+
// Query the number of available bytes
80+
int bytesAvailable = PDM.available();
81+
82+
// Read into the sample buffer
83+
PDM.read(sampleBuffer, bytesAvailable);
84+
85+
// 16-bit, 2 bytes per sample
86+
samplesRead = bytesAvailable / 2;
9787
}
9888
```
9989

100-
In the section below you can find the full sketch. If you upload it to your GIGA Display Shield, you should see the same result as the GIF below is showing.
90+
### Clap Detection Sketch
10191

102-
[GIF of sketch running]()
92+
This sketch uses the [Arduino_Graphics library](https://www.arduino.cc/reference/en/libraries/arduinographics/) to change the color of the background when a loud noise is detected, such as a clap.
93+
94+
```arduino
10395
104-
### Full Sketch
96+
```
97+
98+
### Volume Indication Sketch
10599

106-
You will find the full sketch in the example below:
100+
This sketch requires the [lvgl library](https://github.com/lvgl/lvgl), please make sure that is installed before you upload the sketch. The sketch will show a bar on the screen that is animated when noise is made, functionally making it display the volume of the microphones readings. For more information about using lvgl with the GIGA Display Shield, take a look at our documentation [here](tutorials/lvgl-guide). You will find the full sketch below:
107101

108102
```arduino
109103
#include <PDM.h>
@@ -198,9 +192,12 @@ void onPDMdata() {
198192
}
199193
```
200194

195+
196+
[GIF of sketch running]()
197+
201198
## Conclusion
202199

203-
Now you know how to get readings from the GIGA Display Shield's on-board microphone and how to create simple visual elements with LVGL. And lastly, how these two components could be put together in a sketch to utilize the shield's screen and on-board components.
200+
Now you know how to get readings from the GIGA Display Shield's on-board microphone and how to create simple visual elements that react to those readings.
204201

205202
## Next Step
206203
Now that you know how to use the on-board microphone, feel free to explore the shield's other features, like the IMU with our [Orientation tutorial](/tutorials/image-orientation). Or if you rather dive deeper into LVGL, take a look at our [LVGL guide](tutorials/lvgl-guide).

0 commit comments

Comments
 (0)