How to compute capacitor charge in Python using UliEngineering
You can easily compute the charge stored in a capacitor using the UliEngineering Python library:
from UliEngineering.Electronics.Capacitors import capacitor_charge
from UliEngineering.EngineerIO import *
# Compute charge for 100µF at 5V
charge = capacitor_charge("100uF", "5V")
print(f"Charge (100µF, 5V): {format_value(charge, 'C')}")
# Compute charge for 1nF at 12V
charge = capacitor_charge("1nF", "12V")
print(f"Charge (1nF, 12V): {format_value(charge, 'C')}")Example output
Charge (100µF, 5V): 500 µC
Charge (1nF, 12V): 12.0 nCThe capacitor charge represents the amount of electric charge stored in the capacitor’s electric field when a voltage is applied across its terminals. This charge is directly proportional to both the capacitance and the applied voltage. Understanding capacitor charge is essential for energy storage calculations, timing circuits, and understanding how capacitors function in electronic circuits.
The charge is computed using the formula: $Q = C \times V$, where $Q$ is the charge in coulombs, $C$ is the capacitance in farads, and $V$ is the voltage in volts. One coulomb is equivalent to one ampere-second, representing the charge transported by a constant current of one ampere in one second.
The plot above shows capacitor charge versus voltage for different capacitance values. Notice the linear relationship: charge increases proportionally with voltage for a given capacitance. Higher capacitance values store more charge at the same voltage, which is why larger capacitors are used when more charge storage is needed for applications like power supply filtering or energy storage.
Related posts
- How to compute capacitor charging energy in Python using UliEngineering
- How to compute capacitor voltage by energy in Python using UliEngineering
- How to compute capacitor capacitance by energy in Python using UliEngineering
Plot generation script
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
import sys
sys.path.insert(0, '/home/uli/dev/UliEngineering')
from UliEngineering.Electronics.Capacitors import capacitor_charge
# Voltage range for plotting
V = np.linspace(0, 25, 100) # 0 to 25V
# Create plot
plt.figure(figsize=(10, 6))
# Calculate charge for different capacitances
capacitances = [
(1e-6, '1 µF', 'blue'),
(10e-6, '10 µF', 'green'),
(100e-6, '100 µF', 'red'),
(1e-3, '1 mF', 'purple'),
]
for C, label, color in capacitances:
Q = C * V
plt.plot(V, Q * 1e6, label=label, color=color, linewidth=2)
plt.xlabel('Voltage (V)', fontsize=12)
plt.ylabel('Charge (µC)', fontsize=12)
plt.title('Capacitor Charge vs Voltage', fontsize=14, fontweight='bold')
plt.legend(loc='upper left', fontsize=10)
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig('capacitor_charge_plot.svg', format='svg', dpi=300)