Raspberry Pi GPIO Pins
Control Raspberry Pi GPIO pins for digital input/output and PWM operations.
Raspberry Pi GPIO Pins: Hardware Control Interface
The Raspberry Pi Pin feature provides direct control over GPIO (General Purpose Input/Output) pins on Raspberry Pi hardware, enabling physical interaction with sensors, relays, motors, and other electronic components.
Overview
GPIO Pins bridge the digital world of software automation with the physical world of hardware control. Each pin can be configured for digital input, digital output, or PWM (Pulse Width Modulation) output, providing flexible interfacing with a wide range of electronic components.
Key Features
- Multiple Pin Modes: Digital Input, Digital Output, PWM Output
- Hardware Integration: Direct pi4j driver integration for reliable control
- State Management: Track and control pin states (ON/OFF)
- Startup/Shutdown Configuration: Define pin behavior on system start and stop
- Visual Feedback: Color-coded pins for easy identification
- Remote Control: Manage pins from any client on the mesh network
Pin Operation Flow
graph TD
A[Configure Pin Node] --> B{Pin Mode?}
B -->|Digital Output| C[Set Pin State ON/OFF]
B -->|Digital Input| D[Read Pin State]
B -->|PWM Output| E[Set Duty Cycle %]
C --> F[Hardware Driver Update]
D --> F
E --> F
F --> G[Update Pin State]
G --> H[Trigger Child Executors]
Pin Modes
| Mode | Description | Use Cases |
|---|---|---|
| Digital Output | Set pin HIGH (3.3V) or LOW (0V) | Control relays, LEDs, transistors |
| Digital Input | Read pin HIGH or LOW state | Read switches, buttons, sensors |
| PWM Output | Variable duty cycle 0-100% | Motor speed control, LED dimming |
Pin State Management
Each pin maintains a digital state:
- ON (
DigitalState.ON): Pin is HIGH (3.3V for output, HIGH signal for input) - OFF (
DigitalState.OFF): Pin is LOW (0V for output, LOW signal for input)
Configuration Options
| Field | Description | Default |
|---|---|---|
pinNumber | GPIO pin number (BCM numbering) | 0 |
mode | Pin mode (INPUT, OUTPUT, PWM) | OUTPUT |
hardwareId | Unique hardware identifier | Empty |
state | Current pin state (ON/OFF) | OFF |
color | Visual color for identification | System default |
startupMode | State on server startup | Current |
shutdownMode | State on server shutdown | OFF |
GPIO Pin Numbering
Krill uses BCM (Broadcom) pin numbering, not physical pin numbers. Common BCM pins on Raspberry Pi:
| BCM # | Physical Pin | Common Use |
|---|---|---|
| GPIO 2 | Pin 3 | I2C SDA |
| GPIO 3 | Pin 5 | I2C SCL |
| GPIO 4 | Pin 7 | General purpose |
| GPIO 17 | Pin 11 | General purpose |
| GPIO 18 | Pin 12 | PWM capable |
| GPIO 27 | Pin 13 | General purpose |
| GPIO 22 | Pin 15 | General purpose |
Warning: Avoid using GPIO 2 and 3 if you’re using I2C devices. Always check your Raspberry Pi model’s pinout diagram.
Use Cases
Digital Output:
- Control relays for high-power devices (pumps, heaters, valves)
- Drive LEDs for status indicators
- Trigger transistor circuits
- Control solenoids and valves
Digital Input:
- Read button presses
- Monitor sensor states (motion detectors, limit switches)
- Detect water level sensors
- Read reed switches and magnetic sensors
PWM Output:
- Variable motor speed control
- LED brightness control
- Servo positioning
- Fan speed regulation
Example Workflows
Relay Control (Digital Output):
- Trigger: Button or Cron Timer
- Pin Node: GPIO 17 set to Digital Output
- Action: Set state to ON to activate relay
- Result: External device powered on
Sensor Monitoring (Digital Input):
- Pin Node: GPIO 22 set to Digital Input
- Trigger: Threshold on pin state change
- Executor: Log event to DataPoint
- Result: Automated sensor event logging
Motor Speed Control (PWM):
- Trigger: Calculation Executor
- Pin Node: GPIO 18 set to PWM mode
- Action: Set duty cycle 0-100%
- Result: Variable speed motor control
Safety Interlock:
- Input Pin: Emergency stop button (GPIO 27)
- Logic Gate: NOT gate (inverted logic)
- Output Pin: System enable (GPIO 17)
- Result: System disabled when e-stop pressed
Integration with Logic Gates
Pins work seamlessly with Logic Gates for complex conditional logic:
1
2
3
Temperature OK (Pin) ─┐
├─ AND Gate ─→ Enable Heater (Pin)
Pressure OK (Pin) ────┘
Hardware Driver
Under the hood, Krill uses the pi4j library for reliable hardware communication with the Raspberry Pi GPIO pins. The PiManagerContainer handles all low-level hardware interactions, ensuring thread-safe and robust operation.
Best Practices
- Power Considerations: Raspberry Pi GPIO pins provide 3.3V at limited current (16mA). Use transistors or relays for high-power loads.
- Pull-up/Pull-down: Configure appropriate pull resistors for input pins to prevent floating states.
- Startup/Shutdown: Always configure shutdown behavior to ensure safe states when server stops.
- Color Coding: Use pin colors to visually distinguish different functions in the UI.
Raspberry Pi GPIO Pins are the essential hardware interface that connects Krill automation to the physical world.