8051 – GPIO Tutorial

Hi All…. In this tutorial we are going to see 8051 GPIO Tutorial. Before that if you are not installing Keil please install that. If you don’t know please go through the Here. Please see Here if you don’t know how to create the project. See the introduction of 8051 Here.

Let’s start!!!!!

8051 GPIO Tutorial


Normally 8051 has four GPIO ports.

  1. Port 0 (P0)
  2. Port 1 (P1)
  3. Port 2 (P2)
  4. Port 3 (P3)

I have already discussed about the Ports in introduction. So now we can directly go into the programming.


Writing the data on a particular port is sending the data from the controller to any peripheral device which is connected with the controller. For example here we are using the LED to indicate the data coming out from the parallel port.


Reading operation is getting the data from any peripheral device. Here we are using a simple push button (switch) to give the input to the port pins.

LED Interfacing

A light emitting diode (LED) is essentially a PN junction opto-semiconductor that emits a monochromatic (single color) light when operated in a forward biased direction. LEDs convert electrical energy into light energy. They are frequently used as “pilot” lights in electronic appliances to indicate whether the circuit is closed or not.

1-4 8051 - GPIO Tutorial

Pin Diagramaa 8051 - GPIO Tutorial

Here LEDs are connected into Port 2 (P2). If you are going to connect LEDs into Port 0 (P0) you should connect the pull up resistor.




ezgif-3-40d2da4ce8 8051 - GPIO Tutorial


Switch Interfacing

Pin Diagram

aa-1 8051 - GPIO Tutorial

Here we are connecting switch into Port 1 . 0 (P1.0). Then LEDs are connected into P2. Whenever i press switch these LEDs should glow.




ezgif-3-a8b12877b3 8051 - GPIO Tutorial


Hope you enjoyed in this tutorial. But this is the basic. You can do it whatever you want. I will give you some exercises. Do practice it.


  1. Blink alternate LEDs at P3 using software delay
  2. Blink P0 LEDs in cyclic fashion using software delay
  3. Count the number of times a switch at P1.1 is pressed and display the count in P2If you have any doubt please comment below. In our next tutorial we will see LCD interfacing.
%d bloggers like this: