An Analog to Digital Converter (ADC) is a device that translates an analog voltage to a digital value that a microprocessor can understand. There are several channels available depending on module version and technology; however, these channels all work the same way.

The ConnectCore 8M Mini has several ADC interfaces to measure analog signals and transform them into digital values. In the ConnectCore 8M Mini Hardware Reference Manual you can find information about the available ADC channels.

Digi adds to Android an API to manage these ADC channels. You can configure them, read values, and program periodic sampling among other things. In the Digi APIx javadoc you can find a complete list of the available methods in this API.

Unless noted, all ADC API methods require the com.digi.android.permission.ADC permission.

If your application does not have the com.digi.android.permission.ADC permission it will not have access to any ADC service feature.

First, a new ADCManager object must be instantiated by passing the Android Application Context.

Instantiate the ADCManager
import android.app.Activity;
import android.os.Bundle;

import com.digi.android.adc.ADC;
import com.digi.android.adc.ADCManager;

public class ADCSampleActivity extends Activity {

    ADCManager adcManager;

    [...]

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Instantiate the ADC manager object.
        adcManager= new ADCManager(this);

        [...]
    }

    [...]
}

List ADC chips and channels

The ADCManager allows you to list the available ADCChip and channels in the system.

Get the ADCChip object list
import com.digi.android.adc.ADC;
import com.digi.android.adc.ADCChip;
import com.digi.android.adc.ADCManager;

[...]

ADCManager adcManager = ...;

// Get available ADCChip objects
List<ADCChip> chips = adcManager.getADCChips();

// Print available ADC chips:
for (ADCChip chip : chips)
	System.out.format("ADCChip: %s (driver %s), channels: %s",
			  chip.getName(), chip.getDriverName(),
			  chip.getAvailableChannels());

[...]

Instantiate an ADC channel

The ADCManager allows you to create an ADC object for a given ADCChip and ADC channel.

Create an ADC object
import com.digi.android.adc.ADC;
import com.digi.android.adc.ADCChip;
import com.digi.android.adc.ADCManager;

[...]

ADCManager adcManager = ...;

// Get available ADCChip objects
List<ADCChip> chips = adcManager.getADCChips();

// Use first ADC channel of the first ADCChip
ADCChip firstChip = chips.get(0);
int firstChannel = firstChip.getAvailableChannels().get(0);

// Create ADC object
ADC myAdc = adcManager.createADC(firstChip, firstChannel);

// Print ADC value
System.out.format("ADC current value: %d", adc.getValue());

[...]

Monitor ADC channels

You can monitor a specific ADC channel if you subscribe an IADCListener to the ADC object. Use the registerListener(IADCListener) method to register for particular ADC channel updates. The listener starts receiving ADC samples as soon as the startSampling(int) method is called specifying the interval between samples in milliseconds.

ADC channel updates registration
import com.digi.android.adc.ADC;
import com.digi.android.adc.ADCManager;

[...]

ADCManager adcManager = ...;
ADC adc = ...;

// Create the ADC listener.
MyADCListener myADCListener = ...;

// Register the ADC listener.
adc.registerListener(myADCListener);

// Start monitoring ADC channel every 5 seconds.
adc.startSampling(5000);

[...]

The registered listener class, MyADCListener, must implement the IADCListener interface. This interface defines the sampleReceived(ADCSample) method, that is called when a new ADC sample for the channel is received, in the example above every 5 seconds.

The sampleReceived(ADCSample) method receives a new ADCSample object with the value of the ADC channel (getValue()) and the time when the sample was taken (getTimestamp()).

IADCListener implementation example, MyADCListener
import com.digi.android.adc.IADCListener;

public class MyADCListener implements IADCListener {
    @Override
    public void sampleReceived(ADCSample sample) {
        // This code will be executed every time a new ADC sample is received.
        System.out.format("ADC value (%l): %d%n", sample.getTimestamp(), sample.getValue());
    }
}

You can have more than one IADCListener waiting for updates in the same channel. To stop the ADC channel notifications, use the stopSampling() method. If you no longer wish to receive ADC channel samples in a determined listener, use the unregisterListener(IADCListener) method to unsubscribe an already registered listener.

ADC updates unregistration
[...]

ADC adc = ...;
MyADCListener myADCListener = ...;
adc.registerListener(myADCListener);

adc.startSampling(5000);

[...]

// Stop ADC notifications.
adc.stopSampling();
// Remove the ADC listener.
adc.unregisterListener(myADCListener);

[...]

ADC example

The ADC Sample Application demonstrates the usage of the ADC API. In this example, you can select one of the ADC channels available in your ConnectCore 8M Mini and configure the sample rate of that interface.

You can import the example using Digi’s Android Studio plugin. For more information, see Import a Digi sample application. To look at the application source code, go to the GitHub repository.