InputAnalog Method

Public Function InputAnalog(ByVal channel As Long) As Long

Summary

This method retrieves an analog reading from one of the analog channels on the device. The analog readings are only meaningful when the ADC has been enabled (see the AnalogState Property). The analog readings are 10-bit values. See below for further explanation of their meaning.

Parameters

  • channel - Specifies the logical analog channel (0-7) to read. Note that each logical analog channel may be arbitrarily assigned to physical channels using the AnalogAssignment Property.

Return Value

This method returns a Long. The return value is the reading from the specified channel.

Remarks

The reading that is obtained with this method is a 10-bit value (range of 0-1023) representing the voltage level relative to the analog reference voltage. The exact interpretation depends on whether a single-ended or differential channel has been selected (see the AnalogAssignment Property).

For single-ended channels, the reading is:

(analog reading) = (channel voltage * 1024) / (voltage reference)

For example, a reading of 0 means 0V and a reading of 1023 means a voltage just under the voltage reference (assuming internal 5V reference, about 4.99V). Once you have the analog reading, you can calculate the input voltage that produced it by calculating:

voltage = (analog reading)/1024 * (voltage reference)

For differential channels, the reading is:

(analog reading) = 512 + (positive side voltage - negative side voltage) * GAIN * 512 / (voltage reference)

For example, assuming a gain of 1X, a reading of 0 means the positive pin is (voltage reference) volts less than the negative pin, a reading of 512 means the positive pin and negative pin are at the same voltage, and a reading of 1023 means the positive pin is almost (voltage reference) volts higher than the negative pin. Once you have the analog reading, you can calculate the voltage of the positive pin relative to the negative pin by calculating:

voltage = (analog reading - 512) / 512 * (voltage reference)

Example
Private Sub example()
    Dim chan0 As Long
    Dim voltage As Double
    
    ' Set up error handling for this routine
    On Error GoTo myerror
    
    Set dev = New Eth32
    
    ' .... Your code that establishes a connection here

    ' Enable the Analog to Digital Converter
    dev.AnalogState = ADC_ENABLED
    
    ' Configure logical channel 0 to read the physical channel 0 relative to ground (single-ended)
    ' This is the power-on default anyway, but is shown here for completeness:
    dev.AnalogAssignment(0) = ANALOG_SE0
    
    ' Configure the analog voltage reference to be the internal 5V source
    dev.AnalogReference = REF_INTERNAL
    
    ' Finally, read the voltage on channel 0
    chan0 = dev.InputAnalog(0)
    
    ' Now, determine whether the voltage was >= 3V.  Remember
    ' we're using a 5V voltage reference.
    If chan0 >= (3# / 5# * 1024) Then
        ' The voltage on channel 0 was at least 3V
    Else
        ' The voltage was less than 3V
    End If
    
    ' If you want to calculate the voltage:
    voltage = chan0 / 1024# * 5#

    Exit Sub
myerror:
    MsgBox "ETH32 error: " & dev.ErrorString(Err.Number)
End Sub
        
See Also

AnalogAssignment Property, AnalogReference Property, AnalogState Property