Impedance matching system using SPDT relay

Here is a code for automatic impedance matching with 2 sets of four relay. The relays are toggled and the power transferred is calculated when the distance sensor reads a change in the current distance. The comparison is made with the initial power at the initial distance. How do I get the maximum power at the combination of the relays with resonance capacitors connected to them? I appreciate your comments

long duration;
long distance;


const int distanceSensorTriggerPin = 13; // Example trigger pin
const int distanceSensorEchoPin = 12;   // Example echo pin

// Define analog input pins for voltage and current measurements
const int voltagePin = A0;   // Example voltage measurement pin
const int currentPin = A1;   // Example current measurement pin




// Variables for tracking maximum power and corresponding relay array configuration
float maxPower = 0.0;
int maxPowerRelayArray = 0;

#define RELAY1_PIN_1 2   // Relay 1, Bit 1 pin
#define RELAY1_PIN_2 3   // Relay 1, Bit 2 pin
#define RELAY1_PIN_3 4   // Relay 1, Bit 3 pin
#define RELAY1_PIN_4 5   // Relay 1, Bit 4 pin

#define RELAY2_PIN_1 6   // Relay 2, Bit 1 pin
#define RELAY2_PIN_2 7   // Relay 2, Bit 2 pin
#define RELAY2_PIN_3 8   // Relay 2, Bit 3 pin
#define RELAY2_PIN_4 9   // Relay 2, Bit 4 pin

#define NUM_BITS 4       // Number of bits in each relay array


// Initialize relay array pins as outputs
void initializeRelayArrays() {
  pinMode(RELAY1_PIN_1, OUTPUT);
  pinMode(RELAY1_PIN_2, OUTPUT);
  pinMode(RELAY1_PIN_3, OUTPUT);
  pinMode(RELAY1_PIN_4, OUTPUT);
  
  pinMode(RELAY2_PIN_1, OUTPUT);
  pinMode(RELAY2_PIN_2, OUTPUT);
  pinMode(RELAY2_PIN_3, OUTPUT);
  pinMode(RELAY2_PIN_4, OUTPUT);
}

// Calculate the received power for a given relay combination
float calculateReceivedPower(int relay1Value, int relay2Value) {
  
  return voltagePin*currentPin;
}

// Set the impedance using the relay arrays based on the distance
void setImpedance(float distance) {
  
  float maxPower = 0;
  int maxRelay1 = 0;
  int maxRelay2 = 0;

  // Loop through all possible relay combinations
  for (int i = 0; i < pow(2, NUM_BITS); i++) {
    int relay1Value = i % 16;  // Convert the loop variable to a 4-bit value
    int relay2Value = i / 16;  // Shift the loop variable to get the next 4-bit value

    // Set relay array bits based on the relay values
    digitalWrite(RELAY1_PIN_1, (relay1Value & 0b0001) ? HIGH : LOW);
    digitalWrite(RELAY1_PIN_2, (relay1Value & 0b0010) ? HIGH : LOW);
    digitalWrite(RELAY1_PIN_3, (relay1Value & 0b0100) ? HIGH : LOW);
    digitalWrite(RELAY1_PIN_4, (relay1Value & 0b1000) ? HIGH : LOW);

    digitalWrite(RELAY2_PIN_1, (relay2Value & 0b0001) ? HIGH : LOW);
    digitalWrite(RELAY2_PIN_2, (relay2Value & 0b0010) ? HIGH : LOW);
    digitalWrite(RELAY2_PIN_3, (relay2Value & 0b0100) ? HIGH : LOW);
    digitalWrite(RELAY2_PIN_4, (relay2Value & 0b1000) ? HIGH : LOW);

    // Calculate the received power for the current relay combination
    float receivedPower = calculateReceivedPower(relay1Value, relay2Value);

    // Update the maximum power and corresponding relay values
    if (receivedPower > maxPower) {
      maxPower = receivedPower;
      maxRelay1 = relay1Value;
      maxRelay2 = relay2Value;
    }
  }

  // Set the relay array bits to achieve the impedance for the given distance
  digitalWrite(RELAY1_PIN_1, (maxRelay1 & 0b0001) ? HIGH : LOW);
  digitalWrite(RELAY1_PIN_2, (maxRelay1 & 0b0010) ? HIGH : LOW);
  digitalWrite(RELAY1_PIN_3, (maxRelay1 & 0b0100) ? HIGH : LOW);
  digitalWrite(RELAY1_PIN_4, (maxRelay1 & 0b1000) ? HIGH : LOW);

  digitalWrite(RELAY2_PIN_1, (maxRelay2 & 0b0001) ? HIGH : LOW);
  digitalWrite(RELAY2_PIN_2, (maxRelay2 & 0b0010) ? HIGH : LOW);
  digitalWrite(RELAY2_PIN_3, (maxRelay2 & 0b0100) ? HIGH : LOW);
  digitalWrite(RELAY2_PIN_4, (maxRelay2 & 0b1000) ? HIGH : LOW);
}

