GPIO Analog Read

I am trying to do a very simple gpio_read from an analog input port but without any success.

I am using the b-l072z-lrwan1 board with a simple analog sensor on A2 pin.

gpio_t pin = GPIO_PIN(PORT_A, 4);
gpio_init(pin, GPIO_IN);

int reading = gpio_read(pin);

The value I get is either 16 (when the sensor is connected to the pin), or 0 (when the sensor is not connected). No other values.

As weird as it may sound, I cannot find any kind of example for reading sensor values using gpio read…

Thanks for the help.

gpio_read() will only read a digital value (LOW -> 0, HIGH -> any other value). What you are looking for is the ADC API.

Try this: https://github.com/RIOT-OS/RIOT/blob/master/tests/periph_adc/main.c

Thank you for the responses. At the end I also had to make change the definition of the board and declare the ADC configuration. Now it works.

At the end I also had to make change the definition of the board and declare the ADC configuration

Nice, do you mind opening a PR for that change ? I think it’s useful. Thanks!

I just made a PR. I hope it is useful. Thank you.

1 Like