I just picked up the Nucleo F303K8 dev board to test out the on board ADC found in the STM32 line and could use some help troubleshooting why I cannot get any valid reading from the pin.
The signal I am attempting to read is the output from a potentiometer connected to GND and VDD.
#include <mbed.h>
Serial pc(USBTX, USBRX);
DigitalOut led(LED1);
AnalogIn a1(A2);
int main() {
while(1) {
float val = a1.read();
pc.printf("val: %d \r\n", a1.read()); // this spits out random Unsigned decimal integer
if (val > 1) {
led.write(1);
} else {
led.write(0);
}
}
}
Now what I expect this program to do when run is output the a1.read() value to the console, as well as turn on/off the onboard led, but what actually happens is the led does NOT get lit up, and the serial output is just a bunch of random “Unsigned decimal integers” like so:
Hi Scott,
I checked your program with my F303K Mbed board and confirmed the output data.
I could reproduce problem.
Please modify your source code as follows.
ya so I think I misinterpretted what read() returns. Its actually a float between 0.0 and 1.0, so after updating my LED if statements I was able to toggle the LED on/off based on voltage being read, however I am still unable to see the float value in the console. I want to say it has something to do with this
just for info, code like this already exist in examples and it is working (tested) -Nucleo_read_analog_value
In comments of that code you can found also a description.