The relay clicks and the board reboots. The sensor that was reporting fine a minute ago now returns nothing, and the code has not changed at all. That sequence, repeated in kitchens and dorm rooms everywhere, is usually not a coding problem, and it is the first of the traps below.
Mistakes in the power and wiring
Most “flaky” projects are electrical, not logical. The board resets, the sensor drifts, the relay chatters, and the code looks innocent because it is.
- Running the load from the same weak supply as the logic. A relay, a motor, or a strip of LEDs pulls a burst of current the USB port cannot deliver, so the voltage dips and the microcontroller restarts. Power the load from its own supply, and share only the ground.
- Ignoring voltage levels and grounds. A 5V sensor connected to a 3.3V input may work for a day and then stop, and two boards that do not share a ground will never agree on what a signal means. Check the logic level and the ground before the fourth rewrite of the same code.
- Wiring by color instead of by diagram. Red and black get swapped, a jumper moves one hole over, and nothing survives a rebuild. Draw the pin map before you connect anything, and photograph the wiring once it works.
Mistakes in the code
The first working sketch is usually the one that stops working as soon as it has to do two things at once.
- Blocking the loop with long delays. A delay of several seconds makes the device deaf and motionless for those seconds. Learn the non-blocking pattern of comparing timestamps and doing work when the interval has passed; it is the single most useful habit in embedded code.
- Assuming the network is available. Wi-Fi drops when a microwave runs or a router reboots. If the device only works when the connection is perfect, it will fail in the exact moment you needed it, so add reconnect logic and a timeout on every request.
Mistakes in method and habits
The remaining traps are about how you work rather than what you build, and they cost the most time.
- Buying parts without a datasheet or a library. A sensor that has no documentation and no example code is a weekend gone. Before ordering anything unusual, confirm that someone has published a working example for your board.
- Testing only on the bench. A sensor that reads correctly on a desk can fail in a bathroom, a fridge, or a garage, because heat, moisture, and distance all change the reading. Test in the place the device will actually live.
One habit that prevents most of them
When something misbehaves, change exactly one variable and write down what you changed. Reflashing three edits at once feels faster and makes the result unreadable; you will not know which change fixed it, and you will meet the same bug again next month without the note that would have saved you.
None of these mistakes are fatal, and all of them are cheap to avoid once you know to look. Power the load from its own supply, keep the loop non-blocking, assume the network will fail, buy parts with examples, and test in the place where the device will sit.