void setup() {
  Serial.begin(9600);
  initializeRelayArrays();  // Initialize relay arrays
  //distanceSensor.begin();  // Initialize the distance sensor
  pinMode(distanceSensorTriggerPin, OUTPUT);
  pinMode(distanceSensorEchoPin, INPUT);
  
  // Start serial communication
  

}

void loop() {
 
  digitalWrite(distanceSensorTriggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(distanceSensorTriggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(distanceSensorTriggerPin, LOW);
  duration = pulseIn(distanceSensorEchoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  
  setImpedance(distance);  // Set the impedance based on the distance

  delay(1000);  // Delay between impedance adjustments
}

@sammmty

Your topic was moved to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

It will help you get the best out of the forum in the future.

Thank you

What are You trying to tell?
Can You use a camera looking over Your shoulder and send live?

Hi, @sammmty

What is the application.? RF or power supply?
Can you please post a schematic of your project?
A diagram of the mechanics of your project would also help?

What is your Ultrasonic sensor measuring.

Can you please tell us your electronics, programming, arduino, hardware experience?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Thanks Tom.
First, I am designing a wireless power transfer system. In order to manage the energy generated, I have integrated an energy management system using relays and ultrasonic distance sensor to read the distance of the receiver from the transmitter coil. The general system is my profile pic.

For the last question, I am learning to be perfect in Arduino programming and applications.

Thanks

There are 3 sets of relays: 2 x 4 relay array and 1x2 relay array. The 2 x 4 array are for impedance matching while the 1 x 2 array controls the toggling of the transmitter box as shown

Why would the RX to Tx coil change?

PLEASE post a schematic?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Are you sure that this is the only property that affects power transfer?
I'd implement a feedback like from an RFID reader and adjust the power accordingly.

Need to know the "weight" of each bit for a start. Are you switching caps in and out? Normally impedience matching is done with a motorized variable cap. The switching transients with relays is going to be murderous!
Typically you initially tune by hand and then flip to auto tune so you need some form of feedback for FWD & REV to compute VSWR to drive the variable cap.


You press the "learn" button after auto tune has taken over, to set your presets, so the next start doesn't need initial hand tune.

Not sure of the OPs power level, the image looks like 100W or so, and it is not uncommon for relays to be used for this sort of switching.
In fact if you listen to most Amateur Radio Auto Matching Units you can hear a cacophony of relay actuation, to switch capacitors and inductors.


Both for capacitance and inductance.

Back EMF on the coil is the only thing to worry about, tuning is done at low power to keep reflected power low if the initial match is too high, so relay contacts are not stressed when opening and closing with power applied.

Tom... VK3DMK.. :smiley: :+1: :coffee: :australia:

Perhaps so for some, but my old remote tuner for my ground mounted vertical is rated at 150 watts and tunes at FULL power.

Exactly , RATED...

But does the TX like a 10:1 SWR at Tx full power ON or does it have its own SWR circuit and power down to limit reflected power?

Tom... :smiley: :+1: :coffee: :australia:
PS. A state side Ham running less than 1KW, bit of an oxymoron? :grinning: :smiley: :grinning: :smiley:

Here are the diagrams. In wireless power transfer, the transmitter (Tx) and the Receiver(Rx) are separated by distance (d). In this design, I am designing a self tuning impedance matching system that seek to maximize the power transfer at distances before the maximum allowable distance.


Thankyou, it would have been better if you stated this first.

What frequency?
What power level?

A proper schematic would also be helpful.
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

How are you measuring Voltage and Current?

It looks like you are using an Arduino Mega.

Thanks... Tom.. :smiley: :+1: :coffee: :australia:

Here is the general diagram:
This is a prototype so the power is not relevant at the moment.
The two capacitor banks (Tx(IMN) and Rx(IMN)) are designed to do the automatic tuning for maximum power at every distance greater than SP(Switching Point). SP from the system is a point of intersection and transition where a Single-Coil transmitter (SC) and the Electromagnetic Halbach array (EHA) transmitter meets. So the system is an integration of SC and EHA depending on the distance between the receiver and transmitter as seen in the diagram. The two relay array is used to toggle between the two systems (SC & EHA) . If d<= SP, we have SC WPT and a single optimized capacitor does the transfer process. If d > SP, we invoke the tuning of TX(IMN) and Rx(IMN) resonance unto max power transfer at the receiver. During the tuning, each power obtained and the tuning values are stored as binary bits. They are then compared for the max power at Tx(IMN):Rx(IMN). This values are held for that current distance.

Here is the capacitor bank using relays
relayIm

What pin goes where on your arduino? I don't see that on your schematic.