---
# System prepended metadata

title: How sensor works to microcontroller in internet of things?
tags: [Microcontroller]

---

This is a fundamental concept in IoT. The interaction between a [sensor](https://www.ampheo.com/c/sensors) and a [microcontroller](https://www.ampheo.com/c/microcontrollers) (MCU) in an IoT system is how the physical world gets translated into digital data that can be processed and sent to the internet.

![1624083209428-1024x768](https://hackmd.io/_uploads/SkgvudDClg.jpg)

Here's a detailed breakdown of how it works, from the physical signal to the cloud.

**The Big Picture: The IoT Data Chain**
The entire process can be summarized in this flow, from the physical world to the internet and back:

![deepseek_mermaid_20251023_0437dc](https://hackmd.io/_uploads/r1IWwOwAle.png)

Now, let's dive into the crucial first step: the connection between the Sensor and the Microcontroller.

**Part 1: The Hardware Connection (The Physical Link)**
Sensors connect to a microcontroller's input pins. The type of connection depends on the sensor's output.

**1. Analog Sensors**
* How they work: These sensors output a continuous voltage that varies proportionally to the physical property they measure (e.g., temperature, light intensity, [potentiometer](https://www.onzuu.com/category/potentiometers) position).
* Microcontroller Interface: [Analog-to-Digital Converter](https://www.onzuu.com/category/analog-to-digital-converters) (ADC) Pin.
* Process: The sensor's voltage output (e.g., 0V to 3.3V) is fed into the MCU's ADC pin. The ADC converts this continuous voltage into a discrete digital number (e.g., a 12-bit ADC might map 0V to 0 and 3.3V to 4095).
* Examples: Thermistor (via voltage divider), Photoresistor (LDR), analog [accelerometers](https://www.onzuu.com/category/accelerometers).
* Schematic:

```
text

[Sensor Vout] ---> [MCU ADC Pin]
[Sensor GND]  ---> [MCU GND]
[Sensor Vcc]  ---> [MCU 3.3V/5V]
```
**2. Digital Sensors**
How they work: These sensors output a binary signal (either HIGH or LOW) or communicate data using a digital protocol.

* Simple Digital: A single pin goes HIGH/LOW (e.g., a PIR [motion sensor](https://www.onzuu.com/category/motion-sensors) detects movement and outputs a 3.3V signal).
* Complex Digital/Serial: Uses a communication protocol to send multiple bytes of data. Common protocols include:

  * I2C (Inter-Integrated Circuit): Uses two wires: SDA (Data) and SCL (Clock). Great for connecting many sensors to the same bus. Examples: [BMP280](https://www.ampheo.com/product/bmp280-26874860) (Pressure), MPU6050 (Accel/Gyro).
  * SPI (Serial Peripheral Interface): Uses four wires: MOSI (Master Out Slave In), MISO (Master In Slave Out), SCK (Clock), CS (Chip Select). Faster than I2C. Examples: SD cards, high-resolution ADCs.
  * UART (Universal Asynchronous Receiver/Transmitter): Uses two wires: TX (Transmit) and RX (Receive). Used for simple point-to-point communication. Examples: GPS modules, some [RFID readers](https://www.onzuu.com/category/rfid-reader-modules).

Microcontroller Interface: GPIO Pins (for simple digital), or dedicated I2C, SPI, UART hardware peripherals.

Examples: [DHT11](https://www.ampheo.com/product/dht11-26836202)/[DHT22](https://www.ampheo.com/product/part-dht22-26872164) (Temp/Humidity), [HC-SR04](https://www.ampheo.com/product/hc-sr04-26835950) (Ultrasonic Distance), [BME280](https://www.ampheo.com/product/bme280-26811125) (Temp/Humidity/Pressure).

**3. Pulse-Based Sensors**
* How they work: The information is encoded in the timing of a pulse, either its width (Pulse Width Modulation - PWM) or its frequency.
* Microcontroller Interface: GPIO Pin (often with interrupt capability for accurate timing).
* Process: The MCU measures the duration (e.g., pulseIn() in [Arduino](https://www.ampheo.com/c/development-board-arduino)) or counts pulses over a period.
* Examples: HC-SR04 ([Ultrasonic sensor](https://www.onzuu.com/category/ultrasonic-receivers-transmitters)), PWM-output anemometers (wind speed).

**Part 2: The Software Flow (The "Brain")**
The MCU's firmware is responsible for reading the sensor data. Here’s the typical code flow:

**1. Initialization (setup() in Arduino):**

* Configure Communication: Initialize the correct serial protocol (I2C, SPI) with the desired settings (speed, etc.).
* Configure Pins: Set the GPIO pins as INPUT or OUTPUT.
* Sensor Wake-up: Send wake-up or configuration commands to the sensor (common for advanced digital sensors).

**2. Main Loop (loop() in Arduino):**

Trigger Measurement: For some sensors, the MCU must send a command to take a new reading.

Read Data:

* Analog: Call analogRead(PIN) to get the ADC value.
* Digital Serial: Use library functions (e.g., sensor.readTemperature()) that handle the complex protocol of reading data registers over I2C/SPI.
* Simple Digital: Call digitalRead(PIN).

Process Data (Signal Conditioning): Raw data is rarely perfect.

* Scale/Convert: Convert the raw ADC value to a meaningful unit (e.g., voltage = (adc_value / 4095) * 3.3; temperatureC = (voltage - 0.5) * 100;).
* Calibrate: Apply calibration factors to improve accuracy.
* Filter: Smooth the data to reduce noise (e.g., using a moving average or a Kalman filter).

Prepare for Transmission: Package the processed data into a suitable format (e.g., JSON, CSV).

Example: {"temp": 23.5, "humidity": 65.2}

Send to Communication Module: The MCU passes this data packet to the communication module (Wi-Fi, Bluetooth, LoRa) via UART or SPI.

**Part 3: The IoT Integration (To the Cloud and Back)**
This is what makes it "Internet of Things" instead of just a "Standalone Circuit."

1. Connectivity: The MCU (or a co-processor like an ESP8266) connects to the internet via:

* Wi-Fi: For nearby router access.
* Cellular (LTE-M, NB-IoT): For wide-area coverage.
* Low-Power WAN (LoRaWAN): For long-range, low-bandwidth applications.

2. Cloud Communication: The device sends the sensor data to an IoT cloud platform (e.g., AWS IoT, Azure IoT Hub, Google Cloud IoT, Adafruit IO, Blynk) using protocols like MQTT (lightweight, popular for IoT) or HTTP.

3. Cloud Processing: The cloud platform:

* Stores the data in a database.
* Analyzes it (e.g., generates alerts if temperature is too high).
* Visualizes it on a web dashboard for the user.

4. Actuation (Optional Closing the Loop):

* The cloud can send a command back to the MCU (e.g., "turn on the fan").
* The MCU receives this command and drives an actuator (e.g., a [relay](https://www.onzuu.com/category/relays), a motor, an LED) to affect the physical world.

**A Complete Concrete Example: Smart Plant Monitor**
* Sensor: Capacitive Soil Moisture Sensor (Analog).
* Microcontroller: ESP32 (has Wi-Fi built-in and an ADC).
* Process:

1. The sensor's analog pin is connected to an ADC pin on the ESP32.
2. The ESP32 reads the ADC value (raw_value = analogRead(34);).
3. It converts the value to a moisture percentage (e.g., moisture = map(raw_value, dry_value, wet_value, 0, 100);).
4. The ESP32 connects to your Wi-Fi.
5. It packages the data as JSON: {"plant": "Monstera", "moisture": 42}.
6. It publishes this data to an MQTT topic on a cloud server using the PubSubClient library.
7. The cloud server (e.g., Home Assistant) receives the data. If the moisture drops below 20%, it sends a push notification to your phone: "Your Monstera is thirsty!"
8. (Actuation) You could have a relay connected to a water pump. The cloud could send a command to the ESP32 to turn on the relay and water the plant automatically.

**Summary**
In an IoT system, the [sensor](https://www.ampheoelec.de/c/sensors) is the "sense organ" that gathers raw physical data. The microcontroller is the "local brain" that reads, conditions, and digitizes this data, and then manages its journey to the "cloud brain" via a communication module. This seamless flow from the physical to the digital world is the very essence of the Internet of Things.

