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-In registers are a common piece of electronics. They combine 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.

If the arduino receives 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. It could do it like this because it transforms the parallel incoming signals into a serial signal.

The clue here is to use time as a reference to transform the 8 parallel signals of the independent ones and zeros into a sequence of ones and zeros. This is a very basic principle of computing. But now you ask how the arduino knows it gets 8 bits(1 byte) and not just four or three. How does the arduino know which bit is the first and the last one. Therefore, the shift register has a so-called latch pin. If you activate the latch the arduino says: please send me data. So the shift register locks the state of the input pins and starts to send the bits. We are now sure we get the first bit by latching the register, now to actually transmit the data we need a clock pulse for each bit we want to get. So we do that eight times. And after that we repeat the cycle through the loop function in our arduino sketch.

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

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. So we could read our incoming byte and with the help of the bit mask just take the first four bits to get our first value and then shift the byte 4 places to the right and take these to determine the second value.

one row of switches resambles one BCD Switch
 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; //the five BCD-switches
 byte Digit2 = 0;
 byte Digit3 = 0;
 byte Digit4 = 0;
 byte Digit5 = 0;
 
 byte oldValue = 6;
 
 void setup() {
   Serial.begin(9600);
 
   pinMode(latchPin, OUTPUT);
   pinMode(clockPin, OUTPUT);
   pinMode(dataPin, INPUT);
 }
    
 void loop() {

   digitalWrite(latchPin,1); //locking the data
 
   delayMicroseconds(20);
 
   digitalWrite(latchPin,0);
 
   switchVar1 = shiftIn(dataPin, clockPin); //get the data
   switchVar2 = shiftIn(dataPin, clockPin);
   switchVar3 = shiftIn(dataPin, clockPin); 
 
   Digit1 = switchVar1 & mask; //use mask
   Digit2 = switchVar1>>4; //push four bits to the right
   Digit3 = switchVar2 & mask;
   Digit4 = switchVar2>>4;
   Digit5 = switchVar3 & mask;

   if(oldValue != Digit1 + Digit2 + Digit3 + Digit4 + Digit5){ //just print if the value changed
     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

The good thing about the shift registers is that you could chain them up, because the bit values are going down the lane of the serial signal. In order to get two shift registers we have to read out the date twice after latching the registers. This is the concept of using just three pins on the arduino to control many input pins. If you have a lot of output pins like a LED cube or something like this you could use a shift-out register. It transforms the serial data into parallel data and switches the output pins according to the bit array it gets.