Oscilloscopes and mixed-mode logic analyzers

Lead Image © Oleksiy Tsupe, 123RF.com

Tracing Your Circuit

We show how to use an oscilloscope to see what's going on in a resistor-capacitor circuit.

Sometimes, the projects I build at SwitchDoc Labs don't work quite right the first time I hook them up. Often, the problem can be traced to a software or wiring error, but it can also be a more subtle design error that requires some additional techniques to diagnose. In a previous SwitchDoc Labs column [1], I looked at using a logic analyzer to figure out logic problems.

In this column, I'll look at using a Rigol DS1502E oscilloscope to see what is going on in an resistor-capacitor (RC) circuit connected to a digital input on an Arduino. Then, I will look at the same circuit using a new amazing mixed-mode logic analyzer from Saleae. A mixed-mode logic analyzer is a logic analyzer that has some analog capability like an oscilloscope, albeit at a much lower frequency than a normal oscilloscope. This mixed mode logic analyzer is becoming my favorite tool in my debugging war chest.

To begin, I'll take a look at the RC circuit that I am going to analyze with all this equipment.

SwitchDoc Note

"You can always trust your mother, but you can never trust your ground." If you don't have a common ground through your system and with your test equipment, then you are going to get some really weird results that won't make sense. Check your ground first!

The RC Circuit under Test

I built a simple analog circuit on which to run the test instruments. This simple RC circuit consists of two components: a photoresistor (from Adafruit for $0.95 [2]), and a 1µF electrolytic capacitor (from Jameco for $1.90 for a pack of 10 [3]).

A capacitor (Figure 1) is a two-terminal component used to store electrical energy in an electric field. A capacitor contains at least two electrical conductors (plates) separated by a dielectric (i.e., insulator). A capacitor stores energy in the form of an electrostatic field between its plates. A capacitor can be used as a storage element, a frequency filter, and even a memory element (as inside a dynamic random-access memory – DRAM). Here, I'm using it as an energy storage element.

Figure 1: 1µF electrolytic capacitor.

SwitchDoc Note

The photoresistor I'm using is about 3.5 kilohms (Kohm) with a bright light and about 26Kohm in the dark. The Adafruit product page says it varies from about 5Kohm in the light to 200Kohm in the dark. You should measure it yourself with an ohmmeter and see what you get. If you were producing a bunch of units using this, you would either have to calibrate the circuit or find out the real specifications from the manufacturer and design accordingly. This is one reason analog projects are more difficult than digital projects to design for manufacturability.

A resistor (Figure 2) is a passive two-terminal component that implements electrical resistance as a circuit element. Resistors act to reduce current flow and, at the same time, act to lower voltage levels within circuits. In this circuit, I'm using it to limit the electrical current available to charge the capacitor. A photoresistor is a special kind of resistor. The resistance (measured in ohms) varies according to the amount of light that is shining on the photoresistor.

Figure 2: Photoresistor.

I am using this RC circuit (Figure 3) as a simple analog-to-digital converter (ADC). In the SunAir project [4] I used this ADC with two photoresistors to pinpoint the location of the sun.

Figure 3: RC circuit diagram.

In this circuit, the photoresistor limits the amount of current flowing into the capacitor, and eventually the voltage on the capacitor reaches the input voltage. The brighter the light, the lower the resistance of the photoresistor; hence, the capacitor will charge faster. The darker the light, the higher the resistance of the photoresistor and the slower the capacitor charges.

Mathematically, this is expressed as

where Voutput is the output voltage of the circuit (connected to a GPIO input on the Arduino), Vinitial is the initial voltage on the capacitor, which is set to ~0V by writing a "0" to the GPIO pin (configured momentarily as an output), and t is the time for which you want to know the voltage. The product of the resistance and capacitance (RxC) is known as the RC time constant, shown as the Greek letter tau on the x-axis in Figure 4.

Figure 4: Charging capacitor time constant display.

You can use the code in Listing 1 to calculate the input voltage, or in this case, the resistance of the photoresistor (which depends on the light levels).

Listing 1

Arduino Code for the RC Circuit Test

01 // Simple Analog to Digital Conversion for SwitchDoc Labs Raspberry Pi Geek Magazine
02 // SwitchDoc Labs, LLC
03 // April 2015
04 // Version 1.1
05 //
06 // This software runs through the GPIO to convert A/D
07 //
08
09 int GP0 = 9;
10 int GP1 = 10;
11
12 // the setup routine runs once when you press reset:
13 void setup() {
14   // initialize serial communication at 57600 bits per second:
15   Serial.begin(57600);
16   Serial.println("");
17   Serial.println("----------start of RC Circuit AD Conversion Test script---------");
18   Serial.println("");
19
20    pinMode(GP0, OUTPUT);
21    pinMode(GP1, OUTPUT);
22
23 }
24
25
26 // the loop routine runs over and over again forever:
27 void loop() {
28
29     int i;
30
31     while (1)
32     {
33       // GP0
34       Serial.println("Setting GP0 to GND");
35       // Start by setting GP0 to GND
36       pinMode(GP0, OUTPUT);
37       digitalWrite(GP0, LOW);  // set GP1 Low
38
39       // Hold 10ms
40      delay(10);
41
42      digitalWrite(GP1, HIGH);   // set GP1 high for mark
43
44      // Turn GP0 to HIGH Z (INPUT) to allow cap to charge from Vinput
45      Serial.println("Turning GP1 to High Z");
46      pinMode(GP0, INPUT);
47      digitalWrite(GP0, LOW);  // turn off pullups
48
49      int GPValue;
50      long microsStart;
51      long microsEnd;
52
53       microsStart = micros();
54
55       while(1)
56       {
57         // READ GP0 until it becomes 1
58
59         GPValue = digitalRead(GP0);
60
61
62         microsEnd = micros();
63         if (GPValue == 1)
64           break;
65       }
66
67       digitalWrite(GP1,LOW);  // mark this
68       Serial.print(" microsStart=");
69       Serial.print(microsStart);
70       Serial.print(" microsEnd=");
71       Serial.print(microsEnd);
72       Serial.print(" Charge Time=");
73       Serial.println(microsEnd - microsStart);
74
75       delay(10);
76
77   }
78
79 }

The time from the point at which I set the capacitor to 0V (Vinitial) – GP0 set to GND, then to high impedance (also setting the marker GP1 to 1) – to the point at which the Arduino reads it as a logic 1 (then setting the marker GP1 to 0) is proportional to the resistance of the photoresistor (because the capacitance is constant).

The brighter the light, the faster the voltage increases and the shorter the time the Arduino measures. The dimmer the light, the longer the capacitor takes to charge; thus, the time increases. You are therefore reading an estimate of the resistance of the photoresistor, so you have a measure of how bright the light is.

Note that you can do exactly the same thing with your Raspberry Pi and the GPIO pins. The voltages will be different, but the charge and discharge times will be about the same. Why? Because the time constant tau is independent of the voltage. It just depends on the resistance and capacitance of the circuit.

Pretty cool, eh? If you put a fixed resistor in for R, you could vary the input voltage and get an estimate of the input voltage by measuring how long the capacitor needs to charge. Thus, you have a simple analog to digital converter, with two inexpensive parts and a little software. It's amazing that you can build so much with so little.

Next, I'll show how this looks on an oscilloscope and the mixed-mode logic analyzer.

SwitchDoc Note

Check your wiring before you turn on the power, and then check your wiring again before you turn on the power. You can and will fry your electronics depending on the error. I have a box at SwitchDoc Labs called The Box of Death. It is filled with electronic gear and boards that were fried because I didn't check the wiring. Among other things, it currently contains two Raspberry Pi A+ (bad USB plug connections on the A+ – not a wiring mistake per se but be careful! [5]) one Arduino Uno, one Arduino Mega 2560, a WeatherPi Arduino board, and a Raspberry Pi B. The box is to remind me of this rule.

What Is an Oscilloscope?

An oscilloscope (or "scope") is a type of electronic test instrument that allows you to look at constantly varying analog signal voltages as a two-dimensional plot of one or more signals as a function of time.

Oscilloscopes are used to observe the change of an electrical signal over time, such that voltage and time are graphed against a calibrated scale. The signal can be analyzed for such properties as amplitude, frequency, rise time, time interval, and other characteristics. Modern digital instruments may calculate and display these properties directly.

The oscilloscope (Figure 5) can be adjusted so that repetitive signals can be observed as a continuous shape on the screen. A storage oscilloscope allows single events to be captured by the test instrument and displayed for a relatively long time, thereby allowing human observation of events that are too fast to be seen.

Figure 5: Rigol DS1502E oscilloscope.

The scope I'm using is an inexpensive Rigol DS1052E, which can be found for as low as $280. This scope is perfectly adequate for beginners as well as more experienced makers. It is good for up to 50MHz signals, which is enough for most digital applications and quite a few analog applications. My biggest complaint about the Rigol DS1052E is the lack of control and sampling software for the Mac. Rigol is still in a PC-centric mode. The lack of software makes it difficult to easily get screenshots from the oscilloscope. It can be done, but the process is very clumsy and requires the use of a USB stick. However, some people have hacked the Rigol with Python so you can even run it with a Raspberry Pi [6]. The ability to do this makes up for the lack of a nice Mac GUI to some extent.

The three key things to understand about using an oscilloscope are:

  1. What is the fastest signal that it can accurately display? 50MHz is the number for the DS1052E. By the way, some folks have figured how to hack the firmware on the DS1052E and turn it into a DS1102 at 100MHz. The DS1052E apparently is software-limited to 50MHz. Your results may vary, and you may brick the scope, but it's still interesting.
  2. What is the trigger to display a waveform? The "trigger" on an oscilloscope is the hardest thing to figure out. Think of it as triggering the beginning of the display on the screen. What are you looking for? A rising signal, a voltage level (e.g., 2.0V), or something else? You can set the scope to trigger on these criteria or use the Auto Trigger, which will usually work pretty well. Completely explaining what a trigger can be used for is beyond the scope of this short column. However, Tektronix, the maker for the best scopes in my opinion (Disclaimer: I worked for them in the 1970s as a student intern), has produced an excellent tutorial [7].
  3. Check your probes. They either typically will be a 10X or 1X probe. If you use a 1X probe, 1V will show up as 1V. If you use a 10X probe, 10V shows up as 1V. Why use a 10X probe? It's a bandwidth and loading issue. A 1X probe will load your circuit with 1Mohm and about 100pF of capacitance. A 10X probe will load your circuit with 10Mohms and about 10-15pF of capacitance, which will affect your circuit less than a 1X probe.

Note that in this example, I am measuring the voltage on a 1,000pF (10µF) capacitor, so although either probe will work fine, I'm adding 10 percent more capacitance with a 1X probe, which will affect the results. With a 10X probe, you will also get higher bandwidth from your scope, which will make the signal representation more accurate. If you don't need the accuracy, use 10X as a default. Because I want to see the voltage on the capacitor, and it is low bandwidth and not very accurate to begin with, I will use the 1X probes.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content