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.