Microcontrollers
8051 microcontroller pin diagram and pin description
By Bulan Sarkar, founder of ElectronicsInfoline (since 2000) · About a 9-minute read
The 8051 in a 40-pin DIP has 32 I/O pins in four 8-bit ports, and most of them do a second job. This page gives every pin, what it does, and the circuit you need around the chip before it will run at all.
It is drawn for the AT89S52, the flash 8051 from Microchip (formerly Atmel), and checked against Microchip's datasheet 1919D–MICRO–6/08. Each fact names the page it came from. The older AT89C51 uses the same pinout, with the differences listed further down.
Pin 40 is VCC and pin 20 is ground. Port 1 is pins 1 to 8, RST is 9, Port 3 is 10 to 17, the crystal goes on 18 and 19. Port 2 is pins 21 to 28, then PSEN on 29, ALE on 30 and EA on 31, and Port 0 runs from P0.7 on pin 32 up to P0.0 on pin 39 (AT89S52 datasheet, p. 2).
Three things catch beginners. Tie EA to VCC, or the chip ignores its own flash. Port 0 has no pull-ups, so it cannot drive a pin high without external resistors. And RST is active high, unlike the active-low reset on an AVR or most ARM chips.
8051 pin diagram
Pins count down the left side from the notch and back up the right, so pin 1 sits opposite pin 40 and pin 20 opposite pin 21. Port 0 is numbered against the flow: P0.0 is pin 39, next to VCC, and P0.7 is pin 32. The IC packages lesson explains the numbering convention for DIPs in general.
Pin description, all 40 pins
| Pin | Name | Second function | What it does | Page |
|---|---|---|---|---|
| 1 | P1.0 | T2 | Port 1 I/O; Timer 2 external count input, clock-out (AT89S52) | 4 |
| 2 | P1.1 | T2EX | Port 1 I/O; Timer 2 capture/reload trigger (AT89S52) | 4 |
| 3 | P1.2 | Port 1 I/O | 4 | |
| 4 | P1.3 | Port 1 I/O | 4 | |
| 5 | P1.4 | Port 1 I/O | 4 | |
| 6 | P1.5 | MOSI | Port 1 I/O; MOSI for in-system programming | 4 |
| 7 | P1.6 | MISO | Port 1 I/O; MISO for in-system programming | 4 |
| 8 | P1.7 | SCK | Port 1 I/O; SCK for in-system programming | 4 |
| 9 | RST | Reset input, active high | 5 | |
| 10 | P3.0 | RXD | Port 3 I/O; serial input | 5 |
| 11 | P3.1 | TXD | Port 3 I/O; serial output | 5 |
| 12 | P3.2 | INT0 | Port 3 I/O; external interrupt 0 | 5 |
| 13 | P3.3 | INT1 | Port 3 I/O; external interrupt 1 | 5 |
| 14 | P3.4 | T0 | Port 3 I/O; Timer 0 external input | 5 |
| 15 | P3.5 | T1 | Port 3 I/O; Timer 1 external input | 5 |
| 16 | P3.6 | WR | Port 3 I/O; external data memory write strobe | 5 |
| 17 | P3.7 | RD | Port 3 I/O; external data memory read strobe | 5 |
| 18 | XTAL2 | Oscillator amplifier output | 6 | |
| 19 | XTAL1 | Oscillator amplifier input, or external clock in | 6 | |
| 20 | GND | Ground | 4 | |
| 21 | P2.0 | A8 | Port 2 I/O; address bit A8 on external memory cycles | 4 |
| 22 | P2.1 | A9 | Port 2 I/O; address A9 | 4 |
| 23 | P2.2 | A10 | Port 2 I/O; address A10 | 4 |
| 24 | P2.3 | A11 | Port 2 I/O; address A11 | 4 |
| 25 | P2.4 | A12 | Port 2 I/O; address A12 | 4 |
| 26 | P2.5 | A13 | Port 2 I/O; address A13 | 4 |
| 27 | P2.6 | A14 | Port 2 I/O; address A14 | 4 |
| 28 | P2.7 | A15 | Port 2 I/O; address A15 | 4 |
| 29 | PSEN | Read strobe for external program memory | 6 | |
| 30 | ALE/PROG | Address latch enable output; program pulse input while the flash is programmed | 5 | |
| 31 | EA/VPP | External access enable: tie to VCC to run from on-chip flash; 12 V programming enable | 6 | |
| 32 | P0.7 | AD7 | Port 0 I/O, open drain; multiplexed address/data AD7 | 4 |
| 33 | P0.6 | AD6 | Port 0 I/O, open drain; AD6 | 4 |
| 34 | P0.5 | AD5 | Port 0 I/O, open drain; AD5 | 4 |
| 35 | P0.4 | AD4 | Port 0 I/O, open drain; AD4 | 4 |
| 36 | P0.3 | AD3 | Port 0 I/O, open drain; AD3 | 4 |
| 37 | P0.2 | AD2 | Port 0 I/O, open drain; AD2 | 4 |
| 38 | P0.1 | AD1 | Port 0 I/O, open drain; AD1 | 4 |
| 39 | P0.0 | AD0 | Port 0 I/O, open drain; AD0 | 4 |
| 40 | VCC | Supply voltage | 4 |
Port 0 needs pull-up resistors
Port 0 (pins 32 to 39) is open drain. Each pin can pull low hard, eight TTL loads' worth, but has nothing to pull it high. Write a 1 to a Port 0 pin and it simply lets go: the pin floats (AT89S52 datasheet, p. 4). The datasheet gives Port 0 internal pull-ups only while it is acting as the address/data bus to external memory.
So when Port 0 is used as ordinary I/O, fit a pull-up resistor on every pin you use. We use 10 kΩ, usually as one 9-pin resistor network across all eight. That value is our choice, not the datasheet's: it holds a floating input firmly high and wastes only 0.5 mA while the pin is held low. The pull-up and pull-down resistors lesson works through how to size one.
The datasheet also asks for external pull-ups on Port 0 while the flash is being verified in a programmer, for the same reason (AT89S52 datasheet, p. 4).
Our take: if a design only needs 24 I/O lines, leave Port 0 for last. It is the one port that needs extra parts.
Ports 1, 2 and 3
Ports 1, 2 and 3 have internal pull-ups. To read a pin as an input, first write a 1 to it; the weak pull-up then holds it high and anything outside can pull it low (AT89S52 datasheet, p. 4). There is no separate direction register. After reset every port latch holds 1s (FFH), so every pin starts out as an input (AT89S52 datasheet, p. 7).
Those pull-ups are weak. The datasheet guarantees a high of 2.4 V only up to 60 µA of load, while the same pin can sink 1.6 mA and stay under 0.45 V (AT89S52 datasheet, p. 29). So an 8051 drives an LED or a transistor base by pulling it low, not by pushing it high. Keep each pin under 10 mA, each of Ports 1 to 3 under 15 mA in total, Port 0 under 26 mA, and the whole chip under 71 mA (AT89S52 datasheet, p. 29).
Port 1 (pins 1 to 8). On the AT89S52, P1.0 is also the Timer 2 count input T2 and P1.1 its trigger T2EX. P1.5, P1.6 and P1.7 carry MOSI, MISO and SCK when the chip is being programmed in-circuit (AT89S52 datasheet, p. 4). If you program it on the board, keep those three free of anything that would load them.
Port 2 (pins 21 to 28) puts out the high address byte, A8 to A15, when the chip runs code from external memory or uses 16-bit external data addresses (AT89S52 datasheet, p. 4). With everything on-chip it is a plain I/O port.
Port 3 (pins 10 to 17) holds the serial port, interrupts and timer inputs (AT89S52 datasheet, p. 5):
| Pin | Port bit | Second function |
|---|---|---|
| 10 | P3.0 | RXD: serial input |
| 11 | P3.1 | TXD: serial output |
| 12 | P3.2 | INT0: external interrupt 0 |
| 13 | P3.3 | INT1: external interrupt 1 |
| 14 | P3.4 | T0: Timer 0 external input |
| 15 | P3.5 | T1: Timer 1 external input |
| 16 | P3.6 | WR: external data memory write strobe |
| 17 | P3.7 | RD: external data memory read strobe |
If you use the serial port or an interrupt input, leave that pin out of any code that writes Port 3 as a whole byte.
RST, ALE/PROG, PSEN and EA/VPP
RST, pin 9. Reset is active high. Holding it high for two machine cycles while the oscillator runs resets the chip (AT89S52 datasheet, p. 5). A machine cycle is 12 oscillator periods (AT89S52 datasheet, p. 12), so at 11.0592 MHz two cycles last 2.17 µs. The pin has its own pull-down of 50 kΩ to 300 kΩ inside (AT89S52 datasheet, p. 29). When the watchdog times out, the chip drives RST high itself for 98 oscillator periods (AT89S52 datasheet, p. 5).
ALE/PROG, pin 30. Address Latch Enable pulses to latch the low address byte from Port 0 into an external latch. It runs at one sixth of the oscillator frequency even with no external memory, so it makes a handy check that the crystal is running: 11.0592 MHz gives 1.8432 MHz pulses on a scope. (Setting bit 0 of the AUXR register at 8EH silences it except during MOVX and MOVC.) The same pin takes the program pulse during flash programming (AT89S52 datasheet, p. 5).
PSEN, pin 29. Program Store Enable is the read strobe for external program memory (AT89S52 datasheet, p. 6). With code in the on-chip flash, leave it unconnected.
EA/VPP, pin 31. External Access. Strap it to VCC to run from the on-chip flash; strap it to ground and the chip fetches every instruction from external memory (AT89S52 datasheet, p. 6). A board with EA grounded or floating looks exactly like a dead chip. The pin also takes the 12 V programming voltage in a parallel programmer.
XTAL1 and XTAL2, pins 19 and 18. Input and output of the on-chip inverting amplifier. A crystal or ceramic resonator goes between them, or an external clock drives XTAL1 with XTAL2 left open (AT89S52 datasheet, p. 19). The crystals and resonators lesson explains what the crystal is doing there.
Minimal circuit: what the chip needs to run
Supply. The -24 grade runs from 4.0 V to 5.5 V up to 24 MHz; the -33 grade needs 4.5 V to 5.5 V for 33 MHz (AT89S52 datasheet, p. 34). Put a 100 nF ceramic capacitor across pins 40 and 20, as close to the chip as the board allows (see IC power decoupling).
Crystal. The datasheet asks for C1 and C2 of 30 pF ± 10 pF with a crystal, or 40 pF ± 10 pF with a ceramic resonator (AT89S52 datasheet, p. 19); 33 pF is a common value inside that range. We show 11.0592 MHz rather than 12 MHz because it divides down to serial baud rates exactly: 11,059,200 / (12 × 32 × 3) = 9,600 baud.
Reset. The datasheet gives the timing rule and leaves the parts to you. It needs RST high for two machine cycles with the oscillator already running, and after power-up the oscillator takes time to start. Our choice, and a common one, is 10 µF from VCC to RST and 10 kΩ from RST to ground. At switch-on the capacitor holds RST at VCC and then lets it fall with a time constant of 10 kΩ × 10 µF = 100.0 ms.
RST counts as high down to 0.7 VCC (AT89S52 datasheet, p. 29), so it stays high for 100.0 ms × ln(1/0.7) = 35.7 ms. The internal pull-down sits in parallel with the 10 kΩ; at its lowest, 50 kΩ, the pair is 8.33 kΩ and the time drops to 29.7 ms. Either way that is thousands of times the 2.17 µs the chip needs, which leaves the crystal plenty of time to get going. The RC time constant lesson has the charging curve behind those numbers. A push button across the capacitor adds a manual reset.
The LED. P1.0 sinks it from 5 V through 2.2 kΩ. With a red LED at about 2 V and the pin at its 0.45 V limit, the current is (5 − 2 − 0.45) V / 2.2 kΩ = 1.16 mA. That stays under the 1.6 mA at which the datasheet guarantees the low level (AT89S52 datasheet, p. 29), and a modern indicator LED is plainly visible at a milliamp. For anything heavier, such as a relay, switch it through a transistor: our 2N2222 page works through a relay driver, and relay driving explains the diode it needs.
Driving the pins from code
Each port is also a special function register, at 80H (P0), 90H (P1), A0H (P2) and B0H (P3), and every pin is bit-addressable (AT89S52 datasheet, p. 7). In 8051 assembly:
MOV P1,#0FFh ; 1s in the latch: Port 1 pins become inputs
MOV A,P1 ; read all eight pins into A
CLR P1.0 ; pin 1 low: the LED in Figure 2 lights
SETB P1.0 ; pin 1 released to the pull-up: LED offIn C, Keil C51 names a pin with sbit LED = P1^0; and SDCC's header calls the same bit P1_0. Writing a whole port is P1 = 0xFF; in both.
AT89S52 and AT89C51
The two share the pinout. What changes is the memory, the third timer and how the chip is programmed.
| AT89S52 | AT89C51 | |
|---|---|---|
| Flash | 8K bytes | 4K bytes |
| RAM | 256 bytes | 128 bytes |
| Timers | Three (Timer 2 on P1.0/P1.1) | Two; P1.0 and P1.1 are plain I/O |
| Programming | In-system, over P1.5 to P1.7 | Parallel programmer; 12 V or 5 V on EA/VPP, depending on the version shipped |
| Clock | 0 to 33 MHz | 0 to 24 MHz |
| Datasheet notice | None | "Not recommended for new designs. Use AT89S51." |
Sources: (AT89S52 datasheet, p. 1) (AT89S52 datasheet, p. 2) (AT89C51 datasheet, p. 1) (AT89C51 datasheet, p. 6).
So a board built for an AT89C51 takes an AT89S52 without changes, and gains in-circuit programming if the three SPI pins are brought out to a header.
Lessons underneath this
- Pull-up and pull-down resistorsWhy Port 0 needs them and how to size one.
- Crystals and resonatorsWhat the crystal on pins 18 and 19 does.
- RC time constantThe maths behind the power-on reset.
- Logic levelsWhat counts as high and low on a 5 V port pin.
- LED circuitsSizing the series resistor for a port pin.
- The BJT as a switchDriving loads a port pin cannot carry.
- Reading IC datasheetsHow to find limits like the 10 mA per pin yourself.
- How the ARM1 decoded its instructionsInside a 32-bit processor from the same decade.
Datasheets this page was checked against
- Atmel/Microchip AT89S52 datasheet, 8-bit Microcontroller with 8K Bytes In-System Programmable Flash (1919D–MICRO–6/08)
- Atmel/Microchip AT89C51 datasheet, 8-bit Microcontroller with 4K Bytes Flash (Rev. 0265G–02/00)
Read on 30 September 2026. Makers revise datasheets; if your copy is newer and a number here disagrees with it, the datasheet wins, and we would like to hear about it.