--- title: 3-Position Analog Switch description: A component for handling 3-position analog switches with UP/MIDDLE/DOWN positions keywords: [analog, switch, 3-position, input, modbus, ESP32] --- # 3-Position Analog Switch **Path**: [src/components/3PosAnalog.h](../src/components/3PosAnalog.h) **Revision History**: Initial documentation The 3-Position Analog Switch (`Pos3Analog`) component provides an interface to hardware switches with three positions (UP, MIDDLE, DOWN). It reads from two analog input pins and determines the current position. The component supports both local (physical) and remote (Modbus) control modes, making it suitable for industrial automation applications that require flexible control mechanisms. ## Requirements ### Hardware - ESP32 microcontroller - Two analog input pins for position detection (upPin and downPin) ### Software - Arduino framework - ArduinoLog library - Modbus TCP support ## Features - Three-position state detection (UP, MIDDLE, DOWN) - Dual control modes: - LOCAL: Position determined by physical input readings - REMOTE: Position controlled via Modbus - Modbus integration with read/write capabilities - Configurable sampling interval - State change notifications - Comprehensive error handling and validation ## Dependencies - [Component](../src/Component.h): Base component class providing core functionality - [ModbusTCP](../src/modbus/ModbusTCP.h): Modbus TCP protocol implementation - [Bridge](../src/Bridge.h): Serial command bridge for component communication - [App](../src/App.h): Application framework integration ```mermaid graph TD Pos3Analog --> Component Pos3Analog --> ModbusTCP Pos3Analog --> Bridge Component --> App ``` ## Behaviour The component operates in one of two modes (LOCAL or REMOTE) and has three possible positions (UP, MIDDLE, DOWN). ```mermaid stateDiagram-v2 [*] --> LOCAL LOCAL --> REMOTE: Modbus Write REMOTE --> LOCAL: Modbus Write state LOCAL { [*] --> MIDDLE MIDDLE --> UP: upPin Active MIDDLE --> DOWN: downPin Active UP --> MIDDLE: No pins Active DOWN --> MIDDLE: No pins Active } state REMOTE { [*] --> REMOTE_MIDDLE REMOTE_MIDDLE --> REMOTE_UP: Modbus Write REMOTE_MIDDLE --> REMOTE_DOWN: Modbus Write REMOTE_UP --> REMOTE_MIDDLE: Modbus Write REMOTE_DOWN --> REMOTE_MIDDLE: Modbus Write } ``` ## TODOs ### Performance - Consider implementing debounce functionality for more stable readings - Optimize analog reading frequency based on application requirements ### Security - Implement authentication for remote control mode changes - Add permission levels for Modbus write operations ### Compliance - Ensure compliance with industrial control systems standards - Verify compatibility with Modbus specification ### Recommendations - Use pull-up or pull-down resistors on the analog inputs for more reliable readings - Implement filtering for noisy environments - Consider adding hysteresis to prevent rapid state changes near thresholds ## Example Below is an example of how to initialize and use the 3-Position Analog Switch component: ```cpp #ifdef AUX_ANALOG_3POS_SWITCH_0 analog3PosSwitch_0 = new Pos3Analog( this, // owner AUX_ANALOG_3POS_SWITCH_0, // upPin AUX_ANALOG_3POS_SWITCH_0 + 1, // downPin (assuming sequential pins) COMPONENT_KEY_ANALOG_3POS_SWITCH_0, // id MB_ANALOG_3POS_SWITCH_0_ADDR // modbusAddress ); if (analog3PosSwitch_0) { components.push_back(analog3PosSwitch_0); Log.infoln(F("3-Position Analog Switch 0 initialized. UpPin:%d, DownPin:%d, ID:%d, MB:%d"), AUX_ANALOG_3POS_SWITCH_0, AUX_ANALOG_3POS_SWITCH_0 + 1, COMPONENT_KEY_ANALOG_3POS_SWITCH_0, MB_ANALOG_3POS_SWITCH_0_ADDR); } else { Log.errorln(F("3-Position Analog Switch 0 initialization failed.")); } #endif ``` ### References - [Arduino Analog Input Documentation](https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/) - [Modbus Protocol Specification](https://modbus.org/specs.php)