Reading analog inputs that are not mapped to A0-A5 but on other gpio on stm32

if i try to read a normal analog input it works but if i try to read an input on D10 for example it returns 0
(i am using a nucleo-wb88rg with the framework from grumpyoldpizza modified by @maxgerhardt )

The ADC peripheral can only perform an analog measurement on certain pins. Have you checked wuth the datasheet of the microcontroller which pins those are? D10 may not have an ADCx_ChannelY function available, so you doing an analogRead() would not be possible.

it has an adc behind it:

Then the pin definition declaring ADC_CHANNEL_NONE is wrong for the Nucleo in the core.

Can you change that to ADC_CHANNEL_9?

This should unblock the analogRead() function from returning 0.

where is it in pio that i can change it to test it?

<user home folder>\.platformio\packages\framework-arduinoststm32wb\variants\NUCLEO-WB55RG\variant.cpp line 57.

Home folder being C:\Users\<user>\ on Windows.

it still doesnt work
i read it with this line: “display.print(analogRead(10));” and it still shows 0
and i use linux

That’s weird. Can you try this code directly:

  uint32_t input = 0;
  stm32wb_gpio_pin_configure(STM32WB_GPIO_PIN_PA4, (STM32WB_GPIO_PUPD_NONE | STM32WB_GPIO_MODE_ANALOG));
  input = __analogReadChannel((STM32WB_ADC_CHANNEL_1 + ADC_CHANNEL_9), 2 /* __analog_readPeriod */);
  // downconvert 12 bits to 10 bits result
  input = (input * ((1 << 10) -1)) / 4095;
  display.print(input);

it throws a compiler error:

src/main.cpp: In function ‘void battery_loop()’:
src/main.cpp:341:32: error: ‘STM32WB_GPIO_PIN_PA4’ was not declared in this scope
stm32wb_gpio_pin_configure(STM32WB_GPIO_PIN_PA4, (STM32WB_GPIO_PUPD_NONE | STM32WB_GPIO_MODE_ANALOG));
^~~~~~~~~~~~~~~~~~~~
src/main.cpp:341:32: note: suggested alternative: ‘STM32WB_CONFIG_PIN_VBUS’
stm32wb_gpio_pin_configure(STM32WB_GPIO_PIN_PA4, (STM32WB_GPIO_PUPD_NONE | STM32WB_GPIO_MODE_ANALOG));
^~~~~~~~~~~~~~~~~~~~
STM32WB_CONFIG_PIN_VBUS
src/main.cpp:341:55: error: ‘STM32WB_GPIO_PUPD_NONE’ was not declared in this scope
stm32wb_gpio_pin_configure(STM32WB_GPIO_PIN_PA4, (STM32WB_GPIO_PUPD_NONE | STM32WB_GPIO_MODE_ANALOG));
^~~~~~~~~~~~~~~~~~~~~~
src/main.cpp:341:55: note: suggested alternative: ‘STM32WB_I2C_STATE_NONE’
stm32wb_gpio_pin_configure(STM32WB_GPIO_PIN_PA4, (STM32WB_GPIO_PUPD_NONE | STM32WB_GPIO_MODE_ANALOG));
^~~~~~~~~~~~~~~~~~~~~~
STM32WB_I2C_STATE_NONE
src/main.cpp:341:80: error: ‘STM32WB_GPIO_MODE_ANALOG’ was not declared in this scope
stm32wb_gpio_pin_configure(STM32WB_GPIO_PIN_PA4, (STM32WB_GPIO_PUPD_NONE | STM32WB_GPIO_MODE_ANALOG));
^~~~~~~~~~~~~~~~~~~~~~~~
src/main.cpp:341:80: note: suggested alternative: ‘STM32WB_I2C_OPTION_MODE_MASK’
stm32wb_gpio_pin_configure(STM32WB_GPIO_PIN_PA4, (STM32WB_GPIO_PUPD_NONE | STM32WB_GPIO_MODE_ANALOG));
^~~~~~~~~~~~~~~~~~~~~~~~
STM32WB_I2C_OPTION_MODE_MASK
src/main.cpp:341:5: error: ‘stm32wb_gpio_pin_configure’ was not declared in this scope
stm32wb_gpio_pin_configure(STM32WB_GPIO_PIN_PA4, (STM32WB_GPIO_PUPD_NONE | STM32WB_GPIO_MODE_ANALOG));
^~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.cpp:341:5: note: suggested alternative: ‘stm32wb_i2c_configure’
stm32wb_gpio_pin_configure(STM32WB_GPIO_PIN_PA4, (STM32WB_GPIO_PUPD_NONE | STM32WB_GPIO_MODE_ANALOG));
^~~~~~~~~~~~~~~~~~~~~~~~~~
stm32wb_i2c_configure
src/main.cpp:342:34: error: ‘STM32WB_ADC_CHANNEL_1’ was not declared in this scope
input = __analogReadChannel((STM32WB_ADC_CHANNEL_1 + ADC_CHANNEL_9), 2 /* __analog_readPeriod /);
^~~~~~~~~~~~~~~~~~~~~
src/main.cpp:342:34: note: suggested alternative: ‘ADC_CHANNEL_1’
input = __analogReadChannel((STM32WB_ADC_CHANNEL_1 + ADC_CHANNEL_9), 2 /
__analog_readPeriod /);
^~~~~~~~~~~~~~~~~~~~~
ADC_CHANNEL_1
src/main.cpp:342:13: error: ‘__analogReadChannel’ was not declared in this scope
input = __analogReadChannel((STM32WB_ADC_CHANNEL_1 + ADC_CHANNEL_9), 2 /
__analog_readPeriod /);
^~~~~~~~~~~~~~~~~~~
src/main.cpp:342:13: note: suggested alternative: ‘analogReadPeriod’
input = __analogReadChannel((STM32WB_ADC_CHANNEL_1 + ADC_CHANNEL_9), 2 /
__analog_readPeriod */);
^~~~~~~~~~~~~~~~~~~
analogReadPeriod

Hm. Even with

#include "wiring_private.h"
#include "stm32wbxx.h"
#include "stm32wb_adc.h"

added at the top?

it works now i will make a function that reads it

Still weird how the pin modifications in variant.cpp didn’t work, the analogRead() function code should be equivalent to what I’ve posted above then.

Can you report the missing analog pin definitions in Issues · GrumpyOldPizza/ArduinoCore-stm32wb · GitHub?