I want to read around 6 analog pins, not necessarily simultaneously. I could afford some delay in between them. i am using nrf52832 dev kit with sdk 15. How do i modify the SAADC example to achieve the same because the example only reads 1 channel. i did found this thread https://devzone.nordicsemi.com/f/nordic-q-a/5561/adc-from-various-pins/19481#19481 which looks similar but i was unable to open the GitHub link provided in the thread. Can you please direct me to the relevant examples.
It should be fine to measure 6 ADC channels at once.
Attached is a slightly modified project from the SDK15.0.0\examples\peripheral\saadc example. Only the main.c file is changed. It will measure the 6 pins AIN0 - AIN5, and print the values in the UART log.
Remember that AIN0 is not the same as A0 printed on the PCB. The AINx pins are:
AIN0 = P0.02AIN1 = P0.03AIN2 = P0.04AIN3 = P0.05AIN4 = P0.28AIN5 = P0.29
Hello Edvin, that was a great help. I was wondering what would i have to do to take the readings from the other 2 analog pins? it does not have to be at same time. could be after a delay, but how do i take those readings as well?
In the SAADC example you can see two functions which sets up and enables the saadc callback:
saadc_sampling_event_init() and saadc_sampling_event_enable().
What they do is that they set up a timer and a PPI, and connect the two. This means that the timer interrupt is not called in the application, but when the timeout occurs, the chip will automatically call nrfx_saadc_sample().
When this function is called N times, where N is the size of your SAADC buffer, that is N = SAMPLES_IN_BUFFER, you will get the callback function.
I don't know whether you are familiar with the PPI, but it is a module which, when enabled, can perform tasks without involving the CPU. In this case, it works a bit like an advanced timer.
I really recommend that you use this implementation to trigger the samples. It will not work to call nrfx_saadc_sample() N times in a row in the application, because the sample has to finish before calling nrfx_saadc_sample again.
Regarding the conversion from the raw SAADC value to voltage, there are a few things to take into consideration:
Your resolution (by default set to 10 bits, your gain, usually set to 1/6.
Please see this scheeme:
From the link:
RESULT = [V(P) – V(N) ] * GAIN/REFERENCE * 2(RESOLUTION - m)Which means that V(P) - V(N) = RESULT/ (GAIN/REFERENCE * 2(RESOLUTION - m))The result is linear, so you can also do some measurements to known voltages (using a voltmeter), and map the result to voltage using this.Best regards,Edvin
V(p) : voltage at input p
V(n): voltage at input n, I do not exactly understand this. What are N and P? reference voltage and input voltage?
Thanks and Regards,
This is the positive and negative inputs. I don't know exactly where you found this, but it is possible to use a different reference voltage than GND. The default setup is to use pin_n as ground (NRF_SAADC_INPUT_DISABLE).
If you want to measure the voltage between two pins, where none of them are GND, you can use pin_n = another pin, and the result will be the difference between the pins.
I was referring this post in the datasheet. In my case I only want to measure the voltage from the interfaced sensor on the analog pins. So using this formulae: my Vp would be the voltage on the pin from the sensors and Vn would be 0 if I use reference voltage as 0? does it sound right?
Yes. That is correct. If you set Vn = NRF_SAADC_INPUT_DISABLE (the default setting) it will measure the voltage difference between GND (0) and Vp.
How can i convert my adc values into float?
I do not see any result when i try to cast these values into float.
Don't see any result, or do you see some results, but not the ones you are looking for? (Does the application stop, or does it print blank values?)
Please see this post.
As described, you must use NRF_LOG_FLOAT_MARKER instead of %f when using NRF_LOG to print float values.
Thanks for the answer. It certainly worked for NRF LOG but how can i see the result in the debug terminal window? In the attached image you can see that i am getting float result in putty terminal but the output of printf() function displays nothing in the debug terminal window.
Also, when i am connecting the analog pin to VDD, its giving me 2.87V. should not I be getting 3.3V by connecting to VDD ?
I see that printf isn't defined by default in the saadc example, so I haven't tested, but have you tested the exact same approach in printf? Just swap NRF_LOG_INFO with printf.
printf("My float number " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(R));