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

SDK 1.6.1 Zephyr, How Read nRF52832 AIN7 and configure prj.conf and overlay file.

Hello, how do I configure the prj.conf file and the overlay file and how do I read the AIN7 analog input (P0.31) of the nRF52832. It is the first time that I use the micro to read an analog voltage. I did various research on DevZone, but I couldn't figure out how to do it. Thanks in advance.

Parents
  • Hi!

    I'm having some trouble flashing the application to the board to actually test this, but I will provide the basics here to start with. 

    In the prj.conf file, you will need to enable CONFIG_ADC=y.

    Then we need an overlay file to enable the adc and to create a node and set the correct io-channel:

    &adc {
    	status = "okay";
    };
    
    
    / {
    	n: node {
    		io-channels = <&adc 31>;
    		io-channel-names = "AIN7";
    	};
    };

    The main will look something like below. 

    /*
     * Copyright (c) 2020 Nordic Semiconuctor ASA
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <stdio.h>
    #include <zephyr.h>
    #include <sys/printk.h>
    #include <drivers/adc.h>
    
    
    #define ADC_RESOLUTION			12
    #define ADC_GAIN				ADC_GAIN_1
    #define ADC_REFERENCE			ADC_REF_INTERNAL
    #define ADC_ACQUISITION_TIME	ADC_ACQ_TIME_DEFAULT
    
    #define ADC_CHANNEL_ID		7
    
    static uint16_t adc_raw;
    static const struct device * dev_adc;
    
    struct adc_channel_cfg channel_cfg = {
    	.gain 				= ADC_GAIN,
    	.reference 			= ADC_REFERENCE,
    	.acquisition_time 	= ADC_ACQUISITION_TIME,
    	.differential 		= 0,
    	.channel_id			= ADC_CHANNEL_ID,
    	.input_positive 	= SAADC_CH_PSELP_PSELP_AnalogInput7,
    };
    
    
    void main(void)
    {
    	int err;
    	dev_adc = device_get_binding("ADC_0");
    	if (!device_is_ready(dev_adc)) {
    		printk("ADC device not found\n");
    		return;
    	}
    	err = adc_channel_setup(dev_adc, &channel_cfg);
    	if (err) {
    		printk("adc_channel_setup() failed with error: %d\n", err);
    	}
    	struct adc_sequence sequence = {
    		.channels    = BIT(channel_cfg.channel_id),
    		.buffer      = &adc_raw,
    		.buffer_size = sizeof(adc_raw),
    		.resolution  = ADC_RESOLUTION,
    	};
    
    	while (true) {
    		err = adc_read(dev_adc, &sequence);
    		if (err != 0) {
    			printk("ADC reading failed with error %d.\n", err);
    			return;
    		}
    		printk("ADC reading: %d\n", (int16_t)adc_raw);
    		k_sleep(K_MSEC(1000));
    	}
    }

  • Great! I was able to test it now, and it seems to work. I modified the main.c just now, so try that one instead.

Reply Children
Related