This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Reading GPIO Analog Input on NRF52

I am trying to read an analog value on the GPIO port into my application. Looking through the examples, there aren't any that help solve this problem as the ADC example is only for the NRF51.

I am trying to read a sensor value connected to the board and I am utilizing the schematic shown here for my initial learning process: www.sparkfun.com/.../389

The 5V and GND are corrected accordingly and the analog input is connected to P0.03 (A0) on my board. I modified the "/examples/peripheral/bsp/" code to include GPIO functionality. First I added:

#include "nrf_gpio.h"
#include <inttypes.h>
#define PORT_NUM 3

Then I added:

static void gpio_init(void)
{
		nrf_gpio_cfg_input(PORT_NUM, NRF_GPIO_PIN_NOPULL);
}

In the "bsp_evt_handler" I added this to the bottom:

	uint32_t pin_value = 0;
	uint8_t port_value = 0;
	pin_value = nrf_gpio_pin_read(PORT_NUM);
	port_value = nrf_gpio_port_read(PORT_NUM);
	printf("pin value: %zu\n\r", pin_value);
	printf("port value: %zu\n\r", port_value);
	printf("port value: %" PRIu8 " \n\r", port_value);

And in the main function above uart_init(); I added:

gpio_init();

A full source code is at this pastebin: http://pastebin.com/zZXR0EU6

Whenever I am reading the UART, the value is always 0. No matter if I am pressing the sensor down or not. What could be the issue here?

  • You don't read analog voltages off the pins by using the nrf_gpio_pin_read() functions, they are used for reading digital 0 or 1 from digital inputs. And the nrf_gpio_port_read() does something completely different and unrelated and doesn't take a pin number as parameter.

    Reading analog values is an entirely different process which requires setting up the ADC, asking for a value, waiting for conversion etc. A 10 second grep in the SDK for 'saadc' shows the ble_app_proximity example uses ADC and has both nRF51 and nRF52 code in it. So perhaps you'd be better off taking a look at that.

Related