const int LOAD_RESISTOR = 480; // Load resistor value in ohms
const int CURRENT_SENSE_RESISTOR = 100; // Current sense resistor value in ohms
const int MUX_CONTROL_PIN = 2; // Control pin for multiplexer
const int ANALOG_PIN = A1; // Analog pin for multiplexer output
const float REFERENCE_VOLTAGE = 5.0; // Reference voltage of the Arduino (5V)
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud
pinMode(MUX_CONTROL_PIN, OUTPUT); // Set multiplexer control pin as output
}
void loop() {
// Read voltage from the power supply (PSU)
float psuVoltage;
selectInput(0); // Select input channel 0 on the multiplexer
psuVoltage = analogRead(ANALOG_PIN) * (REFERENCE_VOLTAGE / 1023.0); // Convert analog value to voltage
// Read voltage across the load resistor
float loadVoltage;
selectInput(1); // Select input channel 1 on the multiplexer
loadVoltage = analogRead(ANALOG_PIN) * (REFERENCE_VOLTAGE / 1023.0); // Convert analog value to voltage
// Read voltage across the current sense resistor
float currentSenseVoltage;
selectInput(2); // Select input channel 2 on the multiplexer
currentSenseVoltage = analogRead(ANALOG_PIN) * (REFERENCE_VOLTAGE / 1023.0); // Convert analog value to voltage
// Calculate current flowing through the load resistor using Ohm's Law: I = V/R
float loadCurrent = loadVoltage / LOAD_RESISTOR;
// Calculate current flowing through the current sense resistor using Ohm's Law: I = V/R
float currentSenseCurrent = currentSenseVoltage / CURRENT_SENSE_RESISTOR;
// Calculate current divided between the load resistor and VCC: I = I_sense - I_load
float dividedCurrent = currentSenseCurrent - loadCurrent;
// Print measurements to Serial Monitor
Serial.print("PSU Voltage: ");
Serial.print(psuVoltage, 2); // Display PSU voltage with 2 decimal places
Serial.print(" V, Load Voltage: ");
Serial.print(loadVoltage, 2); // Display load voltage with 2 decimal places
Serial.print(" V, Load Current: ");
Serial.print(loadCurrent, 2); // Display load current with 2 decimal places
Serial.print(" A, Divided Current: ");
Serial.print(dividedCurrent, 2); // Display divided current with 2 decimal places
Serial.println(" A"); // Add a newline character
delay(1000); // Adjust delay as needed
}
void selectInput(int channel) {
digitalWrite(MUX_CONTROL_PIN, LOW); // Disable multiplexer
delayMicroseconds(5); // Wait for multiplexer to settle
for (int i = 0; i < 3; i++) {
digitalWrite(MUX_CONTROL_PIN, (channel >> i) & 1); // Set multiplexer control bits
}
digitalWrite(MUX_CONTROL_PIN, HIGH); // Enable multiplexer
}