arduino lvd

Discussion in 'The Projects Forum' started by mhastie1234, Jul 17, 2012.

  1. mhastie1234

    Thread Starter New Member

    Feb 10, 2012
    29
    2
    Hello everybody.

    I would like to implement the atmega328 as a low voltage disconnect. I
    Have the voltage divider set up, works and reads what it is supposed to. I was wondering which code to use so if it reads say 800 or below (pin low) 800 or above (pin high). Something like the code below. Is it as simple as that?
    if (pinFiveInput > 800) { // do Thing A } else if (pinFiveInput < 800) { // do Thing B
    }
     
  2. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,771
    971
    yes its basically that simple..
    however make sure you code in some hysteresis though so if its floating right at 800 your LVD is not switching on/off rapidly.
     
  3. mhastie1234

    Thread Starter New Member

    Feb 10, 2012
    29
    2
    Here is my code if anyone is interested. Adjust resistor values for individual needs. Also adjust voltage under //VOLTAGE DIVIDER for disconnect set points.

    Enjoy:)


    //Analog volt read pin

    const int voltPin = 0;

    //Variables for voltage divider

    float denominator;

    int resistor1 = 9850;

    int resistor2 = 2000;

    // Disconnect Pin

    int disconnectpin = 4;

    void setup() {

    Serial.begin(9600);
    //Convert resistor values to division value
    // R2 / (R1 + R2)
    denominator = (float)resistor2 / (resistor1 + resistor2);

    pinMode (disconnectpin, OUTPUT);

    } // Void Setup Close

    void loop() {

    float voltage;
    //Obtain RAW voltage data
    voltage = analogRead(voltPin);

    //Convert to actual voltage (0 - 5 Vdc)
    voltage = (voltage / 1024) * 5.0;

    //Convert to voltage before divider
    // Divide by divider = multiply
    // Divide by 1/5 = multiply by 5
    voltage = voltage / denominator;

    // VOLTAGE DIVIDER
    // If voltage divider reads 25 volts or above pin 4 is high
    if (voltage > 25) {
    digitalWrite(disconnectpin, HIGH);}
    // If voltage divider reads between 24 & 25 volts pin 4 will be on and off every 5 sec
    else if (voltage >=24 && voltage <=25){
    digitalWrite(disconnectpin, HIGH);
    delay(5000);
    digitalWrite(disconnectpin, LOW);
    delay(5000);}
    // If voltage divider reads below 24 volts pin 4 is low
    else
    {digitalWrite (disconnectpin, LOW);
    }

    //Output to serial
    Serial.print("Volts: ");
    Serial.println(voltage);

    //Delay to make serial out readable
    delay(500);

    } // void loop close
     
    Last edited: Jul 19, 2012
Loading...