News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

ADC _strange things

Started by Marcel_741, Jun 22, 2023, 11:19 PM

Previous topic - Next topic

Pepe

demo in isis

See_Mos

Silly me. Tumbleweed is correct about readings being relative to VDD but hysteresis is still a good idea

Marcel_741

Pepe,

I tried to make ISIS file work, but got errors. As the circuit and programming allready works, it is on the one hand a pitty not to simulate, but on the other hand further no issue.

I add the error,

I think it got a lot to do with lack of my knowledge.

Regards,

Marcel




Pepe

You could make the circuit in a new sheet with the elements of your library and load the .hex file in the pic

Marcel_741

Pepe,

I made the circuit, load the hex file, but same (exectly) errors occured. Means that my version is not be used.

I leave it.

In real it is operational so, all good.

Thanks for the advise.

Marcel

Marcel_741

Hello,

Since the last post on this topic, I started to complete the project and I must say that it worked out pretty well. The PCB is finished and assambly completed. The circuit was put in service and it worked almost perfectly. The only thing what is missing is the hysteresis to avoid hunting the relais which is activating the led group. The programm reads the ADC on GP0 and the reached steps are activating GP1,GP2,GP4 and evently GP5 which activates a BUZ11 and a relais is powered. Because the programm has no hysteresis and I am not been able to discover the correct way to achieve this, the relais is hunting on his on/off point. So my main question is how to create a hysteris in a software way in the program?

Thanks in front,

Regards,

Marcel

Device 12F675                            ; Used PIC

Config intrc_osc_noclkout,_
       wdt_off,_
       pwrte_on,_
       mclre_off
Xtal 4

All_Digital TRUE                          ;All inputs digital

Symbol yes = 1                            ; 1 is high
Symbol no = 0                             ; 0 is low

Symbol ledr1 = GPIO.1                     ; led red 1 on port_1
Symbol ledr2 = GPIO.2                     ; led red 2 On port_2
Symbol ledge = GPIO.4                     ; led yellow On port_4
Symbol ledgr = GPIO.5                     ; led group On port_5

ADCON0bits_ADON = 1                       ; Enable the ADC         
ADCON0 = %00000001                        ; A/D control register
ADCON0bits_ADFM = 1                       ; Right justify the ADC for a 10-bit result

Declare Adin_Res   = 10                   ; resolution 10 bits     
Declare Adin_Tad   = frc                  ; Set rc osc     
Declare Adin_Stime = 50                   ; sample time 5       
                                       
Dim potmeter As Word                      ; potmeter (0K2 - 23K2)
         ;543210                          ; helpline
GPIO   = %000000                          ; make ports low
TRISIO = %000001                          ; poort I/O



run:   
                                 
 potmeter = ADIn 0
                   
     If potmeter < 194 Then ledr1 = no   ;LED Red 1 off when < 194
         
     If potmeter > 194 Then ledr1 = yes  ;LED Red 1 on when > 194 (1,01V)             
     
     If potmeter < 388 Then ledr2 = no   ;LED Red 2 off when < 388
     If potmeter > 388 Then             
     ledr2 = yes                         ;LED Red 2 on when > 388 (2,04V)
     ledr1 = no                          ;LED Red 1 off when > 388
     End If
     
     If potmeter < 582 Then ledge = no   ;LED Yellow off when < 582
     If potmeter > 582 Then             
     ledge= yes                          ;LED Yellow on when > 582 (3,05V)
     ledr2 = no                          ;LED Red 2 off when > 582
     End If                             
     
     If potmeter < 776 Then ledgr = no   ;LED Group 1 off when < 776
     If potmeter > 776 Then             
     ledgr = yes                         ;LED Group on when > 776 (4,07)
     ledge = no                          ;LED Yellow off when > 776
     End If                             
     
                                                             
GoTo run                               

 
     
End                                        ; end programm

Marcel_741

Quote from: Marcel_741 on Sep 20, 2023, 07:25 AMHello,

