# Toy PCB Brain Transplant Guide

## Overview

Instead of building a whole new board, desolder the original MCU from the
toy's PCB and replace it with a breakout connector wired to the XIAO 
nRF52840. The original PCB keeps doing everything it's good at (power,
buttons, motor, speaker routing) — you just swap the brain.

## What You Need

| Item | ~Cost | Notes |
|------|-------|-------|
| SOP-16 solder-down DIP adapter | $2 | Logical Systems PA-SOF-D420-16, or generic SOIC-16→DIP breakout |
| Seeed XIAO nRF52840 | $10 | The new brain |
| IS25LP128F (SPI flash) | $2 | Audio storage (solder to a small perfboard with the XIAO) |
| 16-pin ribbon cable or Dupont jumpers | $1 | Connect adapter to XIAO |
| Solder, flux, solder wick | — | For desoldering and rework |
| Multimeter | — | For reverse-engineering the pinout |

**Total added cost: ~$15** (and you reuse 90% of the original toy)


## Step 1: Document the Original Board

Before desoldering anything:

1. **Photograph both sides** of the PCB in good light
2. **Identify the MCU** — look for markings like TRSF1602A, chip-on-board
   (black blob), or other SOP-8/SOP-16 packages
3. **Note the pin 1 indicator** — dot, notch, or beveled corner
4. **Count pins** — this guide assumes SOP-16, but adapt for SOP-8
5. **Trace visible connections** by eye:
   - Which pins have fat traces? → probably power (VCC/GND)
   - Which pins connect to the speaker? → audio output
   - Which pins go to the motor area? → motor drive
   - Which pins go to buttons? → GPIO inputs


## Step 2: Desolder the Original MCU

**Hot air method (recommended):**
1. Apply flux to all pins
2. Set hot air to ~350°C, low airflow
3. Heat evenly around the chip until solder reflows
4. Lift chip with tweezers
5. Clean pads with wick and flux

**Soldering iron method:**
1. Apply flux generously
2. Add fresh leaded solder to all pins (lower melting point helps)
3. Drag iron back and forth across pins on each side
4. Use solder wick to remove excess
5. Gently pry chip up while heating one side, then the other
6. Clean pads with wick

**If it's a chip-on-board (black blob):**
Unfortunately, these can't be desoldered. You'll need to cut the traces
instead and jumper to the XIAO. Identify the traces leading to the blob
and cut them with an X-acto knife, then solder wires directly to the
button/speaker/motor pads on the board.


## Step 3: Solder the SOP-16 Breakout Adapter

1. Place the SOP-16 → DIP adapter on the now-empty pads
2. Align pin 1 with the original pin 1 marking
3. Tack-solder two corner pins
4. Check alignment under magnification
5. Solder remaining pins
6. Now you have 16 through-hole pins you can plug jumper wires into


## Step 4: Reverse-Engineer the Pinout

This is the detective work. You need to figure out what the original
MCU was connected to on each pin.

### 4a: Power pins (multimeter, board OFF)

Set multimeter to continuity/beep mode.

1. Touch one probe to the battery negative terminal (GND)
2. Touch the other probe to each of the 16 breakout pins
3. **Mark any pins that beep as GND**
4. Now touch one probe to battery positive (after the power switch)
5. Touch other probe to each remaining pin
6. **Mark any pins that beep as VCC**

Typical SOP-16 MCUs have 1-2 GND pins and 1-2 VCC pins.


### 4b: Speaker pins (visual trace + multimeter)

Follow the traces from the speaker solder pads back to the breakout.
Often the speaker connects through a small capacitor or resistor, then
to one or two MCU pins.

- **One speaker pin** = single-ended PWM audio (most common)
- **Two speaker pins** = bridge-tied-load (BTL) direct drive (no amp chip)


### 4c: Motor pin (visual trace)

Follow the motor wires. In cheap toys, the motor often connects to a
single transistor (TO-92 or SOT-23 package near the motor area). The
transistor's base connects (through a resistor) to an MCU pin.

If there's no transistor and the motor connects directly to an MCU pin
(rare but possible on very cheap toys), you'll need to add a MOSFET —
MCU pins can't drive motors directly.


### 4d: Button pins (multimeter, board OFF)

For each physical button on the toy:
1. Press the button while testing continuity between its pads
2. Trace one pad to GND (common) and the other to an MCU pin
3. Label that breakout pin as a button input

Most cheap toys wire buttons between the MCU pin and GND, relying on
the MCU's internal pull-up resistor — same as our design.


### 4e: Automated pin discovery (firmware)

Upload the pin_discovery sketch (included below) to the XIAO. Connect
each breakout pin one at a time to a XIAO GPIO and the sketch will
tell you what it sees:
- High/low/floating state
- Whether pressing a button changes it
- Whether it shows PWM activity when the toy was running
- Approximate voltage level


## Step 5: Create the Pin Map

Fill out a table like this for your specific toy:

