Programing for DIP-Switch

Hi, I've recently started with a project that includes a DIP-Switch. The idea is that the switch should determine a DMX address.

Can't find anywhere how it "should" be done so I assumed that if a feed all the switches +5v and give them separate inputs where I write something like:

If pin1 = HIGH then result1 is ex 1. Else result1 is 0.

If I do this to all inputs I should end up with result1,2,3 etc that I could add up to get my DMX address. Correct?

The problem as you see is my lack of coding skills, If anyone could point me towards the right tutorials or writing code for one of the inputs I could work from that.

Thank you!

You wire one side of the switch to an Arduino pin, and the other to Arduino ground. Then use pinMode(pinNum, INPUT_PULLUP); which will make the pin high while the switch is open and low when closed. That's called "active low" and is sop.

Then you add the switches up by increasing the power of 2 from the right. The right most switch will be 0 or 1, next one 0 or 2, then 0 or 4, then 0 or 8, and so on. Just normal binary counting.

If you're brand new at Arduino you ought to start with the examples in the IDE at File > Examples which are also here.

Also, depending on the number of switches you are reading, and the number of free pins you have on the Arduino, an encoder chip may help and this description may, anyway, be useful to you:

You could also do this with a shift register like the 74HC165. On the pro side, this saves pins if you use more then 4 bits, and two of those are reusable for SPI. Also, you directly read the address as serial input. On the contra side, I guess you can only put in the required pull-down/up resistors with a double layered PCB or jumper wires.

Here is an example.

SInce you want a settable address, you probably want to do this on multiple modules. I would consider investing 15$ and 3 weeks of waiting and have a PCB made for this for SMD components. You could tile small PCBs and literally get a hundred modules for that price. In the end, you have a module that has 6 pins you can plug into your projects.
Actually, I am surprised that does not exist.

EDIT: this is very similar to 6v6gt suggestion. In his suggestion, you need 3 pins, but they are exclusive. In my solution, you need 4 pins, but 2 of them can be shared with other SPI-like stuff, and another one as some sharing potential (because you can load your register as often as you want, the dip switches are pretty static).

A MCP23008 I2C port expander will allow up to 8 switches per chip (MCP23017, 16 switches). On chip pullup resistors can be enabled for any pin. An interrupt can be enabled to signal any pin state change. Uses only 2 pins that are shared with all other I2C devices (+1 for interrupt if used).

Here's an example of using three pins to get eight addresses. Use manor_royal's wiring scheme and note that a low on a pin is true or switch closed.

#define ABIT0        4       // Bit 0 of ID
#define ABIT1        5       // Bit 1 of ID
#define ABIT2        6       // Bit 2 of ID

void setup()
    // Set up address pins
    pinMode( ABIT0, INPUT_PULLUP );    // Has a value of 1
    pinMode( ABIT1, INPUT_PULLUP );    // Has a value of 2
    pinMode( ABIT2, INPUT_PULLUP );    // Has a value of 4

    // Calculate address
    uint8_t adr = !digitalRead( ABIT0 ) + ( !digitalRead( ABIT1 ) * 2 ) + ( !digitalRead( ABIT2 ) * 4 );

Another (faster) method:

// Calculate address
byte adr = (PORTD & 0b01110000) >> 4; // result is 0-0-0-0-0-ABIT2-ABIT1-ABIT0

Clever, thanks.

OP now sees two ways of getting the same results using sequential pins.

Can’t get it working. Been trying some of your ideas but im getting stuck at how to test if its working. Added a LED-diode on pin 13 just to use as indicator. Posting my code below, hopefully you can sort things out for me. Thanks!

