Saturday, June 6, 2015

Part 2 : ACS712 Current Sensor Board - Programming




   In this part we will explore the programming side of the ACS712 Current reading function. 

   If you didn't read yet the previous article about ACS712 Current sensor Board it might be a good idea to start from there as it will be easier to understant how it's working the entire process. You can find there schematics, theory of operation and technical related things. 


What we will need:



   ACS712  Software
 
  For programming CBDBv2 Board and uploading the drivers and the software we will continue to use the LuaUploader as before.




  1. Reading ACS712  with VLSAM DC

In this case, as VLSAM DC Output is already scaled to ADC input of 0-1V the only things that we need to do for proper measurements of the current are:
  • establish the LSB value for ADC 
  • extablish your own desired VLSAM Offset value. It's desired to not start your currrent range scale exactly at 0V ADC (ADC linearity, etc), for easing the calculations, just set Vdiv to have an output  of about 100mV for ZERO Amps. As ASC712/20 gives us 100mV/A that means that we will have 100mA reading for 200mV and so on, just substract your offset.   

         offset = 0.1130             -- CALIBRATE !! 
          LSB = 0.000976563   -- CALIBRATE !!
 

        function readADC_VLSAM()
            ad = 0
            ad=adc.read(0) --calibrate based on your voltage divider AND Vref!
            print("\nReadADC     : " ..ad)
           ACS712 = ad*LSB
           print("Current Read: " ..string.format("%g",ACS712-offset) .."mA")
           return ACS712
        end


   For increasing the precison of the measurement just take a bigger number of readings and obtain a average value. 128 steps looks quite ok, but in case of more noise in the system just use a bigger number and eliminate the most extreme values for the obtained interval.


function readADC_avg_VLSAM()
      ad1 = 0
      i=0
      while (i<128) do
         ad1=ad1+adc.read(0) --calibrate based on your voltage divider AND Vref!
         tmr.delay(1000)
         --print(ad1)
         i=i+1
      end
      ad1 = ad1/128
      ACS712 = ad1*LSB
      print("Current Read: " ..string.format("%g",ACS712-offset) .."mA")
      return ACS712avg
end





 2. Direct Reading ACS712 Current Sensor

    Another available option is to read directly the ACS Current sensor output.  

    In this case because the sensor output will swing between 0 and 5V, with ZERO Amps reading at 2.5V, we will use the CBD V2 EVO Board voltage divider or add your own in case of using a different board without such capabilities.

   To better understand how it will work in this case, please take a look at the ESP8266 Internal ADC article where was also discussed the voltage divider for ADC input.

      -- Select  ADC SOURCE
     ADC_SRC = 5  -- select Voltage Divider / Current Input by default !
     gpio.mode(ADC_SRC,gpio.OUTPUT, gpio.PULLUP)

     gpio.write(ADC_SRC,1)  -- Voltage Measurement - Voltage Divider Source selected

     dival = 0.0009605      -- ADC volt/div value - CALIBRATE !!  -- USB
     resdiv = 4.31447        -- Voltage divider Ration - CALIBRATE!!
     offset = 2.45               --ACS712 measured offset - CALIBRATE !!



     function readADC_VDIV()
          adcV = 0
          advr = 0
          advr=adc.read(0)
          print("\nADCV Step   : " ..string.format("%g",advr).." steps")
         adcV=advr*dival*resdiv

         print("Current       : " ..string.format("%g",adcV-offset).." mA")
         return adcV
    end

  
    For increasing the precison of the measurement just take a bigger number of readings and obtain a average value as before, but this type you need more, like 512 or even 1024. Also some extra filtering on ACS712 output will be great.

    Despite the fact that might be easier to use the Voltage divider, because of the high noise from the ASC712 output I will recommend you to use option1 - VLSAM DC . It will give you more accurate values than option2 with Voltage divider.




  

1 comment:

Unknown said...
This comment has been removed by the author.

Post a Comment