Edge AI at TinyML sa ESP32 — Beginner's Guide sa Offline Motion Recognition



Magandang araw, KonekNoypi Tech Enthusiasts!

Alam mo bang puwedeng magpatakbo ng maliit na Machine Learning model sa ESP32 kahit walang internet connection?

Sa tulong ng TinyML, maaaring makilala ng isang microcontroller ang ilang patterns mula sa sensors, tulad ng movement, vibration, o orientation.

Sa tutorial na ito, pag-aaralan natin ang basic workflow ng Edge AI gamit ang ESP32, MPU6050 motion sensor, at Edge Impulse.

Matututuhan mo:

  • Ano ang Edge AI at TinyML
  • Paano gumagana ang Machine Learning sa microcontrollers
  • Anong hardware ang kailangan
  • Paano mangolekta ng training data
  • Paano mag-train ng classification model
  • Paano i-export ang model sa Arduino IDE
  • Paano basahin ang motion sensor data
  • Paano subukan ang model sa ESP32
  • Paano mag-troubleshoot ng common errors





1. Ano ang Edge AI?



Ang Edge AI ay ang pagpapatakbo ng AI processing malapit sa pinanggagalingan ng data, sa halip na palaging ipadala ito sa remote cloud server.

Halimbawa, sa isang smart monitoring system, maaaring direktang suriin ng ESP32 ang sensor readings at magdesisyon kung may detected movement.

Hindi na kailangang magpadala ng bawat reading sa internet para sa inference.

2. Ano ang TinyML?



Ang Tiny Machine Learning o TinyML ay tumutukoy sa paggamit ng maliliit at optimized Machine Learning models sa hardware na limitado ang memory, processing power, at energy resources.

Mga halimbawa ng TinyML applications:

  • Motion at gesture recognition
  • Vibration anomaly detection
  • Keyword spotting
  • Simple environmental monitoring
  • Basic image classification sa compatible camera-equipped devices


Important: Hindi lahat ng AI models ay puwedeng patakbuhin sa ESP32. Kailangan itong magkasya sa available memory at suportahan ng hardware at inference runtime.




3. Advantages at Limitations ng Edge AI



Advantages:

  • Offline inference pagkatapos ma-deploy ang model
  • Hindi kailangang ipadala ang lahat ng raw sensor data sa cloud
  • Maaaring mas mabilis ang response sa ilang applications
  • Posibleng makatipid sa network bandwidth
  • Puwedeng gamitin sa remote IoT devices


Limitations:

  • Limitado ang RAM at flash memory
  • May inference processing time pa rin
  • Hindi lahat ng models ay compatible
  • Kailangan ng quality training data
  • Maaaring bumaba ang accuracy sa real-world conditions
  • Hindi awtomatikong mas matipid sa kuryente ang lahat ng Edge AI setups





4. Hardware Requirements



Para sa motion recognition example natin:

  • ESP32 development board
  • MPU6050 accelerometer at gyroscope module
  • USB data cable
  • Breadboard at jumper wires
  • Computer na may Arduino IDE
  • Internet connection para sa initial setup at model training
  • Edge Impulse account


Note: Ang internet ay kailangan para sa cloud-based training workflow, pero ang deployed model ay maaaring mag-inference offline.

Hindi kailangan ng ESP32-CAM para sa motion sensor project na ito.




5. Connect MPU6050 to ESP32



Ang MPU6050 ay may accelerometer at gyroscope na puwedeng gamitin sa motion recognition.

Para sa typical ESP32 DevKit board:


MPU6050 VCC  -> ESP32 3V3
MPU6050 GND  -> ESP32 GND
MPU6050 SDA  -> ESP32 GPIO21
MPU6050 SCL  -> ESP32 GPIO22


Warning: I-check ang exact MPU6050 module at ESP32 board pinout bago magkabit. Ang GPIO21 at GPIO22 ay common defaults sa classic ESP32, pero maaaring mag-iba sa ibang ESP32 variants.

Siguraduhing compatible ang module power supply at I2C logic levels.




6. Install Arduino IDE and ESP32 Support



Step 1:
I-download ang Arduino IDE mula sa official Arduino website.

🔒 You need to login or register to view the link

