ATtiny Machine Learning

We have seen that we can do machine learning in other microprocessors, you can see our article on this topic here. Now we're reducing the dimensions a little bit and machine learning in the ATtiny85 microprocessor!

attiny85
ATtiny85

Given the large number of ready-to-use microcontrollers, microML has already opened the way to embedded machine learning on a new scale. But this was not enough for us: after all, the MicroML builder exports flat C, which should work not only on Arduino cards, but on any embedded system.

That's why we installed attiny85 to test if I can make our microprocessor smaller and run on small chip #1.

We have already installed the ATtiny85 libraries and compiler in the Arduino IDE, you can follow this link for re-installation. After installing this installation, we cannot do any machine learning application directly with ATtiny85. That's why we have to take a different path. This content is actually an ATtiny85-adapted version of our color detection project with Arduino Nano, so we recommend that you read that content first.

Description of Properties

We will use the RGB components of a color sensor (TCS3200) to understand which object we are pointing to.This means that our features will be 3D, which leads to a really simple model with very high accuracy.

Reminder

Attiny85 has 8 Kb flash and 512 bytes of RAM, so you can't install any model that uses more than a few features (probably less than 10).

Saving Sample Data

You should do this step on a development card or microprocessor with a serial interface, such as Arduino Uno/Nano/Pro Mini.See this tutorial for the code for this step.

Training and Exporting the SVM Classifier

This section is exactly the same as the original except for a single parameter: you will pass platform=attiny to port function.

from sklearn.svm import SVC
from micromlgen import port

# place your samples in the dataset folder# one class per file# One propertyvector per line in CSV format

features, classmap = load_features('dataset/')
X, y = features[:, :-1], features[:, -1]
classifier = SVC(kernel='linear').fit(X, y)
c_code = port(classifier, classmap=classmap, platform='attiny')
print(c_code)


ATtiny codes were applied in version 0.8 of micromlgen: if you installed an earlier version, update first.

At this point, you must copy the printed code and export it to a file named model.h in your project.

Running Inference

Since there is no serial connection, we will flash an LED several times depending on the prediction result.

Put a colored object in front of the sensor and see the LED flashing.

The ATtiny program requires 3434 bytes (41%) program space and 21 bytes (4%) RAM. This means that you can run machine learning in fewer areas than Attiny85 provides.

Especially this model is so small that you can even work in an Attiny45 with only 4 Kb flash and 256 bytes of RAM.

Look at the RAM number for a moment: 21 bytes. 21 bytes is all the memory you need to run a Machine learning algorithm in a microcontroller. This is the result of the application that we have chosen: the minimum possible RAM load.