Skip to content

adc value not correct after a period of time. #7832

Open
@dty717

Description

@dty717

CircuitPython version

Adafruit CircuitPython 7.3.3 on 2022-08-29; Raspberry Pi Pico with rp2040
Board ID:raspberry_pi_pico
boot.py output:

Code/REPL

import board
import busio
import digitalio
import analogio

import time
import pwmio

p1 = pwmio.PWMOut(board.GP18,frequency = 4000)
p1.duty_cycle = -1 + 2 ** 0
p2 = pwmio.PWMOut(board.GP19,frequency = 4000)
p2.duty_cycle = -1 + 2 ** 0
p3 = pwmio.PWMOut(board.GP20,frequency = 4000)
p3.duty_cycle = -1 + 2 ** 0
p4 = pwmio.PWMOut(board.GP21,frequency = 4000)
p4.duty_cycle = -1 +  2 ** 0


adc1 = analogio.AnalogIn(board.A0)
adc2 = analogio.AnalogIn(board.A1)
adc3 = analogio.AnalogIn(board.A2)

initWCS1800Val = 5/2 * 65535 / 3.3

K_WCS1800 = 60

def initWCS1800(adc,times = 1000):
    global initWCS1800Val
    initWCS1800SumTimes = times
    initWCS1800Val = 0
    while True:
        initWCS1800Val += adc.value
        initWCS1800SumTimes -= 1
        if initWCS1800SumTimes <= 0:
            break
    initWCS1800Val = initWCS1800Val / times
    return initWCS1800Val


def currentWCS1800(adc, times=1):
    global initWCS1800Val
    val = 0
    _times = times
    while _times > 0:
        val += adc.value
        _times -= 1
    return (val/times - initWCS1800Val) / 65535 * 3.3 / K_WCS1800*1000


initWCS1800(adc1)


initACS758Val = 3.3/2 * 65535 / 3.3

initACS758SumMaxTimes = 1000

K_ACS758 = 20

def initACS758(adc):
    global initACS758Val
    initACS758SumTimes = initACS758SumMaxTimes
    initACS758Val = 0
    while True:
        initACS758Val += adc.value
        initACS758SumTimes -= 1
        if initACS758SumTimes <= 0:
            break
    initACS758Val = initACS758Val / initACS758SumMaxTimes
    return initACS758Val


def currentACS758(adc):
    val = (adc.value - initACS758Val) / 65535 * 3.3
    return val/K_ACS758*1000

initACS758(adc2)


changeTimes = 50
step = 0
while True:
    time.sleep(0.02)
    # amp = v1()
    # voltage = adc1.value
    # (amp,voltage,amp*voltage)
    print((currentWCS1800(adc1,1000),adc1.value/65535))
    changeTimes -= 1
    if changeTimes <= 0:
        changeTimes = 200
        if step == 0:
            p1.duty_cycle = -1 + 2 ** 14
        elif step == 1:
            p1.duty_cycle = -1 + 2 ** 0
        elif step == 2:
            p2.duty_cycle = -1 + 2 ** 14
        elif step == 3:
            p2.duty_cycle = -1 + 2 ** 0
        step += 1
        step %= 4
        # time.sleep(2)
        # a.value = not a.value

Behavior

The adc value is 0.81 when running, but it changes to 0.82 after a period of time. When i restart the program the adc value return to 0.81. The adc value has a little shift that is not correct.

Description

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions