Menu

Unstable value from ADC

Help
JANIS
2025-03-29
2025-04-06
  • JANIS

    JANIS - 2025-03-29

    Hello! I have a problem with ADC - reading analog values ​​specifically for this project. I don't understand why analog values ​​change by themselves, within small limits, but they change and this is unacceptable. The problem is not in the electrical circuit (hardware) solution, but the analog value reading configuration is incorrect I think. Attached is a video where you can see that the values ​​on the display jump around within large limits, but the battery voltage is actually measured, which is stable, and there are no disturbances (high frequency, etc.). GCB helps contents various solutions, but I have tried them, it does not work. There are still solutions out there, but I don't know which one to use. For example, #define LowSpeed ​​0 or Highspeed does not make any difference. Here in the text is a part of the program code for defining and reading the analog input. I used #chip 16f18446, 32 and analog resolution 8bit. I hope someone has had a similar experience and has an idea how to solve it.
    The full program code is attached.

    ''==== ANALOG IN
    #define  volts_AN4 an4
     dir porta.4 in
     #define ACS758 an5
     dir porta.5 in
    
    #define LowSpeed 0
    
    ''========================== U  ========================================
    V_AN4=scale (ReadAD(volts_AN4), 0, 255, 0, 210) 
    
    Volts_disp=str(V_AN4/10)
    Volts_disp_mod=str(V_AN4%10)
    
    ''==================== A ============================ 50A   ACS758-50U (unidirectional)====
    Lim_amps_AN2=ReadAD(ACS758)
    If Lim_amps_AN2<31 then Lim_amps_AN2=31'' 0,6V quiscient voltage  ACS758
    
    amps_AN2=scale(Lim_amps_AN2, 31, 184, 0, 500)    
    if amps_AN2<4 then amps_AN2=0 
    Amp_disp_str=str(amps_AN2/10)
    Amp_disp_str_mod=str(amps_AN2%10)
    
     
  • JANIS

    JANIS - 2025-03-29

    Here is the video, attached is the diagram.

    https://www.youtube.com/shorts/TloefNgLz1Y?feature=share
    
     
  • Anobium

    Anobium - 2025-03-30

    Here is a revised program. Documented and with #option explicit added. I had to reformat and document so I could get to this far, then I added #option explicit, then I resolved those issues.

    But, this analysis ( so far ) will show you why you should always use #option explicit.

    I have not looked at the ADC issue as I think changing the register.bit PREV3 may have an impact on the ADC operations. As PREV3 is an ADC management register.bit

    So, compiling the ERV version

    Warnings have been generated:
    
     50A AKKU CHARGER ACS758_16F18446.gcb (93): Warning: Variable name PREV3 is used for a system bit on the current chip
     50A AKKU CHARGER ACS758_16F18446.gcb (358): Warning: Comparison will fail if NOW3 is any value other than 0 or 1
    

    PREV3 .. is an ADC register.bit and a 0 or 1.
    The warning was present without #option explicit and you must have ignored.


    So, resolve these issues first. Then, we can see look at the ADC issue.

     
  • Anobium

    Anobium - 2025-03-30
    A corrected version with PREV3 removed in the zero_settings() sub.  As PREV3 would have set the register.bit. Changed to a variable PREVAL3
    
     
  • Roger Jönsson

    Roger Jönsson - 2025-03-30

    Unused pins may introduce noise to the circuitry. Try pullups for all unused input/pins (if you haven't already).

     
    • Anobium

      Anobium - 2025-03-30

      Could be the unused pins - good point.

      I just did a code review on my new version. I think this is caused by the interrupt firing during the ADC read.

       
  • JANIS

    JANIS - 2025-03-30

    Thank you very much for the correction! Thanks for the suggestions! I will delve into the correction. But yesterday after posting the post I found a problem!!! The problem was in the switching power supply. The output voltage of which is regulated by this circuit (from 8.6-16V). I noticed that when the charging process is underway, the voltage value is stable. I tried to power my circuit from another power source, and also only from the battery. The value became stable. Increasing the capacitance of the filter capacitor did not help, and a large capacitor does not work for me, because the goal of my project is to create a 20A (30A peak) miniature charger. Then I introduced changes to the circuit (see the circuit in the appendix) - diode D6, which helps power my circuit from the battery. And it helps! The values ​​​​became stable! Maybe this program is useful to someone, as well as some idea from the circuit.

     
  • Roger Jönsson

    Roger Jönsson - 2025-03-30

    You can use the internal voltage reference (in the PIC) for its ADC, then it will not be sensitive to noise from the power supplying as if using VDD for reference (which is default).
    Also if you put a resistor in series with the "supply" then maybe high frequency noise and transients may have a better change of being smoothed out by C4 (if that is the problem).

     
  • JANIS

    JANIS - 2025-03-30

    For the project I used the MW LOP series compact power supply. It is really a successful product from Mean Well. In my case 20A current (about 350W) and the power supply unit did not heat up, but only warm.

    https://www.meanwell.eu/newsInfo.aspx?c=1&i=4144

     
  • JANIS

    JANIS - 2025-03-30

    Thanks Roger! For filtration, you need to use a choke, that would be the best solution. I don't want to try anymore. With this diode I solved my problem, and now I can measure the voltage of the battery without connecting the power supply! So the charger has an additional bonus :)

     
  • JANIS

    JANIS - 2025-03-30

    Thank you Anobium for your great work. You didn't just fix the program, you wrote all the comments exactly according to the program's algorithm. Great. Fantastic.

     
  • Anobium

    Anobium - 2025-03-30

    Do you have some to write up a short overview? All the URL etc.

    I will help you publish.


    And, you should use INTOFF and INTON around the ReadAD()s. Why? You are accessing variables in the ISR that could change when the event happens. Example is amps_AN2 = scale(Lim_amps_AN2, 31, 184, 0, 500) if the ISR happened in between amps_AN2_H and amps_AN2 being set by the scale() function... you WILL have the incorrect value in the ISR for amps_AN2.

    So, examine the code very carefully for places where variables ( specifically non Byte variables) accessed in the ISR could be partially updated.

    Do share you end state code.

     

    Last edit: Anobium 2025-03-30
  • JANIS

    JANIS - 2025-03-30

    I inserted INTOFF and INTON. I hope I understood correctly. But this program code is so large that I can't write any more instructions because the chip memory is full. I wanted to create some more functions with the display, but that's no longer possible.

     
  • Anobium

    Anobium - 2025-03-30

    'chip memory is full' you still have two pages left and plenty of RAM left.

    What are you try to add? Got an example of a memory full issue?

     
  • Anobium

    Anobium - 2025-03-30

    Try cutting up the code in the main loop, and the code prior to the main loop.

    Add the following then call it, as an example. Most of that main loop can be in a sub.

    Sub Splashscreen
    
        // Splash screen display
        GLCDCLS TFT_GREENYELLOW
        GLCDfntDefaultsize = 1
        GLCDPrint(8, 20, "integrator.k 2025", 0xF800)
        GLCDPrintLargeFont(10, 40, "Stena Line", 0x001F)
        call max_Volts_ind
        GLCDfntDefaultsize = 3
        wait 3 s
        GLCDCLS TFT_navy
    
    End Sub
    
     
  • JANIS

    JANIS - 2025-03-30

    Thanks Anobium. Yes, I already did that, as you can see, my program code is divided into subroutines. Otherwise, the chip would be full. But the number of subroutines is also limited. "I will help you publish." Where do you think this could be published? My colleague will beautifully assemble the PCB board and PSU in the next few days in a box and then the device will be tested with real loads. I hope there will be no errors or problems.

     
  • Anobium

    Anobium - 2025-03-30

    I moved the splash screen. That should work.

    You could move all the main loop to a sub and call that sub. This will permit the relocation of this new sub to another place in the page(s).


    Elektormagazine are publishing many GCBASIC projects.

     
    ❤️
    2
  • JANIS

    JANIS - 2025-03-31

    Anobium, good idea. I'd be happy to share the entire project, as there's still a gerber file, etc. But first, you need to put everything in a box and test it in real life, as there are still many nuances that need to be worked on - transistor/diode cooling radiators, ventilation, power supply operation at max load, etc. After the test period, I'll let you know when everything works perfectly, okay?

     
  • JANIS

    JANIS - 2025-04-05

    Hello! However, disturbances appear when the battery starts to be charged with a higher current - over 10A, that is, at the load of the switching power supply unit. Again, the voltage measurement ADC does not work as it should, the values ​​jump. Then maybe these noises come from the transformer of the switching power supply unit? Roger suggested the idea: You can use the internal voltage reference (in the PIC) for its ADC, then it will not be sensitive to noise from the power supplying as if using VDD for reference (which is default). How to implement it? Maybe you can explain please?

     
  • Roger Jönsson

    Roger Jönsson - 2025-04-05

    What if you run the PIC from a separate battery supply, during the high current measurements?
    If it doesn't make a difference, I'm thinking that with the high currents involved combined with PWM It could be RF crap spreading in the grounds etc. Maybe better filtering is needed for the sampled voltage. It could be transients in the measured voltage showing its ugly face, corrupting the sampling (that you are catching some unwanted peaks, while looking for slower, mean values). PWM can be very problematic in this regard. Maybe you even need screening the sensitive parts.

     
  • JANIS

    JANIS - 2025-04-06

    Yess. Thanks! Everything is fine. I fixed the problem. I added C5 = 0.1uF , see the attached electrical diagram. There is C2 = 47uF, I left it. I first tried to change C2 to 0.1uF , then I don't get the result as good as this solution. So far, everything seems to work fine. There will be a test period with max loads.

     
  • Roger Jönsson

    Roger Jönsson - 2025-04-06

    Good.
    Yes, electrolytics are often lossy at high frequencies due to series self induction. So often a polyester capacitor is used in parallel. Having it in a slightly separated stage as you have (as long as the potentiometer is not at its end) yields even better crap-rejection.

     
    👍
    1

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.