+
Charging/Discharging RC circuits

Specialized Javascript material developed by Christopher Orban - Published June 6, 2017

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](http://p5js.org) 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 (orban@physics.osu.edu). The first paper from this study [is available at this link](https://arxiv.org/abs/1701.01867), the second paper which discusses the electromagnetism exercises [is available at this link](https://arxiv.org/abs/1707.00185)
Subject Area Electricity & Magnetism High School and First Year Javascript 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 60 min
  if ( battery_is_connected == true) {
I = 0;
Vcap = Vbatt;
Q = C*Vcap;
} else { // battery is not connected
I = Vcap/R1;
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 capacitor 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/RC/RCdisconnected_v2.png "") 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() {
}

Run the code just to make sure that the new images show up. You have probably noticed that R2 is set to zero near the beginning of the code:
R1 = 5; // Ohms
R2 = 0;

We need to set R2 to some value other than zero near the beginning of the code where the variables are initialized. A good value to use is R2 = 3.
R1 = 5; // Ohms
R2 = 3;

Go ahead and make this change before moving on to the next step. 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 R2, determined by Vcap, Vbatt and R2
Q += I*dt; // charging up
Vcap = Q/C;
} else {  // battery is not connected
I = Vcap/R1; // current in R1
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](http://www.physics.ohio-state.edu/~orban/stemcoding/RCcircuit/RCcircuit_v2/RCcircuit.html) Comments: 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 R2 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 familiar 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 R1. 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!](http://editor.p5js.org/ChrisOrban/sketches/Bk7DFkrMZ) Does the code make sense? How do the values of R, L and C affect the result? [Here is a link if you just want to run the code in the browser without seeing the code](http://www.physics.ohio-state.edu/~orban/stemcoding/RCcircuit/RLCcircuit/index.html) 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 R2 (Step 6) The comments may help you do this. Just think of it as a homework problem. What is the current in R2 as a function of Vbat, Vcap and R2? 4. Choose larger values for C and R1 and check that it takes longer for the capacitor to discharge (Step 7) Write down the values for C and R1 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 number for the max charge on the capacitor (Step 8) Write down your calculation for the max charge on the capacitor and write down what it was measured to be.