Overview
NunchukInput enables reading analog stick and button data from a Nintendo Wii Nunchuk controller connected via I2C. This provides a low-cost analog input option with two buttons (C and Z) and a 2-axis analog stick.
The Nunchuk input source is platform-specific. Implementation differs between Raspberry Pi Pico and Arduino (AVR) platforms.
Class Declaration
Raspberry Pi Pico
Arduino (AVR)
Constructor
Raspberry Pi Pico
Reference to the I2C (Wire) interface to use for communication. Defaults to the primary
Wire instance.Optional GPIO pin for Nunchuk detection. When set to a valid pin number, the pin is read with pull-up enabled. If HIGH, Nunchuk initialization is skipped. Set to
-1 to disable detection.GPIO pin number for I2C SDA (data) line. Must be a valid I2C-capable pin on your board.
GPIO pin number for I2C SCL (clock) line. Must be a valid I2C-capable pin on your board.
Arduino (AVR)
Optional GPIO pin for Nunchuk detection. When set to a valid pin number, the pin is read with pull-up enabled. If HIGH, Nunchuk initialization is skipped. Set to
-1 to disable detection.On Arduino (AVR), I2C pins are fixed by hardware (typically A4/SDA and A5/SCL on Arduino Uno). The Wire library is automatically initialized with these pins.
Methods
ScanSpeed()
ReturnsInputScanSpeed::SLOW, indicating this input source should be polled less frequently than GPIO inputs.
Always returns
InputScanSpeed::SLOWNunchuk communication over I2C is slower than GPIO reads. The SLOW scan speed prevents unnecessary I2C traffic and reduces latency impact on other inputs.
UpdateInputs()
Reads the latest data from the Nunchuk and updates the input state.Reference to the
InputState structure to update with Nunchuk data.Input State Fields Updated
When a Nunchuk is connected and responding:Configuration Example
Raspberry Pi Pico Configuration
config.cpp
Arduino (AVR) Configuration
config.cpp
Combining with GPIO Buttons
config.cpp
Hardware Connection
Pinout
Nunchuk controllers use a 4-wire connection:| Wire Color | Signal | Description |
|---|---|---|
| White | SDA | I2C data line |
| Green | SCL | I2C clock line |
| Red | VCC | 3.3V power supply |
| Yellow | GND | Ground |
Wiring Diagram
Optional Detection Circuit
The detection pin allows the firmware to detect if a Nunchuk is physically connected:Nunchuk Data Mapping
Analog Stick Values
The Nunchuk analog stick provides 8-bit values:- X Axis:
-128(full left) to+127(full right) - Y Axis:
-128(full down) to+127(full up) - Center: Approximately
0for both axes (calibration may vary)
Button States
- C Button:
inputs.nunchuk_c- Larger button on the face - Z Button:
inputs.nunchuk_z- Trigger button on the back
true when pressed, false when released.
Initialization and Detection
The constructor automatically:- Checks the detection pin (if configured)
- Configures I2C pins (Pico only)
- Initializes the Nunchuk controller
- Verifies communication with the Nunchuk
Initialization Failure Handling
If Nunchuk initialization fails:- I2C communication is ended (to prevent pin interference)
- The internal Nunchuk object is deleted
- All subsequent
UpdateInputs()calls do nothing nunchuk_connectedremainsfalse
Failed initialization is normal when no Nunchuk is connected. The firmware continues operating normally with other input sources.
Platform-Specific Details
Raspberry Pi Pico (RP2040)
- Flexible I2C Pins: Can use any GPIO pins with I2C function
- Wire Library: Supports multiple I2C buses (
Wire,Wire1) - Recommended Pins:
- I2C0: GP4 (SDA), GP5 (SCL)
- I2C1: GP6 (SDA), GP7 (SCL)
Arduino (AVR)
- Fixed I2C Pins: Hardware-defined (typically A4=SDA, A5=SCL)
- Wire Library: Single I2C bus only
- Voltage: Ensure 3.3V power is available (may require level shifter on 5V Arduinos)
Performance
- Scan Speed:
SLOW- optimized for I2C communication timing - Update Rate: 100-200Hz typical (limited by I2C speed)
- Latency: 5-10ms for state changes
- I2C Clock: 100kHz (standard mode) or 400kHz (fast mode)
Nunchuk input is slower than GPIO buttons but provides analog input capabilities. Use in combination with fast GPIO inputs for optimal responsiveness.
Troubleshooting
Nunchuk Not Detected
Symptoms:nunchuk_connected always false
Solutions:
- Verify 3.3V power connection
- Check SDA/SCL pin numbers match physical wiring
- Test with a known-good Nunchuk controller
- Verify I2C pins aren’t used for other purposes
Erratic Readings
Symptoms: Unstable stick values or button states Solutions:- Add pull-up resistors (4.7kΩ) on SDA and SCL lines
- Shorten wire length between controller and microcontroller
- Check for electrical interference from nearby wires
Third-Party Controllers
Symptoms: Some clone controllers don’t work Solutions:- Authentic Nintendo Nunchuk controllers are most reliable
- Some clones use different I2C protocols (not supported)
- Test with authentic hardware if issues persist
See Also
- GamecubeControllerInput - Alternative analog input source
- GpioButtonInput - For digital button inputs
- Input Sources - Overview of all input source types
- Pinout Configuration - Configuring hardware pins
