0

I'm working through a set of beginner exercises with the Arduino Uno microcontroller. (A generic one, though, as this is what I've been supplied with.)

The program I'm running, which alternates between sending 1's and 0's to serial output depending on the state of a momentary switch, has set pin 2 to be the input for the switch. But. Whilst wiring up, I accidentally plugged the jumper cable in to pin 3 initially, and found it still mostly sent the 1's when the button was pushed. Some 0's, yet mostly 1's.

Initially I thought maybe it was just the board was a bit dodgy, but thought I'd experiment a bit. Plugging into pin 3 instead of pin 2 still fairly consistently sent 1's when the button was pushed, though the 1's flowed a little bit less consistently than when it was in pin 2. In pin 2 it was completely consistent by comparison. So I tried pin 4, but with that one there's no response at all.

Am I right in presuming the program's readings seems to get a little bit less responsive the further away I move the cable from the pin that I've programmed to act as input? Can anyone help me understand why this happens?

It's probably quite obvious that I'm new to electronics. :)

The program I've got uploaded to the board is as follows:

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input: 
  pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button: 
  Serial.println(buttonState);
  delay(1); // delay in between reads for stability
}
Reb
  • 649
  • 1
  • 8
  • 12

3 Answers3

3

Floating pins are prone to noise. If you are not actually connecting anything to pin 2, you will be reading noise. Any wire connected to pin 2 (even connections on the board) will act like an antenna and pick up noise. You should always use the pin number that you are physically connecting in situations like these.

JMA
  • 1,781
  • 9
  • 18
  • +1 It sounds like you are running into floating pin stuff: floating pins are a little bit like the equivalent of pointing to random memory. The results are often undefined. – Jamie F Sep 06 '16 at 15:42
  • Pretty much, because you are not controlling the value that you assign to them. – JMA Sep 06 '16 at 15:43
  • 1
    Excellent. This obviously explaining why touching the side of the board with my finger seemed like it might be affecting the output too. Thanks! – Reb Sep 07 '16 at 11:41
1

Leaving input pins open makes the microcontroller read a floating value which swings between 0 to 1. Also when wiring a switch to any pin, make sure to hook some pull-down resistor to make the input 0. These are common for many electronics and proper notice to be taken while designing circuits of your own.

0

You need to look into datasheet where described functions of the pins.

The pins of MCU can be assigned various functions through special registers.

Two most common functions of the pins are input and output. MCUs provide internal pull-up and pull-down resistors which when used properly significantly simplifies electronic schemas.

If the input activated as input without any pull-??? then it's state is not defined and can be used as initiator of random number generator. Due this reason it is better to define what is default state of the input pin by connecting pull-??? resistors.

In Arduino IDE you are not limited to functions provided -- you still can use register manipulation directly, you just need to learn internals of the MCU.

If you do it properly then 2kbit program very often can be made as small as a few hundred bytes and it will work hundred times faster.

Operating registers in C is not much different from assembly, in C++ you get right away significant overhead -- although some benefits of registers still can be significant.

Libraries hide from programmers internals of MCU what is nice as it simplifies the programming and does not require to understand how MCU works, what registers are changed in what sequence.

But when you know hardware in and out -- you can squeeze from small MCU what is not possible with use of libraries (code will just not fit into the chip). MCUs are not that complicated (Atmel) to learn about it's internals -- benefits are significant.

Knowledge is a power which many avoid.

Polar Bear
  • 6,762
  • 1
  • 5
  • 12