# [SOLVED]hysteresis gap using arduino atmega8/atmega328

Status
Not open for further replies.

#### codemaster11

##### Member level 5
i want to create hysteresis gap using atmega328p or atmega8, for controlling a 12vdc battery charger circuit.
where the mcu will cut off the charger at 13.7vdc and then again the charger start charging at when
the battery discharge to 12.7vdc , i want to create this gap of 13.7dc (cut off), 12.7vdc (again start charging) how
can i create this gap i don't have any idea how to do this? any help will be appreciated.

Basically flow chart this to give you insight into code needed.

If you are charging and see a trend over time, samples of V, that V is increasing
then setting a trip of 13.7 to stop charging trivial..

If battery is discharging, V dropping (even if you are supply charging current)
and you see samples of V over time that V is dropping then you invoke the low
trip.

Regards, Dana.

##### Super Moderator
Staff member
Create a variable called CHGflag.
Set to 1 when charging. Set to 0 when idle.

To stop charging:
If V>13.7 and CHGflag=1 then CHGflag=0

To begin charging:
If V<12.7 and CHGflag=0 then CHGflag=1

### codemaster11

points: 2

#### codemaster11

##### Member level 5
it's just like schmitt trigger with custom values. i'm trying to solve it with atmega8 using
arduino IDE but facing problem with coding it, using a mosfet based switch with atmega8
so that it may stop/start the charging process by turning on/off through a digital pin of atmega8.

what's i do in my code is as:

step1: define boolean variable as "chgflag = true;" with all other variables as needed.

step2: measure voltage level of 13.7 volts using voltage divider of R1 = 10k & R2 = 5.6k with vin = 13.7vdc

step3: check condition by using

void loop()
{

if(vin >= 13.7)

{
chgflag = false; "turn off mosfet switch"

while(vin >= 12.5) { chgflag = false; turn off mosfet switch; } /* start reading battery status by using while loop while battery discharging

to keep the switch off until the condition false*/

if(vin < 12.5 && chgflag == false)

{ chgflag = true; turn on mosfet switch; /* start charging */ }

}.

/* but if the battery voltage less than 13.7vdc the controller will go to the "else if statement below*/
else if(vin < 13.7 && chgflag == true)
{ turn on mosfet switch; }

} . here there is some problem with my code i'm not identifying. using proteus with arduino IDE simulating this code.

#### betwixt

##### Super Moderator
Staff member
I think with hysteresis there is no need to observe the previous state of the device.
Presumably a condition can exist where the voltage is below 12.5V so you can simply use:

if(vin > 13.7) charge=off;
else charge = on.

otherwise use:

if((vin < 13.7) && (vin > 12.5)) charge=on;
else charge=off;

Brian.

#### codemaster11

##### Member level 5

if we turn off the switch by using

if(vin >= 13.7) then switch = off
else

switch on.

the switch will turn on and the charger will start charging as vin < 13.7 with no hysteresis gap
continuously turn on/off as the voltage < 13.7 - voltage = 13.7. but i think it will be as the above
situation as shown.

#### codemaster11

##### Member level 5
the problem solved using the approach suggested in post#3 by using an external " interrupt/digitalRead(digital pin)",

as according to the image shown in post #6, where lm358 with a define gap, high volt = 13.7vdc (cutoff) & low volt = 12.5vdc.

the output of lm358 is given as digital "high/low" to any digital pin of atmega328p using voltage divider

that fix digital output to 5vdc.

Status
Not open for further replies.