I am excited to unveil a newly developed multi-functional power box, designed speficially for engineering-related projects. This versatile device is equipped with several key features to streamline your work.
Key Features
Dual Relay System:
Outlet 1: Connected to a solid-state relay, perfect for use in PID control systems.
Outlet 2: Equipped with a mechanical relay, ideal for simple on/off power operations.
Integrated AC Power Meter:
Provides instant and accurate measurements of current and voltage, with a precision of ±1 W.
Expandable with Arduino Mega:
The embedded Arduino Mega board allows for easy expansion. You can add modules for monitoring temperature, pressure, and more, making this power box highly adaptable for future developments.
For a closer look, check out the images of the power box below:
Arduino Code
The board comes pre-loaded with essential Arduino code, ensuring the power box is ready to use straight out of the box.
/* Title: Dual Relay Controller Made by: YX Location: Melbourne Date: 20241010 Description: This program is designed to control two relays, one mechanical and one SSR (Solid State Relay), allowing for triggering via serial commands. Expansion: The function can be extended to measure AC power, voltage, current when using PZEM004T. */ #include<PWM.h>//download https://code.google.com/archive/p/arduino-pwm-frequency-library/downloads // edit ATimerDefs.h and add '(0,0,0,0,0), // TIMER1C' after Line 107 // if applied to Arduino Mega 2560 #include<PZEM004Tv30.h>// Power meter library #include<math.h> /* This code runs on mega and uno. */ PZEM004Tv30 pzemOT1(68, 69); // power meter
// initializ the SSRs //use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz // ledTimer0 Pins 13, 4 might influence the internal clock int ledTimer1 = 5; // Pins 11, 12 16-bits timer int32_t frequency = 1; //PWM frequency (in Hz)
int mark = 0; // String comdata = ""; // initialize input string int numdata[2] = { 0 }; float DutyCircle; const byte numChars = 32; char comdata[numChars];
boolean newData = false;
voidsetup(){ //initialize all timers except for 0, to save time keeping functions InitTimersSafe(); pinMode(relayPin, OUTPUT); digitalWrite(relayPin, LOW); //sets the frequency for the specified pin bool success1 = SetPinFrequencySafe(ledTimer1, frequency); Serial.begin(115200); // set up communication digitalWrite(ledTimer1, LOW); Serial.println("Power box");
}
voidloop(){ int j = 0; while (!Serial) { ; // wait for serial port to connect. Needed for native USB } recvWithStartEndMarkers(); if (newData == true) { if (comdata[0] == 'P' && comdata[1] == 'W') { Serial.println("power reading"); powerx = pzemOT1.power(); // voltage1 = pzemOT1.voltage(); // current1 = pzemOT1.current(); // power1 = voltage1 * current1; power1 = pzemOT1.power(); // factor1 = pzemOT1.powerfactor(); if (!isnan(power1)) { // Serial.print("1: "); Serial.print(power1); Serial.println(); } else {
if (comdata[0] == 'S' && comdata[1] == 'O' && comdata[2] == '1') { delay(2); mark = 1; char* commaPtr = strchr(comdata,','); // Check if the comma exists if (commaPtr != NULL) { float value = atof(commaPtr + 1); // Convert string after the comma // Now you can use 'value' as a float if (value < 256){ pwmWrite(ledTimer1, value); // change the duty circle float DutyCycle = float(value) / 255 * 100; Serial.print("SSR Relay Activate and The Duty Cycle is "); Serial.println(DutyCycle); } else { Serial.println("Exceeds 255; ignoring."); } } else { Serial.println("SSR Relay Deactivate"); } }
functions = serialRelayControl(action, varargin) % serialRelayControl Handle serial port actions % Created by YX on 2024.10.12 % % Copyright (c) 2024 YX % All rights reserved. % Commerical use is prohibited without explicit permission % Examples: % Create: s = serialRelayControl('create', 'COM7'); % Write: serialRelayControl('write', s, 'yourCommand'); % Remove: s = serialRelayControl('remove', s); action = lower(action); switch action case'create' port = varargin{1}; s = createPort(port); readFromPort(s); case'write' s = varargin{1}; command = varargin{2}; writeToPort(s, command); readFromPort(s); case'remove' s = varargin{1}; removePort(s); otherwise error('Unknown action.'); end end
functions = createPort(port) try s = serialport(port, 115200); s.Timeout = 5; catch ME warning('Failed to create serial port: %s', ME.message); s = []; end end
functionwriteToPort(s, command) if ~isempty(s) && isvalid(s) try pause(.1); writeline(s, command); pause(.1); catch ME warning('Failed to write to serial port: %s', ME.message); end else disp('No active serial port connection.'); end end
functionreadFromPort(s) if ~isempty(s) && isvalid(s) try pause(.1); if s.NumBytesAvailable > 0 output = readline(s); pause(.1); disp(['Received: ', output]); else disp('No data available.'); end catch warning('Error reading from serial port.'); end else disp('No active serial port connection.'); end end
functionremovePort(s) if ~isempty(s) && isvalid(s) delete(s); disp('Serial port connection removed.'); else disp('No active serial port connection to remove.'); end end