This Exercise Set has been submitted for peer review, but it has not yet been accepted for publication in the PICUP collection.

Charging/Discharging RC circuits

Developed by Chris Orban

This exercise illustrates a charging and discharging capacitor. This process takes time and how much time it takes depends on the resistance and the capacitance. Normally it would require calculus to understand these circuits, but we can use some algebra and a computer program instead! This exercise will use a programming language called [p5.js]( that is very similar to C and C++ programming. (Note: If you are familiar with C or C++ the main difference you will see is that there is no main() function and instead the draw() function serves this role.) **Importantly, this exercise can be completed using any computer or chromebook without downloading any software!** This exercise is designed for an algebra-based physics class at the college or high school level. It may also be useful for calculus-based physics for non-majors (e.g. engineering & science majors). This exercise is part of a series of exercises developed by Prof. Chris Orban. There are pre-and-post assessment questions associated with this exercise (not available here) that are being used in an educational research study. If interested to collaborate on that study please e-mail Prof. Chris Orban ( The first paper from this study [is available at this link](, the second paper which discusses the electromagnetism exercises [is available at this link](
Subject Area Electricity & Magnetism
Levels High School and First Year
Learning Objectives
1. Students will use their knowledge of circuit equations to modify a code that describes a charging and discharging capacitor. In particular they will modify the code so that the circuit includes an extra resistor. 2. Students will change the resistance and capacitance in the code to see the effect on the time it takes to charge and discharge the capacitor 3. Students will think critically about the assumptions in the code and the difference between this "ideal" circuit and what would happen in real life 4. Students will appreciate that although mathematically it takes an infinite amount of time for a capacitor to fully discharge (Q = 0), in practice it always takes some finite amount of time, depending on the resistance and capacitance
Time to Complete 60 min
This programming lab is unlike the previous ones in that it involves circuits. Specifically it involves circuits with a battery, a capacitor and one or two resistors like the circuit you see below. ![](images/RCcircuits/RCdisconnected.png "") Step 1. Think about this circuit! What happens when the switch in the middle is connected to the battery? What happens when it is connected to the resistor instead? Step 2. Here is a demo of what happens when the switch is moved to the battery and back to the resistor. Try it out! Flip the switch to the battery by pressing "c" on the keyboard. Move the switch back to the resistor by pressing "d". Watch what happens to the charge on the capacitor and the current through the resistor. Feel free to flip the switch back and forth. Press spacebar to reset the plot in the bottom right. Step 3. In the demo, why does the charge on the capacitor instantly jump to its maximum value whenever you connect the battery? Would the charge on the capacitor change this quickly in real life? Answer these questions in 2-3 sentences in what you turn in. Step 4. Open up the code in an editor Click on this link to open up the code in an editor! Important! Create an account with the editor or sign in to an existing account. Then click "Duplicate" so you can have your own version of the code! Consider the following code from the demo:
  if ( battery_is_connected == true) {
    I = 0;
    Vcap = Vbatt;
    Q = C*Vcap;
  } else { // battery is not connected
    I = Vcap/R;
    Q += -I*dt;
    Vcap = Q/C;
Note that the current (I) in this case is the current through the resistor. The current through the resistor is zero when the switch is connected to the battery. But when the switch is moved to connect the capacitor and the resistor, there will be a current through the resistor. The only problem is that each unit of charge that leaves the capacitor to travel through the resistor will decrease the charge on the capacitor. This is why there is a minus sign in this line of code: Q += -I*dt; This makes the capacitor like a battery but with a steadily decreasing voltage. This is because the voltage across the capcitor is Q/C and if the capacitor loses charge, the voltage across it will decrease until Q is basically zero. Step 5. Let's make the circuit more realistic like the circuit drawn below: ![](images/RCcircuits/RCdisconnected_v2.png "") In the code change the name of all the R variables to R2. Then add a new variable named R1 to the beginning:
R1 = 3; //Ohms
Now change the code that loads the pictures of the circuit. This happens in the preload function. Just add _v2 to the filenames like this:
function preload() {
  imgDisconnected = loadImage("RCdisconnected_v2.png");
  imgConnected = loadImage("RCconnected_v2.png");
Run the code just to make sure that the new images show up. Step 6 Now we want to modify the if statement we talked about in Step 4. Change your if statement to look like the code below. Your job will be to fill in the ???
  if ( battery_is_connected == true) {
    I = ???; // current in R1, determined by Vcap, Vbatt and R1
    Q += I*dt; // charging up
    Vcap = Q/C;
  } else {  // battery is not connected
    I = Vcap/R2; // current in R2
    Q += -I*dt; // discharging
    Vcap = Q/C;
Figure out what should be in the ??? in the code above! If you do this correctly, your code should behave like this Comments: Notice that everything in the else statement is the same as before except that R has been renamed R2. Also keep in mind that this code will be run over and over! Quantities like Q and I will change with every iteration. The code for when the battery is connected has changed substantially. Whereas before, Vcap is just equal to the potential of the battery (Vbatt), now the code looks more like the code in the else statement except that there isn't a minus sign in this line of code: Q += I*dt;. The capacitor is charging up, instead of discharging. The code for when the battery is connected is supposed to go like this: (1) The current in R1 is determined from the potential of the battery and the potential across the capacitor. (2) The current places more charge on the capacitor. (3) The potential across the capacitor increases because of the increased charge. Repeat! Step 7. As you may know, the characteristic decay time of a discharging RC circuit is given by $\tau = RC$. A fimilar example is when you unplug a wireless router to reset it you are supposed to wait about 30 seconds before plugging it in again. You might think that as soon as you disconnect the power source the charge on the capacitors in the device would quickly dissipate, but what happens is a lot like the circuit shown here. Even though the battery isn't connected when the switch is thrown to the right, the charge doesn't dissipate immediately. Increase either or both the capacitance of the capacitor and the resistance of R2. Run the program to confirm that it takes longer for the charge do dissipate as expected. Step 8 Use print("Q = ",Q); and print("I = ",I); to check if your code is giving the correct values for the max charge on the capacitor when the switch is connected to the battery. It is always important to check the results of your computer simulations to see that it gives you the answer you expect! The code for print("Q = ",Q); and print("I = ",I); is already in the code but it is commented out. All you need to do is remove the // in order to get it to work. Write down your calculation for the max charge on the capacitor and write down what it was measured to be! Just for fun: Step 9 Wouldn't it be interesting to add an inductor to the circuit? [Play around with this RLC circuit]( Does the code make sense? How do the values of R, L and C affect the result?

How to get full credit for this programming lab!!!

1. Comment on the demo (Step 3) Why does the charge on the capacitor instantly jump to its max value whenever the battery is connected? Would this happen in real life? Why? 2. Modify your code as described in Step 5 Just follow the directions in Step 5. There is no guesswork here. 3. Correctly modify the if statement to calculate the current in R1 (Step 6) The comments may help you do this. Just think of it as a homework problem. What is the current in R1 as a function of Vbat, Vcap and R1? 4. Choose larger values for C and R2 and check that it takes longer for the capacitor to discharge (Step 7) Write down the values for C and R2 that you chose and say something like "Yes, it really did take longer" in what you turn in. You can also change R1 if you are curious. Does this affect the discharge time? 5. Quantitative values for Q and I (Step 8) Write down your calculation for the max charge on the capacitor and write down what it was measured to be.

Download Options

Credits and Licensing

The instructor materials are ©2017 Chris Orban.

The exercises are released under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 license

Creative Commons Attribution-NonCommercial-ShareAlike 4.0 license