Menu

how to use ACS758-100B HALL SENSOR

samco
6 days ago
5 days ago
  • samco

    samco - 6 days ago

    I think this can help some newbie to get started to read current with hall sensor ACS785, the accuracy is 98%

    ; ==============================================================
    ; ACS758-100B SENSOR TEST TOOL
    ; Target: PIC16F886 (20MHz)
    ; By Samco, maker-brainy
    ; ==============================================================

    chip 16F886, 20

    option explicit

    ; --- LCD Setup (Change if your pins differ) ---

    define LCD_IO 4

    define LCD_NO_RW

    define LCD_Speed fast

    define LCD_RS PORTB.2

    define LCD_Enable PORTB.3

    define LCD_DB4 PORTB.4

    define LCD_DB5 PORTB.5

    define LCD_DB6 PORTB.6

    define LCD_DB7 PORTB.7

    ; --- Sensor Setup ---
    ; AN1 is Pin 3 on PIC16F886

    define SENSOR_PIN AN1

    Dir PORTA.1 In

    ; --- ADC Configuration ---
    ; Fosc/32, AN0-AN4 Analog
    ADCON0 = 0b00000001
    ADCON1 = 0b10000010
    Wait 20 us

    ; --- Variables ---
    Dim RawADC As Word
    Dim AvgADC As Word
    Dim ZeroPoint As Word
    Dim ADC_Diff As Word
    Dim Amps_Tenths As Word
    Dim Calc_Temp As Long
    Dim i As Byte
    Dim Sign As String * 1

    ; --- CONFIGURATION ---
    ; Theoretical Zero for ACS758-100B is 512 (2.5V).
    ; CHANGE THIS to the "RAW" value you see on screen when 0A flows.
    ZeroPoint = 512

    ; --- Main Loop ---
    Cls
    Print "ACS758 Test Tool"
    Locate 1,0
    Print "GREAT COW BASIC "
    Wait 1 s
    Cls

    Do
    ; 1. Average 50 readings to remove noise (Crucial for Hall Sensors!)
    Calc_Temp = 0
    For i = 1 To 50
    RawADC = ReadAD10(SENSOR_PIN)
    Calc_Temp = Calc_Temp + RawADC
    Wait 1 ms
    Next
    AvgADC = Calc_Temp / 50

    ; 2. Calculate Difference from Zero
    If AvgADC >= ZeroPoint Then
        ADC_Diff = AvgADC - ZeroPoint    ; for POS reading
        Sign = "+"
    Else
        ADC_Diff = ZeroPoint - AvgADC    ; for NEG reading
        Sign = "-"
    End If
    
    ; 3. Calculate Amps (ACS758-100B)
    ; Sensitivity: 20mV/A. ADC Step: 4.88mV.
    ; Steps per Amp = 20 / 4.88 = 4.1
    ; Formula: Amps = Steps / 4.1
    ; Scaled Math: Amps*100 = (Steps * 100) / 4.1  -> (Steps * 1000) / 41
    ; Simplified approx: Steps * 24.4
    
    Calc_Temp = ADC_Diff * 244
    Amps_Tenths = Calc_Temp / 10 ; Result is in hundredths (e.g. 250 = 2.50A)
    
    ; 4. Display
    Locate 0,0
    Print "RAW ADC: " : Print AvgADC
    
    Locate 1,0
    Print "I: " : Print Sign
    Print Amps_Tenths / 100 : Print "."
    
    ; Pad the decimal part (e.g. turn "5" into "05")
    If (Amps_Tenths % 100) < 10 Then Print "0"
    Print Amps_Tenths % 100
    Print " A   "
    
    Wait 200 ms
    

    Loop

    it can also be called as subroutine

    ; ==============================================================
    ; ACS758-100B SENSOR TEST TOOL
    ; Target: PIC16F886 (20MHz)
    ; Using subroutine
    ; By Samco, maker-brainy
    ; ==============================================================

    chip 16F886, 20

    option explicit

    ; --- LCD Setup (Change if your pins differ) ---

    define LCD_IO 4

    define LCD_NO_RW

    define LCD_Speed fast

    define LCD_RS PORTB.2

    define LCD_Enable PORTB.3

    define LCD_DB4 PORTB.4

    define LCD_DB5 PORTB.5

    define LCD_DB6 PORTB.6

    define LCD_DB7 PORTB.7

    '#define LCD_WIDTH 16 ' FOR 4X16 LCD

    ; --- Sensor Setup ---
    ; AN1 is Pin 3 on PIC16F886

    define SENSOR_PIN AN1 ' connect to PIN3 of the hall sensor

    Dir PORTA.1 In

    ; --- ADC Configuration ---
    ; Fosc/32, AN0-AN4 Analog
    ADCON0 = 0b00000001
    ADCON1 = 0b10000010
    Wait 20 us

    ; --- Variables ---
    Dim RawADC As Word
    Dim AvgADC As Word
    Dim ZeroPoint As Word
    Dim ADC_Diff As Word
    Dim Amps_Tenths As Word
    Dim Calc_Temp As Long
    Dim i As Byte
    Dim Sign As String * 1

    ; --- CONFIGURATION ---
    ; Theoretical Zero for ACS758-100B is 512 (2.5V).
    ; CHANGE THIS to the "RAW" value you see on screen when 0A flows.
    ZeroPoint = 512

    ; --- Main Loop ---
    Cls
    Print "ACS758 Test Tool"
    Locate 1,0
    Print "GREAT COW BASIC "
    Wait 1 s
    Cls

    DO

    CALL Read_DC_Current

    ; 4. Display
    Locate 0,0
    Print "RAW ADC: " : Print AvgADC
    
    Locate 1,0
    Print "I: " : Print Sign
    Print Amps_Tenths / 100 : Print "."
    
    ; Pad the decimal part (e.g. turn "5" into "05")
    If (Amps_Tenths % 100) < 10 Then Print "0"
    Print Amps_Tenths % 100
    Print " A   "
    
    Wait 200 ms
    

    Loop

    Sub Read_DC_Current
    '! 1. Average 50 readings to remove noise (Crucial for Hall Sensors!)
    Calc_Temp = 0
    For i = 1 To 50
    RawADC = ReadAD10(SENSOR_PIN)
    Calc_Temp = Calc_Temp + RawADC
    Wait 1 ms
    Next
    AvgADC = Calc_Temp / 50

    '! 2. Calculate Difference from Zero
    If AvgADC >= ZeroPoint Then
        ADC_Diff = AvgADC - ZeroPoint    ; for POS reading
        Sign = "+"
    Else
        ADC_Diff = ZeroPoint - AvgADC    ; for NEG reading
        Sign = "-"
    End If
    
    '! 3. Calculate Amps (ACS758-100B)
    ; Sensitivity: 20mV/A. ADC Step: 4.88mV.
    ; Steps per Amp = 20 / 4.88 = 4.1
    ; Formula: Amps = Steps / 4.1
    ; Scaled Math: Amps*100 = (Steps * 100) / 4.1  -> (Steps * 1000) / 41
    ; Simplified approx: Steps * 24.4
    
    Calc_Temp = ADC_Diff * 244
    Amps_Tenths = Calc_Temp / 10 ; Result is in hundredths (e.g. 250 = 2.50A)
    

    END SUB

     
  • Anobium

    Anobium - 6 days ago

    Very good work and thank you for sharing.

    I have adapted for inclusion in the Demos. Can you please review and potentially test if you have time.


    The ADC setup is not need as the ADC reads does this for you. And, this would look complicated to a new user.
    I added header etc.
    Change LCD to SLOW to ensure it works on breadboards.
    Changed variable i to indexCounter as i can cause issues on chips with a bit called i.
    Other changes are format.

     
  • samco

    samco - 5 days ago

    all in zip

     

Log in to post a comment.