| SOP-16 Pin | Connects To | XIAO Pin | Notes |
|------------|-------------|----------|-------|
| 1          | VCC         | 3V3      | Power supply |
| 2          | GND         | GND      | Ground |
| 3          | Button 1    | D0       | Play/Pause (active LOW) |
| 4          | Button 2    | D1       | Next track |
| 5          | Button 3    | D2       | Motor on/off |
| 6          | Button 4    | D3       | Volume/speed |
| 7          | Speaker +   | D10      | PWM audio output |
| 8          | Speaker -   | GND      | Or second PWM for BTL |
| 9          | Motor ctrl  | D6       | Through transistor on board |
| 10         | NC          | —        | Not connected |
| 11-16      | ...         | ...      | (varies by toy) |


## Step 6: Wire It Up

1. Solder header pins to the XIAO if not already done
2. Solder the IS25LP128F flash chip to a small piece of perfboard
   next to the XIAO (VCC, GND, and 4 SPI wires)
3. Run jumper wires from each SOP-16 breakout pin to the 
   corresponding XIAO pin per your pin map
4. Connect VCC breakout pin → XIAO 3V3 (to power the XIAO from toy batteries)
5. Connect GND breakout pin → XIAO GND

**IMPORTANT voltage check:** Before connecting power, measure the toy's 
VCC with batteries installed. If it's 2×AA (3V) or 3×AA (4.5V), you 
can connect directly to the XIAO's 3V3 pin (which accepts 1.7-3.6V on 
the nRF52840). If the toy uses more than 3 batteries (>5V), you MUST
use the XIAO's 5V pin or add a regulator.

For 2×AA: Connect through the TPS61220 boost to get stable 3.3V,
then feed that to XIAO 3V3. Or if you're fine with the limited voltage
range, connect the battery VCC directly to XIAO 3V3 — the nRF52840
runs down to 1.7V, but the IS25LP128F needs 2.3V minimum, so you'll
lose the bottom of the battery capacity.


## Step 7: Update the Firmware Pin Mapping

In baby_mobile_v2.ino, update the pin definitions to match your wiring:

```cpp
// Adjust these to match YOUR toy's pin map
#define PIN_AUDIO_PWM    D10   // connected to speaker trace
#define PIN_AMP_SD       -1    // set to -1 if toy has no separate amp
#define PIN_MOTOR_PWM    D6    // connected to motor transistor base
#define PIN_BTN1         D0    // play/pause button
#define PIN_BTN2         D1    // next
#define PIN_BTN3         D2    // motor toggle
#define PIN_BTN4         D3    // fourth button
// ... set unused buttons to -1
```

If the toy's original circuit drives the speaker directly from the MCU
(no amp chip), the XIAO's PWM output through the RC filter should work 
similarly. The original PWM was likely at a higher carrier frequency
(the TRSF1602A runs at up to 13MHz), but 8kHz audio content will
sound the same.


## Common Toy PCB Patterns

### Pattern A: Direct-drive speaker (most common in cheap toys)
```
MCU pin → small cap (100nF-1µF) → Speaker+ 
                                   Speaker- → GND
```
The MCU generates PWM, the cap blocks DC. No amplifier. Maximum volume
is limited by the MCU's output current (~20mA). This works fine with
the XIAO — just set the PWM pin and skip the PAM8302A entirely.

### Pattern B: Transistor amplifier
```
MCU pin → resistor (1K-10K) → transistor base
                                collector → Speaker+
                                emitter → GND
                                Speaker- → VCC
```
Again, the XIAO's PWM drives this directly. The transistor provides
current amplification.

### Pattern C: Dedicated amp IC (higher-end toys)
```
MCU pin → coupling cap → amp IC input
                          amp IC output → Speaker ±
```
Here you're feeding the amp's input from the XIAO PWM through the
existing coupling cap. Should work as-is. If there's a shutdown pin
on the amp, trace it back to the MCU and control it from the XIAO.

### Motor drive patterns:
```
Common:  MCU pin → resistor → NPN base → motor → VCC
                                emitter → GND
                                diode across motor

Simple:  MCU pin → motor → VCC  (tiny motors only, <20mA)
```


## Troubleshooting

**No sound:** Check speaker connections. Measure voltage on the audio pin
with a multimeter while playing — you should see ~1.5V average. If 0V,
wrong pin. If VCC, the pin is driving high but not PWMing.

**Distorted sound:** The RC filter values may need adjustment. Try
increasing R2 or C7 to lower the cutoff frequency. Also check that
analogWriteResolution(8) is set in setup().

**Motor doesn't spin:** The XIAO pin may not source enough current to
drive the transistor. Check if the base resistor is reasonable (1K-10K).
If the original design was a darlington or MOSFET, the XIAO should
drive it fine.

**Buttons don't work:** Verify the button wires go to GND when pressed.
Some toys use a resistor ladder (all buttons on one ADC pin with
different resistor values) — this requires reading analogRead() instead
of digitalRead(). The pin_discovery sketch will detect this.

**Board doesn't power up:** The original MCU may have been pulling
certain pins to specific states that other components depend on. Check
if any traces from the MCU footprint go to enable pins on regulators
or other ICs on the board.