Step 2:
I-install ang ESP32 board support gamit ang Boards Manager.

Step 3:
Piliin ang tamang ESP32 board at serial port.

Step 4:
Sa Library Manager, i-install ang:

Adafruit MPU6050
Adafruit Unified Sensor
Adafruit BusIO

Step 5:
Buksan ang isang bagong Arduino sketch.




7. Test the MPU6050 Sensor



Bago gumawa ng AI model, siguraduhing nababasa muna ng ESP32 ang sensor.

Ang code sa ibaba ay isang standalone sensor test. Hindi pa ito gumagamit ng Machine Learning.


#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>

Adafruit_MPU6050 mpu;

void setup() {
  Serial.begin(115200);
  delay(1000);

  Wire.begin(21, 22);

  if (!mpu.begin()) {
    Serial.println("MPU6050 not detected!");
    while (true) {
      delay(100);
    }
  }

  mpu.setAccelerometerRange(MPU6050_RANGE_2_G);

  Serial.println("MPU6050 ready!");
}

void loop() {
  sensors_event_t a, g, temp;

  mpu.getEvent(&a, &g, &temp);

  Serial.print(a.acceleration.x, 4);
  Serial.print(",");
  Serial.print(a.acceleration.y, 4);
  Serial.print(",");
  Serial.println(a.acceleration.z, 4);

  delay(20);
}


Ano ang ginagawa ng code?

Binabasa nito ang acceleration values sa X, Y, at Z axes.

Ang acceleration values mula sa Adafruit library ay nasa meters per second squared (m/s²).

Makakakita ka ng tatlong comma-separated values sa Serial Monitor.

Halimbawa:


0.1200,-0.3400,9.7600
0.1500,-0.2900,9.8100
0.0900,-0.3700,9.7900


Illustrative values lamang ang sample output.

Important: Ang sample code ay gumagamit ng approximately 50 Hz sampling, pero ang actual timing ay maaaring mag-iba. Para sa accurate ML data collection, gumamit ng consistent sampling rate at i-verify ang timestamps.




8. Create an Edge Impulse Project



Pumunta sa:

🔒 You need to login or register to view the link

Gumawa ng bagong project.

Example project name:

ESP32 Motion Recognition

Para sa tutorial na ito, tatlong motion classes ang gagamitin natin:

  • stationary
  • walking
  • shaking


Ang labels ay tumutukoy sa movement ng sensor o device, hindi sa identity ng tao.




9. Collect Training Data



Kailangan ng representative sensor data para maturuan ang Machine Learning model.

Maaari kang gumamit ng supported Edge Impulse data acquisition workflow o mag-upload ng compatible formatted sensor data.

Suggested procedure:

1. Mangolekta ng stationary samples.
2. Mangolekta ng walking samples habang ligtas na nakakabit o hawak ang sensor.
3. Mangolekta ng shaking samples.
4. Gumawa ng maraming recordings para sa bawat class.
5. Gumamit ng hiwalay na testing data.

Data quality tips:

  • Panatilihing consistent ang sampling frequency.
  • Gumamit ng parehong X, Y, Z sensor units sa training at inference.
  • Iwasan ang mislabeled samples.
  • Mag-record sa iba't ibang realistic conditions.
  • Huwag gumamit ng iisang recording bilang training at testing data.


Important: Dapat tumugma ang sensor axes, sampling frequency, at preprocessing sa model na ide-deploy mo.




10. Build the Machine Learning Impulse



Sa Edge Impulse Studio:

1. Buksan ang Impulse Design.
2. Piliin ang appropriate time-series input.
3. Gumamit ng window size at sampling rate na angkop sa collected data.
4. Magdagdag ng compatible processing block.
5. Magdagdag ng classification learning block.
6. I-save ang impulse.

Halimbawa, maaari kang magsimula sa 1000 ms window at 50 Hz sampling.

Ang 1000 ms window sa 50 Hz ay may approximately 50 samples per axis.

Ang values na ito ay starting point lamang. I-adjust ayon sa movement patterns at model performance.




11. Train and Evaluate the Model



Sa training page:

