-1

I'm building the dactyl manuform, and I'm writing all of the code myself, but I've ran into a problem here's my code:

// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(9600);

  for (int i=0;i<20;i++){
    pinMode(i, INPUT);
  }
}

// the loop routine runs over and over again forever:
void loop() {
  for (int i=0;i<20;i++){
    Serial.print(digitalRead(i));
  }
  Serial.print("\n");

  //delay(100); //uncomment if you want to
}

The problem is even when the inputs are not connected to 5V, the serial monitor sometimes gives a 1, and sometimes a 0. At first I was only using analog inputs, so I was suspecting that, but the same happens with digital inputs. I'm 99% sure it's not the switches, because I tested them with a multimeter. I feel like this is something obvious, and I'm just stupid (well I am, but you get the point).

8
  • What is connected to those inputs? Commented Dec 29, 2023 at 10:20
  • @EdgarBonet switches, but just to see if it works, I just bridge the connection with a cable.
    – Umikali
    Commented Dec 29, 2023 at 10:23
  • 1
    INPUT means no internal pullup => super high input resistance == radio antenna. Read something about "floating inputs"
    – KIIV
    Commented Dec 29, 2023 at 10:30
  • 1
    so should I do INPUT_PULLUP instead of just INPUT?
    – Umikali
    Commented Dec 29, 2023 at 10:52
  • Yes, it means there is about 50k pull up resistor to the Vcc and you can use GND to change it's state to LOW
    – KIIV
    Commented Dec 29, 2023 at 11:04

1 Answer 1

0

There are several possible reasons random values are seen at micro controller inputs when connected to switches. Here are two:

  1. Missing pull up or pull down resistor when using a SPST switch.

Micro controller inputs should not be allowed to float. This can lead to unexpected input states. Instead, inputs should be connected to ground or the micro controller's positive voltage supply usually either directly or though a resistor. It is common to use a pull up resistor when using a SPST switch connected to ground. Or a pull down resistor when using a SPST switch connected to the micro controller's positive voltage supply. Not all micro controllers have pull up or pull down resistors built internally. Some only have one or the other. In these cases an external resistor may be required. Some thought should be given to using micro controllers that do have built in pull up or pull down resistors. The actual resistance value of these built in resistors does vary (may not be carefully controlled). This is fine for the majority of cases. However, if a specific value is needed, it may be best if external resistors were used instead.

  1. Long wires between the micro controller and the switch.

Longer wires are more likely to develop unexpected currents due external electrical noise. It is possible wires are long enough and the environment they are in electrically noisy enough to develop currents that will change the voltage across a pull up or pull down resistor. And, in turn, change the value of the micro controller's input. The higher the resistor value the more likely this will happen. However, the lower the resistor value chosen the more power needed when the SPST switch is closed. This is usually a concern when operating on batteries. There are many aspects to mitigating the effects of electrical noise. And will take us off subject. Instead, if long wires are necessary, consider using a "brake-before-make" SPDT switch which will connect the micro controller's input pin directly to ground or the micro controller's positive supply voltage. This will require running 3 rather than 2 wires to the switch.

Last words: Take care not to connect pull up resistors or switches to voltages higher than the micro controller's positive supply voltage! Some Arduino boards regulate higher voltages to lower voltage before supplying power to the micro controller. For instance, an Arduino board that runs from the 5 Volt USB power source may have a micro controller which actually runs at 3.3 Volt. In such a case, use the 3.3 Volts supplied to the micro controller when connecting a pull up resistor or switch.

Not the answer you're looking for? Browse other questions tagged or ask your own question.