# B-G431B-ESC1: ADC reading of Temp, pot, VBUS while foc\_current

**URL:** <https://community.simplefoc.com/t/b-g431b-esc1-adc-reading-of-temp-pot-vbus-while-foc-current/1529>\
**Category:** hardware support\
**Tags:** b\_g431\_esc\
**Created:** [January 2, 2022, 4:23pm UTC](https://community.simplefoc.com/t/b-g431b-esc1-adc-reading-of-temp-pot-vbus-while-foc-current/1529 "2022-01-02T16:23:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoeckDK](https://community.simplefoc.com/user_avatar/community.simplefoc.com/hoeckdk/32/2056_2.png) [@HoeckDK](https://community.simplefoc.com/u/HoeckDK)\
**Post date:** [January 2, 2022, 4:23pm UTC](https://community.simplefoc.com/t/b-g431b-esc1-adc-reading-of-temp-pot-vbus-while-foc-current/1529/1 "2022-01-02T16:23:54Z")

</div>

Setup:  
board = disco\_b\_g431b\_esc1  
framework = arduino  
lib\_archive = false  
lib\_deps =  
[GitHub - simplefoc/Arduino-FOC: Arduino FOC for BLDC and Stepper motors - Arduino Based Field Oriented Control Algorithm Library](https://github.com/simplefoc/Arduino-FOC.git) @ 2.2  
build\_flags =  
-D PIO\_FRAMEWORK\_ARDUINO\_NANOLIB\_FLOAT\_PRINTF  
-D HAL\_OPAMP\_MODULE\_ENABLED  
-D HAL\_TIM\_MODULE\_ENABLED

Motor control seems working. It can spin a lot faster than what it can with modes different to foc\_current. That is good.

But I would like to read temperature, VBUS and pot. I cannot get it to work with analogRead. They just read 0. Using digitalRead I can see observe if the pot is all the way CCW or CV.

```
float Pot = (float)analogRead(A_POTENTIOMETER);
float PCBtemp = ((float)analogRead(A_TEMPERATURE) / 1024.0 * 3300.0 - 1055.0) / 22.7 + 25.0;
float VBUS = ((float)analogRead(A_VBUS) / 1024.0 * 3.3) / 18.0 * 187.0;

```

I have had it compile with different pulls of Simple FOC, where I could use analogRead, but then there would be no current feedback.

Thanks.

---

<div class="post-metadata">

**Author:** ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)\
**Post date:** [January 4, 2022, 10:00pm UTC](https://community.simplefoc.com/t/b-g431b-esc1-adc-reading-of-temp-pot-vbus-while-foc-current/1529/2 "2022-01-04T22:00:24Z")

</div>

Hi @HoeckDK ,

I’m not sure what’s going on there, but I have a hunch: the STM32G4 code for the ESC-1 board sets up the ADC in a specific way, and probably this interferes with using analogRead().

I don’t have a quick solution for you here. You can either play with the library code to (for example) add the analog inputs you need to what is read by the current sensing code, or figure out a way to use them in parallel.

If you’re willing to wait a few weeks to months, the ADC code in SimpleFOC is being refactored, so after this task is completed, then there may be a cleaner way to use the other ADC pins.

Sorry I can’t be more helpful on this!

---

<div class="post-metadata">

**Author:** ![HoeckDK](https://community.simplefoc.com/user_avatar/community.simplefoc.com/hoeckdk/32/2056_2.png) [@HoeckDK](https://community.simplefoc.com/u/HoeckDK)\
**Post date:** [January 5, 2022, 11:48am UTC](https://community.simplefoc.com/t/b-g431b-esc1-adc-reading-of-temp-pot-vbus-while-foc-current/1529/3 "2022-01-05T11:48:07Z")

</div>

Hi

I figured out that it would be the case. Analog read stops working after CurrentSensor.init(). When DMA is configured it dies.

I’ll try to look into configuring the other ADCs under DMA also. I guess I could be able to get hints from CubeIDE generated code?!

I also tried to configure the current sensor pins as pot, temp and VBUS, but that does not work. So I guess that something is hard coded for the actual current pins.

Thanks the reply.

---

<div class="post-metadata">

**Author:** ![HoeckDK](https://community.simplefoc.com/user_avatar/community.simplefoc.com/hoeckdk/32/2056_2.png) [@HoeckDK](https://community.simplefoc.com/u/HoeckDK)\
**Post date:** [January 5, 2022, 9:54pm UTC](https://community.simplefoc.com/t/b-g431b-esc1-adc-reading-of-temp-pot-vbus-while-foc-current/1529/4 "2022-01-05T21:54:28Z")

</div>

So, some progress. However not tested with the motor working yet, but I get ADC readings parallel to current readings:

stm32g4\_mcu.cpp:

> #define ADC\_BUF\_LEN\_1 5 (was 2)

> float \_readADCVoltageInline(const int pin){  
> uint32\_t raw\_adc = 0;  
> if (pin == PA2) // = ADC1\_IN3 = phase U (OP1\_OUT) on B-G431B-ESC1  
> raw\_adc = adcBuffer1[1];  
> else if (pin == PA6) // = ADC2\_IN3 = phase V (OP2\_OUT) on B-G431B-ESC1  
> raw\_adc = adcBuffer2[0];  
> else if (pin == PB1) // = ADC1\_IN12 = phase W (OP3\_OUT) on B-G431B-ESC1  
> raw\_adc = adcBuffer1[0];  
> else if (pin == A\_POTENTIOMETER)  
> raw\_adc = adcBuffer1[2];  
> else if (pin == A\_TEMPERATURE)  
> raw\_adc = adcBuffer1[3];  
> else if (pin == A\_VBUS)  
> raw\_adc = adcBuffer1[4];

stm32g4\_hal.cpp:

> hadc1-\>Init.NbrOfConversion = 5; (was 2)

added at line 135:

> /\* USER CODE BEGIN ADC1\_Init 2 _/  
> /_\* Configure Regular Channel (PB12, Potentiometer)  
> \*/  
> sConfig.Channel = ADC\_CHANNEL\_11;  
> sConfig.Rank = ADC\_REGULAR\_RANK\_3;  
> sConfig.SamplingTime = ADC\_SAMPLETIME\_47CYCLES\_5;  
> sConfig.SingleDiff = ADC\_SINGLE\_ENDED;  
> sConfig.OffsetNumber = ADC\_OFFSET\_NONE;  
> sConfig.Offset = 0;  
> if (HAL\_ADC\_ConfigChannel(hadc1, &sConfig) != HAL\_OK)  
> {  
> Error\_Handler();  
> }
> 
> ```
> /* USER CODE BEGIN ADC1_Init 2 */
> 
> ```
> 
> /\*\* Configure Regular Channel (PB14, Temperature)  
> \*/  
> sConfig.Channel = ADC\_CHANNEL\_5;  
> sConfig.Rank = ADC\_REGULAR\_RANK\_4;  
> sConfig.SamplingTime = ADC\_SAMPLETIME\_47CYCLES\_5;  
> sConfig.SingleDiff = ADC\_SINGLE\_ENDED;  
> sConfig.OffsetNumber = ADC\_OFFSET\_NONE;  
> sConfig.Offset = 0;  
> if (HAL\_ADC\_ConfigChannel(hadc1, &sConfig) != HAL\_OK)  
> {  
> Error\_Handler();  
> }
> 
> ```
> /* USER CODE BEGIN ADC1_Init 2 */
> 
> ```
> 
> /\*\* Configure Regular Channel (PB14, Temperature)  
> \*/  
> sConfig.Channel = ADC\_CHANNEL\_1;  
> sConfig.Rank = ADC\_REGULAR\_RANK\_5;  
> sConfig.SamplingTime = ADC\_SAMPLETIME\_47CYCLES\_5;  
> sConfig.SingleDiff = ADC\_SINGLE\_ENDED;  
> sConfig.OffsetNumber = ADC\_OFFSET\_NONE;  
> sConfig.Offset = 0;  
> if (HAL\_ADC\_ConfigChannel(hadc1, &sConfig) != HAL\_OK)  
> {  
> Error\_Handler();  
> }

Tomorrow: test if it breaks current readings…

---

<div class="post-metadata">

**Author:** ![Grizzly](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/g/a9adbd/32.png) [@Grizzly](https://community.simplefoc.com/u/Grizzly)\
**Post date:** [February 10, 2022, 4:08pm UTC](https://community.simplefoc.com/t/b-g431b-esc1-adc-reading-of-temp-pot-vbus-while-foc-current/1529/5 "2022-02-10T16:08:38Z")

</div>

Just to confirm, that I also implemented your changes on top of V2.21 and they seem to work just fine! Thanks a lot, @ [HoeckDK](https://community.simplefoc.com/u/HoeckDK) this helped me a tremedously!

- Chris
