hi all im trying to control 2 relay independently with 2 potentiometers, ive got them working but they both work the same things they arnt independant, any ideas?
relayonoffonx2channels.ino (1.5 KB)
` i must be missing something hopefully my code is attached
What does that mean? With 2 potentiometers, do they BOTH control the relay? What are you expecting them to do independently? Oh, and you have been reading the forum long enough to know how to post your code properly.
That’s not the requested way of posting code.
i want one pot to turn on one relay above and below the mid point, off at the mid point and the other to do the same thing on the other relay at the moment they are "joined" somehow, ive tryed posting the code but it told me to do what i did, so thats what i did, i find it very hard to navigate the forum so please excuse if i make mistakes
Now we have two relays. That is new. But, asked again, are each of the potentiometers affecting the other’s relay, or are they each affecting their own relay?
Hi, @minidog1
Can you post some images of your project?
So we can see your component layout.
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Tom....
![]()
I have stated at the beginning i have 2 relays,,
Atm One pot controls its own relay and the other,
The other pot also controls its own relay and the other,
Centre pin on pot 0 goes to pin A0
Centre pin on pot 2 goes to pin 1
Relay 1 goes to pin 7
Relay 2 goes to pin 8
I will try again`
"and the other" means what exactly? Post some image or schematic.
You need to use analog pins for both if you are willing to read the pot...
Sorry thats A1, ive got them both A1 and A0 to serial monitor and giveing readings from 0 to 1000 with 2 separate feeds to s/m but they both give the same reading in s/m weather u plug the pot into A1 or A0 ie if u have it wired to A0 you get a reading from A0 and A1 in s/m
Your code doesn't present any connection between one and other.
What is s/m?
You should read the pots twice to reduce "crosstalk" between them.
void loop()
{
pot1 = analogRead(A0);
pot1 = analogRead(A0);
pot2 = analogRead(A1);
pot2 = analogRead(A1);
Serial monitor =s/m
Nice one thank you i will try that, thanks for your help, very new to this but not very good with computers but realy trying to learn it
So you did. sorry.
Are you actually powering the relay from an Arduino pin or are you using a relay module that is powered separately? Schematic or block diagram, please.
I like to add formatting, and remove delays for responsiveness. I did not add the "read each pot twice" from the above advice (maybe you can add that). This sketch will show the readings ONLY if any potentiometer reading changes (so your Serial Monitor is not flooded).
const int potPin = A0; // Potentiometer analog pin
const int potPin1 = A1; // Potentiometer analog pin
const int relayPin = 8; // Relay control pin
const int relayPin1 = 7; // Relay control pin
const int midpoint = 512; // Midpoint of 10-bit ADC
const int tolerance = 20; // Acceptable range around midpoint
int potValueOld, pot1ValueOld; // ADD THESE VARIABLES
// Optional: smoothing filter
int smoothRead(int pin, int samples = 10) {
long total = 0;
for (int i = 0; i < samples; i++) {
total += analogRead(pin);
delay(1); // Small delay for stable readings
}
return total / samples;
}
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Relay OFF initially
pinMode(relayPin1, OUTPUT);
digitalWrite(relayPin1, LOW); // Relay OFF initially
Serial.begin(115200); // INCREASE BAUD FROM 9600
}
void loop() {
int potValue = smoothRead(potPin);
delay(1); // ADD DELAY BETWEEN LAST READ AND NEXT READ
int pot1Value = smoothRead(potPin1);
if (potValueOld != potValue || pot1ValueOld != pot1Value) { // ADD THIS WRAPPER
potValueOld = potValue; pot1ValueOld = pot1Value; // STORE NEW VALUES
// Debug output
Serial.print("Potentiometer:"); //pot1
spacepad(potValue);
Serial.print(" Potentiometer1:"); // pot 2
spacepad(pot1Value);
Serial.println(); // ADD THIS FORMATTING CRLF
}
// Check if within midpoint range pin 8
if (potValue >= (150) && potValue <= (850)) {// above and below centre
digitalWrite(relayPin, HIGH); // Turn relay ON
} else {
digitalWrite(relayPin, LOW); // Turn relay OFF
}
//second output pin7
if (pot1Value >= (450) && pot1Value <= (550)) { // above and below centre
digitalWrite(relayPin1, HIGH); // Turn relay ON
} else {
digitalWrite(relayPin1, LOW); // Turn relay OFF
}
// delay(100); // Loop delay to reduce flicker // DELETE THE DELAY
}
void spacepad(int val) { // ADD THIS FORMATTING FUNCTION
if (val < 1000) Serial.print(" "); // 100 - 999
if (val < 100) Serial.print(" "); // 10 - 99
if (val < 10) Serial.print(" "); // 0 - 9
Serial.print(val);
}
@xfpd Doesn’t prevent Serial flooding, due to propensity for least bit to flicker, resulting in printing on every pass. Yes, even with smoothing, as the pot value can still sit at a threshold. Imagine, returns of
512, 512, 512, 512, 512, 512, 512, 511 (7 x 512, 1 x 511)
that will average to 511(4095 >>3). Change that 511 to 512, or any one 512 to 513, and the return value is then 512.
I don’t think that’s what you intended. Really, difference thresholding must be done to make this robust. Better for simple code for a beginner to just use delay(), or better, millis() scheduling.
A deadband could be added... but I think OP's original is good enough for the purpose.
it did seem to like that and woulndnt load but yes that seems to be the problem "cross talking" Use code tags to format code for the forum
const int potPinA0 = A0; // Potentiometer analog pin
const int potPinA1 = A1; // Potentiometer analog pin
const int relayPin8 = 8; // Relay control pin
const int relayPin7 = 7; // Relay control pin
const int midpoint = 512; // Midpoint of 10-bit ADC
const int tolerance = 20; // Acceptable range around midpoint
// Optional: smoothing filter
int smoothRead(int pin, int samples = 10) {
long total = 0;
for (int i = 0; i < samples; i++) {
total += analogRead(pin);
delay(8); // Small delay for stable readings
}
return total / samples;
}
void setup() {
pinMode(relayPin8, OUTPUT);
digitalWrite(relayPin8, LOW); // Relay OFF initially
pinMode(relayPin7, OUTPUT);
digitalWrite(relayPin7, LOW); // Relay OFF initially
Serial.begin(9600);
}
void loop() {
int potA0Value = smoothRead(potPinA0);
int potA1Value = smoothRead(potPinA1);
// Debug output
Serial.print("PotentiometerA0: "); //pot1
Serial.println(potA0Value);
Serial.print("PotentiometerA1: "); // pot 2
Serial.println(potA1Value);
// Check if within midpoint range pin 8
if (potA0Value >= (150) && potA0Value <= (850)) {// above and below centre
digitalWrite(relayPin8, HIGH); // Turn relay ON
} else {
digitalWrite(relayPin8, LOW); // Turn relay OFF
}
//second output pin7
if (potA1Value >= (450) && potA1Value <= (550)) { // above and below centre
digitalWrite(relayPin7, HIGH); // Turn relay ON
} else {
digitalWrite(relayPin7, LOW); // Turn relay OFF
}
delay(100); // Loop delay to reduce flicker
}