- I am looking to learn and run a simple example of ADC where i initialize pin A0 and attach a potentiometer and read values from it using the nrf5340 DK board.
- is there a simple example that i can use that initializes PIN A0 and just reads a potentiometer connected to it?
- I have used the following post1 and tutorial but i keep getting the following error when trying to build a configuration file using VSCODE.
- It seems the code for ADC found on most of the post are using older SDK or using the PDK version of the board.
main.c code
#include <stdio.h>
#include <string.h>
#include <drivers/uart.h>
#include <drivers/adc.h>
#include <zephyr.h>
struct device *adc_dev;
#include <hal/nrf_saadc.h>
#define ADC_DEVICE_NAME DT_ADC_0_NAME
#define ADC_RESOLUTION 10
#define ADC_GAIN ADC_GAIN_1_6
#define ADC_REFERENCE ADC_REF_INTERNAL
#define ADC_ACQUISITION_TIME ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10)
#define ADC_1ST_CHANNEL_ID 0
#define ADC_1ST_CHANNEL_INPUT NRF_SAADC_INPUT_AIN0
#define ADC_2ND_CHANNEL_ID 2
#define ADC_2ND_CHANNEL_INPUT NRF_SAADC_INPUT_AIN2
static const struct adc_channel_cfg m_1st_channel_cfg = {
.gain = ADC_GAIN,
.reference = ADC_REFERENCE,
.acquisition_time = ADC_ACQUISITION_TIME,
.channel_id = ADC_1ST_CHANNEL_ID,
#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS)
.input_positive = ADC_1ST_CHANNEL_INPUT,
#endif
};
#define BUFFER_SIZE 1
static int16_t m_sample_buffer[BUFFER_SIZE];
static int adc_sample(void)
{
int ret;
const struct adc_sequence sequence = {
.channels = BIT(ADC_1ST_CHANNEL_ID),
.buffer = m_sample_buffer,
.buffer_size = sizeof(m_sample_buffer),
.resolution = ADC_RESOLUTION,
};
if (!adc_dev) {
return -1;
}
ret = adc_read(adc_dev, &sequence);
printk("ADC read err: %d\n", ret);
/* Print the AIN0 values */
for (int i = 0; i < BUFFER_SIZE; i++) {
float adc_voltage = 0;
adc_voltage = (float)(((float)m_sample_buffer[i] / 1023.0f) *
3600.0f);
printk("ADC raw value: %d\n", m_sample_buffer[i]);
printf("Measured voltage: %f mV\n", adc_voltage);
}
return ret;
}
int main(void)
{
int err;
printk("nrf91 saadc sampling AIN0 (P0.13)\n");
printk("Example requires secure_boot to have ");
printk("SAADC set to non-secure!\n");
printk("If not; BusFault/UsageFault will be triggered\n");
adc_dev = device_get_binding("ADC_0");
if (!adc_dev) {
printk("device_get_binding ADC_0 failed\n");
while(1);
}
err = adc_channel_setup(adc_dev, &m_1st_channel_cfg);
if (err) {
printk("Error in adc setup: %d\n", err);
while(1);
}
/* Trigger offset calibration
* As this generates a _DONE and _RESULT event
* the first result will be incorrect.
*/
NRF_SAADC->TASKS_CALIBRATEOFFSET = 1;
while (1) {
err = adc_sample();
if (err) {
printk("Error in adc sampling: %d\n", err);
}
k_sleep(K_MSEC(500));
}
}
CMakelists.txt file
#
# Copyright (c) 2020 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
#
cmake_minimum_required(VERSION 3.8.2)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(empty_app_core)
target_sources(app PRIVATE src/main.c)
nrf5340pdk_nrf5340_cpuapp.overlay
&adc {
status = "okay";
};
&spi1 {
compatible = "nordic,nrf-spim";
status = "okay";
sck-pin = <10>;
mosi-pin = <11>;
miso-pin = <12>;
};
prj.conf file
CONFIG_BOARD_ENABLE_CPUNET=y CONFIG_ADC=y CONFIG_MAIN_STACK_SIZE=4096 CONFIG_LOG=y CONFIG_LOG_MINIMAL=y CONFIG_CBPRINTF_FP_SUPPORT=y
Error shown in Terminal :
* Executing task: nRF Connect: Generate config nrf5340dk_nrf5340_cpuapp for c:\nrf5340_projects\adc_sample Building adc_sample west build --build-dir c:\nrf5340_projects\adc_sample\build c:\nrf5340_projects\adc_sample --pristine --board nrf5340dk_nrf5340_cpuapp -- -DNCS_TOOLCHAIN_VERSION:STRING="NONE" -DCONFIG_DEBUG_OPTIMIZATIONS=y -DCONFIG_DEBUG_THREAD_INFO=y -DBOARD_ROOT:STRING="c:/nrf5340_projects/blinky_tutorial" -- west build: generating a build system Loading Zephyr default modules (Zephyr base). -- Application: C:/nrf5340_projects/adc_sample -- Found Python3: C:/ncs/toolchains/v2.2.0/opt/bin/python.exe (found suitable exact version "3.8.2") found components: Interpreter -- Cache files will be written to: C:/ncs/v2.2.0/zephyr/.cache -- Zephyr version: 3.2.99 (C:/ncs/v2.2.0/zephyr) -- Found west (found suitable version "0.14.0", minimum required is "0.7.1") -- Board: nrf5340dk_nrf5340_cpuapp -- Found host-tools: zephyr 0.15.1 (C:/ncs/toolchains/v2.2.0/opt/zephyr-sdk) -- Found toolchain: zephyr 0.15.1 (C:/ncs/toolchains/v2.2.0/opt/zephyr-sdk) -- Found Dtc: C:/ncs/toolchains/v2.2.0/opt/bin/dtc.exe (found suitable version "1.4.7", minimum required is "1.4.6") -- Found BOARD.dts: C:/ncs/v2.2.0/zephyr/boards/arm/nrf5340dk_nrf5340/nrf5340dk_nrf5340_cpuapp.dts -- Generated zephyr.dts: C:/nrf5340_projects/adc_sample/build/zephyr/zephyr.dts -- Generated devicetree_generated.h: C:/nrf5340_projects/adc_sample/build/zephyr/include/generated/devicetree_generated.h -- Including generated dts.cmake file: C:/nrf5340_projects/adc_sample/build/zephyr/dts.cmake Parsing C:/ncs/v2.2.0/zephyr/Kconfig Loaded configuration 'C:/ncs/v2.2.0/zephyr/boards/arm/nrf5340dk_nrf5340/nrf5340dk_nrf5340_cpuapp_defconfig' Merged configuration 'C:/nrf5340_projects/adc_sample/prj.conf' Merged configuration 'C:/nrf5340_projects/adc_sample/build/zephyr/misc/generated/extra_kconfig_options.conf' C:/nrf5340_projects/adc_sample/prj.conf:10: warning: attempt to assign the value 'y' to the undefined symbol LOG_MINIMAL error: Aborting due to Kconfig warnings CMake Error at C:/ncs/v2.2.0/zephyr/cmake/modules/kconfig.cmake:328 (message): command failed with return code: 1 Call Stack (most recent call first): C:/ncs/v2.2.0/nrf/cmake/modules/kconfig.cmake:29 (include) C:/ncs/v2.2.0/zephyr/cmake/modules/zephyr_default.cmake:108 (include) C:/ncs/v2.2.0/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:66 (include) C:/ncs/v2.2.0/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:92 (include_boilerplate) CMakeLists.txt:9 (find_package) -- Configuring incomplete, errors occurred! FATAL ERROR: command exited with status 1: 'c:\ncs\toolchains\v2.2.0\opt\bin\cmake.EXE' '-DWEST_PYTHON=c:\ncs\toolchains\v2.2.0\opt\bin\python.exe' '-Bc:\nrf5340_projects\adc_sample\build' -GNinja -DBOARD=nrf5340dk_nrf5340_cpuapp -DNCS_TOOLCHAIN_VERSION:STRING=NONE -DCONFIG_DEBUG_OPTIMIZATIONS=y -DCONFIG_DEBUG_THREAD_INFO=y -DBOARD_ROOT:STRING=c:/nrf5340_projects/blinky_tutorial '-Sc:\nrf5340_projects\adc_sample' * The terminal process terminated with exit code: 1. * Terminal will be reused by tasks, press any key to close it. * Executing task: nRF Connect: Build [pristine]: adc_sample/build (active) Building adc_sample west build --build-dir c:\nrf5340_projects\adc_sample\build c:\nrf5340_projects\adc_sample --pristine --board nrf5340dk_nrf5340_cpuapp -- -DNCS_TOOLCHAIN_VERSION:STRING="NONE" -DBOARD_ROOT:STRING="c:/nrf5340_projects/blinky_tutorial" -DCONFIG_DEBUG_OPTIMIZATIONS:STRING="y" -DCONFIG_DEBUG_THREAD_INFO:STRING="y" -DCONF_FILE:STRING="c:/nrf5340_projects/adc_sample/prj.conf" -- west build: generating a build system Loading Zephyr default modules (Zephyr base). -- Application: C:/nrf5340_projects/adc_sample -- Found Python3: C:/ncs/toolchains/v2.2.0/opt/bin/python.exe (found suitable exact version "3.8.2") found components: Interpreter -- Cache files will be written to: C:/ncs/v2.2.0/zephyr/.cache -- Zephyr version: 3.2.99 (C:/ncs/v2.2.0/zephyr) -- Found west (found suitable version "0.14.0", minimum required is "0.7.1") -- Board: nrf5340dk_nrf5340_cpuapp -- Found host-tools: zephyr 0.15.1 (C:/ncs/toolchains/v2.2.0/opt/zephyr-sdk) -- Found toolchain: zephyr 0.15.1 (C:/ncs/toolchains/v2.2.0/opt/zephyr-sdk) -- Found Dtc: C:/ncs/toolchains/v2.2.0/opt/bin/dtc.exe (found suitable version "1.4.7", minimum required is "1.4.6") -- Found BOARD.dts: C:/ncs/v2.2.0/zephyr/boards/arm/nrf5340dk_nrf5340/nrf5340dk_nrf5340_cpuapp.dts -- Generated zephyr.dts: C:/nrf5340_projects/adc_sample/build/zephyr/zephyr.dts -- Generated devicetree_generated.h: C:/nrf5340_projects/adc_sample/build/zephyr/include/generated/devicetree_generated.h -- Including generated dts.cmake file: C:/nrf5340_projects/adc_sample/build/zephyr/dts.cmake c:/nrf5340_projects/adc_sample/prj.conf:5: warning: attempt to assign the value 'y' to the undefined symbol LOG_MINIMAL error: Aborting due to Kconfig warnings Parsing C:/ncs/v2.2.0/zephyr/Kconfig Loaded configuration 'C:/ncs/v2.2.0/zephyr/boards/arm/nrf5340dk_nrf5340/nrf5340dk_nrf5340_cpuapp_defconfig' Merged configuration 'c:/nrf5340_projects/adc_sample/prj.conf' Merged configuration 'C:/nrf5340_projects/adc_sample/build/zephyr/misc/generated/extra_kconfig_options.conf' CMake Error at C:/ncs/v2.2.0/zephyr/cmake/modules/kconfig.cmake:328 (message): command failed with return code: 1 Call Stack (most recent call first): C:/ncs/v2.2.0/nrf/cmake/modules/kconfig.cmake:29 (include) C:/ncs/v2.2.0/zephyr/cmake/modules/zephyr_default.cmake:108 (include) C:/ncs/v2.2.0/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:66 (include) C:/ncs/v2.2.0/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:92 (include_boilerplate) CMakeLists.txt:9 (find_package) -- Configuring incomplete, errors occurred! FATAL ERROR: command exited with status 1: 'c:\ncs\toolchains\v2.2.0\opt\bin\cmake.EXE' '-DWEST_PYTHON=c:\ncs\toolchains\v2.2.0\opt\bin\python.exe' '-Bc:\nrf5340_projects\adc_sample\build' -GNinja -DBOARD=nrf5340dk_nrf5340_cpuapp -DNCS_TOOLCHAIN_VERSION:STRING=NONE -DBOARD_ROOT:STRING=c:/nrf5340_projects/blinky_tutorial -DCONFIG_DEBUG_OPTIMIZATIONS:STRING=y -DCONFIG_DEBUG_THREAD_INFO:STRING=y -DCONF_FILE:STRING=c:/nrf5340_projects/adc_sample/prj.conf '-Sc:\nrf5340_projects\adc_sample' * The terminal process terminated with exit code: 1. * Terminal will be reused by tasks, press any key to close it. * Executing task: nRF Connect: Build [pristine]: adc_sample/build (active) Building adc_sample west build --build-dir c:\nrf5340_projects\adc_sample\build c:\nrf5340_projects\adc_sample --pristine --board nrf5340dk_nrf5340_cpuapp -- -DNCS_TOOLCHAIN_VERSION:STRING="NONE" -DBOARD_ROOT:STRING="c:/nrf5340_projects/blinky_tutorial" -DCONFIG_DEBUG_OPTIMIZATIONS:STRING="y" -DCONFIG_DEBUG_THREAD_INFO:STRING="y" -DCONF_FILE:STRING="c:/nrf5340_projects/adc_sample/prj.conf" -- west build: generating a build system Loading Zephyr default modules (Zephyr base). -- Application: C:/nrf5340_projects/adc_sample -- Found Python3: C:/ncs/toolchains/v2.2.0/opt/bin/python.exe (found suitable exact version "3.8.2") found components: Interpreter -- Cache files will be written to: C:/ncs/v2.2.0/zephyr/.cache -- Zephyr version: 3.2.99 (C:/ncs/v2.2.0/zephyr) -- Found west (found suitable version "0.14.0", minimum required is "0.7.1") -- Board: nrf5340dk_nrf5340_cpuapp -- Found host-tools: zephyr 0.15.1 (C:/ncs/toolchains/v2.2.0/opt/zephyr-sdk) -- Found toolchain: zephyr 0.15.1 (C:/ncs/toolchains/v2.2.0/opt/zephyr-sdk) -- Found Dtc: C:/ncs/toolchains/v2.2.0/opt/bin/dtc.exe (found suitable version "1.4.7", minimum required is "1.4.6") -- Found BOARD.dts: C:/ncs/v2.2.0/zephyr/boards/arm/nrf5340dk_nrf5340/nrf5340dk_nrf5340_cpuapp.dts -- Generated zephyr.dts: C:/nrf5340_projects/adc_sample/build/zephyr/zephyr.dts -- Generated devicetree_generated.h: C:/nrf5340_projects/adc_sample/build/zephyr/include/generated/devicetree_generated.h -- Including generated dts.cmake file: C:/nrf5340_projects/adc_sample/build/zephyr/dts.cmake Parsing C:/ncs/v2.2.0/zephyr/Kconfig Loaded configuration 'C:/ncs/v2.2.0/zephyr/boards/arm/nrf5340dk_nrf5340/nrf5340dk_nrf5340_cpuapp_defconfig' Merged configuration 'c:/nrf5340_projects/adc_sample/prj.conf' Merged configuration 'C:/nrf5340_projects/adc_sample/build/zephyr/misc/generated/extra_kconfig_options.conf' c:/nrf5340_projects/adc_sample/prj.conf:5: warning: attempt to assign the value 'y' to the undefined symbol LOG_MINIMAL error: Aborting due to Kconfig warnings CMake Error at C:/ncs/v2.2.0/zephyr/cmake/modules/kconfig.cmake:328 (message): command failed with return code: 1 Call Stack (most recent call first): C:/ncs/v2.2.0/nrf/cmake/modules/kconfig.cmake:29 (include) C:/ncs/v2.2.0/zephyr/cmake/modules/zephyr_default.cmake:108 (include) C:/ncs/v2.2.0/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:66 (include) C:/ncs/v2.2.0/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:92 (include_boilerplate) CMakeLists.txt:9 (find_package) -- Configuring incomplete, errors occurred! FATAL ERROR: command exited with status 1: 'c:\ncs\toolchains\v2.2.0\opt\bin\cmake.EXE' '-DWEST_PYTHON=c:\ncs\toolchains\v2.2.0\opt\bin\python.exe' '-Bc:\nrf5340_projects\adc_sample\build' -GNinja -DBOARD=nrf5340dk_nrf5340_cpuapp -DNCS_TOOLCHAIN_VERSION:STRING=NONE -DBOARD_ROOT:STRING=c:/nrf5340_projects/blinky_tutorial -DCONFIG_DEBUG_OPTIMIZATIONS:STRING=y -DCONFIG_DEBUG_THREAD_INFO:STRING=y -DCONF_FILE:STRING=c:/nrf5340_projects/adc_sample/prj.conf '-Sc:\nrf5340_projects\adc_sample' * The terminal process terminated with exit code: 1. * Terminal will be reused by tasks, press any key to close it.