User Tools

Site Tools


uphp:functions:adc_read

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

uphp:functions:adc_read [2017/03/28 19:59]
jeff
uphp:functions:adc_read [2021/09/13 05:57]
Line 1: Line 1:
-======adc_read====== 
-<badge>WMPRO FW >= 1.0</badge> <badge>WMMEGA FW >= 2.0</badge> 
- 
-Read an onboard <tooltip title="Analog to Digital Converter">ADC</tooltip> channel 
- 
-====Description==== 
-<well size="sm"><html><span style="font-size:125%;color:green;">int 
-<span style="color:black;"></html>[[adc_read]] 
-<html>( 
-<span style="color:green;">int 
-<span style="color:blue;">$channel 
-<span style="color:black;">) 
-</html></well> 
-Return the raw <tooltip title="Analog to Digital Converter">ADC</tooltip> value of a channel 
- 
-====Parameter==== 
-<html><span style="color:blue;"><b>$channel</b><span style="color:black;"></html>:  Value between 0 and 2 
- 
-Channel 0 is attached to pin <tooltip title="VIN">A1</tooltip>, which is the voltage divider to measure battery voltage and can measure up to 60V DC.   
- 
-Channel 1 is connected to pin A2 which can handle up to 330V DC.   
- 
-Channel 2 is connected to pin A3, which reads 0-5V DC. 
- 
-====Return Value==== 
-<badge>WMPRO</badge> ADC value (10-bit), 0-1023 
- 
-<badge>WMMEGA</badge> ADC value (12-bit), 0-4095 
- 
-====Examples==== 
-**Example #1** - Read Channel 0 and use the constants v_adc_offset and v_adc_mul to obtain a calibrated voltage((**Example #1** demonstrates a legacy method to obtain a calibrated voltage, which was the primary method in early Wattmon releases, and has not yet been removed from the OS. The calibration constants are loaded automatically into [[uphp:uphp_special_variables|$_GLOBALS]] from the SD Card [[os:config:battery.ini|/config/battery.ini]]. These constants can be calculated and set using Control Panel > Voltage Calibration (Calibrate Voltage Sensor). However, that Control Panel interface does not produce or set calibration constants for Channels 1 and 2, and as mentioned, it stores the constants on the SD Card. This "old method" has been superseded by "a new method" that stores the calibration constants for A1, A2 and A3 in NVRAM. The new calibration method is accessed in Control Panel > Devices. There it is possible to calibrate any device on the Wattmon or attached by ModBus, and the constants are then stored in NVRAM on the device itself, instead of on the SD Card. See [[#Additional Information]] for how to obtain calibrated values using the new method.)): 
- 
-<code php> 
-<pre><?  
-  $adc=adc_read(0); 
-  $volts=floatval($_GLOBALS['v_adc_mul'])*(floatval($adc)+floatval($_GLOBALS['v_adc_offset'])); 
-  print("adc=".$adc."\r\n"); 
-  print("v_adc_offset=".$_GLOBALS['v_adc_offset']."\r\n"); 
-  print("v_adc_mul=".$_GLOBALS['v_adc_mul']."\r\n"); 
-  print("volts=".$volts."\r\n"); 
-?></pre> 
-</code> 
- 
-Typical output for the above example (results vary depending on actual voltage and calibration constants): 
-<code> 
-adc=192 
-v_adc_offset=24.811435 
-v_adc_mul=0.065411 
-volts=14.181854 
-</code> 
- 
-====Additional Information==== 
-The raw ADC values can be accessed using [[adc_read()]], but it is recommended to use the functions [[mb_get_val_by_role()]] or [[mb_get_dev_by_id()]] in order to obtain calculated values for A1, A2 and A3 that are adjusted by the calibration values stored in //NVRAM//. 
- 
-**Example #2** - Use [[mb_get_dev_by_id()]] to obtain calibrated values for A1, A2 and A3: 
- 
-<code php> 
-<pre><? 
-  $dev=mb_get_dev_by_id(0); 
-  $a1=$dev['a1']/10;  // scaled by 10 
-  $a2=$dev['a2']/10;  // scaled by 10 
-  $a3=$dev['a3']/100; // scaled by 100 
-  print("A1=".$a1." V\r\n"); 
-  print("A2=".$a2." V\r\n"); 
-  print("A3=".$a3." V\r\n"); 
-?></pre> 
-</code> 
- 
-Typical output for the above example (results vary depending on actual voltage and calibration constants): 
-<code> 
-A1=13.600001 V 
-A2=2.100000 V 
-A3=4.980000 V 
-</code> 
- 
-====See Also==== 
-[[uphp:uphp_special_variables]] - Special arrays that are populated automatically (including ''$_GLOBALS'') 
- 
-[[floatval()]] - Return the floating point value of a number or string 
  
uphp/functions/adc_read.txt · Last modified: 2021/09/13 05:57 (external edit)