adc - Analog-to-digital conversion#

Adapted Air780E/Air700E Air780EP Air601 Air101/Air103 Air105 ESP32C3 ESP32S3

Note

This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!

Tip

This library has its own demo,click this link to view the demo example of adc

Example

-- This library can read hardware adc channels, and also supports reading CPU temperature and VBAT power supply (if supported by the module)

-- Read the CPU temperature, the unit is 0.001 degrees Celsius, it is the internal temperature, not the ambient temperature
adc.open(adc.CH_CPU)
local temp = adc.get(adc.CH_CPU)
adc.close(adc.CH_CPU)

-- Read the VBAT supply voltage in mV
adc.open(adc.CH_VBAT)
local vbat = adc.get(adc.CH_VBAT)
adc.close(adc.CH_VBAT)

-- For physical ADC channels, please refer to the comments in adc.get or adc.read

Constant#

constant

type

explanation

adc.ADC_RANGE_3_6

number

air105 The ADC voltage divider resistor is turned on, and the range 0~3.76V

adc.ADC_RANGE_1_8

number

air105 The ADC voltage divider resistor is turned off, and the range 0~1.88V

adc.ADC_RANGE_3_8

number

air780E Turn on ADC0,1 divider resistor, range 0~3.8V

adc.ADC_RANGE_1_2

number

air780E Turn off ADC0,1 divider resistor, range 0~1.2V

adc.CH_CPU

number

CPU Internal temperature channel id

adc.CH_VBAT

number

VBAT Supply voltage channel id

adc.T1

number

ADC1 (If there are more than one ADC you can use this constant to open using multiple ADCs such as adc.open(ADC1 2)ADC1 channel 2)

adc.T2

number

ADC2 (If there are more than one ADC can use this constant to open using multiple ADCs such as adc.open(ADC2 3)ADC2 channel 3)

adc.open(id)#

open adc channel

Parameters

Incoming Value Type

Explanation

int

The channel ID, which is related to the specific device, usually starting from 0.

Return Value

return value type

explanation

boolean

Open Results

Examples

-- open adc channel 4 and read
if adc.open(4) then
    log.info("adc", adc.read(4)) -- There are 2 return values, the original value and the calculated value, usually only the latter is needed.
    log.info("adc", adc.get(4))  -- There is 1 return value, only the value is calculated.
end
adc.close(4) -- If continuous reading is required, close is not required and the power consumption will be higher..

adc.setRange(range)#

Set the measurement range of ADC. Note that this is related to specific chips. Currently, only air105/Air780E series are supported.

Parameters

Incoming Value Type

Explanation

int

range Parameters, related to specific equipment, such as air105 fill adc.ADC_RANGE_1_8 and adc.ADC_RANGE_3_6

return

nil

Return Value

None

Examples

-- This function should be called before adc.open is called, and then the call is invalid.!!!

-- Turn off the internal partial pressure of air105
adc.setRange(adc.ADC_RANGE_1_8)
-- Turn on the internal partial pressure of air105
adc.setRange(adc.ADC_RANGE_3_6)


-- EC618 Series (Air780E, etc.) support 2 kinds
adc.setRange(adc.ADC_RANGE_1_2) -- Turn off partial pressure
adc.setRange(adc.ADC_RANGE_3_8) -- Enable Divider Voltage

adc.read(id)#

read adc channel

Parameters

Incoming Value Type

Explanation

int

The channel ID, which is related to the specific device, usually starting from 0.

Return Value

return value type

explanation

int

The original value is generally useless and can be discarded directly.

int

The actual value converted from the original value, usually in units mV

Examples

-- open adc channel 2 and read
if adc.open(2) then
    -- What is used here is that adc.read will return 2 values, recommend go through the adc.get function and directly take the actual value.
    log.info("adc", adc.read(2))
end
adc.close(2)

adc.get(id)#

get adc calculated value

Parameters

Incoming Value Type

Explanation

int

The channel ID, which is related to the specific device, usually starting from 0.

Return Value

return value type

explanation

int

The unit is usually mV, and some channels will return the temperature value in 1‰ degrees Celsius. If the reading fails, it will return-1

Examples

-- This API is available in firmware compiled after 2022.10.01
-- open adc channel 2 and read
if adc.open(2) then
    log.info("adc", adc.get(2))
end
adc.close(2) -- Close on demand

adc.close(id)#

close adc channel

Parameters

Incoming Value Type

Explanation

int

The channel ID, which is related to the specific device, usually starting from 0.

Return Value

None

Examples

-- open adc channel 2 and read
if adc.open(2) then
    log.info("adc", adc.read(2))
end
adc.close(2)