Post

Raspberry Pi GPIO Pins

Control Raspberry Pi GPIO pins for digital input/output and PWM operations.

Raspberry Pi GPIO Pins

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

ModeDescriptionUse Cases
Digital OutputSet pin HIGH (3.3V) or LOW (0V)Control relays, LEDs, transistors
Digital InputRead pin HIGH or LOW stateRead switches, buttons, sensors
PWM OutputVariable 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

FieldDescriptionDefault
pinNumberGPIO pin number (BCM numbering)0
modePin mode (INPUT, OUTPUT, PWM)OUTPUT
hardwareIdUnique hardware identifierEmpty
stateCurrent pin state (ON/OFF)OFF
colorVisual color for identificationSystem default
startupModeState on server startupCurrent
shutdownModeState on server shutdownOFF

GPIO Pin Numbering

Krill uses BCM (Broadcom) pin numbering, not physical pin numbers. Common BCM pins on Raspberry Pi:

BCM #Physical PinCommon Use
GPIO 2Pin 3I2C SDA
GPIO 3Pin 5I2C SCL
GPIO 4Pin 7General purpose
GPIO 17Pin 11General purpose
GPIO 18Pin 12PWM capable
GPIO 27Pin 13General purpose
GPIO 22Pin 15General 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):

  1. Trigger: Button or Cron Timer
  2. Pin Node: GPIO 17 set to Digital Output
  3. Action: Set state to ON to activate relay
  4. Result: External device powered on

Sensor Monitoring (Digital Input):

  1. Pin Node: GPIO 22 set to Digital Input
  2. Trigger: Threshold on pin state change
  3. Executor: Log event to DataPoint
  4. Result: Automated sensor event logging

Motor Speed Control (PWM):

  1. Trigger: Calculation Executor
  2. Pin Node: GPIO 18 set to PWM mode
  3. Action: Set duty cycle 0-100%
  4. Result: Variable speed motor control

Safety Interlock:

  1. Input Pin: Emergency stop button (GPIO 27)
  2. Logic Gate: NOT gate (inverted logic)
  3. Output Pin: System enable (GPIO 17)
  4. 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.

This post is licensed under CC BY 4.0 by the author.