DIP Switch 8 Position to address Sensor

So I have a decent amount of experience with arduino, but none with DIP Switches. My problem is that I want to be able to switch addresses as I create more sensors within my array. Each sensor is attached to its own ardiuno. I plan on using the DIP Switch 8 position I bought from Sparkfun to do the job. However, I do not seem to know how to wire it, or read from it, that is how confused I am.

I have searched extensively and found nothing that helped. I have of course tried dozens of approaches, I am currently attaching ground to the on position and digital pin 8 as the off, I have also tried inverting them. What happens is that when i switch to on i get 0 as the value when printed as DEC, when i flip it to off i get 1, and im some instances random noise. I understood that I would be able to get up to 255 possible positions, but without being able to get the proper byte data I would have to manually code each possibility, which seems wrong. Also, why is the on functioning as off whether or not I invert the connections.

Does anybody have any experience with using DIP Switches to control addresses on the arduino, a schematic and brief explanation would go a long way.

Thanks regardless

I have of course tried dozens of approaches

Have you tried using the built-in pull-up resistors?

A DIP switch is just an array of individual, unconnected SPST switches; if you can read one, you can read them all (assuming you've got enough spare pins).

Also, why is the on functioning as off whether or not I invert the connections.

It's probably floating; pull-ups will fix that.

Your help made a big difference. However, I feel that my method is still quite sloppy. I want to get the most possible addresses out of 4 switches (i do not have enough room to use all 8). I created 3 situations:

  • A zero

  • Each switch is a face value and multiplied by one another (off = 1)

  • fake a 5th switch (switch 1 = 5 the others are face values multilpied together).

Is there a simpler and cleaner method. I am currently getting 15 possible addresses from 4 switches.

//Create and Define Global Variables
int ledPin = 11; // LED connected to digital pin 6
int dipPins[] = {2, 3, 4, 5}; //DIP Switch Pins
byte val[] = {0,0,0,0}; // assign val
byte value = 0; //create address variable

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);      // sets the digital ledPin as output
  int i;
  for(i = 0; i<=3; i++){
  pinMode(dipPins[i], INPUT);      // sets the digital pin 2-5 as input
  digitalWrite(dipPins[i], HIGH); //Set pullup resistor on
  }
  delay(100);
}

void loop()
{
  //address();
 //Print Address
  Serial.print(address(), DEC);
  Serial.print("\n");
 }

//Create Address from DIP Switch (4 positions used)
byte address(){
  
  //Set Switch Values
  int i;
  
  //Loop True False through the val[]
  for(i=0; i<=3; i++){
  val[i] = digitalRead(dipPins[i]);   // read the input pin
  }
  
  //create 0 position
  if(val[0] == 0 && val[1] == 0 && val[2] == 0 && val[3] == 0){ 
    for(i=0; i<=3; i++)
    {
     val[i] = 0;
     }
     
  //Fake a switch 5 on Switch 1 if open with 1 or more other switches
  }else if(val[0] == 1 && (val[1] == 1 || val[2] == 1 || val[3] == 1)){
    val[0] = 5; //set pin 1 to value of 5
    for(i=1; i<=3; i++) 
       {
        if(val[i] == 1){ //set face values to pins that are true
          val[i] = i+1;
        }else if(val[i] == 0){ //set face switch value to 1 for pins that are false
          val[i] = 1;
        }
       }
       
   //Set Switch 1 - 4 at face value
   }else{ 
     for(i=0; i<=3; i++) 
     {
        if(val[i] == 1){ //set face values to pins that are true
          val[i] = i+1;
        }else if(val[i] == 0){ //set face switch value to 1 for pins that are false
          val[i] = 1;
        }
     }
   }
  //End Switch Values
  
  //Create Address
  value = val[0] * val[1] * val[2] * val[3]; //multiply values to create address
  
  analogWrite(ledPin, int(value)); //write to LED to give visual feedback (temporary)
   
  return value; //return address
}

Untested code but try this:-

//Create and Define Global Variables
int ledPin = 11; // LED connected to digital pin 6
int dipPins[] = {2, 3, 4, 5}; //DIP Switch Pins

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);      // sets the digital ledPin as output
  int i;
  for(i = 0; i<=3; i++){
  pinMode(dipPins[i], INPUT);      // sets the digital pin 2-5 as input
  digitalWrite(dipPins[i], HIGH); //Set pullup resistor on
  }
  delay(100);
}

void loop()
{
  Serial.print(address(), DEC);
  Serial.print("\n");
delay(1000);
 }

//Create Address from DIP Switch (4 positions used)
byte address(){
  int i,j;
  
  //Get the switches state
  for(i=0; i<=3; i++){
  j = (j << 1) | digitalRead(dipPins[i]);   // read the input pin
  }
    
  return j; //return address
}

Is there a simpler and cleaner method. I am currently getting 15 possible addresses from 4 switches

24 = 16.

AWOL, thanks, I thought that was what I was gunning for but uncertain.

GRUMPY_MIKE, Very elegant answer. I could not figure out how to get the binary version to work. the bitwise or function is exactly what I was missing. This solves a lot of problems I would have ran into again.

With your answer I was able to get 16 combinations.
Binary - Address :slight_smile:
0000 - 144
0001 - 145
0010 - 146
0011 - 147
0100 - 148
0101 - 149
0110 - 150
0111 - 151
1000 - 152
1001 - 153
1010 - 154
1011 - 155
1100 - 156
1101 - 157
1110 - 158
1111 - 159

That doesn't look right. there should be small numbers. Try replacing the bit that says:-
int i,j;
with:-
int i,j=0;

To initialise the variable.
If that doesn't work try :-
j = j & 0x0F;
just before the return.

Thanks again for all your help. I have been working on another element of the project and just got back to this. Your new code worked perfectly.

The final script to sequence addresses using a dip switch (4 switch) between address 0 and 15

//Create and Define Global Variables
int dipPins[] = {2, 3, 4, 5}; //DIP Switch Pins
int transAddress;
void setup()
{
  Serial.begin(2400);
  int i;
  for(i = 0; i<=3; i++){
  pinMode(dipPins[i], INPUT);      // sets the digital pin 2-5 as input
  digitalWrite(dipPins[i], HIGH); //Set pullup resistor on
  }
  transAddress = address();
  delay(100);
}

void loop()
{
 
 }

//Create Address from DIP Switch (4 positions used)
byte address(){
  int i,j=0;
  
  //Get the switches state
  for(i=0; i<=3; i++){
  j = (j << 1) | digitalRead(dipPins[i]);   // read the input pin
  }
  return j; //return address
}

This results in the following sequence setup:

0000 - 0
0001 - 1
0010 - 2
0011 - 3
0100 - 4
0101 - 5
0110 - 6
0111 - 7
1000 - 8
1001 - 9
1010 - 10
1011 - 11
1100 - 12
1101 - 13
1110 - 14
1111 - 15

Interesting.. That means you could get 64 possible settings (82) by using an 8-Position DIP switch... :).

Thought provoking... :wink:

With 8 switches, you get 28 possibilities, not 82.

aye

each switch has 2 states, on or off

2^8 = 256

binary values:
128,64,32,16,8,4,2,1

binary notation:
B11111111

~OR~

128
064
032
016
008
004
002
001
+-----
255
+----
All zeros

256

another example

binary notation:
B10101010

~OR~
128
032
008
002
+----
170

Wow I'm too tired to be trusted with exponents. LMAO ;D!