German English
Peripherals

Sample analog values using an ADC

The microcontroller on the radio module Preon32 provides some ADCs (Analog to Digital Converters). After a configuration of these ADC devices analog values can be sampled. The analog input signal must be connected to the right GPIO pin to work correctly.

This examples chooses one channel from an ADC instance and uses this channel to sample 100 values with a time delay of 50 milliseconds. The sampled values are then normalized to a voltage unit which can be computed with respect to the resolution of 12 bit (4096) and the voltage range 0V to 3.3V.
/*
 * Copyright (c) 2011., Virtenio GmbH
 * All rights reserved.
 */
package com.virtenio.webdemo;

import com.virtenio.drivers.adc.NativeADC;
import com.virtenio.drivers.adc.NativeADCChannel;

import java.io.IOException;

public class ADC {

    public static void main() throws IOException, InterruptedException {

        int idADC = 0;
        int idCH = 0;
        long millis = 50;
        int num = 100;

        double[] samples = new double[num];

        NativeADC adc0 = NativeADC.getInstance(idADC);
        NativeADCChannel ch0 = adc0.getChannel(idCH);
        ch0.open();

        for (int i = 0; i < num; i++) {
            //
            int val = ch0.getValue();
            double sample = val * 3.3 / 4096.0;
            samples[i] = sample;
            //
            System.out.println("CH[" + idCH + "]=" + sample + " [V]");
            Thread.sleep(millis);
        }

        ch0.close();
        adc0.close();
    }

}