An Arduino has an inside world and outside world. With an oscilloscope we can measure the outside world. However, everything what happens inside remain hidden. To debug complex sketches, it would be nice if we could measure electronic signals outside the board together with internal sketch-variables. So, I have built this oscilloscope library, which is an extreme powerful tool for debugging.
This oscilloscope is in fact a circular buffer with oscilloscope functions. It has been kept as simple as possible to save memory space and optimize the speed. After the measurements are done, the output data is printed to the serial port. The data can be shown in a graph by Excel. Because the scope can measure all internal variables, complicated tests can be done:
Software oscilloscope for signals inside the microcontroller
With the help of the trigger() we can catch very hard to find bugs:
Download the library from GitHub.
Of course, it is possible to use the ADC for measuring external signals, just like a normal oscilloscope.
Specify the number of channels and start: scope.start(2); (for 2 channels)
The scope waits for the trigger: scope.trigger();
After the trigger, the scope records the samples.
probeAB(...); (inside a loop, for 2 channels)
We have to give a trigger() or stop() signal, else nothing happens.
When more triggers are given, only the first trigger is taken.
Use showIfReady() to print the recorded values.
The record length, this is the same as the circular buffer size, depends on the number of channels and the kind of processor that is used. For the SAMD21G18 on the Arduino Zero, the size is set to 2000 bytes. Since the samples are short integers (2 bytes), this results in maximum 1000 samples for the SAMD21G18. For 2 channels, these are respectively 500 samples.
A large buffer size can have the disadvantage that it may take too much time until all samples have been taken. Therefore, you can specify a smaller buffer size, for example:
scope.start(2, 0, 50); (2 channels, record length 25 samples (50/2) instead of 1000)
Instead of waiting until the scope is ready, we can also abort sampling and show the values immediately at the serial monitor. This is done with the stop signal:
stop();
The stop works also when no trigger was given before.
Optionally, the number of pre samples can be specified, for example:
scope.start(1, 20, 100); (1 channel, pre samples = 20, record length = 100)
The number of post samples may be specified with a negative value, for example:
scope.start(1, -10, 100); (1 channel, post samples = 10, record length = 100)
As part of the oscilloscope library, I have developed a fast 10-bit ADC, the maximum sample rate with one analog input is 50kS/s..
#include "avdweb_SWscope.h" const byte ADCpin = 1; SWscope scope; void setup(void) { Serial.begin(9600); scope.start(2, 5); // 2 channels, preSamples = 5 for(int i=100; i<1000; i++) { scope.probeAB(i, analogRead(ADCpin)); // combination of a value i and ADC if(i==200) scope.trigger(); } } void loop(void) { scope.showIfReady(); }
Serial port output:
usPerDiv 26
ptr value
96 196 683
97 197 553
98 198 419
99 199 290
100 200 176
101 201 78 trigger
102 202 37
103 203 59
104 204 128
105 205 231
106 206 355
107 207 487
108 208 620
109 209 744
#include "avdweb_SWscope.h" #include <avdweb_SAMDtimer.h> const byte ADCpin = 1; void ISRtest(struct tc_module *const module_inst); SAMDtimer timerISR = SAMDtimer(4, ISRtest, 1000, 0); // ISRenable=0 SWscope scope; int i; void setup(void) { scope.start(2, 5); // 2 channels, preSamples = 5 timerISR.enableInterrupt(1); // start ISR here } void loop(void) { scope.showIfReady(); // printing scope values to Serial is not possible in ISR } void ISRtest(struct tc_module *const module_inst) { scope.probeAB(i++, analogRead(ADCpin)); scope.trigger(); }
Create chart in Excel
Copy the data from the serial port window and paste it to Excel with "paste special".
The maximum circular buffer size is about
If the oscilloscope library is part of a large sketch, the circular buffer would have to reduced, do this by changing maxBytes in the library header file.
If the buffer is made too large then we get an uncontrolled situation. While there are no compiler errors, the content at the end of the circular buffer may be overwritten by nonsense. The reason is that the Arduino works without operating system that keeps control.