Analog input on PB0?

Post here first, or if you can't find a relevant section!
Post Reply
jhitt50
Posts: 3
Joined: Mon Oct 31, 2022 9:39 pm

Analog input on PB0?

Post 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!
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Analog input on PB0?

Post 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.
jhitt50
Posts: 3
Joined: Mon Oct 31, 2022 9:39 pm

Re: Analog input on PB0?

Post 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.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Analog input on PB0?

Post 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);
jhitt50
Posts: 3
Joined: Mon Oct 31, 2022 9:39 pm

Re: Analog input on PB0?

Post 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.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Analog input on PB0?

Post by fpiSTM »

hidearchery
Posts: 1
Joined: Thu Jan 05, 2023 1:27 am

Re: Analog input on PB0?

Post by hidearchery »

Do I have it right that the PeripheralPins.c file for the Nucleo-L432KC also lists PB1 (D6) as an analog input?
Post Reply

Return to “General discussion”