# ML-Based Classifier Example

This guide is about the advanced usage example of the **SEK-SGP4x** where it can be used for machine learning-based smell detection. 
Please refer to the associated application note **GAS_AN_SGP43**.

## Get started
This example is using 2 different scripts:
- An Arduino sketch to run on your microcontroller
- A python script to run on your computer

The Arduino sketch executes the temperature ramps and returns the resulting measurement using serial to the computer.
From there, the python script can simply log the measurements to a file or run the example model prediction.


### Wiring
Make sure the SGP43 sensor is connected to your Arduino board according to the [library instructions](../../README.md#connect-the-sensor)

### Arduino script
0. Ensure Arduino IDE is installed, as well as the `Sensirion I2C SGP43` library and its dependencies. Please refer to the [library installation instructions](../../README.md#installation-of-the-library) if needed.

1. Open the `exampleSGP43Classifier` sample project within the Arduino IDE:

   `File` ➔ `Examples` ➔ `Sensirion I2C SGP43` ➔ `exampleSGP43Classifier`

2. In the top bar of the Arduino IDE, select the board matching the model you are using and the correct port.

3. Click the `Upload` button in the Arduino IDE or `Sketch` ➔ `Upload`

4. When the upload process has finished, open the `Serial Monitor` via the `Tools` menu to observe the measurement values. Note that the baud rate in the used tool has to be set to `115200 baud`.

If you see measurement values printed to the serial monitor, then this section is complete.
You are ready to collect the incoming data with the provided python script (see next section).

### Python script
You can now use the python script (located in the `python_sgp43_classifier` folder) to log and classify the measured values.

This script converts the serial output from the Arduino measurement into an EDF file which can be read by Sensirion's DataViewer. DataViewer is included when installing [ControlCenter](https://sensirion.com/products/sensor-evaluation/control-center).

In this step by step guide, we will use the provided model that can detect the presence of banana or coffee beans scent.

> [!IMPORTANT]
> Please make sure you close the Serial Monitor before running the classifier, otherwise it will block the serial communication.



#### Requirements
> [!TIP]
> To run the python scripts, you need Python 3.11 or higher.


To install the dependencies you can either use `uv` (recommended) or `pip`.

Using `uv` you can simply run while being located in the `python_sgp43_classifier` folder:
```
uv sync
```

Using `pip` you can run while being located in the `python_sgp43_classifier` folder:
```
pip install -r requirements.txt
```


#### Log measurements to a .edf file
To log the measurements to a file (e.g. for later processing) use the command `measure`.

```
python main.py measure --port <port>
```
where `<port>` should be replaced by the port where the board is connected.


A new EDF file will be created as soon as a new loop is detected.  
Each time a new loop is started, the previous one is
written directly to the file.

> [!TIP]
> Make sure the board is plugged in before running the script.

#### Live predictions using example model
To get live predictions from the provided model we can use the command `measure-and-predict`.

```
python main.py measure-and-predict --port <port>
```
where `<port>` should be replaced by the port where the board is connected.

The predicted category is printed to the console together with the output values of all three predictors.
If the script cannot confidently determine a category, it outputs ???. This typically occurs immediately after powering
up the sensor (during the initial blackout period) or when no category can be selected with a sufficient margin.
The latter case also occurs during transitions between categories (for example, when moving the sensor from ambient air
into a jar filled with coffee beans).

A predictor value close to +1 indicates a strong match with the trained category, whereas a value close to -1 indicates
no match. Therefore, if one of the three predictors shows a clearly positive value while the other two show negative
values, it can be reliably assumed that the category associated with the positive predictor has been detected.

> [!TIP]
> Make sure the board is plugged in before running the script.


## Advanced  topics

### Add New Temperature Profile

1. Copy the `default_tprofile.h` and rename it.
2. Edit the array.
3. In exampleSGP43Classifer.ino, change `#include "default_tprofile.h"` to include your newly created profile.

### Manual installation of the Arduino library
To manually install the library (without using the library manager) you can start by downloading the [latest release from github](https://github.com/Sensirion/arduino-i2c-sgp43/releases) as a `.zip` archive.

Once the downloaded, it can be added in Arduino IDE using:  
`Sketch` ➔ `Include Library` ➔ `Include .ZIP library...`

>[!TIP] 
> The manual installation will not prompt for dependency libraries. It is up to you to make sure all [dependencies](../../README.md#dependencies) are installed