Reading ADC Value

pid_read function is used to read value of ADC.

pid_read($pid, $value);

Parameter $value is a variable to contain the returned ADC value.

The maximum value of reference voltage is 3.3V and this is used as a default value. If you want to use lower voltage than this, you can input it through reference voltage interface pin(AREF).

An analog input which ranges from 0V to the reference voltage is linearly converted to a digital value which ranges from 0 to 4095.


example of reading ADC value

The example below reads ADC value and calculates the equivalent analog input voltage

$adc_value = 0;
$pid = pid_open("/mmap/adc0");         // open ADC 0
pid_ioctl($pid, "set ch 0");           // set channel to 0
pid_read($pid, $adc_value);            // read the ADC value
echo "adc value: $adc_value\r\n";      // print the ADC value
$voltage = $adc_value * 3.3 / 4095.0;
echo "voltage : $voltage[V]\r\n";      // print the voltage
pid_close($pid);                       // close ADC