TINY MACHINE LEARNING LESSON 5
- Copyright Notice
- MP34DT05 Omnidirectional Digital Microphone
- The Capacitor
- The RGB LED on the Arduino Nano 33 BLE Sense
- PAI-010: The RGB LED on board
- Sketch analysis of Project AI-010 – The RGB LED on board
- PAI-011: Sound Analyzer with Microphone MP34DT05
- PAI-012: RGB LED driven by sound analysis
- Sketch analysis of Project AI-012 – RGB LED driven by sound analysis
- The BLE module in Arduino Nano 33 BLE Sense
- PAI-013: Nano RGB LED switching on via Iphone
- Sketch analysis of Project AI-013 – Nano RGB LED switching on via Iphone
With regard to the safety aspects, since the projects are based on a very low voltage power supply supplied by the USB port of the PC or by support batteries or power supplies with a maximum of 9V output, there are no particular risks of an electrical nature. It is however necessary to specify that any short circuits caused during the exercise phase could produce damage to the PC, to the furnishings and in extreme cases even to burns, for this reason every time a circuit is assembled, or changes are made on it, it will be necessary to do so in the absence of power and at the end of the exercise it will be necessary to provide for the disconnection of the circuit by removing both the USB cable connecting to the PC and any batteries from the appropriate compartments or external power connectors. In addition, always for safety reasons, it is strongly recommended to carry out projects on insulating and heat-resistant carpets that can be purchased in any electronics store or even on specialized websites.
At the end of the exercises it is advisable to wash your hands, as the electronic components could have processing residues that could cause damage if ingested or if in contact with eyes, mouth, skin, etc. Although the individual projects have been tested and safe, those who decide to follow what is reported in this document, assume full responsibility for what could happen in the execution of the exercises provided for in the same. For younger children and / or the first experiences in the field of Electronics, it is advisable to perform the exercises with the help and in the presence of an adult.
Note sul Copyright
All trademarks are the property of their respective owners; third-party trademarks, product names, trade names, corporate names and companies mentioned may be trademarks owned by their respective owners or registered trademarks of other companies and have been used for purely explanatory purposes and for the benefit of the owner, without any purpose of violation of the copyright rights in force. What is reported in this document is the property of Roberto Francavilla, Italian and European laws on copyright are applicable to it – any texts taken from other sources are also protected by the Copyright and property of the respective Owners. All the information and contents (texts, graphics and images, etc.) reported are, to the best of my knowledge, in the public domain. If, unintentionally, material subject to copyright or in violation of the law has been published, please notify firstname.lastname@example.org by email and I will promptly remove it.
MP34DT05 Omnidirectional Digital Microphone
The MP34DT05 microphone on the Arduino Nano 33 BLE Sense board allows you to capture and analyze sound even in real time and can be used to create a voice interface for our TinyML designs.
This microphone is defined as “omnidirectional” because it is capable of capturing sound from any direction.
The sensitive element of the microphone is capacitive. To better understand its operation it is appropriate to clarify what “capacity” means in electrical terms and to do this it is appropriate to know the “capacitor”.
The capacitor, in electrical engineering, is a passive bipole composed of two metal sheets between which an insulating material (dielectric) is interposed which, subjected to a continuous electrical voltage, after a first transient phase (called charging), no longer allows current to pass through and is capable of accumulating electric charges.
The “Capacity” is therefore the amount of electric charges that the capacitor manages to accumulate in its initial charging phase. It depends, in addition to the surface of the two facing sheets (the sheets can also be wound “roller” type, provided that there is no metal contact between them, but the dielectric material is always interposed ), also on the type of dielectric material used and also on the distance between the two sheets.
The interesting thing about this electrical component, as mentioned earlier, is that the ability to accumulate electric charges also depends on the distance of the two sheets and if this distance varies, the charges present in the capacitor also vary.
So, imagine a capacitor powered by a direct voltage and one of the two sheets turns out to be mobile (for example a membrane that moves according to the sound waves that hit it), it is easy to imagine that there is a release of electric charges when the two sheets approach and an absorption (recharge) of electric charges when they move away.
In this way, we have created an electric transducer that transforms what are the sound waves, into electrical signals that can then be used for different purposes.
In the next paragraphs we will see two projects that show us the use of the microphone on the Nano 33 BLE Sense, one of these projects will also make use of the RGB LED on the same board and for this reason, before moving on to the projects, let’s see how to activate the RGB LED on board.
The RGB LED on the Arduino Nano 33 BLE Sense
The Nano 33 BLE Sense board has a built-in RGB LED that is connected to pins 22, 23 and 24, this LED can be used to our liking depending on the projects we want to realize.
It is a common anode LED, so to activate it you have to bring the PIN to a low state that is “LOW”, to turn it off (obviously) to a high state “HIGH”.
But let’s see immediately with a dedicated project, the use of this RGB LED.
For all the projects that follow we only need the Nano BLE Sense card, a PC and the USB connection cable.
PAI-010: The RGB LED on board
The sketch to use for this project is the one shown below:
Once the sketch is loaded, as described above and the port to which the board is connected has been changed, you will be able to observe the RGB LED in operation.
In particular, every second a different color LED lights up in rotation.
However, the video of the project shows everything in detail.
Sketch analysis of Project AI-010 - The RGB LED on board
There is nothing to highlight for this sketch except to point out the PINs to which the RGB LED is connected which are 22, 23 and 24 and with the instruction #define:
#define RED 22
#define GREEN 23
#define BLUE 24
The PIN number is associated with the name of the respective variables, namely: RED, GREEN and BLUE.
PAI-011: Sound Analyzer with Microphone MP34DT05
This project reads audio data from the microphone and prints it via the IDE’s serial plotter.
In this project we will use, without any modification, the example made available by the PDM library (Pulse Density Modulation) which for convenience is also shown below.
Once the sketch is loaded and the port to which the Nano BLE Sense is connected has been changed, open the Serial Monitor and the output will be a series of numbers. Opening the Serial Plotter instead will show you in graphic form the readings of the microphone.
The output shown is very important, because it will serve in all those projects where speech recognition will be needed.
However, the video of the project shows everything in detail.
PAI-012: RGB LED driven by sound analysis
This project, unlike the first, uses the audio data captured by the microphone and transforms them into signals to activate / turn off the RGB LED on board the board.
Also in this project we will use, the PDM library (Pulse Density Modulation) to transform the analog signal coming from the MP34DT05 microphone into states (HIGH or LOW) for PIN 22, 23 and 24.
The sketch to use is as follows:
Once the sketch has been loaded and the port to which the Nano BLE Sense is connected has been changed, open the Serial Plotter and you will be shown in graphic form the readings of the microphone, at the same time the RGB LED will vary the color according to these values.
However, the video of the project shows everything in detail.
Sketch analysis of Project AI-012 – RGB LED driven by sound analysis
As you can see from the sketch, the reference library is the PDM.h, there are instructions that can change the configuration parameters:
PDM.setGain(30); // come settare il guadagno, di default è 20
PDM.begin (1, 16000) //come settare nel caso di mono mode e campionamento a 16kHz
The BLE module in Arduino Nano 33 BLE Sense
First of all, BLE stands for Bluetooth® Low Energy, that is, it is a type of short-range wireless communication, different, in terms of functionality, from classic Bluetooth® (and therefore not compatible), created to allow communication between small electronic devices with low energy consumption. This type of communication was therefore born specifically as an alternative to classic Bluetooth® for reasons of energy saving.
Devices that have Bluetooth® 4.0 or higher, can communicate both via classic Bluetooth® and in BLE.
Bluetooth® communication is a vast world and to explain it appropriately would require a specific course (maybe I will do it later 🙂 ) so I will limit myself to giving only intuitive indications on how to work and how to use it in the projects of our interest, leaving to you the insights, if necessary, which can be easily found on the internet.
First of all, the devices connected via BLE form a network called “piconet” and they can perform functions of Central Unit (called Master or even Server) and Peripheral Units (called Slave or even Client). What defines whether a device is behaving as a Server or a Client is who of the two devices makes their Services available.
Within the Services there are the Characteristics of the Service, an example of services and characteristics is the detection of the Temperature and Humidity that the HTS221 sensor of the Nano BLE Sense can return as information, but also the detection of gestures, or colors or proximity through the APDS9960 sensor. In this case the availability of the sensor is called service and the measurement of temperature and humidity are characteristic
Quindi, in this case, the board makes available its services, as Servers, and those who request them, such services, is called Client.
Each service, but also the features, has a unique identification code called UUID. This code, for official Bluetooth-specific® services , can be 16-bit long or 32-bit, while unofficial Bluetooth® services (i.e. those that we can develop arbitrarily) are 128 bits long. UUIDs can be created, in this case, randomly. A Prophile is a group of services that contains multiple features.
The type of BLE communication between two devices is based on three types of actions: Read, Write and Notification.
This communication is activated only on specific events and is deactivated immediately after finishing the action (this is the main reason why there is a strong energy saving).
Let’s take an example to better understand what it means to Read, Write and Notify taking as a reference a piconet formed by a smartphone and by our Arduino Nano 33
We represent the three actions with images:
The smartphone asks the Nano to receive the temperature reading that the sensor on board the board is detecting at that time. The Nano is activated and transmits the required information.
The smartphone asks the Nano to place one of its digital PINs in LOW status. The Nano is activated and places the digital PIN in a LOW state.
The smartphone sends information to the Nano, for example, about its battery status.
To better understand BLE communication, we carry out the following project, but first you need to download the appropriate library in the Arduino IDE.
Open the Arduino IDE, click on Library Management Tools…, the Library Manager window appears and type in the search bar “ArduinoBLE“:
Proceed with the installation of the Library by clicking on “Istalla”.
As for our smartphone, it is necessary to install the LightBlue application to access the service and the expected features.
You can download the Iphone App at: LightBlue for Iphone
You can download the Android App at: LightBlue per Android
PAI-013: Nano RGB LED switching on via Iphone
Once you open the Arduino IDE, type the following sketch:
Once the sketch is loaded and the communication port between the PC and the board has been changed, moving from the writing to the reading port, you can open the serial monitor, at this point the board is visible to all devices with the BLE active. By scanning the devices on the phone app you can connect the Nano to your smartphone. Through the App at this point it is possible to give instructions to the Nano. More details are shown in the video-project.
Sketch analysis of Project AI-013 – Nano RGB LED switching on via Iphone
After including the library “ArduinoBLE.h” in the sketch, in the service function “ledService()” the argument to be inserted is “180A” which indicates “Device information” [this is an official UUID], while in the function “switchCharacteristic()” the argument to be inserted is ” 2A57″ which indicates “Digital Output”.
In the function “BLE.setLocalName()” we must change the name to “Nano 33 BLE Sense” to identify the card we are using.
The rest of the sketch are instructions and known functions so I invite you to go and read the Arduino Basic Course.