using several voltage inputs to trigger a relay

Hi, total newb here
I have a few projects in mind and have a lot to learn but just need to check with one of them that it will do as I intend it to do.

I want to use 3 different analog voltage inputs of 0-5 volts to trigger a relay to operate when all 3 meet certain conditions i.e

If analog input pin A0 is between 2-3 volts and A1 is greater than 2.5 volts and A2 is greater than 3 volts it will send a signal from output pin 7 to operate a relay sheild.

Obviously the sheild will have a 5 volt connection and earth already attached from the Arduino board

Thank you for any help

If analog input pin A0 is between 2-3 volts and A1 is greater than 2.5 volts and A2 is greater than 3 volts it will send a signal from output pin 7 to operate a relay sheild.

Piece of cake. Except that your speel cheker failed you.

lol thank you, was it just shield or something else as well?

ratter11: lol thank you, was it just shield or something else as well?

newb

Depending on what platform I’m on< i’ve seen it as Newb or Noob :slight_smile:

const int pinInput1 = A0;
const int pinInput2 = A1;
const int pinInput3 = A2;
//
const int pinRelay = 7;

//depending on how your relay is wired
//you may need to reverse these
#define RELAY_ON        HIGH
#define RELAY_OFF       LOW

void setup() 
{
    Serial.begin(9600);
    //
    pinMode( pinInput1, INPUT );
    pinMode( pinInput2, INPUT );
    pinMode( pinInput3, INPUT );
    //
    pinMode( pinRelay, OUTPUT );
    digitalWrite( pinRelay, RELAY_OFF );
    
}

void loop() 
{
    static unsigned long
        timeRelay = 0;
    unsigned long
        timeNow;
    float
        v1, v2, v3;
    int
        a1, a2, a3;

    //update every 50mS
    timeNow = millis();
    if( timeNow - timeRelay >= 50 )
    {
        timeRelay = timeNow;
        //read the analog channels
        a1 = analogRead( pinInput1 );
        a2 = analogRead( pinInput2 );
        a3 = analogRead( pinInput3 );
        
        //convert to floating point voltages
        //for easy compares
        v1 = (float)a1*5.0/1023.0;
        Serial.print( "V1 : ");Serial.println(v1,2);
        v2 = (float)a2*5.0/1023.0;
        Serial.print( "V2 : ");Serial.println(v2,2);
        v3 = (float)a3*5.0/1023.0;
        Serial.print( "V3 : ");Serial.println(v3,2);
        
        //check the thresholds
        //If analog input pin A0 is between 2-3 volts and 
        //A1 is greater than 2.5 volts and 
        //A2 is greater than 3 volts 
        //it will send a signal from output pin 7 to 
        //operate a relay shield.
        if( (v1 >= 2.0 && v1 <= 3.0) &&
                (v2 > 2.50) &&
                    (v3 > 3.0 ) )
        {
            //turn on the relay
            digitalWrite( pinRelay, RELAY_ON );
            Serial.println( "Relay on" );
            
        }//if
        else
        {
            //turn off the relay
            digitalWrite( pinRelay, RELAY_OFF );
            Serial.println( "Relay off" );
            
        }//else
        Serial.println();
    }//if
        
}//loop

ratter11: Obviously the sheild [sic ;) ] will have a 5 volt connection and earth already attached from the Arduino board

You may like to post details of the relay shield to get some advice on that, unless you're 100% sure. Some are more complicated than just that, ie those ones with the JD-Vcc (sometimes labelled RY-Vcc) jumper.

Blackfin:

const int pinInput1 = A0;

const int pinInput2 = A1;
const int pinInput3 = A2;
//
const int pinRelay = 7;

//depending on how your relay is wired
//you may need to reverse these
#define RELAY_ON        HIGH
#define RELAY_OFF      LOW

void setup()
{
    Serial.begin(9600);
    //
    pinMode( pinInput1, INPUT );
    pinMode( pinInput2, INPUT );
    pinMode( pinInput3, INPUT );
    //
    pinMode( pinRelay, OUTPUT );
    digitalWrite( pinRelay, RELAY_OFF );
   
}

