What the standard asks
Section 5, Fault finding, is worth 5% of the marks (WorldSkills Occupational Standards, Electronics, WorldSkills Shanghai 2026, checked 2026-11-25). It asks you to plan a test strategy, use tools and software to narrow a fault down, and test whole units and single parts. On a microcontroller board, the first question is always the same: is it the program, or the board?
The section page warns against blaming the wrong one. This lesson gives you a method to decide in minutes, and the numbers behind the two buses that fail most often at the bench: UART and I2C.
The theory
The split test
Change one side at a time, using something you know works:
- A known-good program on your board. A blink at a known rate, an I2C scanner, a serial echo. If it fails, the board or the wiring is at fault.
- Your program on a known-good board. If it fails there too, your program is at fault.
- A loopback. Join TX to RX and type. If the characters come back, the port, the adapter and the cable work.
Keep these test programs ready before you need them. In a competition, you do not want to write a scanner while the clock runs.
Power and clock first
Before any bus: the chip's supply measured at its pin, every board's ground joined, the reset pin HIGH, and a blink timed on a scope. A blink that should be 1.00 Hz and measures 1.00 Hz proves the clock and the program run.
UART: the baud rate is never exact
An STM32F103 makes its baud rate by dividing its bus clock by 16 × USARTDIV, where USARTDIV has a whole part and a fraction in sixteenths (RM0008, USART section). The HAL works it out from the clock tree you set in CubeMX. The UART lesson does it for USART2 at 36 MHz: 115 200 baud comes out at 115 385, +0.16%. The fraction keeps the divider's error small. The bigger risk is the clock itself.
| Clock | USART2 clock | USARTDIV used | Error from the divider | Error from the clock | Worst total |
|---|---|---|---|---|---|
| HSE: the 8 MHz from a Nucleo's ST-LINK, PLL to 72 MHz | 36 MHz | 19 + 8/16 | +0.16% | a crystal's, tiny | +0.16% |
| HSI, the internal RC, PLL to 64 MHz, at 25 °C | 32 MHz | 17 + 6/16 | −0.08% | −1.1% to +1.8% | +1.7% |
| HSI, the same, from −40 to 105 °C | 32 MHz | 17 + 6/16 | −0.08% | −2% to +2.5% | +2.4% |
The HSI's limits are from the STM32F103 datasheet's HSI oscillator table. A receiver samples each bit in its middle, so the two ends can differ by only a few percent in total before bytes go wrong. Aim for each end within about ±2%. On the HSI, 115200 may work on the bench and fail in a hot box, or work with one adapter and fail with another. When a UART must work every time, run the chip from HSE: a crystal, or the 8 MHz that a Nucleo's ST-LINK feeds it. And work out the other end's error too.
Other UART faults: TX joined to TX instead of crossed, no common ground, a 5 V TX into a 3.3 V RX with no level shift, and the wrong frame (8N1 at one end, 8E1 at the other).
I2C: pull-ups and rise time
I2C lines are only ever pulled LOW by the devices. A resistor pulls each line HIGH. That resistor and the bus capacitance set how fast the line rises:
- The rise time from 30% to 70% of the supply is 0.8473 × R × C (TI SLVA689). The I2C specification allows at most 1000 ns in standard mode (100 kHz) and 300 ns in fast mode (400 kHz).
- With a 200 pF bus: 10 kΩ gives 1.69 µs, too slow even for 100 kHz. 4.7 kΩ gives 0.80 µs: fine for 100 kHz, too slow for 400 kHz. 1.5 kΩ gives 0.25 µs: fine for 400 kHz.
- The pull-up cannot be too small either: the devices must pull the line below 0.4 V while sinking 3 mA. At 3.3 V the smallest is (3.3 − 0.4) ÷ 0.003 ≈ 970 Ω.
Other I2C faults:
- The address. Datasheets give a 7-bit address, such as 0x68. Some write it shifted left with the read or write bit added: 0xD0 and 0xD1. Libraries differ too. ST's HAL wants the 7-bit address shifted left by one, 0xD0, and adds the read or write bit itself: the comment above each of its I2C functions says so. The I2C library in the Lab's sketches wants 0x68. Read the comment above the function you call.
- A line stuck LOW. A device was reset in the middle of a transfer and is still holding SDA. Clocking SCL nine times usually frees it.
- An address pin. Many sensors change address with a pin. An MPU-6050 is at 0x68 with AD0 LOW and 0x69 with AD0 HIGH.
Code faults that look like hardware
- A dim LED. The pin was set as GPIO_Input with Pull-up in CubeMX, not GPIO_Output. Its pull-up, 30 to 50 kΩ on an STM32F103 (its datasheet), lets only a fraction of a milliamp through the LED, and it glows faintly. On an STM32F1, writing to an input pin with a pull only switches between the pull-up and the pull-down (RM0008, port bit configuration), so the LED is faint or off, never fully on. The pin was never set as an output.
- A missed button. A blocking delay, not a bad switch.
- Rubbish on the serial port. A wrong clock setting in the program, not a bad cable.
Hardware faults that look like code
- Resets when a motor or relay switches: the supply dips (a brown-out), not a crash in the program.
- Random readings from an input with nothing pulling it: a floating pin.
- A program that works until you touch the board: a missing decoupling capacitor or a cracked joint.
The logic analyser
When the scanner and the loopback do not settle it, look at the wire. A cheap 8-channel analyser with PulseView decodes UART, I2C and SPI into bytes. Sample at 4 to 10 times the fastest signal: at least 1 MS/s for 115200 baud, at least 4 MS/s for 400 kHz I2C.
Worked example
Symptom. A NUCLEO-F103RB reads an MPU-6050 on I2C1, with 4.7 kΩ pull-ups to 3.3 V on the board. Every reading is zero.
- Power and clock. 3.3 V at the module's VCC pin, grounds joined, the Nucleo's blink correct. Fine.
- Split. Run the known-good test: an I2C scan you keep ready as its own CubeIDE project, with I2C1 at 100 kHz and USART2 to the virtual COM port, set up in CubeMX. Its USER CODE block 2:
/* USER CODE BEGIN 2 */
char msg[40];
int n, found = 0;
uint8_t who = 0;
n = snprintf(msg, sizeof msg, "Scanning 0x08 to 0x77 at 100 kHz\r\n");
HAL_UART_Transmit(&huart2, (uint8_t *)msg, (uint16_t)n, 100);
for (uint16_t addr = 0x08; addr <= 0x77; addr++) {
/* the HAL wants the 7-bit address shifted left by one */
if (HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(addr << 1), 2, 5) == HAL_OK) {
n = snprintf(msg, sizeof msg, " found 0x%02X\r\n", (unsigned)addr);
HAL_UART_Transmit(&huart2, (uint8_t *)msg, (uint16_t)n, 100);
found++;
}
}
n = snprintf(msg, sizeof msg, "%d device(s) found\r\n", found);
HAL_UART_Transmit(&huart2, (uint8_t *)msg, (uint16_t)n, 100);
/* MPU-6050: WHO_AM_I is register 0x75 and reads 0x68 */
if (HAL_I2C_Mem_Read(&hi2c1, 0x68 << 1, 0x75, I2C_MEMADD_SIZE_8BIT, &who, 1, 100) == HAL_OK) {
n = snprintf(msg, sizeof msg, "WHO_AM_I = 0x%02X\r\n", (unsigned)who);
HAL_UART_Transmit(&huart2, (uint8_t *)msg, (uint16_t)n, 100);
}
/* USER CODE END 2 */
Put #include <stdio.h> in the USER CODE Includes block, as for any snprintf. It reports:
Scanning 0x08 to 0x77 at 100 kHz
0 device(s) found
The known-good program fails, so the fault is in the hardware. Your own code is not the problem yet.
- Idle levels. Program stopped, meter on each line: SDA 3.29 V, SCL 0.00 V. SCL is not being pulled up.
- Power off. Measure. SCL to 3.3 V: OL. SDA to 3.3 V: 4.7 kΩ, the pull-up. SCL's pull-up resistor should also read 4.7 kΩ. Under the loupe, one end of it is not soldered.
- Repair. Reflow the joint. SCL to 3.3 V now reads 4.7 kΩ. Run the scanner again:
Scanning 0x08 to 0x77 at 100 kHz
found 0x68
1 device(s) found
WHO_AM_I = 0x68
0x68 is the right answer: the bus and the sensor work.
- Check the edge. Scope on SCL: the rise from 30% to 70% takes 0.26 µs. With a 4.7 kΩ pull-up that means about 65 pF on the bus (0.26 µs ÷ (0.8473 × 4.7 kΩ)). Well inside 1000 ns for 100 kHz.
- Back to your program. Now that the bus works, your own code reads real values. The record: symptom, split test (scanner failed, so hardware), evidence (SCL 0 V, OL to 5 V), cause (unsoldered pull-up), action, result.
Practice task
- Keep three test programs on your laptop as CubeIDE projects for your board, built and tested: a blink, the I2C scan from the worked example, and a serial echo.
- Ask a partner to plant one fault in a working I2C or UART setup while you are away: a missing pull-up, crossed or uncrossed TX and RX, a wrong baud rate in the program, a wrong address in the program, or an address pin moved.
- Work down the downloadable checklist. Write which test told you "code" or "board".
- Target: under 10 minutes per fault. Then swap.
- Measure the rise time of SCL on your own bus with a 10 kΩ, a 4.7 kΩ and a 2.2 kΩ pull-up. Work out the bus capacitance from each reading. Do the three answers agree?
Common mistakes
- Debugging your own program on a board nobody has tested. Run a known-good test first.
- The address in the wrong form for the library. ST's HAL wants 0xD0, the 7-bit 0x68 shifted left. Pass it 0x68 and it finds nothing.
- Two sets of pull-ups. Many modules have their own. Three modules on one bus can mean three resistors in parallel, too strong for some devices.
- No common ground between boards. The UART or I2C levels mean nothing without it.
- Trusting the baud rate in the code. Work out the real rate from the clock and the divider.
- A logic analyser sampling too slowly. The decoder shows nonsense, and you blame the bus.
- Changing the program and the wiring at the same time. If it then works, you do not know which fixed it.
Self-check
- A known-good I2C scanner finds your sensor, but your own program reads zeros. Code or board?
- A NUCLEO-F103RB runs on its HSI at 64 MHz, so USART2 gets 32 MHz. Work out the real baud rate for 115200 and the worst error at 25 °C.
- A bus has 200 pF on it. Is a 4.7 kΩ pull-up fast enough for 400 kHz? Show the number.
- A datasheet gives the device's 7-bit address as 0x68. What address do you pass to
HAL_I2C_Mem_Read, and why? - An LED on an output pin never lights fully: at best it glows very dimly. Give a likely set-up fault in CubeMX on an STM32F103.
- A board resets every time its relay switches. Code or hardware, and what do you measure?
- What sample rate does a logic analyser need for 400 kHz I2C, at least?
Show answers
- Code. The scanner proves the power, wiring, pull-ups and sensor work, so the fault is in your program: the address, the register, or how it reads the bytes.
- USARTDIV = 32 MHz ÷ (16 × 115 200) = 17.36. In sixteenths the HAL uses 17 + 6/16 = 17.375, so the real rate is 32 MHz ÷ (16 × 17.375) = 115 108 baud: −0.08%. But the HSI can be up to 1.8% fast at 25 °C, so the worst total is about +1.7%: near the ±2% each end should stay within, and past it when the board is hot or cold. Use HSE for a UART that must always work.
- No. 0.8473 × 4.7 kΩ × 200 pF ≈ 0.80 µs, and fast mode allows 300 ns. You need about 1.5 kΩ (0.25 µs).
- 0xD0: the 7-bit address shifted left by one,
0x68 << 1. The comment above each HAL I2C function says the 7-bit address must be shifted left before the call; the HAL adds the read or write bit itself. Pass 0x68 and the HAL calls address 0x34, so nothing answers. - The pin was never set as an output: it is GPIO_Input with Pull-up in CubeMX. The pull-up, 30 to 50 kΩ, lets only a fraction of a milliamp through the LED. Writing the pin only switches between the pull-up and the pull-down. Set it to GPIO_Output in CubeMX and generate the code again.
- Hardware, most likely: the supply dips when the relay coil switches. Put a scope on the chip's supply pin, triggered on the relay's switching, and look for the dip below the brown-out level. Check the coil's flyback diode too.
- At least 4 MS/s, four times the clock rate. Ten times is better for timing.
Downloads
- i2c_scan_and_check.ino: the same known-good I2C test as a sketch for ARGYNIX Lab and a real Uno: a scan of every address, then the MPU-6050's WHO_AM_I register. Save it in a folder named
i2c_scan_and_check. - code-or-board-checklist.md: our split-test checklist, from power and clock to the logic analyser, with the record.
Practise in ARGYNIX Lab
Open the I2C scan and check in ARGYNIX Lab (opens in a new tab): an Uno with an MPU-6050 on A4 and A5, running the download. Run it. The Serial page shows the sensor found at 0x68 and its WHO_AM_I read correctly.
Now make the fault from the worked example. Stop, delete the wire from the sensor's SCL to A5, and run again: the scan finds nothing. Put it back, and it works. The Lab hands the bus's bytes straight to the sensor, so the Logic page shows no edges on A4 and A5: to watch SDA and SCL at work, put a logic analyser on a real bus, as the theory above shows.
Then make code faults: change MPU_ADDR to 0xD0, or WHO_AM_I to the wrong register, and read what the program reports. Type r in the serial monitor to run the checks again.
Run it in your browser, then do it again on your own board.
Open in ARGYNIX Lab (opens in a new tab)