1526  Best Practices and Debugging

1526.1 Learning Objectives

By the end of this chapter, you will be able to:

  • Apply design principles for reliable hardware prototypes
  • Calculate power budgets and estimate battery life
  • Use systematic debugging strategies for hardware issues
  • Avoid common pitfalls in hardware prototyping
  • Transition from prototype to production-ready designs

1526.2 Design Principles

1526.2.1 Start Simple

Build complexity incrementally, validating each stage before adding more features.

1526.2.2 Modular Design

Separate functional blocks for independent testing and reuse.

Example: Separate boards for power, MCU, sensors, communications allow independent testing and upgrades.

1526.2.3 Document Everything

  • Schematics and PCB layouts
  • Bill of Materials (BOM)
  • Assembly notes and test procedures
  • Code comments and change logs

1526.2.4 Version Control

  • Git for code
  • Track PCB revisions (v1.0, v1.1, etc.)
  • Note changes between versions

1526.2.5 Design for Testability

  • Include test points for key signals
  • LED indicators for power and status
  • Debug headers (UART, JTAG)
  • Accessible pin headers

1526.2.6 Design for Manufacturing

  • Use standard component packages
  • Avoid tight tolerances
  • Minimize custom or rare parts
  • Consider assembly processes early

1526.3 Power Management

1526.3.1 Power Budget Calculation

Calculate total current draw:

Component Type Typical Current
MCU (active) 50-200 mA
MCU (sleep) 1-100 uA
Sensors 1-50 mA each
Communications (TX) 50-500 mA
Actuators 100 mA - several A

1526.3.2 Battery Life Estimation

Formula: Battery capacity (mAh) / Average current (mA) = Hours

Example: 2000 mAh battery / 20 mA average = 100 hours

1526.3.3 Knowledge Check

1526.3.4 Power Optimization Techniques

  • Deep sleep between measurements
  • Power down peripherals when not needed
  • Efficient code (avoid busy-wait loops)
  • Appropriate sensor sampling rates
  • Batch communications (transmit multiple readings together)

1526.4 Debugging Strategies

1526.4.1 Systematic Approach

  1. Visual inspection - solder joints, component orientation
  2. Power verification - voltages correct at all points?
  3. Communication testing - UART output, I2C scanning
  4. Subsystem isolation - test each part independently
  5. Signal probing - oscilloscope, logic analyzer

1526.4.2 Common Issues and Solutions

Symptom Likely Causes Solutions
No power Bad connections, fuses, regulators Check continuity, replace
Random resets Power supply sag, decoupling Add capacitors, check supply
I2C fails Wrong address, missing pull-ups Scan bus, add 4.7k pull-ups
Sensor returns 0 Wrong pins, no power to sensor Verify wiring, measure voltage
Overheating Short circuit, wrong component Check for shorts, verify parts

1526.4.3 Debugging Tools

Tool Use Case
Serial monitor Debug print statements
LED blink codes Status indication without serial
Multimeter Voltage, current, continuity
Logic analyzer I2C/SPI/UART protocol debugging
Oscilloscope Signal integrity, timing issues

1526.5 Common Pitfalls

1526.5.1 Power Issues

Problem: Device resets randomly or behaves erratically

Solutions: - Use adequate power supply (headroom above calculated requirements) - Add bulk capacitors (100-470uF) near high-current components - Place 0.1uF ceramic capacitors near each IC power pin - Use shorter, thicker power traces on PCB

1526.5.2 Logic Level Mismatches

Problem: Communication failures between 3.3V and 5V devices

Solution: Use bi-directional level shifters (never connect directly)

1526.5.3 Floating Inputs

Problem: Unpredictable readings from digital inputs

Solution: - Enable internal pull-up/pull-down resistors - Add external 10k resistors to VCC or GND

1526.5.4 Inadequate Current Capacity

Problem: Motors/relays donโ€™t work, or MCU resets when activated

Solution: - Use transistors or MOSFETs for switching - Separate power supply for high-current devices - Add flyback diodes across inductive loads

1526.5.5 Noise and Interference

Problem: Unreliable sensor readings, communication errors

Solutions: - Separate analog and digital grounds - Use shielded cables for sensitive signals - Keep wire runs short - Add filtering capacitors


1526.6 Worked Example: Battery-Powered Sensor

Scenario: Design an outdoor environmental sensor that measures temperature, humidity, and air quality every 15 minutes and transmits via LoRaWAN. Target: 2+ years on batteries.

Power Budget Analysis:

Phase Duration Current Energy (uAs)
Wake 5ms 3.5 mA 17.5
Sense (BME680) 300ms 12 mA 3,600
Sense (SHT31) 15ms 0.3 mA 4.5
TX (LoRa) 100ms 120 mA 12,000
Sleep 899.6s 1 uA 899.6
Total per cycle 900s - 16,522

Calculation: - Per cycle: 16,522 uAs = 4.59 uAh - Daily: 4.59 x 96 cycles = 440.6 uAh = 0.44 mAh/day - Battery (3,000 mAh): 3,000 / 0.44 = 6,818 days = 18.7 years theoretical - With 60% derating: 11.2 years practical

Key Insight: Sleep current dominates when transmission duty cycle is low. The air quality sensor (BME680) uses 75% of sensing energy - remove if not required to extend life further.


1526.7 Worked Example: Debugging I2C Failures

Scenario: BME280 sensor works in lab but fails outdoors after 2-48 hours.

Investigation:

  1. Temperature testing: Fails at -5C and +45C (but works at room temp)
  2. Logic analyzer: SDA rise time increases from 10us to 150us at cold
  3. Root cause: Pull-up resistors too weak for cable capacitance at temperature extremes

Fix: - Replace 10k pull-ups with 4.7k for faster rise times - Disable ESP32 internal pull-ups - Add I2C bus recovery code for hang conditions

Result: Zero failures over 168 hours of testing from -10C to +55C.


1526.8 Prototype-to-Production Checklist

Before moving to production:

WarningCritical: Certification Costs

Production IoT devices require regulatory certification: - FCC (US): $5-15K - CE (Europe): $3-10K - UL (Safety): $5-20K - Timeline: 8-12 weeks each

Budget and plan for these from the start!


1526.9 Whatโ€™s Next

Continue to Case Studies and Worked Examples for comprehensive real-world examples including a smart thermostat development journey and review quizzes.