charging current ic is a function of the capacitance and
the small internal resistances of the voltage source and
capacitor; C quickly charges to its final value of V0.
Once the capacitor is charged, ic drops to zero
(ignoring any leakage current), and the circuit is ready
If we keep the charge constant (same charging
voltage V0 on the same capacitor C), the voltage across
the capacitor upon discharging will decay solely as a
function of the resistance R. Figure 5 shows the
Once the capacitor has been fully charged to V0,
we can flip the switch down to begin the discharge
cycle, and watch voltage Vt decay as C discharges
through R. This voltage decay is precisely predictable,
and is described by the equation Vt = V0 e-t/RC, where V0
and Vt are in volts, t is in seconds, R is in ohms, and C
is in farads.
This means that if we know the initial voltage V0
and the capacitance C, and if we can measure the time
t for V0 to decay to a reference voltage Vt, we can
solve for the resistance R. So, how can we do that with
the Amigo and Color BASIC?
There are a few pieces to this puzzle. First, how do
we charge the capacitor with the Amigo and Color BASIC,
and what will be the initial voltage on the cap? This first
puzzle piece is actually pretty easy to figure out.
Recall that the OUTA[x] command converts a logic
level in Color BASIC to a voltage level on a Propeller I/O
pin. This means that we can use a Prop I/O pin to replace
the switch and battery in Figure 4, charging the capacitor
to a V0 of + 3. 3 VDC using the command OUTA[x]=1.
Next puzzle piece: How do we know when the
voltage across the discharging capacitor in Figure 5 equals
some constant reference voltage? Again, Color BASIC
makes this pretty easy, if we are willing to accept a little
imprecision in our measurements.
We know from Figure 1 that INA[x] returns
a logic 1 for voltages of 1.6 VDC or greater, and
will begin to return a logic 0 at some
indeterminate (but reasonably consistent)
voltage between 1.3 and 1.6 VDC. This voltage
can be our reference voltage.
So, we can charge the capacitor with
OUTA[x]=1; start our software timer; tell Color
BASIC to keep checking INA[x] until the first
instance of INA[x]=0; then stop our timer to
measure the discharge time.
This brings us to the final puzzle piece:
How do we measure the time between starting
the discharge cycle and reaching the reference
voltage? This is a bit trickier.
The capacitance value used in a “typical”
microcontroller RC decay circuit is on the order
of 0.01 uFd. If we try to write a counter/timer
using Color BASIC commands on a “typical” RC decay
circuit, we quickly realize that Color BASIC simply is not
fast enough to meet our needs. The capacitor will
discharge before our software counter can measure the
Here’s why Color BASIC is not fast enough. Recall
that the tinyBASIC portion of Color BASIC is a “software
interpreter” written in Spin: the purpose-built high-level
programming language for the amazing Propeller chip.
This means that it works by scanning your program code
for BASIC commands (actually, the opcodes of those
commands), then executing a Spin routine for each
discovered command and its arguments. Executing all
these Spin commands takes time.
FIGURE 4: The basic RC circuit, charging the capacitor to the initial voltage V0.
FIGURE 5: The RC circuit, discharging the capacitor through the resistor. The voltage Vt decays at a predictable rate (refer to the above equation) based on the values of R and C.
January 2018 43