# STM32 - Analogread

**URL:** https://community.simplefoc.com/t/stm32-analogread/4476
**Category:** Uncategorized
**Created:** [February 18, 2024, 7:02pm UTC](https://community.simplefoc.com/t/stm32-analogread/4476 "2024-02-18T19:02:49Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![Candas1](https://community.simplefoc.com/user_avatar/community.simplefoc.com/candas1/32/5296_2.png) [@Candas1](https://community.simplefoc.com/u/Candas1)
#### Post date: [February 19, 2024, 11:34am UTC](https://community.simplefoc.com/t/stm32-analogread/4476/5 "2024-02-19T11:34:29Z")

</div>

Some tips about analoRead on STM32DUINO.

**Pin Map:**  
analogread sequentially scans through PinMap\_ADC to find the pin’s ADC information (eg. [F405](https://github.com/stm32duino/Arduino_Core_STM32/blob/0ca1f4ca3d5936cf10855d2cde37e44735425edf/variants/STM32F4xx/F405RGT_F415RGT/PeripheralPins.c#L45)).  
The further the pin is in the list, the more time it takes.  
You can redefine PinMap\_ADC in your sketch, keeping only the analog pins you need(analoRead, current sense, MagneticSensorAnalog, …).

> const PinMap PinMap\_ADC = {  
> {PC\_5, ADC1, STM\_PIN\_DATA\_EXT(STM\_MODE\_ANALOG, GPIO\_NOPULL, 0, 15, 0)}, // ADC1\_IN15  
> {PA\_3, ADC1, STM\_PIN\_DATA\_EXT(STM\_MODE\_ANALOG, GPIO\_NOPULL, 0, 3, 0)}, // ADC1\_IN3  
> {PC\_0, ADC1, STM\_PIN\_DATA\_EXT(STM\_MODE\_ANALOG, GPIO\_NOPULL, 0, 10, 0)}, // ADC1\_IN10  
> {PC\_1, ADC1, STM\_PIN\_DATA\_EXT(STM\_MODE\_ANALOG, GPIO\_NOPULL, 0, 11, 0)}, // ADC1\_IN11  
> };

Analogread Execution went from 17us to 14us for PA3, and from 22us to 14us for PC5 😱

**Sampling Time:**  
STM32DUINO uses ADC\_SAMPLINGTIME as default sampling time in analogread for pin channels, and ADC\_SAMPLINGTIME\_INTERNAL for internal channels (Voltage reference, internal temperature, …).  
On STM32F405, ADC\_SAMPLETIME\_15CYCLES for pin channels, and ADC\_SAMPLETIME\_480CYCLES for internal channels.

By using this build flag, analogread is using the lowest sampling time, which is ADC\_SAMPLETIME\_3CYCLES on F405

> -DADC\_SAMPLINGTIME0

This only saved 0.1us, but it’s worth knowing how to change the sampling time

---

_[View the full topic](https://community.simplefoc.com/t/stm32-analogread/4476)._
