Help request for a code modification for using two timer/relays

I have been using this setup, and it has been working great for my needs: Resistance to voltage converter for activating a timer/relay module
Here is the corresponding code:


```cpp
const int sensorPin = A0;  // Analog input pin  
int relayPin = A1;        // Digital output pin that tiggers relay
int sensorValue = 0;       // sensorPin default value
float Vin = 5;             // Input voltage
float Vout = 0;            // Vout default value
float Rref = 1000000;      // Reference resistor's value in ohms (you can give this value in kiloohms or megaohms - the resistance of the tested resistor will be given in the same units)
float R = 0;               // Tested resistors default value

void setup ()
{
  Serial.begin(9600);      // Initialize serial communications at 9600 bps
}

void loop ()
{
  sensorValue = analogRead(sensorPin);  // Read Vout on analog input pin A0 (Arduino can sense from 0-1023, 1023 is 5V)

if (sensorValue < 800)
  analogWrite(relayPin, 200);

if (sensorValue > 800)
  analogWrite(relayPin, 0);
}

Now, I need to add another timer/relay module that will be activated using the same output signal. I tried to use the A1 output common to both timer/relay modules, but these modules started clicking erratically. Thinking this was too much of a power draw, I created another relayPin and assigned it to A3 (below), which I would use for the added timer/relay. The only change is int relayPin = A1 and A3. It didn't work because the original relay no longer worked. I admit I know next to nothing about making these codes, and I feel pretty stupid, but I am in the process of learning more.

const int sensorPin = A0;  // Analog input pin  
int relayPin = A1 and A3;  // Analog output pin that tiggers relays
int sensorValue = 0;       // sensorPin default value
float Vin = 5;             // Input voltage
float Vout = 0;            // Vout default value
float Rref = 1000000;      // Reference resistor's value in ohms (you can give this value in kiloohms or megaohms - the resistance of the tested resistor will be given in the same units)
float R = 0;               // Tested resistors default value

void setup ()
{
  Serial.begin(9600);      // Initialize serial communications at 9600 bps
}

void loop ()
{
  sensorValue = analogRead(sensorPin);  // Read Vout on analog input pin A0 (Arduino can sense from 0-1023, 1023 is 5V)

if (sensorValue < 800)
  analogWrite(relayPin, 200);

if (sensorValue > 800)
  analogWrite(relayPin, 0);
}

Is this a reasonable approach, and if so, how do I create another relayPin?

No

Do you understand what an analogWrite to an analogue pin gives you?
EDIT:-
On an Nano every if you write anything higher that 127 then you will get about 4.7V, otherwise you will get nothing.

What do you think this does?

You might think it does, but no one agrees with you on that thread. I think you are fooling yourself.

1 Like

I figured you'd be back with this.
Have you located a schematic for the input of that module?

You didn't. There is no additional relayPin in your code.

Is this your intended pin configuration?

int relayPin1 = A1;
int relayPin3 = A3;

Or will you only be hard-wiring the new relay to A1?

I will keep the original timer/relay on A1 and then the additional one hooked to A3. I tried this below:


```cpp
const int sensorPin = A0;  // Analog input pin  
int relayPin = A1;        // Digital output pin that tiggers relay
int relayPin2 = A3;
int sensorValue = 0;       // sensorPin default value
float Vin = 5;             // Input voltage
float Vout = 0;            // Vout default value
float Rref = 1000000;      // Reference resistor's value in ohms (you can give this value in kiloohms or megaohms - the resistance of the tested resistor will be given in the same units)
float R = 0;               // Tested resistors default value

void setup ()
{
  Serial.begin(9600);      // Initialize serial communications at 9600 bps
}

void loop ()
{
  sensorValue = analogRead(sensorPin);  // Read Vout on analog input pin A0 (Arduino can sense from 0-1023, 1023 is 5V)

if (sensorValue < 800)
  analogWrite(relayPin, 150);
  analogWrite(relayPin2, 150);

if (sensorValue > 800)
  analogWrite(relayPin, 0);
  analogWrite(relayPin2, 0);
}

The A1 still works (goes from 1 mV to 4.6V), but the A3 goes from 490 mV to about 250 mV weird. I changed the program to relayPin2 = A4 and the A4 pin had the same voltage change.

I will keep the original timer/relay on A1 and then the additional one hooked to A3. I tried this below:

```cpp
const int sensorPin = A0;  // Analog input pin  
int relayPin = A1;        // Digital output pin that tiggers relay
int relayPin2 = A3;
int sensorValue = 0;       // sensorPin default value
float Vin = 5;             // Input voltage
float Vout = 0;            // Vout default value
float Rref = 1000000;      // Reference resistor's value in ohms (you can give this value in kiloohms or megaohms - the resistance of the tested resistor will be given in the same units)
float R = 0;               // Tested resistors default value

void setup ()
{
 Serial.begin(9600);      // Initialize serial communications at 9600 bps
}

void loop ()
{
 sensorValue = analogRead(sensorPin);  // Read Vout on analog input pin A0 (Arduino can sense from 0-1023, 1023 is 5V)

if (sensorValue < 800)
 analogWrite(relayPin, 150);
 analogWrite(relayPin2, 150);

if (sensorValue > 800)
 analogWrite(relayPin, 0);
 analogWrite(relayPin2, 0);
}

The A1 still works (goes from 1 mV to 4.6V), but the A3 goes from 490 mV to about 250 mV weird. I changed the program to relayPin2 = A4 and the A4 pin had the same voltage change.

The pins A0-A5 are the analog INPUT pins. None of them can be used in analogWrite() command.
Use the digitalWrite() instruction instead.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.