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 adul
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.
Artificial Intelligence and Machine Learning
In the first lesson we only mentioned the definition of “Artificial Intelligence” now I would like to go further and try to explain, in my own way and in the simplest possible way, what the implementation of an artificial intelligence in a machine consists of. Obviously I am aware that this is a topic that requires theoretical bases at university level, so, to the most experienced, allow me the simplifications that I will make to try to make the topic understandable even for the youngest and for those who wish to face this wonderful world.
Until the last twenty years, Artificial Intelligence was meant to create a huge database with all the behaviors that a computer had to have. Each behavior was associated with conditions that had to occur. For example: if it’s sunny outside, the computer has to smile, etc. so the computer did nothing but check the various conditions and depending on them determined the behavior to be kept … these huge “behavioral” databases were called “Expert Systems” (which I remember, IBM was one of the first to implement one of these expert systems… the famous Deep Blue that beat the well-known champion Kasparov in chess thanks to the memorization of millions of combinations of moves deriving from experiences of other games played really and previously memorized).
Over time we saw the limits of this way of programming machines so that they could establish a certain behavior…. First of all, hardware limitations… the enormous memory spaces to contain all the possible behavioral combinations to be predicted and also the need to have available “multiple computers” working in parallel for the computing power necessary to process the comparisons of all the conditions for decision-making operations. But with the advent of Neural Networks things change… in fact, thanks to them, new analysis algorithms were born and therefore we moved from Expert Systems to “Machine Learning” and with this there is the turning point!
An example of this is the famous Alpha Zero computer that unlike Deep Blue did not receive any pre-memorization of games played, but basically learned to play chess on its own, making millions of games against itself and knowing only the rules of the game of chess.
Already from what has been anticipated, you understand, however, that we will have to deal with a programming methodology different from the usual one.
In fact, a program for a computer (also called software) made with the classic method is based on defining the processing algorithms by the programmer and transforming them into program code, after which the computer. based on the inputs that are entered into it. it will define corresponding outputs based on the implemented algorithms.
In Machine Learning the program actually self-generates and it is the machine itself that realizes it, in fact it works like this: the programmer establishes a model, the model is taught to understand what is the correct output compared to the incorrect one (also indicating the error of deviation from the correct output), after which the machine continues to learn by itself reducing more and more the error it commits compared to the expected output.
Hoping I haven’t sent you anything into a tailspin… I need more of your attention, so let’s make one last effort to concentrate.
Let’s see in more detail what has just been explained by introducing new terms used in the world of Machine Learning.
First of all I would like to explain that what will be covered in the next pages is applicable to the so-called microcontrollers, this will mean that we will not need super-powerful computers, but our Arduino Nano 33 BLE Sense Microcontroller, our PC and a simple connection to the Internet will suffice. For this reason this science is not called Machine Learning, but rather “Tiny Machine Learning” (in abbreviated form “tinyML” – tiny stands for “tiny”).
The thing I want you to understand is that we are talking about a young technology, although Machine Learning has been talked about for years, but its field of application, until a few years ago, was relegated to computers with high computing power. The TinyML instead concerns all those small applications (even for the so-called, wearable devices).
In fact, the rule to classify what is “Tiny” is that of the “1”, that is, devices that have processors that cost about $ 1, which have a maximum of 1 Mb of RAM, which have a maximum clock speed of 1 Ghz, that the neural network has a maximum consumption of about 1 mW and that must be able to be powered by button batteries with a duration of at least 1 year.
It is estimated that in the near future the tinyML will occupy a significant slice of the market and the most receptive sectors will be: the industry for the realization of vehicles in general (cars, trains, planes, etc ..) , industrial automation and that of household appliances including home automation. We are not yet noticing, but a small revolution is already underway…
Creating an Artificial Intelligence with Deep Learning
The process of creating an Artificial Intelligence consists of very specific phases:
Phase 1 – choice of the “neural network” model (or, simplifying, choice of the learning algorithm) that will allow the Microcontroller to create a database of experiences deriving from training, depending on the objective to be achieved.
Phase 2 – creation of the database of experiences, this phase is called “training” and is distinguished in two moments, an initial one where the initial database is practically made “at the table” by the programmer. This database introduces the concepts of “correct result” and “incorrect result”. The second step is the enrichment of the database of experiences (with correct and incorrect results) during the self-learning phase. That is, the model is provided with the inputs and then the result is validated with the indication of the deviation (accuracy) of the output from the expected one.
Phase 3 – at the end of the training there is the implementation of the “trained model” according to the chosen microcontroller. In this phase, the program code is optimized and resized according to the memory capacities of the microcontroller.
Step 4 – loading the model trained on the microcontroller which in our case is Arduino Nano to generate the “inference“. that is, the decision-making process that leads to the output resulting from inputs based on what the trained model has processed. So inference can be defined as that process of reasoning built on the basis of learning.
Once the ML program is loaded on the microcontroller, it is ready to carry out its activity and will give an output depending on the inputs that will be sent to it.
The phases described above are a synthesis of several sub-activities that for simplicity I have not reported, but that we will see later, now the thing I wanted to draw your attention to is that the process described above, the one indicated with Phases 1 to 4, is called “Deep Learning“.
Let’s try to graphically represent the above:
As for the whole process from the choice of the Model, up to the creation of the AI program, with the pre-trained model, to be loaded on the microcontroller (ie the contents of the box of light green color), on the Internet there are several platforms that help us to achieve it. The one we are going to refer to, because it is open source, it is totally free and the support is given by a large community is TensorFlow, but there are also many other platforms (which I personally do not know) available, including Edge Impulse, Neuton , etc.. that provide free resources of which I still insert some links if you want to view them.
Before going into new chapters of theory to learn how to make an Artificial Intelligence of the TinyML type, let’s familiarize ourselves with this new way of operating. To do this we will use Models already pre-trained and available on TensorFlow Lite.
It should be noted that TensorFlow Lite is the version of TensorFlow for TinyML applications or for Machine Learning applied to Microcontrollers.
Installation of the TensorFlow Lite Library
To be able to dive into the world of TinyML you need to load the TensorFlow Lite Library in our development environment for Arduino. To do this you need to go to Tools -> Library Manager… and type “arduino_tensorflow lite” and click on install.
For all the projects that follow we only need the Nano BLE Sense card, a PC and the USB connection cable.
PAI-014: Hello World - TinyML
This project represents the entry into the world of TinyML, for this reason as in other areas, the first project, the “simplest” one, is always called Hello World.
Now, for us, although this is the simplest project, opening the code that follows, we will not understand each other much, but it does nothing. The goal is not to understand how the code was made (because we will see it later), but to understand what Arduino Nano is doing.
The goal of the Hello World project is to have Arduino Nano build a function already known to verify the correct learning. The function he must learn to build is that of the sine, that is, a function that derives from trigonometry.
The sine function, denoted by sin(x) or sen(x), is a trigonometric, periodic, and bounded function. This function is obtained from the rule that in a right triangle, of hypotenuse 1 (i.e. the radius of the circle)1, the cathet relative to the height of the right triangle is the sine of the angle between the hypotenuse and the axis of the abscissa (i.e. the opposite angle). If we rotated the hypotenuse, we would describe a circle of radius R = 1 and the height of the triangle would be the sine of the opposite angle. It is easy to understand that at 0 ° or rather at 0 radians, the sine is equal to 0, to 90 °, that is to PiGreco / 2, the sine is equal to the radius R = 1, then to 180 °, the sine returns to be 0 and to 270 °, that is to 3/2 of PiGreco, the sine becomes -1. The trend of the sine function is therefore the one shown below.
Now imagine saying to the Dwarf, build the sine of the angle x, without giving it the function, but give only a limited series of pairs of numbers that represent the ordinate and the abscissa of the function and that the Nano itself will have to build the resulting function. Of course we will also have to tell him when he is wrong and in what terms the mistake is. Well! The result of this model can be seen in this project.
Once the library is installed, go to :
Files -> Sample -> Arduino_TensorFlow Lite -> Hello Word
At this point proceed as usual, connect the Nano via USB to the PC and double-click the button to upload the sample program.
Once you have loaded the code on the Nano and changed the COM port, activate the Serial Plotter and you will see that the Nano draws a curve very close to the sine function, here are two screenshots:
I want to make you observe how the curve presents “teeth”,the reason is that the model obviously makes approximations and therefore there is still an error, the error will be smaller, how much better the trained model will be.
Obviously, the downside is that a better trained model means heavy code in terms of memory demand, what the microcontroller does not have and therefore the right compromise is always needed.