"The simplest RC circuit is a capacitor and a resistor in series. When a circuit consists of only a charged capacitor and a resistor, the capacitor will discharge its stored energy through the resistor. The voltage across the capacitor, which is time dependent, can be found by using Kirchoff's current law, where the current charging the capacitor must equal the current through the resistor."
What does all that mean to us? The key in the above is that the voltage across the capacitor is time dependent. That means we can measure it. There is a threshold in digital circuits where a signal is recognized as a High (logic 1) or a Low (logic 0). We often dismiss this by saying "A high is 3.3v or 5v and a Low is 0v." This is not necessarily true, what about the voltages in between. Sure, 0 volts is a LOW, but what about 0.5 volts? It's not a HIGH. So until it reaches a certain voltage logically we will treat it as a logic 0, once it exceeds the threshold, the device will register the level as a HIGH and we will treat it as a logic 1. This threshold on the Raspberry Pi seems to be around 1.4 volts. Back to the fact that we can measure this time, since the current charging the capacitor must equal the current through the resistor, and our resistor value is changing due to external stimulus, the time to charge and discharge the capacitor will also change, and that is what we will measure.
In this example we will use a photocell and a capacitor together in series. A photocell is simply a device that changes resistance based on ambient light. The brighter the light, the lower the resistance, the dimmer the light, the higher the resistance. Very simple.
The way we will take advantage of all of the above information is:
1) We will start by driving our sensing pin on the B+ to low, this will give us a known starting point of 0 volts
2) We will change the sensing pin to an input
3) We will count in a loop until the capacitor charges to a level recognized as a logic level 1 on the input pin (approximately 1.4 volts)
4) We will print the value and do it again
What we expect to see is that as the amount of light is decreased, the time it take to charge the capacitor will be longer, so the counted output from our loop will be higher, if we increase the brightness of the light, the resistance will decrease, causing the capacitor to charge more quickly, giving us a lower number from our count. This is sensitive enough to be done by simply waving your hand over the photocell, or you can place a finger over the sensor to simulate total darkness. Bear in mind, this is not an exact measurement, but it is good enough to tell if there is a light on in a room, or if the sun is up, but that is the extent of it. If we were to use an analog temperature sensor instead of a photocell we would probably need to calibrate the outputs to known good values.
没有评论:
发表评论