# Project 5: Physical Relay Control

## Overview

This project demonstrates PLC-controlled physical hardware using an industrial Start/Stop/Seal-in ladder logic pattern. A relay module is driven through a transistor interface, with the seal-in circuit (latching logic) maintaining relay state using momentary push buttons. This forms the foundation of industrial motor control systems used throughout manufacturing and building automation.

## Implementation

![Ladder Logic Diagram](logic.png)

The ladder logic implements an industrial-standard Start/Stop circuit with seal-in contact. The Stop button (normally closed contact) has priority and can interrupt the circuit at any time. The Start button initiates relay energization, and the Relay_Coil self-contact maintains the energized state after the Start button is released.

## Demo
![Relay Control Demo](demo.gif)
Demonstration: START energizes relay (Green LED), STOP de-energizes relay (Red LED)

## Hardware

### Components

* STM32 Nucleo-F411RE development board
* HK19F DPDT relay (5V coil, 125Ω, 40mA)
* 2N2222 NPN transistor (relay coil driver)
* 2× momentary pushbuttons (Start and Stop)
* 2× LEDs (Red for NC contact, Green for NO contact)
* 5× resistors (1kΩ base, 2× 10kΩ pull-downs, 2× 330Ω current-limiting)
* Breadboard and jumper wires

### I/O Mapping

| Signal       | OpenPLC Address | MCU Pin | Connector   |
| ------------ | --------------- | ------- | ----------- |
| Start_Btn    | `%IX0.0`        | PA8     | Arduino D7  |
| Stop_Btn     | `%IX0.1`        | PB6     | Arduino D10 |
| Relay_Coil   | `%QX0.0`        | PB10    | Arduino D6  |

### Wiring Configuration

**Power Distribution:**
* Left breadboard rail: 5V (relay coil, LED commons)
* Right breadboard rail: 3.3V (button inputs)
* Both GND rails connected

**Relay Driver Circuit:**
* Nucleo 5V → Relay Coil+ (Pin 1)
* Relay Coil- (Pin 5) → 2N2222 Collector
* 2N2222 Emitter → GND
* 2N2222 Base → 1kΩ Resistor → PB10

**Button Inputs (Pull-down configuration):**
* Start: 3.3V → Button → PA8 → 10kΩ → GND
* Stop: 3.3V → Button → PB6 → 10kΩ → GND

**Relay Contact Indicators:**
* Pole 1 COM (Pin 2) → 5V
* Pole 1 NC (Pin 3) → Red LED Anode → 330Ω → GND (indicates relay OFF)
* Pole 1 NO (Pin 4) → Green LED Anode → 330Ω → GND (indicates relay ON)
* Pole 2 contacts unused (reserved for hardware latching enhancement)

### Logic Behavior

* **Initial State:** Relay de-energized, Red LED illuminated (NC contact closed)
* **Start Pressed:** Relay energizes with audible click, Green LED illuminates, Red LED extinguishes
* **Start Released:** Relay remains energized via seal-in contact (self-holding circuit)
* **Stop Pressed:** NC contact breaks circuit, relay de-energizes, Red LED illuminates, Green LED extinguishes
* **Safety Feature:** Stop button has priority - interrupts circuit even if Start is held

### Relay Specifications

**HK19F DPDT Configuration:**
* Pins 1-4: Coil+, Pole 1 COM, Pole 1 NC, Pole 1 NO
* Pins 5-8: Coil-, Pole 2 COM, Pole 2 NC, Pole 2 NO
* Coil Resistance: 125Ω (measured)
* Coil Current: 40mA at 5V
* Contact Rating: 10A at 250VAC

## Lessons Learned

* Transistor driver necessary for relay control - GPIO pins limited to ~25mA, relay coil draws 40mA
* 2N2222 pinout critical: E-B-C configuration (flat side facing, left to right)
* Pull-down resistors prevent floating inputs on momentary buttons
* Relay provides audible and visual feedback - click confirms physical switching
* OpenPLC pin mapping determined by list position in hardware configuration
* Blackpill STM32-F411CE board profile compatible with Nucleo F411RE
* NC contact in series provides Stop priority - essential safety pattern in industrial control

## Industrial Context

The Start/Stop/Seal-in circuit is fundamental in industrial automation with applications including:

* **Motor Control** - Starting and stopping motors with maintained run state
* **Pump Stations** - Controlling pumps in water treatment and HVAC systems  
* **Conveyor Systems** - Operator-controlled material handling equipment
* **Machine Tools** - Spindle and feed motor control with emergency stop capability
* **Building Automation** - Fan control, damper actuation, and valve positioning
* **Safety Systems** - Two-hand control stations and light curtain interfaces

The seal-in (self-holding) circuit eliminates the need for maintained contact switches, allowing the use of momentary pushbuttons which are more reliable and safer in industrial environments. The Stop button's NC contact ensures fail-safe operation - if the button or wiring fails, the circuit cannot be energized.

## Alignment with Circuits II

This project directly prepares for **Lab 14: Magnetic Devices** which covers:
* Relay coil operation and electromagnetic principles
* NC vs NO contact behavior and applications
* Latching/seal-in circuits (Figure 14-3 in lab manual)
* Pull-in and drop-out voltage measurements
* Contact resistance and current handling

## Repository Contents

* `SimpleRelay.xml` - OpenPLC ladder logic program
* `README.md` - Project documentation
* `demo.gif` - Hardware demonstration recording
* `logic.png` - Ladder logic screenshot
* `p5_wired.jpg` - Complete breadboard wiring photograph

## Future Enhancements

**Hardware Latching (Version B):** Wire Pole 2 NO contact in parallel with Start button to create physical self-latching independent of software logic. This demonstrates redundant safety systems used in critical industrial applications.

## Author

John West  
Electrical and Computer Engineering Technology Student

Date  
January 2026
