The ATmega328P receives and echoes UART characters, but that does not prove its PORTB outputs or LED wiring work; isolate the GPIO path first, then reconnect UART and verify each stage.
GPIO-only LED baseline on PORTB
Prerequisite: disconnect the UART adapter and any programmer connections that could load or drive PORTB. Keep the LED circuit disconnected until the MCU pins pass a GPIO-only test. A successful UART echo proves that characters travel through the serial path; it says nothing about the output register, pin direction, transistor wiring, or LED polarity.
- Flash a test that sets
DDRB = 0xFF, then alternatesPORTBbetween0x11and0x88with a one-second delay. This pattern alternates four selected bits at a time and does not depend on UART input. - Measure the relevant PORTB pins with a multimeter or oscilloscope while the pattern runs. Confirm that each tested pin changes between the expected logic levels. The measurement is the gate: if the pins do not switch, debug code, pin configuration, clock/program execution, or connected loads before examining LEDs.
- After confirming the pin transitions, connect one LED channel at a time using a series current-limiting resistor and a topology compatible with the output polarity. Confirm that the LED changes state with the pin. Do not use the transistor stage as the initial proof of GPIO function.
The source mentions the ATmega328P's 40 mA per-pin figure, but that figure is not a target operating current for a design. Select LED current and resistor from the LED and MCU datasheets, and check the MCU's per-pin and aggregate port/device limits. A transistor does not remove the need for a correctly wired LED current path or a resistor limiting base current.
PORTB pin-to-LED path
Prerequisite: confirm that the GPIO-only test changes the measured pin voltage. Then trace one channel from the MCU pin through every component to the supply return. A working LED tested separately proves only that the LED itself lights; it does not prove the installed polarity, resistor, transistor terminals, or shared return are correct.
- Identify the transistor's exact part and pinout from its own datasheet. Do not rely on the claim that seven BC547 devices and one 2N2222 have the same pinout; transistor lead arrangements can differ between types and manufacturers.
- Check the base path for a resistor and verify the transistor's base, collector, and emitter connections against the intended circuit. The described arrangement puts an LED anode at +V and its cathode at a transistor base; inspect that current path carefully rather than assuming it forms a valid switched LED channel.
- Measure the MCU output, transistor control terminal, and switched LED path while alternating the test pattern. If the MCU pin toggles but the transistor control node does not, correct the interconnect or base path. If the control node toggles but the LED remains off, check transistor pinout, LED polarity, series resistance, and supply return.
An NPN transistor is not automatically a suitable high-side switch. Its behavior depends on how its collector, emitter, and load are connected; a high-side emitter-follower arrangement cannot provide an output above the base drive and may not deliver the intended LED current. Verify the actual schematic and voltage readings before selecting a replacement topology.
UART byte-to-port behavior
Prerequisite: prove that at least one direct GPIO-driven LED follows the one-second test. Reflash the loopback program, then send one known character such as U, whose ASCII value is 0x55. The program assigns the received byte directly to PORTB, so each bit controls its corresponding port output; the LEDs do not display decimal character codes.
- Confirm that the terminal shows the expected echoed character and that the measured PORTB pins represent the received byte's bit pattern, allowing for the LED circuit's active-high or active-low polarity.
- Inspect YaT's transmit settings for carriage return or line feed. If the terminal sends extra control characters after the typed character, the program receives and writes those bytes too; the last byte can replace the visible pattern almost immediately.
- Check whether
Readyappears after the minimal test starts. If it does not, resolve program execution or serial configuration before interpreting the LED test. If it does, send only one character without a line ending and measure the pins during and after reception.
receiveByte() normally blocks while waiting for the next character. Therefore, after a single received byte, the assigned PORTB value remains in the output register until another write occurs. A blanket claim that the main loop runs too fast does not explain an output that should persist while waiting; rapid terminal-sent CR/LF bytes can, however, make intermediate patterns too brief to see.
External supply and UART reference
Prerequisite: run the GPIO test from a supply whose voltage is within the ATmega328P and peripheral ratings. The reported echo worked when powered from the USB-serial adapter but disappeared on four AA batteries. That change makes the power and reference wiring part of the diagnostic, not proof of a particular brown-out or current problem.
- When an external supply powers the MCU, connect its ground to the USB-serial adapter ground so UART TX and RX use a common voltage reference. Verify continuity between grounds with power removed, then confirm the voltage at the MCU supply pins while the circuit runs.
- Measure supply voltage at the MCU during activity, not only at the battery pack. A low or unstable reading directs the next check to battery condition, wiring drop, regulator, or load. Read the MCU's datasheet limits and the adapter's logic-level specifications before connecting signal or supply pins.
- Do not tie the adapter's VCC output to an external supply unless the power arrangement explicitly supports it. Use one defined source for the MCU rail and preserve the common signal ground.
After changing the supply, repeat the Ready message and UART echo test before reconnecting LEDs. This separates serial reference or supply faults from the GPIO and transistor path.
Symptoms and discriminating measurements
Prerequisite: record pin and rail readings during the tests above. Use the observed boundary between a passing and failing stage to choose the next correction rather than changing code and wiring at once.
| Symptom | Next measurement | Interpretation and action |
|---|---|---|
| UART echo works; PORTB pins do not follow the one-second pattern | Measure PORTB directly | Stay at the MCU/code boundary. Check output direction, program execution, pin use, and any connected load. |
| PORTB pin toggles; its transistor control node does not | Measure across the base path and inspect pinout | Correct the wiring, missing connection, or base resistor path. |
| Transistor control node toggles; LED does not | Measure the LED supply and current path in-circuit | Check LED polarity, transistor terminals, current limiting, and return wiring. |
| Single character appears to produce no stable pattern | Disable terminal CR/LF and probe PORTB | Determine whether extra bytes overwrite the first pattern; compare measured pins, not visual impression alone. |
| UART echo fails only on battery power | Measure MCU rail and verify common ground | Correct the supply/reference path if readings show a missing reference or out-of-range/unstable rail. |
Reconnect the transistor stage and verify the channel
Prerequisite: direct GPIO operation and one direct LED channel both pass. Reconnect the transistor circuit one channel at a time so a wiring fault on one channel cannot obscure the others.
- With power off, confirm the LED's series current-limiting path, transistor lead identification, base resistor, and common return against the circuit design and component datasheets.
- Power the circuit and apply the alternating
0x11/0x88test. Probe at the MCU pin, transistor control terminal, and LED path. Advance to another channel only when each preceding point shows the expected change. - Once the hardware pattern works on all intended channels, run the UART program and send
Uwith terminal line-ending transmission disabled. Confirm the echo and the expected pin bit pattern electrically; then test the terminal's normal line-ending setting separately. - Repeat with the intended external supply. Confirm the MCU rail remains within its datasheet range, the grounds remain common, and the UART echo and PORTB readings still pass.
Frequently asked questions
Can I power the ATmega328P from an external battery and use the USB-serial adapter for UART?
Yes, if the MCU and adapter signal levels are compatible and their grounds are connected. Measure the MCU supply at its pins and avoid paralleling the adapter's VCC with the external rail.
Does a working UART echo prove PORTB is configured correctly?
No. Echo confirms the serial receive/transmit path, not PORTB direction, pin voltage, or the LED driver circuit. Verify PORTB with a GPIO-only alternating-pattern test and a meter or scope.
Can carriage return or line feed hide the character's LED pattern?
Yes. If YaT sends CR or LF after a character, the program writes those bytes to PORTB as additional values. Disable line endings for a single-byte test, then probe the pins.
Does the PORTB value stay set after receiveByte returns?
Yes, the output register retains its last written value until the program writes another value or the device state changes. The receive function's wait for the next byte means a single character should not disappear merely because the loop is fast.
Can I use BC547 and 2N2222 transistors with the same wiring?
Only after checking the datasheet for each exact device and manufacturer; do not assume matching lead order. Verify the MCU pin, transistor control terminal, and LED path for each channel, then finish by confirming UART echo and PORTB readings on the intended supply.