Multi 0-25v Voltage Sensors triggering individual 12 volt relays/LEDs

Hi guys,
I am new to arduino but I am not letting that slow me or stop me.
I have been playing around this evening with the basic functions I want to incorporate into my current project I am using a UNO R3, 0-25v Voltage Sensor, 1 channel 12 volt arduino relay and a 12 volt LED.

I need to be able to detect changes in Voltage (I am using 0-25v Voltage Sensors) and power 12v LEDs when over a set voltage (via a 12v relay).

How would I write in another Voltage Sensor? I plan on having up to 6 if I need more I will change to a MEGA

I have not incorporated my last requirement however I need to be able to detect if a circuit is open or closed. I guess I would maybe use a ohm to ground??

However I cant get the damed LED to light up the wiring is sound as I am using a sensor shield which makes building the project so much easier.

Please help any assistance you can offer is greatly appreciated.

int analogInput = A0; // 0 - 25 voltage sensor
int LEDpin = 1; // connected to 12 volt relay and 12 volt LED
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; 
float R2 = 7500.0;
int value = 0;


void setup(){
   pinMode(analogInput, INPUT);
   pinMode(LEDpin, OUTPUT);
   
   Serial.begin(9600);
   
}
void loop(){
   // read the value at analog input
   value = analogRead(analogInput);
   vout = (value * 5.0) / 1024.0; // see text
   vin = vout / (R2/(R1+R2)); 
   
   if(analogInput<=5)
   {
    digitalWrite(LEDpin, LOW); //voltage lower than 5 volts no 12 volt LED
    }
   
  if(analogInput>=5)
  {
    digitalWrite(LEDpin, HIGH); // voltage higher than 5 volts switches relay and 12 volt LED
    }



Serial.print("INPUT V= ");
Serial.println(vin,2);
delay(500);
}

w1r3d:
Hi guys,
I am new to arduino but I am not letting that slow me or stop me.
I have been playing around this evening with the basic functions I want to incorporate into my current project I am using a UNO R3, 0-25v Voltage Sensor, 1 channel 12 volt arduino relay and a 12 volt LED.

I need to be able to detect changes in Voltage (I am using 0-25v Voltage Sensors) and power 12v LEDs when over a set voltage (via a 12v relay).

How would I write in another Voltage Sensor? I plan on having up to 6 if I need more I will change to a MEGA

I have not incorporated my last requirement however I need to be able to detect if a circuit is open or closed. I guess I would maybe use a ohm to ground??

However I cant get the damed LED to light up the wiring is sound as I am using a sensor shield which makes building the project so much easier.

Please help any assistance you can offer is greatly appreciated.

int analogInput = A0; // 0 - 25 voltage sensor

int LEDpin = 1; // connected to 12 volt relay and 12 volt LED
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
int value = 0;

void setup(){
  pinMode(analogInput, INPUT);
  pinMode(LEDpin, OUTPUT);
 
  Serial.begin(9600);
 
}
void loop(){
  // read the value at analog input
  value = analogRead(analogInput);
  vout = (value * 5.0) / 1024.0; // see text
  vin = vout / (R2/(R1+R2));
 
  if(analogInput<=5)
  {
    digitalWrite(LEDpin, LOW); //voltage lower than 5 volts no 12 volt LED
    }
 
  if(analogInput>=5)
  {
    digitalWrite(LEDpin, HIGH); // voltage higher than 5 volts switches relay and 12 volt LED
    }

Serial.print("INPUT V= ");
Serial.println(vin,2);
delay(500);
}

analogInput = pin A0 not the analog level on the pin , but “value” is.

I am using 0-25v Voltage Sensors

Which one?

   pinMode(analogInput, INPUT);

Analog pins are input only. The pinMode() function does NOTHING to analog pins. It could possibly be fking with a pin you don’t want fked with.

How would I write in another Voltage Sensor?

Use an array of pin numbers, an array of pin values, etc.

I believe the voltage sensor is a VDS001 or similar to ebay item 181264851574.

I will adjust the analogue input in the morning and give it ago.

Thanks for the tips.

Am i using the correct format and coding where i have my "if" actions for the triggering of the relay? In particular where i have tried to use the value of 5 volts??

This is my first solo project

I have read about arrays but do not fully understand but that bridge when i get there.

Am i using the correct format and coding where i have my “if” actions for the triggering of the relay?

No, because you are basing the conditions on the pin number, not the value read from the pin.

