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
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
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|
Set the reference voltage
INT – Internal 1.22V reference
VDD – 3.3V reference
AIN3/AIN9 – external reference
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
Set the conversion mode
SINGLE_STEP – Single step
CONTINOUS - Continuous
Set ADC resolution
RES_6BIT – 6 bit resolution
RES_8BIT – 8 bit resolution
RES_10BIT – 10 bit resolution
RES_12BIT – 12 bit resolution
Justify the position of output data
JUST_LEFT – Left position
JUST_Right – Right position
|hal_adc_start()||-||-||Start ADC conversion|
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
hal_adc_start (); // Start the ADC conversion
while( hal_adc_busy() ) // Wait for the ADC to finish a conversion
P1 = hal_adc_read_LSB(); // Write the ADC result to P1
We have demonstrated the experiment using a video too. Here we have given the analog voltage between 0 and 3.3V using potentiometer.
We have attached Led’s at P1 port. The numbers of led’s that are ON will vary according to the input analog voltage given at the Analog pin
You can compile the code provided here using Keil software and burn the NRF using the burner to see the experiment by yourself.
Stay tuned with us while we are writing more for you.
Have a good day!!