Configuring digital pins only if an input is received in Arduino UNO

Hello Everyone,

I am a newbie in terms of using Arduino. As the subject says, my question is if we can control a digital pin to send output current only if some other digital pin receives input.

I have a nodemcu esp 8266 connected to my Arduino UNO. So, the result I am trying to achieve is if my Digital pin D12 recieves an input from Nodemcu, then and then only my pin D11 should send output. otherwise it should remain off. I want to write a sketch for the same but cant figure how to write it. Maybe by using some if-else statements ? or any other way ?

Hope I am able to explain my requirement clearly. Any help would be appreciated

Cheers,
Aditya

Yeah maybe.

Look at some examples in the IDE.

digitalRead to check input pins
digitalWrite to set output values

if statements to figure, well, if you wanna. Whatever.

Extremely basic programming, start reading.

a7

The blink without delay example under the Arduino IDE File>Examples menu would be one place to start. It contains an if statement.

The Arduino language reference is a good resource as well as the examples that come with the Arduino IDE.

Thanks for your reply.

I actually tried using digitalread() and digitalwrite() functions with if statement but can't figure out how to implement the logic. Always end up getting some error.

If you could provide a sketch, it would be of great help. or even a skeleton of the sketch will work.

Cheers,
Aditya

Post the code that you tried. Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Tell us what the code actually does and how that differs from what you want.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

We do not usually write code for people. We gladly help you to get code that you write to work.

+1 for the idea of posting what you tried and why it didn't work.

Either because you got the syntax wrong, and your code didn't even get to the point of actually running, or you got the logic wrong and it didn't do what you wanted it to.

a7

take a look at file - Examples - Control - ifStatementConditional in the Arduino examples. You'll get your answers there.

What does the "input" received on Pin 12 look like?

its a 3V input from the Nodemcu

A valid HIGH on an Uno is greater than 0.6 * Vcc which would be 3.0V with default 5V Vcc so 3.3V should read as as valid HIGH.

A valid LOW on an Uno is less than 0.3 * Vcc which would be 1.5V with default 5V Vcc. So if the ESP sends less than, say, 1V for a LOW it should be read properly as LOW.

Be aware that you can, safely, send from the ESP to the Uno with no external hardware, but to send from the Uno to the ESP a level shifter is required because the ESP cannot safely handle 5V on any of its inputs. The level shifter can be a simple voltage divider or MOSFETS and other methods for level shifting.

1 Like
const byte InputPin = 12;
const byte OutputPin = 11;

void setup()
{
  pinMode(InputPin, INPUT);
  pinMode(OutputPin, INPUT); // "Disconnected"
}

void loop()
{
  if (digitalRead(InputPin) == HIGH)
  {
    pinMode(OutputPin, OUTPUT);
    pinMode(OutputPin, HIGH);
  }
}

thanks. will surely give it a try. I was struggling on the if() part where you checked for HIGH and configured OUTPUT for that pin using pinmode

What's the benefit to / purpose of / reasoning behind keeping the pin as an input until it's time to make it a high output?

Well now, while @johnwasser's code does that in a manner, certain things are missing, in significant part because you failed to fully specify your requirement. :roll_eyes:

That code omits setting pinMode to OUTPUT at the start of the code, because he interpreted "otherwise it should remain off" as "not connected to any voltage" rather than logic LOW. When it does receive an input HIGH, it generates an output HIGH, presumably what you meant by "send output".

What you did not specify, is what should happen when the input returns LOW. So his code leaves the output HIGH forever, whatever happens to the input.

Is that what you intended? :face_with_raised_eyebrow:

I want the arduino pin 11 to send output only when its neighbour 12 gets some input from node mcu. I have bridged pin 12 and node mcu pin D3. where D3 sends input only when I say to turn on the lights (its a home automation project btw. sorry for not clarifying that earlier).

I have already achieved this using only node mcu and relay but wanted to achieve using arduino also.

Cheers,
Aditya

Ok. How about defining "send output" and "get some input"? Normally, anyone with any experience speaks of "when input is HIGH, Set output HIGH", not "sending and not sending output". This is why you are receiving less-than-complete answers.
Please take the time to learn a little bit about the terminology required to communicate unambiguously in both the hardware and software worlds. That will help you, and help others help you.
C

1 Like

Well johnwasser did answer to @adityachitale's satisfaction and he ticked that as a solution.

OP's last post was in response to my last question (after the selection of johnwasser's answer as solution) about the point of keeping the pin as an input until it needs to be made high as an output.

I can't see any point in that, so am curious still to know why that was desirable. It's more usual @adityachitale to do all the pinMode()s in setup- it would be normal to make the pin an output up front, but make it low then, and make it high later when required.

Perhaps as a safety feature if you mess up the code, and write it high too soon, then if it's still an input it won't output a high prematurely. But somehow I doubt that was OP's intention. I would, in the absence of a pressing reason, put pinMode() it as output at the start,

Just noticed this error in @johnwasser's solution... that should be a digitalWrite().

Oops. Sorry.