I have a problem where I need to use the opposite HIGH LOW commands to get and output to do what I want.
When I write digitalWrite (LEDpin, HIGH); it goes LOW and turns off and vice versa when LOW ,led turns on
Has anybody come across this??
I have a problem where I need to use the opposite HIGH LOW commands to get and output to do what I want.
When I write digitalWrite (LEDpin, HIGH); it goes LOW and turns off and vice versa when LOW ,led turns on
Has anybody come across this??
Yes, the LED can be wired differently, so that it is either on or off when the output is HIGH. How is yours wired?
See LEDs D7 vs D12
So, what's wrong with simply writing LOW instead of HIGH?
A cleaner way might be to 'simply' use e.g. ON and OFF. Works great if all LEDs are wired consistently.
At the top of your code
#define ON LOW
#define OFF HIGH
And in the code
digitalWrite(LEDpin, ON);
delay(1000);
digitalWrite(LEDpin, OFF);
delay() only for demo purposes
Dave1024:
I have a problem where I need to use the opposite HIGH LOW commands to get and output to do what I want.When I write digitalWrite (LEDpin, HIGH); it goes LOW and turns off and vice versa when LOW ,led turns on
Has anybody come across this??
are you saying that when you digitalWrite (LEDpin, HIGH) that the pin actually goes low ?
or, are you saying that you have to write digitalWrite (LEDpin, LOW) to make the LED's light ?
if you wire your LED to 5v, then to the pin, you have to bring that pin LOW to complete the circuit.
if you wire your LED to the pin, then the LED to ground, you have to bring that pin HIGH to complete the circuit.
although the suggestion by sterretje is good, simple, and works, it does not force you to think about what you are doing.
it only works on those pins that you have to pull to ground to turn on.
I think we all ran across this concept when we first started.
another way is to use the inversion of the value, the asterick.
digitalWrite(LEDpin, !HIGH); // pulls pin to ground
it says high to you, but it says opposite of high to the controller.
dave-in-nj:
although the suggestion by sterretje is good, simple, and works, it does not force you to think about what you are doing.
it only works on those pins that you have to pull to ground to turn on.
As I said, works great if all LEDs are connected consistently.
And you should not have to think You know
Now we have to think too, gee. :o
Hi,
Yes the pin goes high when writing LOW. Does the opposite to what I tell it.
I am currently using the opposite command (LOW/HIGH) to get what I want.
The LED was used as an example. I am building a cheap basic car alarm for my work van.
The outout is actually connected to a 4 way chinese relay board with low level inputs. This is my first project that I have completed and the code is basically done.
It is good to know that the pin grounds to 0v when low. I thought the power was simply disconnected from that pin somehow. That is very useful linfo.
Thanks to all who are commenting. :o
It's not the pins that go low when a high is written. It's the relay board. It inverts the logic sense. Put a multimeter on the pins and you will see.
If your relay board has the JD-VCC jumper, here's the best way to connect it.
Try putting an "!" Before "HIGH"
digitalWrite(LEDpin, !HIGH);
#3 was the right answer.
My brain hurts!
#3 was the right answer.
As mentioned, only if all LEDs are wired the same thou.
.
LarryD:
As mentioned, only if all LEDs are wired the same thou.
.
Then you should encapsulate the LEDs along with their polarity. There are different methods for doing that.
Agreed.
Since I was in hardware, I look at the schematic and write the code to control things from that level.
A simple comment that says:
//LOW turns on the LED
or
//HIGH turns on the LED
is my method.
LarryD:
Agreed.
Since I was in hardware, I look at the schematic and write the code to control things from that level.
A simple comment that says:
//LOW turns on the LED
or
//HIGH turns on the LED
is my method.
The (I guess, hypothetical) question was, what if some of the LEDs were wired differently than the others, so that a LOW turns on some LEDs, but a HIGH turns on others?
const byte relay1Pin = 2;
const byte relay2Pin = 3;
const byte relayON = 0;
const byte relayOFF = 1;
void setup() {
pinMode(relay1Pin, INPUT_PULLUP); // make pinMode default HIGH (relay1 OFF)
pinMode(relay2Pin, INPUT_PULLUP); // make pinMode default HIGH (relay2 OFF)
pinMode(relay1Pin, OUTPUT); // defaults HIGH, Relay1 OFF
pinMode(relay2Pin, OUTPUT); // defaults HIGH, Relay2 OFF
}
void loop() {
digitalWrite(relay1Pin, relayON);
delay(500);
digitalWrite(relay2Pin, relayON);
delay(500);
digitalWrite(relay1Pin, relayOFF);
delay(500);
digitalWrite(relay2Pin, relayOFF);
delay(500);
}
OK, just to make sure I have it straight......
when the output goes HIGH, the LED's go low, but they are not LED's they are relays, and the output does go high, it is just that the signal goes low, at least some of the time, the rest of the time, it could be the other way around ?
does that sum it up ?
when the output goes HIGH, the LED's go low, but they are not LED's they are relays, and the output does do high, it is just that the signal goes low, at least some of the time, the rest of the time, it could be the other way around ?
:o