Almost every “my code does not work” moment in embedded programming is one of a dozen recurring faults, and nearly all of them are found by checking the physical setup before reading the code again. What follows is an order of operations worth running every time, from the cheapest check to the most expensive.
Check the boring things, in order
Work down this list before you change a single line of code.
- Is the board powered, and is the power LED on?
- Is the cable a data cable, and is it in the same port as before?
- Is the correct board and port selected?
- Do all the grounds connect to each other?
- Does the pin in the code match the pin you wired?
- Is the component oriented the right way round?
This takes two minutes and resolves most reports. Skipping it costs an hour.
Faults that are actually electrical
A pin with nothing connected to it does not read zero; it floats and picks up noise, which is why an unconnected input can appear to work and then behave randomly when you move your hand. Use a pull-up or pull-down resistor, or the chip’s internal one.
A missing common ground between two powered parts is the second classic. The signal has no reference, so the receiving side sees nonsense. The third is power: a supply that sags when a motor starts produces resets that look exactly like a crash in software.
Faults that are actually timing
Long delays freeze the whole program, so buttons seem dead and serial output arrives in bursts. Anything that waits should be rewritten as a check against the last time something happened, so the loop keeps running.
Mismatched baud rates produce plausible-looking garbage rather than an obvious error, so check the number twice when the serial monitor shows strange characters. And if a value read from a sensor is stable but wrong, suspect the timing of the read rather than the sensor.
Faults in your assumptions
Read the actual error message, not the one you expect. Compile errors that name a library usually mean the library is missing rather than that the code is wrong. And when a program works on your desk and not on the bench, the difference is almost always a wire, a power source or a pin you moved and forgot.
Keep a notebook line for each fix. After ten entries you will notice that three faults account for most of your wasted evenings, and you will start checking them first.
Why does my board reset when a motor runs?
Current draw is pulling the supply down. Use a separate supply for the motor, tie the grounds together, and add a capacitor near the motor driver.
Why does the serial monitor show odd symbols?
The baud rate does not match between code and monitor. Set both to the same value and reopen the port.
Debugging embedded systems is less about cleverness than about ordering. Power, ground, connection, code, timing — checked in that order, most problems surrender in minutes rather than evenings.