Chat freely about anything...

User avatar
By veljani
#58180 Hi guys.
I have problem with current sensor.
Image

First I tried to read ADC from ACS712 20A (0-5V -> 0-1V voltage divider) and I got a lot of rubbish data.
Then I tried to read with ADC connected to GND - I got 0-12 reading.
Last I tried to read ADC connected to 3.3V (0-5V -> 0-1V votage divider) and I got 0-12 error.

Every time I did 1000 measurements with tmr.delay(1000) between every measurement.
I flash ESP with newest nodemcu from nodemcu-custom.

Is this problem with ESP or there is solution?

Thank you in advance.
User avatar
By trackerj
#58182 I would suggest you to read the related Articles:

- Internal ADC - ESP8266
- ESP8266 - Internal ADC 2 - the easy way example
- ACS712 Current Sensor Board

Happy breadboarding,
TJ.
User avatar
By rudy
#58184 Read this issue - Fluctuating ADC with stabilized source #2070

https://github.com/esp8266/Arduino/issu ... -258660760

Try adding

Code: Select allextern "C" { #include "user_interface.h" }

wifi_set_sleep_type(NONE_SLEEP_T);


Also a small capacitor, 0.1uF, from the A0 to ground may help. Successive approximation register (SAR) A/D convertors created dynamic loads on the input and typically work better with a low impedance source. And for the sampling time a small capacitor will look like a lower impedance source.
User avatar
By veljani
#58186 Hey guys, thank you for your answers. I tried/read everything you suggested.
This is my source:
Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config(ssid, passwd)
wifi.sta.connect()
wifi.sleeptype(wifi.NONE_SLEEP)

local refreshInterval = .2

local mVperAmp = 100 --100mV on 1A
local Voltage = 0
local VRMS = 0
local AmpsRMS = 0
local maxErr = 0
local avgErr = 0
local totalMeasurement = 0
local TotalPwr = 0

tmr.alarm(0, refreshInterval*1000, tmr.ALARM_SEMI, function()
    totalMeasurement = totalMeasurement + 1
    Voltage = getVPP()
    VRMS = (Voltage/2.0) * 0.707
    AmpsRMS = (VRMS * 1000) / mVperAmp
    Pwr = AmpsRMS * 220 /10
    TotalPwr = TotalPwr + Pwr
    print(totalMeasurement..". AvrPwr: "..string.format("%.2f",TotalPwr * 10/totalMeasurement).."W - "..string.format("%.2f",AmpsRMS).."A - "..string.format("%.2f",Pwr * 10).."W") 
    print(" ")
    tmr.start(0)
end)


function getVPP()
    local result = 0
    local readValue = 0
    local maxValue = 0
    local minValue = 1024
    local nbrReadings = 0
    while (nbrReadings<1000) do
        readValue = adc.read(0)
        if (readValue > maxValue) then
            maxValue = readValue
        elseif (readValue < minValue) then
            minValue = readValue
        end
        nbrReadings = nbrReadings + 1
    end
    print(totalMeasurement..". Max: "..maxValue.." - Min: "..minValue)
    result = (math.abs(maxValue - minValue - 6) * 5)/1024
    if (maxValue - minValue > maxErr) then
        maxErr = maxValue - minValue
    end
    avgErr = avgErr + (maxValue - minValue) / 10
   
    print(totalMeasurement..". Err: "..maxValue - minValue.." maxErr: "..maxErr.. " AvgErr: "..string.format("%.2f", avgErr * 10/totalMeasurement))
    return result
end


This is output (10W LED):
Code: Select all69. Max: 365 - Min: 312
69. Err: 53 maxErr: 63 AvgErr: 13.41
69. AvrPwr: 43.97W - 0.71A - 155.69W
 
70. Max: 379 - Min: 310
70. Err: 69 maxErr: 69 AvgErr: 14.20
70. AvrPwr: 46.44W - 0.98A - 216.45W
 
71. Max: 363 - Min: 332
71. Err: 31 maxErr: 69 AvgErr: 14.44
71. AvrPwr: 46.80W - 0.33A - 72.15W
 
72. Max: 363 - Min: 337
72. Err: 26 maxErr: 69 AvgErr: 14.60
72. AvrPwr: 46.89W - 0.24A - 53.16W
 
73. Max: 360 - Min: 327
73. Err: 33 maxErr: 69 AvgErr: 14.85
73. AvrPwr: 47.34W - 0.36A - 79.74W
 
74. Max: 362 - Min: 306
74. Err: 56 maxErr: 69 AvgErr: 15.41
74. AvrPwr: 48.96W - 0.76A - 167.08W
 
75. Max: 361 - Min: 315
75. Err: 46 maxErr: 69 AvgErr: 15.81
75. AvrPwr: 50.02W - 0.59A - 129.11W