Introduction

This scope is a very powerful tool for debugging. I often want to measure analogue input signals together with some sketch-variables. To do so, I have built this oscilloscope library, which 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.
The simplest way of debugging is printing values directly to the serial monitor. However, this is not possible in time-critical situations. Therefore, I have built this debug oscilloscope library.
After the measurements, 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:

Oscilloscope base library for the Arduino
Oscilloscope base library for the Arduino

With the help of the trigger() we can catch very hard to find bugs:

Arduino debug oscilloscope
Arduino debug oscilloscope

Features of the oscilloscope base library

  • Measuring all kinds of sketch-variables, with or without analog input values.
  • Suitable for ATmega328 and SAMD21 Arduino boards: Arduino Uno, Arduino Zero, SAM 15x15, etc.
  • The trigger signal can be generated by the sketch.
  • It can be used for 1, 2, 3 or 4 channels.
  • It has pre-trigger and post-trigger.
  • The sample rate is measured and printed.

Operation of the debug oscilloscope

Start

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.
sampleAB(...); (inside a loop, for 2 channels)
When the circular buffer is filled completely, the values are printed to the serial monitor automatically.

Trigger

We have to give a trigger() or stop() signal, else nothing happens.
When more triggers are given, only the first trigger is taken.

RecordLenght

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 integers, this results in maximum 500 samples for the SAMD21G18, since the int size is 4byte. For 2 channels, these are respectively 250 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(1, 0, 50); (1 channel, record length = 50 instead of 500)

Stopping immediately

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.

Pre trigger

Optionally, the number of pre samples can be specified, for example:
scope.start(1, 20, 100); (1 channel, pre samples = 20, record length = 100)

Post trigger

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)

Fast 10-bit ADC

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..

Example with two channels and pre-trigger 

#include <Albert.h>
#include <avdweb_scope.h>

const byte ADCpin = 1;
Scope scope;

void setup(void)
{ Serial.begin(9600);
  scope.start(2, 5); // 2 channels, preSamples = 5
  for(int i=100; i<1000; i++)
  { scope.sampleAB(i, analogReadFast(ADCpin)); // combination of a value i and ADC
    if(i==200) scope.trigger();
  }
   
}

void loop(void)
{
}

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

Download

Download the library from GitHub.

Create chart in Excel

Copy the data from the serial port window and paste it to Excel with "paste special".

Circular buffer size

The maximum circular buffer size is about

  • ATmega328 used in the Arduino Uno: 700 bytes
  • SAMD21 used in the Arduino Zero: 7000 bytes

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.

Links

 

 

Do you have any comments? Please let me know.
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.