We have created this sketch because we don’t have a real oscilloscope and we’ve tried to find a confortable way to plot signals we read from different kind of analog sensors usign our Arduino board.
This can be a solution when you are sending data coming from multiple sensors and you can’t use the Arduino Serial plotter tool.
Here’s a screenshot of the virtual oscilloscope plotting signals from two potentiometers.
As you can see, the graphs from the two potentiomenters are drawn one below the other. The oscilloscope shows also a series of vertical red lines in order to mark the time (one line for each second passed).
You can also use the screenshot mode to freeze the image. You can use the mouse and place the cursor over the curves to examine them in more detail.
In order to use this virtual oscilloscope you simply need to have Processing and Arduino up and running.
How to use it
We know that Arduino quantizes the analog input values usign 10bit: analog readings from Arduino can be expressed as number from 0 to 1023. So, In order to maintain the higher fidelity with the original 10bit sampled signal and not to loose any information in sending these information via serial, we need 2 byte at least.
Taking inspiration from the MIDI protocol, we decided to split these 10bit numbers into two part:
the most significant bits are contained inside a first byte that is always sent as the first of a sequence of two; we call it the status byte;
a second byte, we call it the data byte, is sent immediately after the status one and contains the remainig bits (the least significants).
On the receiver side, we needed a way for Processing to differentiate between status and data bytes, so we dedicated the most significant bit of these two types of byte for the purpose:
when the oscilloscope reads a byte whose most significan bit is 1, he knows it is a status byte;
when this bit is 0, he knows he is reading a data byte instead;
We use the remaining 4 bit of the status byte to send information about the sensor number: this is a useful information we can use if there are more than one analog signal we want to plot (using this protol we can plot up to 16 sensors simultaneosly)!
Sender side: Arduino
Once you have created your circuit and your Arduino program and you want to plot some data from it, you have to add some line of code to your Arduino sketch in order to correctly format the data and send them via serial to the virtual oscilloscope.
Suppose your circuit is a circuit like that:
Here we are using 2 potentiometers and we are reading their analog values via the Arduino analog pins 0 and 3 respectively. The code we will use to convert data and send them over serial is the following:
As you see, here we call the function analogPlot once for each potentiometer, passing the analogPin number and the plotter graph address as arguments. This way the values coming from the first potentiometer will be plotted on the first graph, the upper one, while the second graph will plot data coming from the second potentiometer which is connected to analog pin 3.
Receiver side: Processing
By default the virtual oscilloscope expects receiving 2 analog sensor readings and it plots them as line graphs of 512 points in resolution.
If you want you can always change these values acting respectively on the variable int N = 2; and int K = 512; in the Processing code. Obviously you have also to update your Arduino code accordingly.
This is the Processing code:
Once the virtual oscilloscope has been lauched, you can enable the serial communication with the Arduino board by pressing the o or O keys. This way the Arduino board will start to send bytes over the USB connection to the Processing sketch.
If you want to close the serial communication press c or C: this will interrupt the byte flow.
Every time you want to pause the continuous plotting and examine a fixed portion of the graphs simply press the spacebar. This will make the plotter enter its screenshot mode.
When in screenshot mode the plotter will continue receiving serial data from the Arduino board but it will discard them while continuously drawing a fixed image showing the last portion of the graphs.
Every time you move the mouse on a graph, both in screenshot mode than not, a vertical and an horizontal line will appear.
These lines respectively represent the mouse horizontal position - among the 512 stored values - and the corresponding graph value. You will also see a numeric value on the left indicating the 10bit value as originally read by the Arduino board.
If you find this article useful and you like it, please leave a comment below: let us know what do you think about it, we'd really appreciate it.
Thank you very much and, as always, stay tuned for more to come!