Software oscilloscope for inside microcontroller

Published: 31 December 2015
Last updated: 02 June 2023

Oscilloscope for measuring internal Arduino values

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.

How does it work?

Software oscilloscopeSoftware oscilloscope

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 microcontrollerSoftware oscilloscope for signals inside the microcontroller
With the help of the trigger() we can catch very hard to find bugs:

Arduino debug oscilloscopeArduino debug oscilloscope

Library download

Download the library from GitHub.

Use as a normal oscilloscope

Of course, it is possible to use the ADC for measuring external signals, just like a normal 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.
probeAB(...); (inside a loop, for 2 channels)

Trigger

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

Print to the serial monitor

Use showIfReady() to print the recorded values. 

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

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

Example with interrupt service routine 

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

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

 

 

© 2023 AvdWeb. All rights reserved.