Hi,
I am trying to use two inputs to activate output pin 8.
The application is to have output pin 8 activate a 5 volt relay when the arduino receives input from pin 5 or 9. I have the inputs wired in the same way, but for some reason only when input 9 is activated am I able to get proper voltage to activate the relay. When input 5 is activated, output pin 8 only gives 2.3 volts. If you see anything wrong with my code could you let me know?
Code below:
void setup() {
pinMode(5, INPUT); // sets the digital pin 5 as input
pinMode(8, OUTPUT); // sets the digital pin 8 as output
pinMode(9, INPUT); // sets the digital pin 9 as input
digitalWrite(8, LOW); // sets the digital pin 8 off
delay(7000); // waits for 7 seconds
}
void loop() {
if (digitalRead(9) == LOW) { // check if input 9 is made
digitalWrite(8, LOW); // sets the digital output pin 8 off
} else {
digitalWrite(8, HIGH); // sets the digital output pin 8 on
delay(7000); // waits for 7 seconds
digitalWrite(8, LOW); // sets the digital output pin 8 off
delay(2000); // waits for 2 seconds
} if (digitalRead(5) == HIGH) { // check if input 2 is made
digitalWrite(8, HIGH); // sets the digital output pin 8 on
} else {
digitalWrite(8, LOW); // sets the digital output pin 8 off
}
}
I can't see your code.
I can't see your schematic
There are some delays in the code associated with pin 9. Enough for the relay to switch. With pin 5, there are not. I suspect that input 5 at least is switching rapidly and the 2.3V that you measure is actually changing quickly between 0 and 5. A scope or some serial prints will confirm.
Or put some delays in for the pin 5 code too, as a test.
Edit: Or of course, even if pin 5 code switches the relay, it may be that pin 9 code immediately turns it off.
To be fair, the OP did at least attach his code. Not the most convenient way to present it but better than nothing
void setup() {
pinMode(5, INPUT); // sets the digital pin 5 as input
pinMode(8, OUTPUT); // sets the digital pin 8 as output
pinMode(9, INPUT); // sets the digital pin 9 as input
digitalWrite(8, LOW); // sets the digital pin 8 off
delay(7000); // waits for 7 seconds
}
void loop() {
if (digitalRead(9) == LOW) { // check if input 9 is made
digitalWrite(8, LOW); // sets the digital output pin 8 off
} else {
digitalWrite(8, HIGH); // sets the digital output pin 8 on
delay(7000); // waits for 7 seconds
digitalWrite(8, LOW); // sets the digital output pin 8 off
delay(2000); // waits for 2 seconds
} if (digitalRead(5) == HIGH) { // check if input 2 is made
digitalWrite(8, HIGH); // sets the digital output pin 8 on
} else {
digitalWrite(8, LOW); // sets the digital output pin 8 off
}
}
ceiynck - I have posted your code in code tags as advised in How to use this forum
Please do this in future when posting code
Please post details of your circuit such as how the inputs are wired
Pin 9 is LOW, turn relay OFF
Pin 5 is HIGH, turn relay ON
Repeat
Pin 9 is HIGH, turn relay ON
do nothing for 7 seconds
turn relay OFF
do nothing for 2 seconds
Pin 5 is LOW, turn relay OFF (again)
Repeat
Alright,
I added the code in the original post and added my schematic.
Here is the updated code that ended up working. I added a delay as suggested and now this works as intended. Thank you all for you help!
void setup() {
pinMode(5, INPUT); // sets the digital pin 5 as input
pinMode(8, OUTPUT); // sets the digital pin 8 as output
pinMode(9, INPUT); // sets the digital pin 9 as input
digitalWrite(8, LOW); // sets the digital pin 8 off
delay(7000); // waits for 7 seconds
}
void loop() {
if (digitalRead(9) == LOW) { // check if input 9 is made
digitalWrite(8, LOW); // sets the digital output pin 8 off
} else {
digitalWrite(8, HIGH); // sets the digital output pin 8 on
delay(7000); // waits for 7 seconds
digitalWrite(8, LOW); // sets the digital output pin 8 off
delay(2000); // waits for 2 seconds
} if (digitalRead(5) == LOW) { // check if input 5 is made
digitalWrite(8, HIGH); // sets the digital output pin 8 on
delay(100);
} else {
digitalWrite(8, LOW); // sets the digital output pin 8 off
}
}