Get state of dip switch with less then 8 cables.

Status
Not open for further replies.

Darcade

New member
Hey,
I am working on a project with my Teensy 3.

For that project I will need a DIP Switch with 8 switches.

Is it possible to use less then 8 cables for the switch.
I do not want to use 8 pins from my teensy just for the switch, but I need all the 8 switches.

Hope you'll help me!

lg Darcade
 
Yes, there are several methods:

1) Get a MCP23008 i2c chip. With i2c you can have multiple devices on the bus, and with each MCP23008, you can have 8 digital inputs or 8 digital outputs (or some mixture between the two). If you need more than 8 dip switches, you can get the MCP23017 i2c chip which has 16 digital input/outputs The MCP23017/MCP23008 have 3 address pins, so using 8 MCP23017's, you could put up to 128 dip switches on a system. There is a chip with 32 digital input/outputs (pca9555), but since it only has 2 address pins, it doesn't give you any more capability than 8 MCP23017's.

On the Teensy 3.0, i2c is controlled by pins A4 (SDA) and A5 (SCL). You will need active pullups (4.7K Ohm is recommended), on both SDA/SCL, connecting the 3.3v power line to the A4/A5 pins. You can have additional devices on the i2c bus as well as the MCP23017/MCP23008's, such as display units, analog input/outputs, real time clocks, etc. I bought my MCP23008 from Adafruit: http://www.adafruit.com/products/593, but you can find it from other sellers as well.

Here is the button code example from the Adafruit MCP23008 library:
Code:
// Basic pin reading and pullup test for the MCP23008 I/O expander
// public domain!

// Connect pin #1 of the expander to Analog 5 (i2c clock)
// Connect pin #2 of the expander to Analog 4 (i2c data)
// Connect pins #3, 4 and 5 of the expander to ground (address selection)
// Connect pin #6 and 18 of the expander to 5V (power and reset disable)
// Connect pin #9 of the expander to ground (common ground)

// Input #0 is on pin 10 so connect a button or switch from there to ground

Adafruit_MCP23008 mcp;
  
void setup() {  
  mcp.begin();      // use default address 0

  mcp.pinMode(0, INPUT);
  mcp.pullUp(0, HIGH);  // turn on a 100K pullup internally

  pinMode(13, OUTPUT);  // use the p13 LED as debugging
}



void loop() {
  // The LED will 'echo' the button
  digitalWrite(13, mcp.digitalRead(0));
}

2) You can combine a few dip switches together using resistors to be read from an analog pin. You might not be able to get all 8 dip switches on a single analog input due to noise considerations, but splitting them between two pins would be workable: http://forum.arduino.cc/index.php/topic,8558.0.html

3) I believe you could use shift registers, but I'm not familiar with the use of shift registers.
 
Last edited:
Yes, there are several methods:

1) Get a MCP23008 i2c chip. With i2c you can have multiple devices on the bus, and with each MCP23008, you can have 8 digital inputs or 8 digital outputs (or some mixture between the two). If you need more than 8 dip switches, you can get the MCP23017 i2c chip which has 16 digital input/outputs The MCP23017/MCP23008 have 3 address pins, so using 8 MCP23017's, you could put up to 128 dip switches on a system. There is a chip with 32 digital input/outputs (pca9555), but since it only has 2 address pins, it doesn't give you any more capability than 8 MCP23017's.

On the Teensy 3.0, i2c is controlled by pins A4 (SDA) and A5 (SCL). You will need active pullups (4.7K Ohm is recommended), on both SDA/SCL, connecting the 3.3v power line to the A4/A5 pins. You can have additional devices on the i2c bus as well as the MCP23017/MCP23008's, such as display units, analog input/outputs, real time clocks, etc. I bought my MCP23008 from Adafruit: http://www.adafruit.com/products/593, but you can find it from other sellers as well.

Here is the button code example from the Adafruit MCP23008 library:
Code:
// Basic pin reading and pullup test for the MCP23008 I/O expander
// public domain!

// Connect pin #1 of the expander to Analog 5 (i2c clock)
// Connect pin #2 of the expander to Analog 4 (i2c data)
// Connect pins #3, 4 and 5 of the expander to ground (address selection)
// Connect pin #6 and 18 of the expander to 5V (power and reset disable)
// Connect pin #9 of the expander to ground (common ground)

// Input #0 is on pin 10 so connect a button or switch from there to ground

Adafruit_MCP23008 mcp;
  
void setup() {  
  mcp.begin();      // use default address 0

  mcp.pinMode(0, INPUT);
  mcp.pullUp(0, HIGH);  // turn on a 100K pullup internally

  pinMode(13, OUTPUT);  // use the p13 LED as debugging
}



void loop() {
  // The LED will 'echo' the button
  digitalWrite(13, mcp.digitalRead(0));
}

2) You can combine a few dip switches together using resistors to be read from an analog pin. You might not be able to get all 8 dip switches on a single analog input due to noise considerations, but splitting them between two pins would be workable: http://forum.arduino.cc/index.php/topic,8558.0.html

3) I believe you could use shift registers, but I'm not familiar with the use of shift registers.


Thank you a lot :)

I think I will use the MCP23008 i2c chip for my project.

lg Darcade
 
Status
Not open for further replies.
Back
Top