Since the last post on this topic, I started to complete the project and I must say that it worked out pretty well. The PCB is finished and assambly completed. The circuit was put in service and it worked almost perfectly. The only thing what is missing is the hysteresis to avoid hunting the relais which is activating the led group. The programm reads the ADC on GP0 and the reached steps are activating GP1,GP2,GP4 and evently GP5 which activates a BUZ11 and a relais is powered. Because the programm has no hysteresis and I am not been able to discover the correct way to achieve this, the relais is hunting on his on/off point. So my main question is how to create a hysteris in a software way in the program?

Thanks in front,

Regards,

Marcel

Device 12F675                            ; Used PIC

Config intrc_osc_noclkout,_
      wdt_off,_
      pwrte_on,_
      mclre_off
Xtal 4

All_Digital TRUE                          ;All inputs digital

Symbol yes = 1                            ; 1 is high
Symbol no = 0                            ; 0 is low

Symbol ledr1 = GPIO.1                    ; led red 1 on port_1
Symbol ledr2 = GPIO.2                    ; led red 2 On port_2
Symbol ledge = GPIO.4                    ; led yellow On port_4
Symbol ledgr = GPIO.5                    ; led group On port_5

ADCON0bits_ADON = 1                      ; Enable the ADC       
ADCON0 = %00000001                        ; A/D control register
ADCON0bits_ADFM = 1                      ; Right justify the ADC for a 10-bit result

Declare Adin_Res  = 10                  ; resolution 10 bits   
Declare Adin_Tad  = frc                  ; Set rc osc   
Declare Adin_Stime = 50                  ; sample time 5     
                                     
Dim potmeter As Word                      ; potmeter (0K2 - 23K2)
        ;543210                          ; helpline
GPIO  = %000000                          ; make ports low
TRISIO = %000001                          ; poort I/O



run: 
                               
 potmeter = ADIn 0
                 
    If potmeter < 194 Then ledr1 = no  ;LED Red 1 off when < 194
       
    If potmeter > 194 Then ledr1 = yes  ;LED Red 1 on when > 194 (1,01V)             
   
    If potmeter < 388 Then ledr2 = no  ;LED Red 2 off when < 388
    If potmeter > 388 Then             
    ledr2 = yes                        ;LED Red 2 on when > 388 (2,04V)
    ledr1 = no                          ;LED Red 1 off when > 388
    End If
   
    If potmeter < 582 Then ledge = no  ;LED Yellow off when < 582
    If potmeter > 582 Then             
    ledge= yes                          ;LED Yellow on when > 582 (3,05V)
    ledr2 = no                          ;LED Red 2 off when > 582
    End If                             
   
    If potmeter < 776 Then ledgr = no  ;LED Group 1 off when < 776
    If potmeter > 776 Then             
    ledgr = yes                        ;LED Group on when > 776 (4,07)
    ledge = no                          ;LED Yellow off when > 776
    End If                             
   
                                                           
GoTo run                             

 
   
End                                        ; end programm
I forgot to mention that the potmeter is replaced for a LDR, so it will activate the outside lighting house when it becomes dark.

trastikata

Hello,

can you try to verbally explain how do you imagine this hysteresis should work?
Then we can help with the coding if you haven't figured it yet by the end of that explanation.  ;)


Frizie

#28
As an example to create a hysteresis:

Original piece of your code:
If potmeter < 388 Then ledr2 = no  ;LED Red 2 off when < 388
If potmeter > 388 Then           
  ledr2 = yes                      ;LED Red 2 on  when > 388 (2,04V)
  ledr1 = no                       ;LED Red 1 off when > 388
End If

Changed with hysteresis:

Dim Hysteresis As Byte

If (potmeter + Hysteresis) < 388 Then
  ledr2      = no                  ;LED Red 2 off when < 388
  Hysteresis = 0                   ;Remove the hysteresis
Else         
  ledr2      = yes               ;LED Red 2 on  when > 388 (2,04V)
  ledr1      = no                ;LED Red 1 off when > 388
  Hysteresis = 50                ;Give a hysteresis of 50 (for example)
EndIf

This is of course a simple example to give you an impression.
But this is much nicer to program.
Ohm sweet Ohm | www.picbasic.nl

Marcel_741

Hello,

I think I allready found a way to improve the hunting effect.

