Demystifying ATmega328P I/O Registers: a Simplified Mechanical Switch Architecture for Arduino Input/Output Pins Featuring Digital Pin 13 Logic
by BoboacaC in Circuits > Arduino
124 Views, 0 Favorites, 0 Comments
Demystifying ATmega328P I/O Registers: a Simplified Mechanical Switch Architecture for Arduino Input/Output Pins Featuring Digital Pin 13 Logic
When we program an Arduino Nano, we use functions like pinMode() or digitalWrite(). But what happens physically inside the microcontroller? This hand-drawn schematic shows the hardware reality. It maps out the real logic switches and wires that connect the registers to the external pin.
Today's microcontrollers (like ESP32 or STM32) are powerful but complex. Because of this, the ATmega328P of the Arduino Nano remains the ideal tool for teaching. Its 8-bit layout is clean. It lets you understand basic digital electronics without getting lost in thousands of pages of datasheets.
Supplies
arduino nano
tool online - https://costycnc.github.io/avr-compiler-js/
The Three Core Registers
The circuit of a single PIN is split into three vertical sections. These are controlled by three specific registers:
- DDRB (Direction): Decides if the pin is an input or an output. It moves the direction switch.
- PORTB (Data/Pull-up): Decides the output state (HIGH/LOW) or activates the internal pull-up resistor.
- PINB (Input): The direct line that reads the physical voltage level present on the pin from the outside.
Real Example — Activating the Internal Pull-Up Resistor
Let's look at the example written at the bottom of the sketch. We want to configure Pin 13 as an Input with an active Pull-Up resistor.
Instead of slow Arduino code, we use two fast Assembly commands:
Follow the red line on the sketch to see what happens inside the chip:
pinMode(13, Input); CBI 4,5
digitalWrite(13, HIGH); SBI 5,5
- CBI 4,5: This opens the direction switch in REG-4 (DDRB). The pin is now an input.
- SBI 5,5: This closes the top switch in REG-5 (PORTB).
The Result: The input line connects directly to VCC through the internal 40k$\Omega$ resistor. Even if nothing is plugged into the pin from the outside, the chip reads a stable HIGH signal. It will never float!
Real Example — Standard Input (High Impedance / Floating)
Now, let's look at how to configure Pin 13 as a Standard Input with the Pull-Up resistor disabled.
This requires two CBI (Clear Bit) Assembly commands:
Follow the red line on this new sketch to trace the circuit path:
pinMode(13,Input);
digitalWrite(13,LOW);
- CBI 4,5: The switch in REG-4 (DDRB) moves to 0. The pin is now an input.
- CBI 5,5: The switch in REG-5 (PORTB) moves to 0.
The Result: The path to the 40k resistor is open. The pin is completely disconnected from VCC and GND. It is in a high-impedance state, reading only the voltage applied by your external circuit.
Real Example — Output HIGH Mode
Now, let's see what happens when we want to configure Pin 13 as an Output and drive it to a HIGH state (5V).
This requires two SBI (Set Bit) Assembly commands:
Follow the red line on this third sketch to trace the circuit path:
pinMode(13,OUTPUT)
digitalWrite(13,HIGH)
- SBI 4,5: The switch in REG-4 (DDRB) moves to 1. This disconnects the floating input mode and engages the output driver.
- SBI 5,5: The lower switch in REG-5 (PORTB) moves to 1.
The Result: The external pin is now physically tied directly to VCC. The microcontroller actively pushes 5V to the pin to power your external component.
Real Example — Output LOW Mode
Finally, let's look at how to configure Pin 13 as an Output and drive it to a LOW state (0V / Ground).
This requires an SBI (Set Bit) command followed by a CBI (Clear Bit) command:
Follow the red line on this final sketch to trace the circuit path:
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
- SBI 4,5: The switch in REG-4 (DDRB) moves to 1 to enable the output driver.
- CBI 5,5: The lower switch in REG-5 (PORTB) moves to 0.
The Result: The external pin is now physically tied directly to GND. The microcontroller actively pulls the line down to 0V.
Interactive Hardware Model
To make this concept even easier to understand, I built a physical, interactive 3D model of the circuit logic using springs, wires, and LEDs!
(Inserisci la tua foto qui)
As you can see in the photo:
- The springs act as the physical switches controlled by the SBI and CBI commands.
- The LEDs immediately show the state of the logic lines.
- By physically moving the springs to match the commands written on the paper (SBI 4,5 and SBI 5,5), you can trace the path of the electricity just like a real microchip handles the signal!
This proves that hardware isn't abstract—it is a physical set of connections that you can see, build, and test.
Hardware Model — Output LOW Mode
Here is the second physical hardware configuration, showing what happens when we drive the pin to a LOW state (0V / Ground).
Look closely at the commands written at the bottom:
By changing the instruction from SBI to CBI 5,5, we visually change the path:
- The middle spring in REG-5 (PORTB) is now flipped down to the lower contact.
- This physically cuts off the VCC line and routes the circuit directly to GND.
- The shift in the electrical current immediately turns off the previous state and activates the second LED (the blue one on the right)!
This side-by-side physical comparison makes it crystal clear how a single change in your Assembly code mechanically alters the internal routes of the processor to ground out the signal.
See the Practical Assembly Application!
Code tells the chip what to do, but this schematic shows what actually happens in the silicon. It is not magic; it is pure hardware.
To see how these sbi and cbi instructions are used directly in code to drive this logic at maximum speed, visit my dedicated page: https://costycnc.github.io/avr-compiler-js/.