XOR Simulation using Arduino

In this post, an experiment with Arduino to create a setup which simulates the XOR truth tables using two tactile buttons and an LED is explained.  The circuit setup is as below. The diagram is created using Fritzing – An EDA tool (Check it out!)


Setting up the Arduino IDE in GNU/Linux :

Dowload the latest version of the Arduino IDE.

After downloading, extract the archive and the execute the arduino script to open the IDE.

After opening the IDE, set the board and port used in the Tools menu. Goto Tools-> Board -> Arduino UNO (or any board which you use). Then again goto Tools -> Serial Port -> /dev/ttyACM0 (or any other port to which the arduino is connected.) To check which port the arduino is using, you can disconnect and reconnect the Arduino USB and check kernel logs using ‘dmesg’ command. For Arduino UNO (which is an ACM device) you will mostly see the following line in dmesg :

[ 1956.390390] cdc_acm 2-1.6:1.0: ttyACM0: USB ACM device (The port is mentioned in the dmesg line)

If you get such a line the port is, as mentioned above, /dev/ttyACM0.

After setting the IDE, you can start a new Sketch and use the below code to setup the Arduino.

 Xor Buttons

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbuttons attached to pin 2 and pin 4 based
 on the XOR truth table

 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * pushbutton attached to pin 4 from +5V
 * 10K resistor attached to pin 2 from ground
 * 10K resistor attached to pin 4 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.

 created 2005
 by DojoDave
 modified 30 Aug 2011
 by Tom Igoe
 modified from original "Button" code on 30 Mar 2013
 By Rajgopal

 The original Button code is an example code is in the public domain.


// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2;     // the number of the pushbutton1 pin
const int buttonPin2 = 4;    // the number of the pushbutton2 pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variables for reading the pushbutton status
int buttonState2 = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbuttons pin as an inputs:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);

void loop(){
  // read the state of the pushbuttons value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  // check if the pushbuttons is pressed.
  // Based on the pushbutton status and truth table of XOR, turn on or off the LED at Pin 13
  if ((buttonState1 == HIGH && buttonState2 == LOW) || (buttonState1 == LOW && buttonState2 == HIGH)) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);

First you should “Verify and Compile” the code. Goto Sketch->Verify/Compile. If the compilation and verification is successful, then you can go ahead and upload your sketch’s binary to the Arduino micro-controller. Goto File -> Upload. If the upload is successful, then you can test the setup. Press either one of the tactile button and the LED should glow. If both tactile buttons are pressed together or if both are released together, then the LED shouldn’t glow, thus perfectly simulating XOR gate.


2 thoughts on “XOR Simulation using Arduino

  1. Woah! I’m really loving the template/theme of this blog. It’s
    simple, yet effective. A lot of times it’s tough to get that “perfect balance” between user friendliness and visual appearance. I must say you have done a awesome job with this. In addition, the blog loads extremely quick for me on Chrome. Superb Blog!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s