If potmeter < 776 Then ledgr = no  ;LED Group 1 off when < 776
If potmeter > 776 Then           
ledgr = yes                        ;LED Group on when > 776 (4,07)
ledge = no                          ;LED Yellow off when > 776

When the value reach the 776, then last LED goes out and the group will be activate. I changed it to

If potmeter < 770 Then ledgr = no  ;LED Group 1 off when < 770
If potmeter > 780 Then           
ledgr = yes                        ;LED Group on when > 780 (~4,07)
ledge = no                          ;LED Yellow off when > 780

So, the gap is 10 and I will test it tonight. I found it difficult to simulate day/night effects so, I do it live.

Trastikata,

How the hysteresis must work is that the moment of activation by reached value will not follow by hunting effects. For me the only way is to gap off and on with required value, which I only know by testing.

Marcel


Frizie

Marcel, see the example from me (post #28)
Ohm sweet Ohm | www.picbasic.nl

See_Mos

Marcel code:-

QuoteIf potmeter < 770 Then ledgr = no  ;LED Group 1 off when < 770
If potmeter > 780 Then           
ledgr = yes                        ;LED Group on when > 780 (~4,07)
ledge = no                          ;LED Yellow off when > 780

is how I did it in a recent project:-

Quote'        Input Battery_Volts
        ADC_wResult = ADC_Get()             ' Perform an ADC conversion
                                            ' 11.5 = 750, 12.2 = 800, 12.5 = 815, 13.0 = 848       
        If ADC_wResult > 790 Then Low Warning_LED ' battery OK so turn off low battery LED 790 = 12.05v
        If ADC_wResult < 770 Then High Warning_LED ' turn on LED to show low battery        770 = 11.7v

trastikata

Quote from: Marcel_741 on Sep 20, 2023, 10:12 AMSo, the gap is 10 and I will test it tonight. I found it difficult to simulate day/night effects so, I do it live.

Trastikata,

How the hysteresis must work is that the moment of activation by reached value will not follow by hunting effects. For me the only way is to gap off and on with required value, which I only know by testing.

That was my point, as soon as you can narrate with your own words what must happen, you can easily program it.

Often people ask for code to be given directly while they are perfectly capable of doing it on their own ... and learn in the process.

Marcel_741

Frizie

Thanks for that and I will also use it as a option.

See_Mos

Aha, thanks that you show this. I must be thinking in that direction

Trastikata

I agree, allthough for me it doesn't allways leads to a solution. Good there are people like on this forum.

So, first I try the gap (10) option and I will come back on it.

Thanks all,

Marcel

Marcel_741

Hello,

Just to update the topic, I managed to let the program do what works fine without any hunting of the relais. I add
delayMS 30000 in the programm to scan every 30 sec so, the actual value in relation the ammount of light exceeds the
fixed setpoints (ADC steps) and activation is more accurate.

Thanks all.

Marcel
run:   
                                 
 potmeter = ADIn 0
                   
     If potmeter < 192 Then ledre = no   ;LED Red off when < 194
         
     If potmeter > 196 Then ledre = yes  ;LED Red on when > 194 (1,01V)             
     
     If potmeter < 386 Then ledye = no   ;LED Yellow off when < 388
     If potmeter > 390 Then              ;Hysteresis
     ledye = yes                         ;LED Yellow on when > 388 (2,04V)
     ledre = no                          ;LED Red off when > 388
     End If
     
     If potmeter < 580 Then ledgr = no   ;LED Green off when < 582
     If potmeter > 584 Then              ;Hysteresis
     ledgr= yes                          ;LED Green on when > 582 (3,05V)
     ledye = no                          ;LED Yellow off when > 582
     End If                             
     
     If potmeter < 770 Then ledgp = no   ;LED Group 1 off when < 770
     If potmeter > 780 Then              ;Hysteresis
         
     ledre = no                          ;LED Red off when > 770
     ledye = no                          ;LED Green off when > 770
     ledgr = no                          ;LED Green off when > 776
     
     If potmeter >779 Then
     ledgp = yes                         ;LED Group on when > 779 (~4,07)
     End If
     
     End If                             
     
     DelayMS 30000
                                                             
GoTo run