# TLDR
>[!TLDR] TLDR
>
Use `machine.ADC(machine.Pin(pin))` (e.g., pin 36) to read analog values on an [[ESP32]] with [[MicroPython]]. `adc.read()` gives you a raw value (0-4095), multiply by `3.3 / 4095` to get the voltage. Check your board's pinout and voltage.
# Information
Using analog pins on the [[ESP32]] with [[MicroPython]] involves reading the voltage levels from the pin. The [[ESP32]] has several ADC (Analog-to-Digital Converter) channels, which are accessible through specific pins. Here's a breakdown of how to use them:
**1. Importing the `machine` module:**
```python
import machine
```
This module provides the necessary classes for interacting with hardware peripherals, including the ADC.
**2. Creating an ADC object:**
```python
adc = machine.ADC(machine.Pin(36)) # Replace 36 with the desired analog pin
```
This creates an ADC object associated with the specified pin. Pin 36 is a commonly used analog pin on the [[ESP32]]. Other available analog pins are 39, 34, and 35. Consult your ESP32 board's documentation for the exact pinout.
**3. Reading the raw value:**
```python
raw_value = adc.read()
```
This reads the raw ADC value, which is an integer representing the voltage level. The range of this value depends on the ADC's resolution (typically 12 bits on the [[ESP32]]), meaning the raw values will be between 0 and 4095.
**4. Converting to voltage:**
```python
voltage = adc.read() * (3.3 / 4095) # For a 3.3V ESP32
```
This converts the raw value to voltage. The [[ESP32]] typically operates at 3.3V, so we divide by 4095 (the maximum raw value) and multiply by 3.3 to get the voltage. If your [[ESP32]] uses a different voltage, adjust this calculation accordingly.
**Key Considerations:**
* **Pin Selection:** Double-check your ESP32 board's documentation to confirm the available analog pins. Using an incorrect pin will not work.
* **Voltage Range:** The ESP32's ADC has an input range of 0-3.3V (or the operating voltage of your specific board). Applying a voltage outside this range can damage the ESP32.
* **Resolution:** The 12-bit resolution means the ADC can distinguish between 4096 ($2^{12}$) different voltage levels. This determines the precision of your readings.
* **Attenuation:** Some ESP32 boards allow you to configure the ADC's attenuation. This allows you to measure higher input voltages, but it reduces the resolution. Consult the `machine.ADC` documentation for details on setting the attenuation. Example: `adc.atten(machine.ADC.ATTN_11DB)` for a higher input range (up to approximately 2.5V depending on the specific ESP32 chip).
* **Calibration:** For more accurate readings, you might consider calibrating the ADC. This involves measuring a known voltage and adjusting your calculations accordingly.
This comprehensive explanation should help you get started with using analog pins on your ESP32 with MicroPython. Remember to adapt the code and considerations to your specific hardware and application requirements.