void loop()
{
    static unsigned long
        timeRelay = 0;
    unsigned long
        timeNow;
    float
        v1, v2, v3;
    int
        a1, a2, a3;

//update every 50mS
    timeNow = millis();
    if( timeNow - timeRelay >= 50 )
    {
        timeRelay = timeNow;
        //read the analog channels
        a1 = analogRead( pinInput1 );
        a2 = analogRead( pinInput2 );
        a3 = analogRead( pinInput3 );
       
        //convert to floating point voltages
        //for easy compares
        v1 = (float)a15.0/1023.0;
        Serial.print( "V1 : ");Serial.println(v1,2);
        v2 = (float)a2
5.0/1023.0;
        Serial.print( "V2 : ");Serial.println(v2,2);
        v3 = (float)a3*5.0/1023.0;
        Serial.print( "V3 : ");Serial.println(v3,2);
       
        //check the thresholds
        //If analog input pin A0 is between 2-3 volts and
        //A1 is greater than 2.5 volts and
        //A2 is greater than 3 volts
        //it will send a signal from output pin 7 to
        //operate a relay shield.
        if( (v1 >= 2.0 && v1 <= 3.0) &&
                (v2 > 2.50) &&
                    (v3 > 3.0 ) )
        {
            //turn on the relay
            digitalWrite( pinRelay, RELAY_ON );
            Serial.println( “Relay on” );
           
        }//if
        else
        {
            //turn off the relay
            digitalWrite( pinRelay, RELAY_OFF );
            Serial.println( “Relay off” );
           
        }//else
        Serial.println();
    }//if
       
}//loop

Thank you very much for that, a lot of that does not make sense to me at this stage but I will break it down bit by bit so I learn to understand it rather than just copy it, I like to understand on how and why things work

arduin_ologist: You may like to post details of the relay shield to get some advice on that, unless you're 100% sure. Some are more complicated than just that, ie those ones with the JD-Vcc (sometimes labelled RY-Vcc) jumper.

I have no relay details at this point as it came in my starter kit, this may not be the end relay I use though on the relay shield it has www.epalsite.com on the printed circuit board Also has 5v Relay Module V1.0 on the other side of the board The relay has part Tianbo HJR-3FF-S-Z 05VDC The 3 pins are labled Sig, 5V & GND 2 screw type output terminals

ratter11: The 3 pins are labled Sig, 5V & GND

Righto, seems you have it connected correctly then, but I thought I'd just check.

ratter11: this may not be the end relay I use though

You may in the long run want to consider those ones with the JD-Vcc pin and jumper, since they offer full isolation between the Arduino side and the load side, when wired correctly. But no point clouding the issue now; come back if you do go for one of those

ratter11: The relay has part Tianbo HJR-3FF-S-Z 05VDC

Just to be on the safe side I checked a datasheet for that model and the 5V version draws just over 70mA. So that's OK to power from the Arduino 5V but if you need more than a couple you would want to look for an external power supply.

Thank you again This project is just about this relay itself and the 3 voltage triggers for it The output side (or switched side) of the relay is only going to be used to enable a voltage divider circuit so I assume it will not have much load

I appreciate the help, it's helping me to learn this stuff

Easy as just use a relay with optocoupler. No need for a shield. The code is simply if ( input a <=1 $$ input b <=1 && input c <1){digitalWrite ( relay ,high )}

Thank you but I do not know anything about optocoupler but a quick search seems like they are similar to a relay, I'm not smart enough to understand why the code would be any different, I would still need something to analyse the 3 different voltages to send a signal to the optocoupler when the target voltages are acheived wouldn't I?

Blackfin:

const int pinInput1 = A0;

const int pinInput2 = A1;
const int pinInput3 = A2;
//
const int pinRelay = 7;

//depending on how your relay is wired
//you may need to reverse these
#define RELAY_ON        HIGH
#define RELAY_OFF      LOW