If you use Pin in the name of all variables that contain pin numbers and Value in the names of all variables that contain values read from the pin, it is unlikely that you would have written:

   if(analogInputPin <= 5)
   {

Thanks PaulS Do i need to state the pin number? A0? Is the code smart enough to know i am referring to it's volt age number not the raw value since i have placed the statement after the voltage conversion data?

Is the code smart

Code is smart. Computers are stupid. Stupid as a box of rocks. They do only what you tell them to do.

If you tell the Arduino to use the value of a chocolate pie to calculate the area of a circle, it will not know that you meant to use the value of PI.

You said chocolate pie, you get chocolate pie.

So, no the Arduino does not know that you meant this or that. What you said is what it does.

So by placing the recipe for the chocolate pie before asking it to calculate the area of the circle will my code work??

I have taken advise and a good googling session but I still can not get the relay to trigger…
Below is a new version of similar code, how do I get the relay to trigger???
My test should have a blinking LED via the Relay if the voltage is below 5 volts and a solid light if the voltage is above 5 volts.

Please someone, I seem to be stuck and jjust need a little bit of help to get over this bump in the road

int LEDpin = 0;         // relay-pin powers LED
int analogInPin = A0;   // Voltage sensor 'S'

float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; 
float R2 = 7500.0;
int value = 0;


void setup () {
 Serial.begin(9600);                            
 pinMode(LEDpin, OUTPUT);                  // LED via relay
}


void loop () {
 
   value = analogRead(analogInPin);  // read voltage at A0
   vout = (value * 5.0) / 1024.0; 
   vin = vout / (R2/(R1+R2)); 


 if(vin < 5) {  
   delay(100); 
   digitalWrite(LEDpin, HIGH); // turn on LED to indicate voltage above 5 volts
 }
else {
 digitalWrite(LEDpin, HIGH);   // Blink LED as voltage is less than 5 volts
 delay(100);                
 digitalWrite(LEDpin, LOW); 

 }
 
Serial.print("INPUT V= "); // display voltage from A0 with 2 decimal points
Serial.println(vin,2);
delay(500);
}

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png? What type of relay PCB do you have, some require LOW input to activate the relay. Have you got the gnd of the relay PCB connected to the gnd of the arduino?

Also put another delay(100) after

digitalWrite(LEDpin, LOW);

Thanks ..Tom...... :)

I have taken advise and a good googling session but I still can not get the relay to trigger...

Then, dump all of that code. In setup(), declare (one of the) relay pin(s) as OUTPUT. In loop(), do nothing more than toggle the pin(s) off or on, with a 1000 millisecond delay(). If the relay(s) do(es) not toggle, then the problem is with the hardware.

   value = analogRead(analogInPin);  // read voltage at A0
   vout = (value * 5.0) / 1024.0;
   vin = vout / (R2/(R1+R2));

value will be between 0 and 1023.
vout will be between 0 and 4.995
vin will be between 0 and 0.999

 if(vin < 5) { 
   delay(100);
   digitalWrite(LEDpin, HIGH); // turn on LED to indicate voltage above 5 volts
 }
else {
 digitalWrite(LEDpin, HIGH);   // Blink LED as voltage is less than 5 volts
 delay(100);               
 digitalWrite(LEDpin, LOW);

 }

So, the if test will ALWAYS be true. The comments are flat out wrong.

Sorry for any confusion regarding the relays, voltage sensor and LEDs.

The 6 analogue input pins will be the 6 voltage sensors which are connected to the Factory sensors of the engine and chassis.

These 6 inputs each have a individual relay output on an 12 volt 8 channel octcoupler and a 12 volt LED positioned on the dash cluster. I have chosen the 12 volt relays and LEDs as they are easily found and due to the construction each to swap if found faulty. Being able to change parts in and out is vital to keep the systems functioning and troubleshoot any fault or worn out parts

Outputs 7 and 8 on the 12 volt 8 channel octcoupler will indicate the status of the arduino system itself as I will not have any lcd display. A green LED will indicate that the arduino system has power and error free. The Red LED will then indicate if there are errors within the arduino system not the car.

I hope this helps you get an idea of my reasions for the selection of parts and the format of the required code.
I will be back at the warehouse over the weekend. I look foward to combining both the last sketch from Archibald (with a little changes) and the error code of Koepel.

Archibald’s code

float vin;                           // now not an array
float R1=30000, R2=7500;             // R2 connected to ground
float vpb = (R1+R2)/R2 * 5/1024;     // volts per bit
float vmax;
int index;

void setup()
{
}

void loop()
{
  vmax = 0;
  
  for(index=0 ; index<=5 ; index++ )
  {
    vin = analogRead(index)*vpb;
    digitalWrite(index,vin>3.0);          // will write HIGH or LOW to digital outputs 0 to 5
    if(vin>vmax) vmax = vin;
  }

  if(vmax>=3.0) digitalWrite(6, HIGH);    // assuming the red and green LEDs are on output 6
  
  delay(1000);
}

Koeple’ s code

int inputerror;

setup() {
  ...
}

loop() {
  ...
  
  // test sequence
  inputerror = 0;                               // start with zero errors
  for(int i=0; i<NUM_INPUTS; i++) {
    // do the tests
    ...
    if ( vin < trigger[i] ) {                 // test for wrong input voltage
      inputerror++;                          // increase error count
    }
  }

  if ( inputerror == 0) {
    // okay, no errors, turn green led on
    ...
  }
  else {
    // there was (one or more) errors !
    // turn red led on
    ...
  }

Thanks for all the help I will post pics when I have it installed.