ADC with NRF24LE1
We have studied about the first program on the NRF module which was very simple and we have been successful in blinking the LED.
Now, if we wish to make anything with NRF, we must know how to take analog inputs as most of the sensor is analog. In this article, we will see how to use the inbuilt ADC of NRF and how we can use any analog sensor and convert its value into digital
Fig. 1: Prototype of NRF24LE1 Interfacing with ADC
Some important specs about inbuilt ADC of NRF24LE1 are :
• 6, 8, 10 or 12 bit resolution – It means we have option to choose 6-bit, 8-bit, 10-bit or 12-bit ADC.
• 14 input channels – 14 input pins for input analog signal
• Single ended or differential input- Single line input or Two line input
• Full-scale range set by internal reference, external reference or VDD – To set the maximum analog voltage to be sensed.
• Single step mode – Convert the signal and then stop
• Continuous mode with 2, 4, 8 or 16 kbps sampling rate – Continuously converts the signal unless interrupted.
• Low current consumption; only 0.1mA at 2 kbps
Fig. 2: Image showing interfacing of NRF24LE1 with ADC
These are the specifications we have extracted from the datasheet. Now to understand the code, we have taken an excerpt from the example codes provided by Nordic. Let’s understand the main part of code line by line.
Functions provided by Nordic library
FUNCTION NAME | INPUT PARAMETER | OUTPUT | DESCRIPTION |
---|---|---|---|
hal_adc_set_input_channel() | AIN0 – AIN13 | – | Set the input channel |
hal_adc_set_reference() | INT/VDD/AIN3/AIN9 | – |
Set the reference voltage INT – Internal 1.22V reference VDD – 3.3V reference AIN3/AIN9 – external reference |
hal_adc_set_input_mode() |
SINGLE/DIFF_AIN2/ DIFF_AIN6 |
– |
Set the type of input SINGLE – Single ended input DIFF_AIN2 – Differential input with AIN2 as inverting input DIFF_AIN6 – Differential input with AIN6 as inverting input |
hal_adc_set_conversion_mode() | SINGLE_STEP/CONTINOUS | – |
Set the conversion mode SINGLE_STEP – Single step CONTINOUS – Continuous |
hal_adc_set_resolution() |
RES_6BIT/ RES_8BIT/ RES_10BIT/ RES_12BIT |
– |
Set ADC resolution RES_6BIT – 6 bit resolution RES_8BIT – 8 bit resolution RES_10BIT – 10 bit resolution RES_12BIT – 12 bit resolution |
hal_adc_set_data_just() | JUST_LEFT/JUST_RIGHT | – |
Justify the position of output data JUST_LEFT – Left position JUST_Right – Right position |
hal_adc_start() | – | – | Start ADC conversion |
hal_adc_busy() | – | 0/1 |
Check status of conversion 0 – Not busy 1 – Busy |
hal_adc_read_LSB() | – | Byte | Read the LSB of the converted data |
hal_adc_read_MSB() | – | Byte | Read the MSB of the converted data |
// Configure ADC
hal_adc_set_input_channel (HAL_ADC_INP_AIN0) ;
Here we need to assign which input pin we are setting for the analog input. As we are using P0.0 so we have set that by HAL_ADC_INP_AIN0
hal_adc_set_reference (HAL_ADC_REF_VDD) ;
The ADC reference voltage is kept same as the VDD (Supply Voltage), which is 3.3V
hal_adc_set_input_mode (HAL_ADC_SINGLE) ;
As we are using single input, so it is set at HAL_ADC_SINGLE.
hal_adc_set_conversion_mode (HAL_ADC_SINGLE_STEP) ;
While in ADC conversion, we can set the conversion mode. Here we have set it as single step
hal_adc_set_resolution (HAL_ADC_RES_8BIT) ;
We can also set the resolution before the controller starts the ADC operations. We are setting it to 8 bit
hal_adc_set_data_just (HAL_ADC_JUST_RIGHT) ;
We can adjust the data right or left in ADC operation. We are adjusting it to the right
Please find below the main loop where ADC operation is taking place
Project Source Code
###
//Program to
/* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
** The information contained herein is confidential property of Nordic* Semiconductor ASA.Terms and conditions of usage are described in detail* in NORDIC SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.** Licensees are granted free, non-transferable use of the information. NO* WARRENTY of ANY KIND is provided. This heading must NOT be removed from* the file.** $LastChangedRevision: 133 $*//** @file* @brief ADC continuous mode example* @defgroup adc_cont_example ADC continuous mode example* @{* @ingroup nrf_examples** @brief This example samples P00 using ADC continuous mode a rate of 2 Ksps* and outputs the 8 bit sample value on P1.**///lint -e716//lint -e714#include "nrf24le1.h"#include "hal_adc.h"#includevoid delay(unsigned int us){while(us--){_nop_();}}void main(){// Set P1 as outputP1DIR = 0;P10 = 0;// Configure ADChal_adc_set_input_channel(HAL_ADC_INP_AIN0);hal_adc_set_reference(HAL_ADC_REF_VDD);hal_adc_set_input_mode(HAL_ADC_SINGLE);hal_adc_set_conversion_mode(HAL_ADC_CONTINOUS);hal_adc_set_sampling_rate(HAL_ADC_2KSPS);hal_adc_set_resolution(HAL_ADC_RES_8BIT);hal_adc_set_data_just(HAL_ADC_JUST_RIGHT);// Enable MISC interrupts to enable ADC interruptMISC = 1;// Enable global interruptsEA = 1;// Start ADC conversionhal_adc_start();for(;;){}}// ADC (MISC) interruptADC_ISR(){int val = 0;val = hal_adc_read_LSB();P10 = 1;delay(1000);delay(val * 4);P10 = 0;delay(19000 - val * 4);}/** @} */###
Circuit Diagrams
Project Video
Filed Under: Tutorials
Filed Under: Tutorials
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.