FritzeEngineering
Published

Part 2 - Arduino as a universal logic module

Digital technology with Arduino. Understanding digital technology with Arduino – explained in a practical way.

BeginnerWork in progress30 minutes35
Part 2 - Arduino as a universal logic module

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Resistor 10k ohm
Resistor 10k ohm
×2
PTS 645 Series Switch
C&K Switches PTS 645 Series Switch
×2
resistor 1,5k ohm
×1
LED, Low Current
LED, Low Current
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Breadboard, Plastic
Breadboard, Plastic

Story

Read more

Schematics

Arduino as a universal logic module

Arduino simulates different logic gates.

Code

Arduino as a universal logic module

Arduino
Arduino simulates different logic gates.
 //Arduino as a universal logic module
const int IN_PIN_A = 2;
const int IN_PIN_B = 3;
const int OUT_PIN_Q = 5;
int command = 'U';
void setup() {
  // Initialize serial port
  Serial.begin(19200);
  ShowBehaviour();
  //Inputs
  pinMode(IN_PIN_A, INPUT);
  pinMode(IN_PIN_B, INPUT);
  //Output
  pinMode(OUT_PIN_Q, OUTPUT);
}
void loop() {
  bool q = false;
  bool a = digitalRead(IN_PIN_A) == LOW;
  bool b = digitalRead(IN_PIN_B) == LOW;
  int ret;
  if (Serial.available() > 0) {
    command = Serial.read();      // Save character received.
    ret = Serial.read();          // Save character received <return>.
    command &= ~0x20;             //Kleinbuchstaben in Grossbuchstaben ändern
    ShowBehaviour();
  }
  switch(command) {
    case 'U': {  //UND
      q = a && b;
    }
    break;
    case 'O': {  //ODER
      q = a || b;
    }
    break;
    case 'A': {  //NAND
      q = !(a && b);
    }
    break;
    case 'N': {  //NOR
      q = !(a || b);
    }
    break;
     case 'E': { //XOR
       q = a ^ b;
    }
    break;
    case 'X': { //XNOR
      q = !(a ^ b);
   }
   break;
  }
  Serial.print("A: ");
  Serial.print(a);
  Serial.println();
  Serial.print("B: ");
  Serial.print(b);
  Serial.println();
  Serial.print("Q: ");
  Serial.print(q);
  Serial.print("\n-----\n");
  digitalWrite(OUT_PIN_Q, q ? HIGH : LOW);
  delay(500);
  }
void ShowBehaviour(void) {
  switch(command)
  {
    case 'U': {  //UND
      Serial.print("Arduino works as an AND.\n");
    }
    break;
    case 'O': {  //ODER
      Serial.print("Arduino works as an OR.\n");
    }
    break;
    case 'A': {  //NAND
       Serial.print("Arduino works as a NAND.\n");
    }
    break;
    case 'N': {  //NOR
      Serial.print("Arduino works as a NOR.\n");
    }
    break;
    case 'E': {//XOR
      Serial.print("Arduino works as a XOR.\n");
    }
    break;
    case 'X': { //XNOR
      Serial.print("Arduino works as a XNOR.\n");
    }
    break;
  }
}

Credits

FritzeEngineering
4 projects • 0 followers
Hard- and software engineer specialised in embedded systems. Circuit design, PCB layouts and PLC programming.

Comments