input/output - need pullups?

Hi, all!
I'm trying to get pin 7 to reflect the state of pin 19:

void setup() {
  pinMode (19, INPUT);
  pinMode (7, OUTPUT);
}

  int in;

void loop() {
  in = digitalRead(19);
  digitalWrite(7,in);
}

When I load this, pin 7 just goes high, and doesn't respond at all. Do I need a pullup resister or something somewhere?

This is probably a very noob question, but I'm so new to microcontrollers I still have afterbirth in my ears. :-/

It's hard to say without seeing a picture of your setup or at least a schematic.

But you can turn on an internal pull-up resistor. In your setup, after you turn the pin to an Input, use digitalWrite(19, HIGH); and that will enable the internal pullup.

pinMode(19, INTPUT);
digitalWrite(19, HIGH);
pinMode(7, OUTPUT);

Just like that. Then you'll connect the other side of the Button to Ground.

Also, you could just use:

digitalWrite(7, digitalRead(19));

:slight_smile:

Good luck, hope those help.

void setup() {
  pinMode (19, INPUT);
  digitalWrite(9, HIGH);
  pinMode (7, OUTPUT);
}
  int in;

void loop() {
  in= digitalRead(19);
  digitalWrite(7,in);
}

Now it comes on when I put my hand near the board and turns off when I move it away. :-? It doesn't matter if 19 is grounded or not, same behavior.

I just have a 10-segment display hooked to pins 2-11 (which I was using for a tracer, which worked just fine), and seeing that the impedance is so high for the pins, I'm just shorting 19 to ground.

I have a Duemilanove, with the 10-segment, as described. When I upload this:

void setup() {
  pinMode (7, OUTPUT);
}
  int del=500;

void loop() {
  digitalWrite(7,HIGH);
  delay(del);
  digitalWrite(7,LOW);
  delay(del);
}

pin 7 flashes just as expected. The only difference with the first code is that I am shorting pin 19 to ground.

I took a pic of the board, but I can't figure out how to upload it to the forum.

I don't know if this was just a typo in the forum post, but you wrote to Pin 9 and not Pin 19.

void setup() {
  pinMode (19, INPUT);
  digitalWrite(9, HIGH);  // <-- is this suppose to be 9 or 19?
  pinMode (7, OUTPUT);
}

I don't know if this was just a typo in the forum post, but you wrote to Pin 9 and not Pin 19.

oops :smiley: sorry

...

Now pin 7 just comes on and stays on.

With a pullup, the input is supposed to invert, right? The switch has no effect.

Certainly other noobs have had this problem, right?

OK, I found this...

...and everything is working. Thanks for your replies!

While we are happy that you have it working, it would have been nice to let us (and future searchers) what was wrong and how you fixed it. It might help someone who comes after you.

That is selfish of me, I'm sorry.

Well, like the "Button" page says, the input will float all over unless you nail it directly to ground with a 10k pull-down resister. Then you just feed your input 5v to make it go high. The resistor keeps the 5v from shorting directly to ground.

One typical noobish brain toot after another. I just wasn't getting it until I found that particular page.

I would think that directly connecting 2 pins like this was not safe unless there was a resistor between them. Maybe there are internal resistors that limit current enough?

like this

Like what?

I would think that directly connecting 2 pins like this was not safe unless there was a resistor between them. Maybe there are internal resistors that limit current enough?

Well the only way it would be unsafe is if one made a programing error where both pins were set to output mode and then set to opposite logic level states. A 200 ohm resistor between the pins would protect against that kind of brain fart.

Lefty

The thread starts with:

I'm trying to get pin 7 to reflect the state of pin 19:

Code:

void setup() {
pinMode (19, INPUT);
pinMode (7, OUTPUT);
}

int in;

void loop() {
in = digitalRead(19);
digitalWrite(7,in);
}

When I load this, pin 7 just goes high, and doesn't respond at all. Do I need a pullup resister or something somewhere?

I read that to imply an electrical connection between 7 and 19. did I completely miss the point? RetroLefty's answer seems to the point. thanks.