How to Make Logic Gates with Digital I/O using ESP32 Board

Rahmat Al Fajri
4 min readFeb 13, 2022

In this article i am going to show you how to make logic gates using ESP32 Board. To make logic gates we need to understand how digital I/O (Input/Output) works on ESP32. ESP32 Boards have several digital pins that can be used as an input or an output. When there is electricity flows into a pin that pin act as input, and when there is electricity flows out from a pin that pin act as output.

Required Hardware

  1. A breadboard
  2. A micro USB cable
  3. An ESP32 DEVKIT V1 Board
  4. 7 pcs of jumper wires
  5. 2 push buttons
  6. A LED of any colour
  7. A 220 ohm resistor
  8. 2 pcs of 10k ohm resistor

Required Software

Install Arduino IDE and USB to UART driver. For installation process check my previous article about blinking LED in this link: https://rahmatalf.medium.com/how-to-make-blinking-led-with-esp32-devkit-v1-6b2df5bc9798 (See “Required Software”).

Simple I/O : Turn On a LED

Before we jump into logic gates, i am going to show you a simple I/O contraption to turn on an LED. The way it works is when a certain pin received a electric signal, a certain another pin will send out electric signal to turn on an LED. The steps for making the contraption are:

Step 1, Install Arduino IDE and USB to UART driver. For installation process check my previous article about blinking LED in this link: https://rahmatalf.medium.com/how-to-make-blinking-led-with-esp32-devkit-v1-6b2df5bc9798 (See “Required Software”).

Step 2, Assemble the contraption as following diagram and connect the ESP32 board to your computer with micro USB cable. In this case, D4 act as input and D5 act as output. You can choose another GPIO pins if you want.

Step 3, Open Arduino IDE, and go to File > 02.Digital > Button.

Step 4, Change buttonPin value to 4 and ledPin value to 5.

Step 5, Upload the code into ESP32 board, for uploading step check my previous article about blinking LED in this link: https://rahmatalf.medium.com/how-to-make-blinking-led-with-esp32-devkit-v1-6b2df5bc9798 (See the fifth step of How to Blink Built-In ESP32 LED). This step is the same for every uploading process.

After the code uploaded the process are done. This is the result if you succeed.

How to Make Logic Gates (AND, OR, XOR)

The steps for making those 3 gates are similar. For the steps for making the logic gates are:

Step 1, Assemble the contraption as following diagram and connect the ESP32 board to your computer with micro USB cable. In this case, D4 and D21 act as input and D5 act as output. You can choose another GPIO pins if you want.

Step 2, Copy the code below to Arduino IDE.

// set pin numbers
const int button1Pin = 4; // the number of pushbutton 1 pin
const int button2Pin = 21; // the number of pushbutton 2 pin
const int ledPin = 5; // the number of the LED pin
// variable for storing the pushbuttons status
int button1State = 0;
int button2State = 0;
void setup(){
Serial.begin(115200);
// initialize the pushbutton pins as an input
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
// initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
}
void loop(){
// read the state of the pushbuttons value
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
// check if the pushbuttons is pressed
if (STATEMENT){
// turn LED on
digitalWrite(ledPin, HIGH);
}
else{
// turn LED off
digitalWrite(ledPin, LOW);
}
}

Change the “STATEMENT” into:

  1. For AND gate : “button1State == HIGH && button2State == HIGH”
  2. For OR gate : “button1State == HIGH || button2State == HIGH”
  3. For XOR gate : “!button1State != !button2State”

Copy without the quotation mark

Step 3, Upload the code. After that you should have logic gates like this.

And that’s all. Thank you and feel free to try!

--

--