sensor - Sensor Operation Library#

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 sensor

Example

-- Please consult demo/dht11 demo/ds18b20

sensor.ds18b20(pin, check_crc)#

Get DS18B20 temperature data

Parameters

Incoming Value Type

Explanation

int

gpio Port number

boolean

whether to verify the crc value. the default value is true. not verifying the crc value can increase the probability of successful reading, but the wrong value may be read.

Return Value

return value type

explanation

int

Temperature data, unit 0.1 degrees Celsius, error code returned when reading failure

boolean

Returns true on success, otherwise false

Examples

while 1 do
    sys.wait(5000)
    local val,result = sensor.ds18b20(17, true) -- GPIO17 and check the CRC value
    -- val 301 == 30.1 Celsius
    -- result true Read success
    log.info("ds18b20", val, result)
end

sensor.w1_reset(pin)#

Single bus protocol, reset device

Parameters

Incoming Value Type

Explanation

int

gpio Port number

Return Value

return value type

explanation

nil

No Return

Examples

None


sensor.w1_connect(pin)#

Single bus protocol, connecting devices

Parameters

Incoming Value Type

Explanation

int

gpio Port number

Return Value

return value type

explanation

boolean

Returns true on success and true on failure false

Examples

None


sensor.w1_write(pin, data1,data2)#

Single bus protocol, write data to the bus

Parameters

Incoming Value Type

Explanation

int

gpio Port number

int

The first data

int

The second data, can write n data

Return Value

return value type

explanation

nil

No return value

Examples

None


sensor.w1_read(pin, len)#

Single bus protocol, read data from the bus

Parameters

Incoming Value Type

Explanation

int

gpio Port number

int

Read Length

Return Value

return value type

explanation

int

Returns N integers by the length of the read

Examples

None


sensor.hx711(pin_date,pin_clk)#

Obtaining pressure sensing data for the Hx711

Parameters

Incoming Value Type

Explanation

int

gpio port number of the data

int

gpio port number of the clock

Return Value

return value type

explanation

int

hx711 Read the data

Examples

--  If the device is not present it will get stuck on the read interface
sys.taskInit(
    function()
        sys.wait(1000)
        local maopi = sensor.hx711(0,7)
        while true do
            sys.wait(2000)
            a = sensor.hx711(0,7) - maopi
            if a > 0 then
                log.info("tag", a / 4.6)
            end
        end
    end
)

sensor.cs1237(pin_date,pin_clk)#

Get cs1237 sensor data

Parameters

Incoming Value Type

Explanation

int

gpio port number of the data

int

gpio port number of the clock

Return Value

return value type

explanation

int

cs1237 Read the data

Examples

--  If the device is not present it will get stuck on the read interface
sys.taskInit(
    function()
        sys.wait(1000)
        local cs1237_data = sensor.cs1237(0,7)
        while true do
            sys.wait(2000)
            cs1237_data = sensor.cs1237(0,7) - maopi
            log.info("cs1237_data:", cs1237_data)--Get raw data
        end
    end
)

sensor.ws2812b(pin,data,T0H,T0L,T1H,T1L)#

set ws2812b output (gpio drive mode)

Parameters

Incoming Value Type

Explanation

int

ws2812b gpio port number

string/zbuff

The data to be sent (if it is zbuff data, it will ignore the pointer position and always start from 0 offset.)

int

T0H Time, indicating how many nops are delayed, each model is different, adjust it yourself

int

T0L Time, indicating how many delays nop

int

T1H Time, indicating how many delays nop

int

T1L Time, indicating how many delays nop

Return Value

None

Examples

local buff = zbuff.create({8,8,24})
buff:drawLine(1,2,5,6,0x00ffff)
sensor.ws2812b(7,buff,300,700,700,700)

sensor.ws2812b_pwm(pin,data)#

Set ws2812b output (pwm drive mode, pwm is required to output 800k frequency, otherwise this method cannot be used)

Parameters

Incoming Value Type

Explanation

int

pwm Port number

string/zbuff

The data to be sent (if it is zbuff data, it will ignore the pointer position and always start from 0 offset.)

Return Value

None

Examples

local buff = zbuff.create({8,8,24})
buff:setFrameBuffer(8,8,24,0x0000ff)
sensor.ws2812b_pwm(7,buff)

sensor.ws2812b_spi(pin,data)#

Set the ws2812b output (spi drive mode, spi is required to output 5M frequency, otherwise this method cannot be used)

Parameters

Incoming Value Type

Explanation

int

spi Port number

string/zbuff

The data to be sent (if it is zbuff data, it will ignore the pointer position and always start from 0 offset.)

Return Value

None

Examples

local buff = zbuff.create({8,8,24})
buff:setFrameBuffer(8,8,24,0x0000ff)
sensor.ws2812b_spi(2,buff)

sensor.dht1x(pin)#

Obtain temperature and humidity data of DHT11/DHT12

Parameters

Incoming Value Type

Explanation

int

gpio Port number

boolean

whether to verify the crc value. the default value is true. not verifying the crc value can increase the probability of successful reading, but the wrong value may be read.

Return Value

return value type

explanation

int

Humidity data in 0.01%, error value returned on read failure

int

Temperature data in 0.01 degrees Celsius, error value returned when reading failed

boolean

Returns true on success, otherwise false

Examples

while 1 do
    sys.wait(1000)
    local h,t,r = sensor.dht1x(17, true) -- GPIO17 and check the CRC value
    log.info("dht11", h/100,t/100,r)--90.1 23.22
end

sensor.sc12a(sda,scl)#

Get channel data for sc12a touched

Parameters

Incoming Value Type

Explanation

int

gpio port number of the data

int

gpio port number of the clock

Return Value

return value type

explanation

int

If the read is successful, the reshaped data is returned. If the read fails, the error value is returned.

Examples

while true do
  local temp1=sensor.sc12a(4,7)
  if bit.rshift(bit.band( temp1, 0x8000), 15 )==0x01 then
    log.info("There is a channel that is pressed.0")
  end
  if bit.rshift(bit.band( temp1, 0x4000), 14 )==0x01 then
    log.info("There is a channel that is pressed.1")
  end
  if bit.rshift(bit.band( temp1, 0x2000), 13 )==0x01 then
    log.info("There is a channel that is pressed.2")
  end
  if bit.rshift(bit.band( temp1, 0x1000), 12 )==0x01 then
    log.info("There is a channel that is pressed.3")
  end
  if bit.rshift(bit.band( temp1, 0x800), 11 )==0x01 then
    log.info("There is a channel that is pressed.4")
  end
  if bit.rshift(bit.band( temp1, 0x400), 10 )==0x01 then
    log.info("There is a channel that is pressed.5")
  end
  if bit.rshift(bit.band( temp1, 0x200), 9 )==0x01 then
    log.info("There is a channel that is pressed.6")
  end
  if bit.rshift(bit.band( temp1, 0x100), 8 )==0x01 then
    log.info("There is a channel that is pressed.7")
  end
  if bit.rshift(bit.band( temp1, 0x80), 7 )==0x01 then
    log.info("There is a channel that is pressed.8")
  end
  if bit.rshift(bit.band( temp1, 0x40), 6 )==0x01 then
    log.info("There is a channel that is pressed.9")
  end
  if bit.rshift(bit.band( temp1, 0x20), 5 )==0x01 then
    log.info("There is a channel that is pressed.10")
  end
  if bit.rshift(bit.band( temp1, 0x10), 4 )==0x01 then
    log.info("There is a channel that is pressed.11")
  end
  sys.wait(200)
end