1. I-check ang class distribution.
2. I-train ang model.
3. Tingnan ang accuracy at confusion matrix.
4. Subukan ang held-out test data.
5. Suriin kung alin sa mga classes ang madalas mapagkamalan.

Important: Hindi sapat ang mataas na training accuracy para sabihing reliable ang model.

Mas mahalagang gumana ito sa bagong sensor recordings na hindi ginamit sa training.




12. Export the Model as Arduino Library



Pagkatapos ng training:

1. Pumunta sa Deployment.
2. Piliin ang Arduino library deployment option kung supported ng project.
3. I-download ang generated ZIP.
4. Buksan ang Arduino IDE.
5. Piliin ang Sketch > Include Library > Add .ZIP Library.
6. I-import ang downloaded library.

Ang generated library ay may project-specific header name at inference settings.

Tip: Subukan muna ang examples na kasama sa exported library. Ito ang pinakamadaling paraan para ma-verify ang compatibility bago gumawa ng custom sensor integration.




13. How TinyML Inference Works



Kapag na-deploy na ang model, ganito ang typical inference flow:


Read sensor data
       |
Collect a sample window
       |
Apply model preprocessing
       |
Run TinyML classifier
       |
Read prediction scores
       |
Display detected motion


Halimbawa ng conceptual result:


stationary: 0.04
walking:    0.91
shaking:    0.05


Ang prediction scores ay model outputs, hindi garantiya ng correct classification.

Important technical note:

Hindi sapat ang simpleng pagdeklara ng signal_t variable bago tawagin ang run_classifier().

Kailangang magbigay ng valid signal data source na may tamang sample count, feature order, sampling rate, at preprocessing na inaasahan ng exported model.

Dahil project-specific ang Edge Impulse model, walang iisang universal inference sketch na siguradong gagana sa lahat ng exported libraries.

Para sa working deployment, gamitin ang generated example bilang base at i-integrate ang MPU6050 readings ayon sa model input requirements.




14. Common ESP32 TinyML Problems



Problem: MPU6050 not detected

Possible causes:
- Incorrect SDA/SCL pins
- Loose wiring
- Incorrect supply voltage
- I2C address or module issue

Suggested action:
I-check ang wiring at gumamit ng I2C scanner kung kinakailangan.

Problem: Compilation error

Possible causes:
- Missing Arduino libraries
- Incorrect ESP32 board selection
- Incompatible exported model library
- Missing project-specific header

Suggested action:
I-check ang full compiler output at gamitin muna ang generated library examples.

Problem: ESP32 runs out of memory

Possible causes:
- Model too large
- Large input window
- Insufficient RAM
- Unsupported inference configuration

Suggested action:
Subukan ang smaller model, reduced input features, o compatible ESP32 board na may mas maraming memory.

Problem: Low prediction accuracy

Possible causes:
- Poor training data
- Different sensor orientation
- Incorrect sampling frequency
- Different units or preprocessing
- Unbalanced classes

Suggested action:
Magdagdag ng representative data at siguraduhing pareho ang training at inference pipeline.

Problem: Slow inference

Possible causes:
- Large neural network
- Limited CPU resources
- Expensive preprocessing
- Memory constraints

Suggested action:
Gumamit ng optimized model at sukatin ang actual inference time sa device.




15. Frequently Asked Questions



Q: Kaya bang magpatakbo ng AI ang ESP32 nang offline?

Oo. Kayang magpatakbo ng compatible small ML models nang walang internet pagkatapos ma-deploy ang model.

Q: Kailangan ba ng ESP32-CAM?

Hindi para sa motion recognition gamit ang MPU6050. Kailangan lamang ang camera-capable hardware para sa camera-based projects.

Q: Kaya bang patakbuhin ang full ChatGPT sa ordinary ESP32?

Hindi praktikal ang pagpapatakbo ng modern full-scale ChatGPT-class language models sa ordinary ESP32 dahil sa memory at compute requirements.

Q: Libre ba ang Edge Impulse?

May mga available na free at paid options ang Edge Impulse. Tingnan ang kasalukuyang plan limitations at terms sa official website.

Q: Magkano ang ESP32 at MPU6050?

Nag-iiba ang presyo depende sa board model, seller, availability, at shipping. Magkumpara muna ng compatible hardware bago bumili.

Q: K