Page 1 of 1

Analog input on PB0?

Posted: Mon Oct 31, 2022 10:06 pm
by jhitt50
Hello,

I'm new to the forum, transitioning a project from Arduino to the L432KC. I'm running out of ADC pins and need 1 additional. I can't use A7 (PA2) because that pin is reserved for USB VCP TX, which I need for debugging.

The mbed pinout at the link below shows that D3 (PB0) is ADC1/15. However, it is not listed as ADC in the datasheet. I have tried setting it up as an analog input in a simple sketch and it doesn't work. Behaves just like any other digital pin when you try analog read.

Can PB_0 be used as an ADC input pin as the pinout indicates? If so, how do I set it for that? Alternatively, is there another way to free up A7 without losing my USB serial comms?

https://os.mbed.com/platforms/ST-Nucleo-L432KC/

Code: Select all

void setup() {
  Serial.begin(9600);
  pinMode(3, INPUT_ANALOG);
}

void loop() {
  int val = analogRead(3);
  Serial.println(val);
  delay(250);
 }
Thanks!

Re: Analog input on PB0?

Posted: Tue Nov 01, 2022 8:19 am
by fpiSTM
Yes PB0 is an ADC.
In fact your code is not correct:

Code: Select all

  int val = analogRead(3);
will read ADC value on A3 not D3.
Correct code to read ADC of PB0 is (all line is equivalent):

Code: Select all

  int val = analogRead(8);
  int val = analogRead(A8);
  int val = analogRead(PB0);
  
Moreover

Code: Select all

pinMode(3, INPUT_ANALOG);
is not needed.

Re: Analog input on PB0?

Posted: Tue Nov 01, 2022 8:17 pm
by jhitt50
Thank you for the reply!

I tried all those combinations without any success. However, I did find that the following works (on a hunch) after reviewing the PeripheralPins.c file.
https://github.com/ARMmbed/mbed-os/blob ... eralPins.c

Code: Select all

int val = analogRead(PB0_ALT0);
For anyone else who is interested PeripheralPins.c also lists PB1 (D6) as an analog input for the Nucleo-L432KC even though it is not identified in the pinout or datasheet. I confirmed that it also works with this method.

Code: Select all

int val = analogRead(PB1_ALT0);
Is anyone aware of documentation for these two pins reflecting this result? I feel like I must be missing something.

Re: Analog input on PB0?

Posted: Wed Nov 02, 2022 9:39 am
by fpiSTM
I don't know why you refers to mbed peripheralPins as we differs from it.
PB0_ALT0 is the same than PB0.
I've tested and it works as expected.

I've just made a mistake when told you D3 should work. As D3 is 3 then it is interpreted like A3.
Anyway all these 3 lines works:

Code: Select all

  int val = analogRead(8);
  int val = analogRead(A8);
  int val = analogRead(PB0);

Re: Analog input on PB0?

Posted: Wed Nov 02, 2022 5:56 pm
by jhitt50
Thanks again fpiSTM.

What file / documentation should I be referencing? I have been looking but haven't found it. Sorry if it's a dumb question, still learning my way around.

Re: Analog input on PB0?

Posted: Thu Nov 03, 2022 9:56 am
by fpiSTM

Re: Analog input on PB0?

Posted: Thu Jan 05, 2023 1:31 am
by hidearchery
Do I have it right that the PeripheralPins.c file for the Nucleo-L432KC also lists PB1 (D6) as an analog input?