void setup()
{
    Serial.begin(9600);
    //
    pinMode( pinInput1, INPUT );
    pinMode( pinInput2, INPUT );
    pinMode( pinInput3, INPUT );
    //
    pinMode( pinRelay, OUTPUT );
    digitalWrite( pinRelay, RELAY_OFF );
   
}

void loop()
{
    static unsigned long
        timeRelay = 0;
    unsigned long
        timeNow;
    float
        v1, v2, v3;
    int
        a1, a2, a3;

//update every 50mS
    timeNow = millis();
    if( timeNow - timeRelay >= 50 )
    {
        timeRelay = timeNow;
        //read the analog channels
        a1 = analogRead( pinInput1 );
        a2 = analogRead( pinInput2 );
        a3 = analogRead( pinInput3 );
       
        //convert to floating point voltages
        //for easy compares
        v1 = (float)a15.0/1023.0;
        Serial.print( "V1 : ");Serial.println(v1,2);
        v2 = (float)a2
5.0/1023.0;
        Serial.print( "V2 : ");Serial.println(v2,2);
        v3 = (float)a3*5.0/1023.0;
        Serial.print( "V3 : ");Serial.println(v3,2);
       
        //check the thresholds
        //If analog input pin A0 is between 2-3 volts and
        //A1 is greater than 2.5 volts and
        //A2 is greater than 3 volts
        //it will send a signal from output pin 7 to
        //operate a relay shield.
        if( (v1 >= 2.0 && v1 <= 3.0) &&
                (v2 > 2.50) &&
                    (v3 > 3.0 ) )
        {
            //turn on the relay
            digitalWrite( pinRelay, RELAY_ON );
            Serial.println( “Relay on” );
           
        }//if
        else
        {
            //turn off the relay
            digitalWrite( pinRelay, RELAY_OFF );
            Serial.println( “Relay off” );
           
        }//else
        Serial.println();
    }//if
       
}//loop

I’ve had a read and a play with your code, It basically does what I want and I am greatful for the help, but I did run into a problem.
When one of the input wires was disconnected from the board, (any of the 3 inputs), the relay would start to go on and off randomly,
I played with the code, sometimes making it worse but not eliminating it, but have found the analog inputs already have a voltage across them as soon as the board is powered up which may be affecting it.
Also noticed the voltage across all pins would change when a voltage was applied to any input, almost like it was bleeding through some how.
I don’t know whether that’s normal, but would think not, maybe I have a dud board.

But thank you once again, the sketch you gave me has given me a good head start

ratter11:
I’ve had a read and a play with your code, It basically does what I want and I am greatful for the help, but I did run into a problem.
When one of the input wires was disconnected from the board, (any of the 3 inputs), the relay would start to go on and off randomly,

A disconnected analog input is going to float around as charge accumulates and dissipates on it; readings will drift around and cause the behaviour you’re seeing. If there’s a chance of disconnect like this I suggest putting a large value (e.g. 100K, 1M) resistor between the analog pins and ground. Make the resistor large enough so that it doesn’t adversely affect the voltage you want to measure.

Thank you again
I’ve learned a bit the last couple of days and have read about the voltage float.
The final voltages would probably exceed the float value so may not be an issue, but do you mean just one resistor or a resistor for each analog pin?

For each analog pin that may become disconnected, install a 100K or 1M resistor to GND on that pin.

thank you again,

your sketch was perfect by the way, I've played with it a bit, but keep coming back to the original, I just need to set the voltages correctly once I have the data fromt the sensors.

just tried a 10K resistor as it's the largest I have with me at present, it drops it down to zero at times with an occasional jump to maybe .1v and does not seem to affect the signal too much

ratter11: just tried a 10K resistor as it's the largest I have with me at present, it drops it down to zero at times with an occasional jump to maybe .1v and does not seem to affect the signal too much

Depending on what is driving the analog input 10K may be fine. If the device driving it has a high output impedance the lower value (10K in this case) will act as a "dominant" resistor divider and drop the voltage seen at the pin. If the driving device has low impedance (e.g. an opamp) the 10K shouldn't affect things too much.

Sounds like you've got a reasonable value there.