// - - - - -
// DmxSerial - A hardware supported interface to DMX.
// DmxSerialRecv.ino: Sample DMX application for retrieving 3 DMX values:
// address 1 (red) -> PWM Port 9
// address 2 (green) -> PWM Port 6
// address 3 (blue) -> PWM Port 5
// Copyright (c) 2011-2015 by Matthias Hertel,
// This work is licensed under a BSD style license. See
// Documentation and samples are available at
// 25.07.2011 creation of the DmxSerial library.
// 10.09.2011 fully control the serial hardware register
//            without using the Arduino Serial (HardwareSerial) class to avoid ISR implementation conflicts.
// 01.12.2011 include file and extension changed to work with the Arduino 1.0 environment
// 28.12.2011 changed to channels 1..3 (RGB) for compatibility with the DmxSerialSend sample.
// 10.05.2012 added some lines to loop to show how to fall back to a default color when no data was received since some time.
// - - - - -

#include <DMXSerial.h>

// Constants for demo program

const int RedPin =    9;  // PWM output pin for Red Light.
const int GreenPin =  6;  // PWM output pin for Green Light.
const int BluePin =   5;  // PWM output pin for Blue Light.

#define RedDefaultLevel   0
#define GreenDefaultLevel 0
#define BlueDefaultLevel  0

void setup () {

  // enable pwm outputs
  pinMode(RedPin,   OUTPUT); // sets the digital pin as output
  pinMode(GreenPin, OUTPUT);
  pinMode(BluePin,  OUTPUT);
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);


void loop() {

  int dipswitch1 = digitalRead(2);

  if (dipswitch1 == HIGH) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);

  // Calculate how long no data backet was received
  unsigned long lastPacket = DMXSerial.noDataSince();

  if (lastPacket < 5000) {
    // read recent DMX values and set pwm levels

  } else {
    // Show pure red color, when no data was received since 5 seconds or more.
    analogWrite(RedPin,   RedDefaultLevel);
    analogWrite(GreenPin, GreenDefaultLevel);
    analogWrite(BluePin,  BlueDefaultLevel);
  } // if

// End.

You'll have to insert print statements in various locations to check the values of incoming data. You can also set these variables to fixed values to see if the output results are reasonable.

When you say, “can’t get it working” it doesn’t tell us much. Can you describe what happens and how that compares with what you expected?

What I'm expecting: As I pull the switch connected to pin 2 to ON, the LED connected to pin 13 should light up. As I then pull it down, the light should go off.

What's happening: As I'm uploading the sketch the LED lights up and no matter what state it's in the LED stays on.

Any better ways to show the values? How would I do to print them?

Put a print statement immediately after reading pin 2.

Since that pin has a pullup, your switch should be connect to pin 2 and Gnd. When the switch is on, the LED should be off, and opposite for the other state.

How do you have the dip wired? According to the code it should be between the pin and ground. And when the switch is in the on position the pin should read LOW. In the off position it should read HIGH.

The dip is connected to pin 2 and GND.. So it should work as I expected?

Will look into how to print values tomorrow.

That wiring only works if the mode for pin 2 is set to INPUT_PULLUP, which the posted code has done. In that case, DIP switch closed will be LOW, and DIP switch open will be HIGH. Is that working as you expected?

What voltage do you measure at pin 2 for DIP switch closed and for DIP switch open? (Of course, that is with this code loaded into the Arduino and running.)

Think im on the right track of solving things now. Just having trouble with starting serial connection, im guessing thats because there seems to be a running serial connection already.

Is the "DMXSerial.init(DMXReceiver);" the issue when im trying to use "Serial.begin(9600);"?

If so, can i somehow use the first syntax to print in my serial monitor? Thanks

there seems to be a running serial connection already.

What leads you to that conclusion? Is there an error message? What pins is the DMX serial connected to?

Im just assuming after trying to understand the error report.

Arduino:1.8.1 (Windows 10), Kort:"Arduino/Genuino Uno"

HardwareSerial0.cpp.o (symbol from plugin): In function `Serial':

(.text+0x0): multiple definition of `__vector_18'

libraries\DMXSerial-master\DMXSerial.cpp.o (symbol from plugin):(.text+0x0): first defined here

HardwareSerial0.cpp.o (symbol from plugin): In function `Serial':

(.text+0x0): multiple definition of `__vector_19'

libraries\DMXSerial-master\DMXSerial.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

[u]What pins is the DMX serial connected to?[/u]