How to work with BCD(binary-coded decimal) switch?

Aus hyperdramatik
Zur Navigation springen Zur Suche springen

--entry is still in work--

Foto by Laura Alapfy

How does it work?

A BCD-Switch is actually not one switch, it contains basically four switching circuit at once. There is a wheel which closes depending on the number the switch shows in the front. The picture on the right are 5 BCD switches stacked sideways together. The four switches translating the number into its binary form. So if you see a 5 in the front the second and the fourth switch are on and resembling the one the others are off and resembling the zero. Because 5 in binary is 0101. The tricky part is that you read binary from left to right in computing because you have the most significant bit always on the left side. So the rightest bit stands for 2 to the power of zero which is 1 if it is 1 if it is zero its value will not be counted so it is zero. So the fourth switch determines the least significant bit. So I will now give you a table so you don't have to remember and calculate.

BCD Switch
Number in Front BitArray Switch 1 Switch 2 Switch 3 Switch 4
1 0001 off off off on
2 0010 off off on off
3 0011 off off on on
4 0100 off on off off
5 0101 off on off on
6 0110 off on on off
7 0111 off on on on
8 1000 on off off off
9 1001 on off off on
0 0000 off off off off

What will you need?

  • an Arduino (every typ will do)
  • a BCD-Switch
  • a shift register (CD4021)
  • jumperwires
  • a breadboard

How to connect a shift register with an Arduino?

Shift registers are a common piece of electronic and it combines transistors in that way that you could survey eight input pins in that way that one shift register sends a so called bit array of eight bits called a byte. So if you receive a bit array looking like this [0,0,1,0,1,0,0,0] it means you have an input voltage on pin 4 and pin 6. So switch 4 and 6 are on and all others are off. One BCD switch contains 4 switches so one shift register could match two switches. Luckily the BCD switch transforms the numbers as described earlier in four binary bits.

How to read the BCD switch with an Arduino and a shift register?

one row of switches resambles one BCD Switch
 const int buttonUpPin = 8;
 const int buttonRightPin = 11;
 const int buttonDownPin = 10;
 const int buttonLeftPin = 9;
 
 int buttonUpState = 0;
 int buttonRightState = 0;
 int buttonDownState = 0;
 int buttonLeftState = 0;
  
 int latchPin = 5;
 int dataPin = 4;
 int clockPin = 6;
 
 byte switchVar1 = 72;  //01001000
 byte switchVar2 = 159; //10011111
 byte switchVar3 = 201; //10011111
 
 byte mask = 15; //00001111
 
 byte Digit1 = 0;
 byte Digit2 = 0;
 byte Digit3 = 0;
 byte Digit4 = 0;
 byte Digit5 = 0;
 
 byte oldValue = 6;
 
 void setup() {
   Serial.begin(9600);
 
   pinMode(buttonUpPin, INPUT_PULLUP);
   pinMode(buttonRightPin, INPUT_PULLUP);
   pinMode(buttonDownPin, INPUT_PULLUP);
   pinMode(buttonLeftPin, INPUT_PULLUP);
 
   pinMode(latchPin, OUTPUT);
 
   pinMode(clockPin, OUTPUT);
 
   pinMode(dataPin, INPUT);
 }
    
 void loop() {
   
   buttonUpState = digitalRead(buttonUpPin);
   buttonRightState = digitalRead(buttonRightPin);
   buttonDownState = digitalRead(buttonDownPin);
   buttonLeftState = digitalRead(buttonLeftPin);
 
   if (buttonUpState == LOW) {
     Serial.println("UP");
     delay(100);
   }
 
   if (buttonRightState == LOW) {
     Serial.println("RIGHT");
     delay(100);
   }
 
   if (buttonDownState == LOW) {
     Serial.println("DOWN");
     delay(100);
   }
 
   if (buttonLeftState == LOW) {
     Serial.println("LEFT");
     delay(100);
   }
   if (buttonLeftState == LOW && buttonRightState == LOW) {
     Serial.println("START");
     delay(100);
   }
 
   digitalWrite(latchPin,1);
 
   delayMicroseconds(20);
 
   digitalWrite(latchPin,0);
 
   switchVar1 = shiftIn(dataPin, clockPin);
 
   switchVar2 = shiftIn(dataPin, clockPin);
 
   switchVar3 = shiftIn(dataPin, clockPin); 
 
   Digit1 = switchVar1 & mask;
   Digit2 = switchVar1>>4;
   Digit3 = switchVar2 & mask;
   Digit4 = switchVar2>>4;
   Digit5 = switchVar3 & mask;

   if(oldValue != Digit1 + Digit2 + Digit3 + Digit4 + Digit5){
     Serial.print(Digit5, DEC);
     Serial.print(Digit4, DEC);
     Serial.print(Digit3, DEC);
     Serial.print(Digit2, DEC);
     Serial.println(Digit1, DEC);
     oldValue = Digit1 + Digit2 + Digit3 + Digit4 + Digit5;
   }
 
 delay(100);  
 
 } 
 byte shiftIn(int myDataPin, int myClockPin) {
   int i;
   int temp = 0;
   
   int pinState;
   
   byte myDataIn = 0;
 
   pinMode(myClockPin, OUTPUT);
 
   pinMode(myDataPin, INPUT);
 
   for (i = 7; i >= 0; i--)
 
     {
 
       digitalWrite(myClockPin, 0);
 
       delayMicroseconds(2);
 
       temp = digitalRead(myDataPin);
 
       if (temp) {
   
         pinState = 1;
   
         myDataIn = myDataIn | (1 << i);
   
       }
   
       else {
   
         pinState = 0;
   
       }
   
       digitalWrite(myClockPin, 1);
   
     }
   
     return myDataIn;
   }

How to connect multiple BCD switches?

two switches
four switches with two registers