Using ADCs on Azure Sphere
This topic describes how to use analog-to-digital converters (ADCs) in a high-level application. See Use peripherals in a real-time capable application for information about ADC use in RTApps.
Azure Sphere supports analog to digital conversion. An ADC converts an analog input to a corresponding digital value. The number of input channels and the resolution (as number of ADC output bits) are device dependent.
The ADC_HighLevelApp sample demonstrates how to access ADCs on an MT3620 device.
The MT3620 contains a 12-bit ADC with 8 input channels. The ADC compares an input voltage to a reference voltage and produces a value between 0 and 4095 as its output. The ADC input channels and the GPIO pins GPIO41 through GPIO48 map to the same pins on the MT3260. However, if your application uses the ADC then all 8 pins are allocated for use as ADC inputs. None of them can be used for GPIO.
Applications that communicate with ADCs must include the adc.h header file and add ADC settings to the application manifest.
All applications must set their target hardware and include the corresponding hardware definition header file.
#include <applibs/adc.h> #include "path-to-your-target-hardware.h"
Replace "path-to-your-target-hardware.h" with the path to the header file for your hardware.
Application manifest settings
The ADC setting in the application manifest lists the ADC controllers that are accessed by the application. To configure these settings, add the
Adc capability to the application manifest, and then add each ADC controller to the capability. The Azure Sphere application manifest topic has more details.
In your code, use the constants that are defined for your hardware to identify the ADC controllers. The compiler will translate these values to raw values when you build the app.
For example, here's an excerpt from an application manifest for an application that targets an MT3620 reference development board (RDB).
"Adc": [ "$MT3620_RDB_ADC_CONTROLLER0" ]
The following excerpt shows how to specify the same ADC controller in an application that targets the Avnet MT3620 Starter Kit:
"Adc": [ "$AVNET_MT3620_SK_ADC_CONTROLLER0" ]
NOTE: Currently, on the MT3620 you can only access ADC controller 0.
Azure Sphere high-level applications can access ADCs by calling Applibs ADC APIs.
Open an ADC controller
To open an ADC controller for access, call ADC_Open and pass the ID of the controller as a parameter. A file descriptor will be returned if the call is successful. Otherwise, an error value will be returned.
int ADC_Open(ADC_ControllerId id);
Read from an ADC
To read from the ADC, call ADC_Poll. You pass the following parameters to ADC_Poll: the file descriptor returned by ADC_Open, the ADC channel, and a pointer to where the ADC value will be stored.
To get the number of valid bits (resolution) returned by the ADC_Poll function, call ADC_GetSampleBitCount.
Set the ADC reference voltage
Call ADC_SetReferenceVoltage to set the ADC reference voltage.
The MT3620 has an on-chip, 2.5 volt reference. Alternatively, it can be connected to an external voltage reference that is less than or equal to 2.5 volts.