Use the same digitalPIN to write and read a relay status

Hi,

I have an Arduino MEGA board and a 8 Relay Module. I've connected both boards as:

5V Arduino to VCC Relay
GND Arduino to GND Relay
13 digital PIN (PMW) Arduino to IN1 Relay.

I want to use the same digitalPIN to change the relay status (HIGH/LOW) and check if it works properly by reading the status (1/0), respectively using the void loop().

This is my code:

const int relay1 = 13; // the number of the relay1 pin
void setup() {
pinMode(relay1, OUTPUT);
Serial.begin (9600);
}

void loop(){
digitalWrite(relay1, HIGH);
pinMode(relay1, INPUT);
Serial.println(digitalRead(relay1));
delay(10000);
pinMode(relay1, OUTPUT);
digitalWrite(relay1, LOW);
pinMode(relay1, INPUT);
Serial.println(digitalRead(relay1));
}

I've run it but it reads 0 all the time. What I'm doing wrong?

Thanks.

Ok it’s about time you learned how to post code.
See the icon top right that looks like </>
Press that and post your code between the two square barcketed words that appear.

Go back to your first post and correct it.

REM14:
I have an Arduino MEGA board and a 8 Relay Module. I've connected both boards as:

5V Arduino to VCC Relay
GND Arduino to GND Relay
13 digital PIN (PMW) Arduino to IN1 Relay.

I shall ignore your code until you follow Mike's instructions. You may refer to the forum instructions also.

But how are you going to power this? You cannot power relays from the Arduino board. You need a regulated 5 V supply to power the relay module and the Arduino via the 5 V pin.

Not sure why people seem to want to use Mega2560s either - cumbersome and unnecessarily expensive board for the vast majority of applications.

Hi,

I have an Arduino MEGA board and a 8 Relay Module. I've connected both boards as:

5V Arduino to VCC Relay

GND Arduino to GND Relay
13 digital PIN (PMW) Arduino to IN1 Relay.

I want to use the same digitalPIN to change the relay status (HIGH/LOW) and check if it works properly by reading the status (1/0), respectively using the void loop().

This is my code:

const int relay1 = 13; // the number of the relay1 pin
void setup() {
pinMode(relay1, OUTPUT);
Serial.begin (9600);
}

void loop(){
digitalWrite(relay1, HIGH);
pinMode(relay1, INPUT);
Serial.println(digitalRead(relay1));
delay(10000);
pinMode(relay1, OUTPUT);
digitalWrite(relay1, LOW);
pinMode(relay1, INPUT);
Serial.println(digitalRead(relay1));
}

Relay don't have signal output to measure if relay did work. When You send signal from digital pin as OUTPUT to relay input, its just that, You don't have ability to disconnect that relay input and measure that input if is high or low.
You should use another pin on arduino and by two resistors connect them to output of relay. If You use high voltage, then NEVER don't do THAT! You will destroy arduino.

Maybe I can add some idea.
Try use electronic switch as flip-flop. This switch could give some idea in Your project.

But still You must use second pin on arduino to measure output of that switch, otherwise input will collide with output and switch will work not properly.

Ps, to insert Your code, first option before fat B. I did this same mistake with my first posts.

Ps. 2 : Its possible to use arduino pin as output and later as input. Like charging small bartery. When You turn on pin as output for 1 minute, then turn this pin ass INPUT to check what voltage actually this battery have, and if bartery needs more charging or not.

Thank you maxim2511, I see it now.

Thanks for modifying that post.

Your code will work if you just remove the three pinMode function calls in the loop function.

Thank you Grumpy_Mike, is it worthy to do it like that to assure the well performance of the relay? I mean, should be the relay feed in order to check its status?

is it worthy to do it like that to assure the well performance of the relay?

No, in fact it is useless. All it does is to see that the output has been set, it tells you nothing about the relay, nor the voltage going to it.