ADC (Analog to Digital)
Microcontrollers are digital, but the world is analog. An Analog-to-Digital Converter (ADC) allows your project to “see” varying voltages from sensors.
1. The Quantization Process
An ADC takes a continuous voltage (e.g., 1.25V) and converts it into a discrete digital number. Intarva simulates 12-bit ADC resolution, which means it can divide the 0V-3.3V range into 4,096 distinct steps.
Raw Values (0-65535)
While the hardware resolution is 12-bit, MicroPython scales this to a 16-bit value (0 to 65,535) for consistency across different microcontrollers. The JavaScript SDK follows the same scaling for cross-language compatibility.
2. Reading Analog Signals
To read an analog signal, you must connect your sensor to a specialized ADC-capable pin. On the Raspberry Pi Pico, these are pins GP26, GP27, and GP28.
import { ADC } from 'intarva';
const sensor = new ADC('GP26');
const rawValue = sensor.read(); // Returns 0-65535
console.log(`Raw Value: ${rawValue}`);
from machine import ADC, Pin
sensor = ADC(Pin(26))
raw_value = sensor.read_u16() # Returns 0-65535
print(f"Raw Value: {raw_value}")
3. Calculating Voltage
To convert the raw value back into a human-readable voltage, use this standard conversion formula:
const voltage = rawValue * (3.3 / 65535);
console.log(`Voltage: ${voltage.toFixed(2)} V`);
voltage = raw_value * (3.3 / 65535)
print(f"Voltage: {voltage:.2f} V")