> ## Documentation Index
> Fetch the complete documentation index at: https://launchpad.anxlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Read Sensor Data with the ANX Dev Module

> Read analog sensor values over ADC and I2C sensor data on the ANX dev module, log readings to the serial monitor, and store data to the file system.

This example demonstrates reading from two types of sensors — an analog potentiometer via the built-in ADC peripheral and a temperature/humidity sensor (SHT31) via I2C — and logging both readings to the serial monitor every two seconds. It's the natural next step after Blinky and introduces the two most common sensor interfaces you'll encounter in embedded development.

## Hardware Needed

Gather the following components before you wire anything up:

* ANX dev module
* 10 kΩ potentiometer (for the ADC channel)
* SHT31 temperature and humidity sensor breakout board (for I2C)
* Jumper wires and a breadboard

## Wiring

Connect your components to the ANX dev module as described below. Double-check the connections before powering on — reversing VCC and GND on the SHT31 breakout can damage the sensor.

<Tabs>
  <Tab title="Potentiometer (ADC)">
    | Potentiometer Pin | ANX Dev Module |
    | ----------------- | -------------- |
    | Left end          | 3.3V           |
    | Wiper (middle)    | GPIO18 (ADC0)  |
    | Right end         | GND            |

    Rotating the potentiometer knob varies the voltage on the wiper between 0 V and 3.3 V, which the ADC converts to a 12-bit value between 0 and 4095.
  </Tab>

  <Tab title="SHT31 (I2C)">
    | SHT31 Pin | ANX Dev Module    |
    | --------- | ----------------- |
    | VCC       | 3.3V              |
    | GND       | GND               |
    | SDA       | GPIO2 (I2C0\_SDA) |
    | SCL       | GPIO3 (I2C0\_SCL) |

    Most SHT31 breakout boards include onboard pull-up resistors on SDA and SCL. If yours does not, add 4.7 kΩ pull-up resistors from each line to 3.3V.
  </Tab>
</Tabs>

## Application Code

The example is split into two helper functions — `read_adc()` and `read_sht31()` — each responsible for a single sensor. The `app_main()` function calls both in a loop with a two-second delay between readings.

```c sensor_data.c theme={null}
#include "anx/anx.h"
#include "anx/adc.h"
#include "anx/i2c.h"
#include "anx/log.h"

#define SHT31_ADDR  0x44

void read_adc(void) {
    anx_adc_init(ANX_ADC0);
    uint16_t raw = anx_adc_read(ANX_ADC0);
    float voltage = (raw / 4095.0f) * 3.3f;
    ANX_LOGI("ADC", "Raw: %d  Voltage: %.2fV", raw, voltage);
}

void read_sht31(void) {
    anx_i2c_config_t cfg = {
        .bus   = ANX_I2C0,
        .speed = ANX_I2C_FAST,
    };
    anx_i2c_init(&cfg);

    // Send measurement command
    uint8_t cmd[] = {0x24, 0x00};
    anx_i2c_write(ANX_I2C0, SHT31_ADDR, cmd, 2);
    anx_delay_ms(20);

    // Read 6 bytes: temp MSB, temp LSB, CRC, hum MSB, hum LSB, CRC
    uint8_t data[6];
    anx_i2c_read(ANX_I2C0, SHT31_ADDR, data, 6);

    uint16_t raw_temp = (data[0] << 8) | data[1];
    uint16_t raw_hum  = (data[3] << 8) | data[4];
    float temp_c   = -45.0f + 175.0f * (raw_temp / 65535.0f);
    float humidity = 100.0f  * (raw_hum  / 65535.0f);

    ANX_LOGI("SHT31", "Temp: %.1f°C  Humidity: %.1f%%", temp_c, humidity);
}

void app_main(void) {
    while (1) {
        read_adc();
        read_sht31();
        anx_delay_ms(2000);
    }
}
```

## Run and Monitor

Flash the example to your connected module and open the serial monitor with the following two commands:

```bash theme={null}
anx run examples/sensor-data
anx monitor
```

With the potentiometer set to mid-travel and the SHT31 at room temperature, you should see output similar to:

```
[ADC]   Raw: 2048  Voltage: 1.65V
[SHT31] Temp: 23.4°C  Humidity: 51.2%
```

Rotate the potentiometer knob and watch the `Raw` and `Voltage` values update in real time. Move the SHT31 near a warm surface or breathe gently on it to see temperature and humidity respond.

<Note>
  The default I2C address for the SHT31 is **0x44**, which is what `SHT31_ADDR` is set to in the example. Some breakout boards pull the ADDR pin high, changing the address to **0x45**. If your readings never appear or you see I2C timeout errors in the monitor, change the define to `0x45` and re-flash.
</Note>

## How the SHT31 Conversion Works

The SHT31 returns raw 16-bit values for temperature and humidity that you convert to physical units using the formulas from the datasheet. The example applies both conversions inline:

<Accordion title="Temperature conversion formula">
  ```
  T (°C) = -45 + 175 × (raw_temp / 65535)
  ```

  A raw value of `0` maps to −45 °C and a raw value of `65535` maps to +130 °C, giving a full-scale range of 175 °C across 16 bits.
</Accordion>

<Accordion title="Humidity conversion formula">
  ```
  RH (%) = 100 × (raw_hum / 65535)
  ```

  A raw value of `0` maps to 0 % RH and `65535` maps to 100 % RH. The sensor's specified accuracy is ±2 % RH between 20 % and 80 % RH.
</Accordion>

<Tip>
  Once your sensor readings are working, take the natural next step and publish them to the cloud. The [Connectivity](/examples/connectivity) example shows you how to connect to Wi-Fi and push sensor payloads to an MQTT broker over TLS with just a few extra lines of code.
</Tip>
