DIP switch giving faulty reads

Hello,

I have this DIP switch connected to a pro micro. The DIP switch has 8 switches connected to VCC and ports 2-10 except 3. The problem comes when I try to read these with this code:

int pins[] = {10, 2, 4, 5, 6, 7, 8, 9};
int pinCount = 8;
String DIPRead = "";

int port = A0;

void setup() {
  //Serial.println(port);
  Serial.begin(9600);
  while (!Serial) ;

  for (int index = 0; index < pinCount; index++) {
    pinMode(pins[index], INPUT);
    DIPRead += String(digitalRead(pins[index]));
  }
}

I keep getting faulty results. I have soldered the dip switch to the pro micro and tested if the wires hit each other but thats not the case. Does someone know if this is a code problem or a skill issue on my soldering?

Try the above as follows and check that you have consistent readings. This is to avoid floating conditions of the pins that are not activated by VCC. You need to de-solder the VCC wire and connect/solder it to GND. A closed swicth will assert logic LOW and an open swicth will assert logic HIGH.

pinMode(pins[index], INPUT_PULLUP);

2 Likes

Is one end of the switch connected to the arduino pin and the other connected to 5V?

Yes

The problem is that when I try to solder the switches to ground they dont seem to give output at all.

The common/tie pin of the DIP switch is to be connected with GND and not the DPin side.

Enable the internal pull-ups and the inputs will read high when nothing is connected, or when the switch is off (open). Turning-on (closing) the switch "overpowers" the pull-up, pulling the input down and you'll read low. (You may have to reverse the logic in your code.)

Or, you can use external pull-down resistors and wire the switch to 5V, but it's usually easier to do it in software. :wink:

Yes

Then use INPUT_PULLUP as @GolamMostafa suggested BUT connect the switches to GND instead of +5V.

Now when the switch is ON it will read 0 and when OFF it will read 1.

2 Likes

@tuinboon
Your DIP swicth connection must match either with Fig-1 or Fig-2.
image
Figure-1:

image
Figure-2:

Everything in my code works fine, when the switch is on it returns a 1 (sometimes) I think it may me something with floating conditions because when I print the output in a loop it keeps changing.

For example I just layed down the pro micro flat and plugged it in and out and got these numbers.

1111010
1111011
1111011
1111010
1111111
1111111
1111111
1111111
1111010

While I only had the first one on.

No dangerous Strings, and the eight switches are stored in a single byte.
Leo..

// switches between pins and ground, no resistors
const byte pins[] = {10, 2, 4, 5, 6, 7, 8, 9};
byte dipValue;

void setup() {
  Serial.begin(9600);
  for (byte i = 0; i < 8; i++) {
    pinMode(pins[i], INPUT_PULLUP);
    bitWrite(dipValue, i, !digitalRead(pins[i]));
  }
  Serial.println(dipValue);
}

void loop() {
}
1 Like

What do you think of inserting a tiny delay(20) in the for() loop after each reading for the stabilization of data?

Tested the sketch on an Uno.
No delay was needed.
This is a version that also prints binary.
Leo..

// switches between pins and ground, no resistors
const byte pins[] = {10, 2, 4, 5, 6, 7, 8, 9};
byte dipValue;

void setup() {
  Serial.begin(9600);
  //while (!Serial);
  for (byte i = 0; i < 8; i++) {
    pinMode(pins[i], INPUT_PULLUP);
    bool x = !digitalRead(pins[i]);
    Serial.print(x);
    bitWrite(dipValue, i, x);
  }
  Serial.print(" ");
  Serial.println(dipValue);
}

void loop() {
}

A print/screen shot of the output of the sketch of post #14 could have been presented.

The above two lines insert some implicit delay.

1 Like

But, digitalRead() function returns HIGH/LOW type value (Fig-1); why the data type of x is bool? should it not be int type?


Figure-1:

bool, byte, int, unsigned int, long, unsigned long, they can all hold a 1 or a 0, High or LOW.
bool and byte are the least wasteful on an 8-bit MCU. They both use one byte.
boolean can only represent a HIGH or LOW.

int ledPin = 13; // uses two bytes in flash and two bytes in program memory
const byte ledPin = 13; // only uses one byte in flash
Don't be wasteful on an 8-bit MCU if you can avoid it.
Leo..

1 Like

When I use this code it always returns 0 I don't know if im doing something wrong?

If you use the code from post#12, and you get a 0, then the switches are the problem.
Did you read the first line of the sketch.
Are you sure you connected them between pin and ground,
not between pin and 5volt, as you did before.

Every new setting of the dip switches requires a re-start of the Arduino of course.
Leo..

Can it be avoided by putting the codes in the loop() function?

1 Like