8051 – GPIO Tutorial

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

Let’s start!!!!!

Before we start, please read this article to get to know how the GPIO is working in any microcontroller.

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 the Ports in the 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.

Pin Diagram

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


void main()
    unsigned int i; 


8051 GPIO Example

[ Image: LED Output ]

Switch Interfacing

Pin Diagram

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


sbit sw=P1^0;  
void main() 
{      while(1) 
        if( sw == 0)  
            P2 = 0xFF; 
            P2 = 0x00; 



8051 GPIO Tutorial

[ Image Output ]


Hope you enjoyed this tutorial. But this is the basic. You can do 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.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Table of Contents