Reading Analog Data in Zephyr with a nrf52840

Mark Zachmann
Oct 28, 2019 · 1 min read

It turned out to be much harder to read the ADC in Zephyr than I expected. As usual the documentation is slim and the syntax was a little strange partly because of the way the nordic nrf52 devices do analog input.

What’s key to know is that there’s one ADC (run as a nrf,saadc — successive approximation ADC) and it has 8 channels, corresponding to the 8 possible analog input pins (AIN0…AIN7). To do an analog read you specify which analog channel to use (out of the 8) and that defines which physical pin is used.

I added the following to prj.conf in the project to enable the adc sections during compiles,


and, i enabled it on the board by adding this to myboard.dtsi:

&adc { /* ADC */
status = "okay";

Which enables the adc module.

Source Code

Working C code is below

Home Wireless

Home automation in the wireless IOT era

Mark Zachmann

Written by

Entrepreneur, software architect, electrical engineer. Ex-academic.

Home Wireless

Home automation in the wireless IOT era

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade