Introduction: How to Make an Inexpensive 16 MHz Arduino Oscilloscope Using Excel and Your Computer Screen to Display
Often an electronics hobbyist will design and build something only to find that it doesn't behave the way he or she intended. Sometimes in these situations the problem is related to an electrical signal, but that stuff moves at the speed of light! It's hard to troubleshoot. Wouldn't it be nice if you could just take a video of your signal, so you could actually see what is happening? Well, guess what? Over a hundred years ago a guy by the name of Karl Braun figured out how to do just that when he invented a machine called an oscilloscope. You can imagine that in the course of that hundred years, the technology has gotten better and better, and it has. The trouble is that the price reflects it. I can't afford one. Plus there is that other little matter of not knowing how it works internally or how to fix it when it breaks. I created this oscilloscope for those three reasons. If I build it, I know I can fix it cheap. I know what it does, so I know how to make it do stuff. Best of all, it's mine, so if I blow it up (nooooooo) I don't lose any friends, and if I want to use it at 3a.m. it's right here under my bed waiting.
Step 1: STEP 1: GET THE BOOK AND GET THE STUFF
When I first started thinking about this project, I explored a lot of different approaches. I really liked the Girino Instructable, but I'm a bit of an amateur, and his work looked scary complicated. There were some simple scope plans floating around that could only see signals in the lower frequencies, but for reasons unknown to me I felt like making a faster version. After a bit of research I landed on a book by Robert J. Davis II called "Arduino Oscilloscope Projects". This Davis guy is something of a celebrity in the electronics world, so I trusted him. Plus I noticed that when people were having trouble with their builds, he was pretty friendly and helpful online. It seemed like a sure bet, so I bought his book on Amazon for around ten bucks.
It turns out that Davis' "Projects" is not a book for beginners, but more of a research guide where you can build your scope from a huge number of different configurations. I was overwhelmed! Luckily I am a student at Utah Valley University, and a couple of sainted professors (thanks, David Dunlop and Scott Walker) helped me understand the different components and make some design decisions. So, depending on how much you know about electrical circuits and how much you want to customize your own personal oscilloscope (doesn't that sound sexy!), your first step might be to buy that book. If you are slightly less knowledgeable you might just skip that step for your initiation and buy it next time when you want to play with the big dogs.
You will also want to print out the parts list and get the stuff from ebay. I had to use an electronics dealer to get the parts for my first one because the school didn't want to bother with searches (can't blame them, time is money when you are paying a secretary), and it ended up costing 5X as much as the one I built for myself at home.
Step 2: STEP 2: ATTENUATION
As you probably know, an Arduino can't really handle a signal outside of it's cushy little .0049V-to-5 volt range. That's a problem for those of us who do a lot of troubleshooting because we can't usually predict what our magic circuitry spell is going to conjure up, and we don't enjoy turning our beloved little microcontroller minions into toads. So the first thing our oscilloscope has to do is an attenuation, which simply means shrinking a signal down to a manageable size. This is easily accomplished with a switch that either sends the signal through a voltage divider or does not. See the above schematic.
For a quick review of the math, let's recall that the voltage coming out of the middle of a voltage divider is calculated by
R2 * V
R1 + R2
So for our circuit where R1=100 and R2=900 you can see that we are going to end up with about 10% of our original voltage when we switch the signal through the divider.
Step 3: STEP 3: IMPEDANCE
You know how people tend to change their behavior when they know their boss is watching? Yeah, electrical signals are like that too. Put them into an oscilloscope and suddenly you are looking at the employee of the year when just yesterday productivity was a big fat zero. The smart boss knows this. He generously installs a personal vending machine in each employee's office and fails to mention the ethernet cam slyly embedded in the upper right-hand corner behind the Diet Coke sign.
For our purpose, this camera comes in the form of an AD744 IC. The input signal runs straight into this little op-amp configured as a unity gain voltage follower and voila, nothing! In fact, an ideal op amp allows zero current flow, but in reality the one million ohms of impedance it provides change the signal so insignificantly we can't even measure it. See the schematic for wiring. I also included a picture of the circuit installed on a breadboard, but mostly just so you can feel good about yourselves when yours looks cleaner. Keep in mind when you are putting it together that the shortest distances possible are the most desirable.
Step 4: STEP 4: GAIN
So maybe our problem isn't too much signal, but instead too little. It's a total wimp. What then? We employ another magical electronics technique of course. It's called amplification or gain.
Davis' book recommends we gain with an NE5532 dual op amp IC. Here's how it works. An inverting op amp is much like a copy machine that has an enlargement button. It takes a picture of the original signal and outputs a new signal that is magnified according to the ratio of resistor values. The math is pretty simple:
Vout = R2
Looking at the schematic, the signal enters this part of the circuit across a 1K resistor. That is R1. Then a switch sends it through a combination of resistors that make up R2. The options are 1K, 2K, and 3K. That means we have gains of 2, unity, or 5 available with the touch of a switch.
Putting a signal into pin 2 of the NE5532 means that it comes out inverted, as does putting it into pin 6. Combining the two results gives the inverse of the inverse, which is about like cleaning a clean house. Nothing looks any different. That's the reason we have that second half of the dual op amp in our circuit. I guess we could hang upside down from the ceiling when looking at our screen, but this just seemed a little easier.
Step 5: STEP 5: BIAS
Now we've got this sexy signal, and she's a beauty. Perfect shape, perfect size. Trouble is, she likes to swim underwater below trigger voltage sometimes, and our ADC can't see down there. If we are just looking at a square wave, it doesn't really matter. Low looks like low, and high looks like high. It's easy to see the pattern. Put in a sign wave without bias and you have a whole different outcome. It chops everything below trigger level off and you have a bunch of fish jumps marching across your screen. Signals are sneaky like that. Somehow we have to convince her she's a bird, not a dolphin, and get her to travel above water. Not a problem. Turning fins into wings is actually easier than you might think.
If we connect a 10K potentiometer to the -12V rail and send it through an inverting op amp and a couple of resistors, we can send out a flat EMF that raises our signal voltage. That's all it takes, and this is the final adjustment to the signal before it gets translated in the ADC. Hopefully you can tell where this piece of the schematic joins with the last one.
Step 6: STEP 6: CONVERTING ANALOG TO DIGITAL
This is my favorite part. If the signal went into the analog pins of the Arduino right now the scope would totally work. However, it would bottleneck on the Arduino analog to digital converter (ADC) and limit at 100,000 samples per second. Bob's book teaches us to overcome this limitation utilizing an external ADC. Chapter 3 covers several of these converters, only one of which is going to get any playtime here. If you are interested in trying different versions, I totally recommend spending the ten bucks to get the book. He does a good bit of research and explanation, and I have a feeling I will be using these things for a lot more than just oscilloscopes in the future.
In order to output a picture of your signal to a digital monitor, the signal must be converted from analog to digital by something. I chose a TDA8703 24 pin DIP because it was less than 8 bucks on ebay brand new, and it's lightning fast. It will do 40-50MHz! This chip runs on an external clock, and since my knock-off Arduino UNO runs on a 16MHz clock, I decided to buy a 16MHz for my chip too. Looking back, I can't really remember why I bought the 4-pin version of the clock, but it works, so I'm not messing with it.
The schematic is pretty straightforward except for that one wire that goes off to the Arduino clock. That part took me a while to figure out because Bob had a nice authentic Arduino with a removable processor. Mine was a cheapy knock off. The book wanted me to link the external clock of my ADC through a capacitor to the clock pin (9) on the Arduino's processor. Unfortunately Arduweenies sometimes have a teeny little surface mount processor with very limited access to pins, and effectively no access when you take mucky soldering skills into account. My brilliant professor, Dave Dunlop, took one look at my microcontroller and said, "See that little silver elliptical dome that says 16 on top? That's your clock." I plugged it in and sure enough, my borrowed oscilloscope showed a 16MHz output on the ends at the base. It was even substantial enough to be soldered onto! Now if I look at the signal being sent to the input of my ADC, it's kinda ugly because it is the result of the addition of these two clocks. This doesn't really matter because the ADC is set up to trigger on the rising side of the wave, and as long as it doesn't dip back down below trigger level between pulses it will work just fine.
Note: I got pretty lucky here because I had already downloaded my program. Later on I realized that once you solder a wire onto that clock, you have about a 1 in 20 shot at any future success with downloads even if the other end of the wire is just hanging out in space. Not sure why. I haven't tried unsoldering it and testing the Arduino because my scope works, and I'm keeping it. Skip over to Step 7 and download the program before you go ahead and solder anything to the clock.
If you are fortunate enough to have access to a signal generator you can test your circuit for functionality now. Input a slow pulse, say around 10 Hz and stick an LED between any of your digital outs (D0-D7) and ground. If it's working you'll get a nice steady blink. My smart instructor Scott Walker at UVU turned me onto this little trick!
Step 7: STEP 7: PROGRAMMING
There are two parts to the programming because the oscilloscope uses two brains. The first part is the Arduino code which is mostly just a bunch of repetition. It waits for a trigger and then collects digital inputs using the command PIND to read pins D0-D7 simultaneously. I have the loop set up to take 1000 samples and then send 1000 samples. You could change it to take more or less pretty easily. Each different wavelength will have a minimum sample rate necessary to get a clear picture.
Frequency * Wavelength = Speed of Light (a constant)
so the slower frequencies have proportionally longer wavelengths and are therefore more susceptible to errors from sample size. Really high frequencies on the other hand might not get sampled fast enough to represent what they are doing. I haven't spent the time to find my cutoffs yet, but I will probably update this instructable with that info later on.
Originally I had intended to build my entire scope using a model from Davis' book. Then I fried my little screen a week before it was due and had to come up with a different solution overnight. I googled how to connect my Arduino output to my laptop screen and came up with an obscure little data acquisition macro for Excel. This baby does two things I like. First, it logs all incoming data in an Excel spreadsheet. Second, it displays the info graphically on a chart. Be careful though. You can only connect for 10 or 20 seconds before you have so much data that it overloads Excel and locks up. This is not really a problem for a scoper since the signals we want to view are happening a bazillion times per second. If it isn't happening in 20, it probably isn't going to.
This is the link for the download: https://www.parallax.com/downloads/plx-daq Once you get that open, simply move the "Simple Data with Plots" tab into first position. The program may start with a warning about ActiveX controls, but just hit okay, and it chugs right along. You are going to see a small box on your screen with Control and Settings listed. Change the baud rate to 128000 because that's the rate I put in the code, and it needs to match. Figure out which port your oscilloscope is connected to using your computer's control panel, and set that as well. Now just click Connect and watch the data start streaming in. Stop it at around 5-10000 by hitting Disconnect. Delete the chart that automatically pops up and click on the Insert tab. If you select recommended charts you should see a line graph, and when you click on it it will automatically populate with your data. You can change the range of data it covers, the look of the axes, add secondary axes...I don't frankly know everything it can do, but it does a lot! Now if you click on Clear Columns and hit Connect again, you will see your signal come across the screen in real time. Voila, your scope is done.
Note: If you are not lucky enough to have Excel, you are going to have to find some other way to output your signal image. Davis details programming for several different displays. They are relatively inexpensive, and the book provides the entire code for about 8 different varieties.