Charging and discharging a capacitor automatically with MOSFET

I am working on charging and discharging a polarized capacitor (100 mF) automatically with a nMOSFET. Charging and discharging are done through constant current via a resistor of 10 kohm, and the voltage must be recorded.

The purpose is to calculate the energy as well as the power density of the cap. But while doing, there is neither discharging nor charging . Nothing happens as I measured with voltage data logger separately.
I am attaching the code and the circuit here. I request advice and suggestions from the community. Thank you.

// Pin assignments
const int capacitorPin = A0;   // Analog input pin for measuring capacitor voltage
const int mosfetPin = 2;       // Digital output pin for controlling MOSFET
const int chargingTime = 500;  // Charging time in milliseconds
const int dischargingTime = 500;  // Discharging time in milliseconds

void setup() {
  pinMode(mosfetPin, OUTPUT);  // Set MOSFET pin as output
  Serial.begin(9600);  // Initialize serial communication
}

void loop() {
  // Charge the capacitor
  digitalWrite(mosfetPin, HIGH);  // Turn on MOSFET to start charging
  delay(chargingTime);            // Wait for the capacitor to charge
  
  // Measure and print the voltage
  int voltage = analogRead(capacitorPin);  // Read voltage from the capacitor
  float voltageValue = voltage * (5.0 / 1023.0);  // Convert analog reading to voltage (assuming 5V reference)
  Serial.print("Charging Voltage: ");
  Serial.print(voltageValue, 2);  // Print voltage with 2 decimal places
  Serial.println(" V");
  
  // Discharge the capacitor
  digitalWrite(mosfetPin, LOW);  // Turn off MOSFET to start discharging
  delay(dischargingTime);        // Wait for the capacitor to discharge
  
  // Measure and print the voltage
  voltage = analogRead(capacitorPin);  // Read voltage from the capacitor
  voltageValue = voltage * (5.0 / 1023.0);  // Convert analog reading to voltage (assuming 5V reference)
  Serial.print("Discharging Voltage: ");
  Serial.print(voltageValue, 2);  // Print voltage with 2 decimal places
  Serial.println(" V");
  
  delay(1000);  // Delay between cycles
}
![circuit_MKS|659x500](upload://mQ6a8XAI295jDhveBzzlVNoe8cF.png)

The first suggestion is to draw a real schematic and post a scan or photo of it.

1 Like

It isn't being charged by a constant current if it is being charged via a resistor.

Your MOSFET discharges the capacitor rapidly - it isn't being discharged through the resistor.

  // Charge the capacitor
  digitalWrite(mosfetPin, HIGH);  // Turn on MOSFET to start charging

When the MOSFET is turned on the capacitor is discharged.

  // Discharge the capacitor
  digitalWrite(mosfetPin, LOW);  // Turn off MOSFET to start discharging

The capacitor is allowed to charge through the resistor when the MOSFET is turned off.

Thanks for your reply.
I swapped but the exhibited output is 5V on both charging as well as discharging.

c

What is the part number of the MOSFET?

5V is an OUTPUT NOT an INPUT.

Connecting a 9V battery to 5V probably damaged the UNO

You will need two MOSFETs, one for chatging and one for discharging.

1 Like

Thank you Mr. Jim-P
Yes I understand
You nailed it and pointed out the error.
Thank you so much.
Its irf530n.
And now I am started to modify the circuit.

As the charging / discharge current is less than 1mA do you even need a MOSFET?

A digital output can easily supply that current. High to charge, low to discharge.
RC

1 Like

Let me check manually for the amerage. This is proto. We are planning for the cap with few F values. That's why MOSFET is implemented.
Thanks

Not solved yet!
The IRF530N only has a guaranteed Rds(on) at 10V so it will not work with a 5V Arduino.
We need to find a different MOSFET.
Can you buy parts from Digi-Key or Mouser.

Sure sir. Based on our previous works, it charges a 3.3 V cap . But let me recheck after integrating one more MOSFET.

Did you understand what I said in post #12

Is your capacitor really 100mF (100millifarads) ?
or did you use the 'm' to denote microfarads, because you couldn't type 'µ' ?

If it really is 100mF, what voltage are you expecting to get after charging it for 500ms through a 10kΩ resistor?
(Hint: it won't be much after 0.0005 time constants.)

1 Like

Yeah... got it. Let me check in real-time.

Yep, parameters are correct and its 100 mF. Because the charging rate must be slow, may be in a few hours and 10k is just too high and it represents real-time load factors. Since the fabricated cap has Faradaic/pseudo cap characteristics, not EDLC type. So charge rate must be slow to reverse the reaction.

Is it working?

Then why thinking of using an external mosfet.
An Arduino output pin is just a switch with mosfets. Perfectly capable of switching that low current. You can change the current by choosing a different resistor value. 10k is 5volt/10k = 5mA.

More current?
I made once a 400F cap tester by connecting 12 Arduino output pins with 470 ohm resistors (10mA each) to the cap. Charging/discharging with that ~100mA was not a problem.
Leo..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.