Giving Arduino’s a unique number using dipswitches

Programming Arduino’s is not that hard. The biggest challenge is just getting started. Just start with the ease ones, like Blinky. Then you start to buy stuff (modules, components) and connect them. After that, your knowledge will grow as your projects grow.

But sometimes you will still have these little challenges. I just had one: how can I make my Arduino’s unique? And with that I mean: how can I load the same sketch to multiple clients but still each Arduino is unique?

My solution is to use dip switches.

I use these 4 in-a-row dip switches. And this gives me 16 positions (0-15). The only drawback is that I have to sacrifice four digital GPIO ports. And these dip switches behave like regular buttons. So I need four resistors of 10k (brown, black, orange, gold) to protect my Arduino.

This is the layout:

dip_bb

Each dipswitch is connected to five Volt and to the ground (using a resistor). So each pin is protected while reading out the switch.

The code is also very simple:

const int buttonPinOne = 2;
const int buttonPinTwo = 3;
const int buttonPinFour = 4;
const int buttonPinEight = 5;

int buttonStateOne = 0;
int buttonStateTwo = 0;
int buttonStateFour = 0;
int buttonStateEight = 0;

int binaryValue = 0;

void setup() {
Serial.begin(115200);

pinMode(buttonPinOne, INPUT);
pinMode(buttonPinTwo, INPUT);
pinMode(buttonPinFour, INPUT);
pinMode(buttonPinEight, INPUT);
}

void loop() {
buttonStateOne = digitalRead(buttonPinOne);
buttonStateTwo = digitalRead(buttonPinTwo);
buttonStateFour = digitalRead(buttonPinFour);
buttonStateEight = digitalRead(buttonPinEight);

  binaryValue = 0;

  if (buttonStateOne == HIGH) {
binaryValue += 1;
}

  if (buttonStateTwo == HIGH) {
binaryValue += 2;
}

  if (buttonStateFour == HIGH) {
binaryValue += 4;
}

  if (buttonStateEight == HIGH) {
binaryValue += 8;
}

  Serial.println( binaryValue );

  delay(4000);
}

Running this code gives me the following output while changing the switches:

output

And this is all it takes. It works for me. Do you need a bigger id? Add more ports.

 

 

 

Advertenties