A simple card reader using two pins of the Arduino.
A travel in time to the old days when programs were typed on punch cards.

Each card represents a Byte,  characters  can be seen in the terminal of the Arduino IDE .

In the following diagram you can see the timing.
When the CLOCK changes from LOW to HIGH the DATA is loaded.

The Sketch:

/* PunchCardReader
   Arduining.com 29/06/2011
*/
#define  clock  8   //clock input in digital pin 8. 
#define  data   9   //data input in digital pin 9.
byte Byte;          //store the incoming 8 bits.

void setup() {                
  digitalWrite(clock, HIGH);    // Connect Pull-Up resistor.
  digitalWrite(data, HIGH);     // Connect Pull-Up resistor.
  Serial.begin(9600);  
}

void loop() {
  Byte=0;
  while(digitalRead(clock)==0);     //wait for card in(rising edge).
  delay(20);                        //debounce
  for(int i=0; i<8 ;i++){
    while(digitalRead(clock)==1){}; //wait for a falling edge.
    delay(20);                      //debounce
    while(digitalRead(clock)==0);   //wait for a raising edge.
    delay(20);                      //debounce
    Byte= Byte << 1;
    Byte= Byte | !(digitalRead(data));
    Serial.print(!(digitalRead(data)));        
    }
  while(digitalRead(clock)==1){};   //wait for card out.
  delay(20);                        //debounce 
  Serial.print(" = ");
  Serial.println(Byte,BYTE);
}

Enjoy the Youtube presentation:

Advertisement