AI in IoT: Sensors, Microcontrollers and TinyML

How small machine learning models on microcontrollers turn raw sensor data into useful events, with a TinyML workflow, machine anomaly detection, MQTT connectivity and power budgets.

A typical IoT project starts with sensors that send readings to the cloud every few seconds. That is fine for a handful of temperature probes, but it breaks down quickly with vibration, sound or motion data. A single accelerometer sampled hundreds of times a second produces far more data than a battery-powered device can afford to transmit, and most of it is uninteresting: the motor is running normally.

TinyML is the practice of running very small machine learning models directly on microcontrollers, so the device itself decides what is worth reporting. For Indian manufacturers, cold-chain operators and facility managers, that can mean monitoring equipment on sites with weak connectivity and no spare power sockets. This article covers the building blocks: sensor data, microcontrollers, the TinyML workflow, a machine anomaly example, connectivity and power.

Sensor data: know what you are measuring

Machine learning on a device can only be as good as the signal it receives. Before choosing a model, be clear about:

  • Sensor type. Accelerometers for vibration, microphones for sound, current sensors for motor load, thermistors for temperature, gas and humidity sensors for environmental monitoring.
  • Sampling rate. It must be high enough to capture the effect you care about. To see a vibration at a given frequency you must sample at more than twice that frequency, and in practice with some margin.
  • Mounting and noise. A vibration sensor glued to a motor housing gives a different signal from one screwed to a nearby bracket. Keep mounting consistent across machines.
  • Labels. Supervised models need examples of each condition. For faults that rarely happen, you may only have "normal" data, which points towards anomaly detection rather than classification.

Microcontrollers that can run models

Microcontrollers (MCUs) have kilobytes to a few megabytes of memory, no operating system or a small real-time one, and run on milliwatts. Common choices:

  • ESP32 family. Inexpensive, widely available in India, with built-in Wi-Fi and Bluetooth. Some variants, such as the ESP32-S3, add instructions intended to speed up signal processing and neural network workloads.
  • Arm Cortex-M based MCUs from many vendors. Cortex-M4 and M7 cores include DSP instructions, and Arm's CMSIS-NN library provides optimised neural network kernels for Cortex-M processors.
  • Development boards that combine an MCU with an accelerometer and microphone are useful for collecting data and prototyping before you design custom hardware.

The model, the input buffer and your application code all have to fit in RAM and flash together, so check memory budgets early.

The TinyML workflow

  1. Collect data from the real machine, in real conditions, across operating modes: idle, light load, full load, start-up.
  2. Extract features. Raw samples are usually converted into compact features such as RMS values, peak values or frequency bands from an FFT. Feature extraction often matters more than the model.
  3. Train a small model on a computer, typically in TensorFlow or Keras, or with a platform such as Edge Impulse that handles the pipeline.
  4. Quantise and convert to an 8-bit integer model suitable for TensorFlow Lite for Microcontrollers, which runs on MCUs without an operating system.
  5. Deploy and test on the device, measuring accuracy, inference time, RAM use and power, not just accuracy on the laptop.
  6. Monitor in the field and retrain when machines, mounting or operating conditions change.

Example: anomaly detection for machines

Consider pumps or motors in a small factory. Breakdowns are rare, so there are few examples of failure. A practical approach is to learn what "normal" looks like and flag deviations:

  • Every few seconds, the MCU captures a window of accelerometer data and computes features such as RMS per axis and energy in several frequency bands.
  • A small model trained only on normal operation, such as an autoencoder or a simple clustering model, scores how unusual the current window is.
  • If the score stays above a threshold for several consecutive windows, the device publishes an alert. Requiring several windows avoids alarms from a single knock.
  • Otherwise, it sends only a periodic summary, for example once every 15 minutes.

Start even simpler if you can: fixed thresholds on RMS vibration and temperature, tuned with maintenance staff, may cover the most important cases and give you a baseline to beat. Anomaly detection tells you something has changed, not what is wrong, so the alert should prompt an inspection rather than a diagnosis.

Connectivity with MQTT

MQTT is a lightweight publish/subscribe protocol widely used in IoT. Devices publish small messages to topics on a broker, and dashboards or back-end services subscribe to them. Useful features:

  • Quality of service levels 0, 1 and 2, trading delivery guarantees against overhead. Level 1 (at least once) suits most alerts, provided the back end tolerates duplicates.
  • Retained messages, so a new subscriber immediately gets the last known state.
  • Last Will and Testament, a message the broker publishes if a device disconnects unexpectedly, which is useful for detecting offline devices.

Use a clear topic structure such as site/line/machine/alerts, send compact payloads, and always use TLS and per-device credentials. A device that sends only events and summaries keeps both data charges and broker load low.

Power budgets

On battery-powered devices, the radio is usually the most power-hungry component, which is a strong argument for deciding on the device and transmitting rarely. Design around a duty cycle: wake, sample, run inference, transmit only if needed, return to deep sleep. Measure actual current draw in each phase with a proper power meter instead of relying on datasheet figures, and calculate battery life from the measured average, with margin for heat, battery ageing and retries when the network is poor.

Project checklist

  • Problem defined as a decision the device must make, not "collect all data"
  • Sensor type, sampling rate and mounting agreed and documented
  • Data collected from real machines across all operating modes
  • Simple threshold baseline measured before any ML model
  • Model, buffers and firmware fit in RAM and flash with headroom
  • Inference time and accuracy measured on the actual MCU
  • MQTT topics, QoS, TLS and per-device credentials defined
  • Duty cycle and battery life calculated from measured current
  • Over-the-air firmware and model updates with rollback
  • Dashboard, alert routing and a process for who responds to alerts

How NextWebGen can help

NextWebGen is a software company, so we work on the side of an IoT project that sits above the device: MQTT brokers and APIs, databases for sensor data, monitoring dashboards, alert workflows and mobile apps for maintenance teams, hosted on servers we can manage for you. If you have devices producing data and need the platform to make it useful, see our server management services or tell us about your setup.