Serial Plotter displaying square wave as sawtooth like

Hi, just got an Uno R4 yesterday and I want it to display the output of six opto sensors, essentially I'm trying to make a six channel oscilloscope.

Just to get started I wrote a loop to output six random high/low values (coded so that Ch1 is 0 or 1, Ch2 is 4 or 5 etc in order to separate them on the plot) and written to the serial port for display by the Serial Plotter (IDE 2.3.3 Windows 10).

Problem is the display is not what I expected, the high/low/high transitions are not displayed as vertical lines but rather as slopes.

Below is the type of output I expect, taken from my PC 'scope, and below that is the actual output plus the raw data.

I tried various baud rates, plus tried changing the delay() in the loop sending the data from 1mS to 1,000 mS

Any ideas? Thanks!

My guess would be that you do not have nearly enough samples along the time line to show vertical slopes. You are seeing two sequential samples, which if they were a lot closer could approach a vertical line, as a slope.

The above scope image will have thousands of samples along the time line.

2 Likes

Thanks for that, I assume you mean samples at the plotter end?

You could be right, but my instinct suggests if that were the issue, it would actually produce a square output. I tried the sine wave example which worked fine, if it was not doing enough samples that would have been notchy, I feel? Plus, I've seen fairly good squarewaves on YouTube.

  • With a R4?
  • With the same IDE?

I don't have the Uno R4 and I'm currently not in a position to use IDE 2.x.

1 Like

Perhaps not, it was yesterday I say them so not sure.

Can you share your code with us, so that we can comment on it or try and replicate your issue?

Your program is not printing nearly enough points to create a square wave.

Plot the points in the serial number as dots. Now connect the dots, then draw the square wave you are hoping to get.

Look at your Scope settings, see how many samples per second you are actually reading. My old TEK scope reads 2 GSa/sec.

3 Likes

Printing the results can be a bottleneck, even if you use a high baud rate.

You can get round this by taking a few hundred samples as fast as you can, and storing them in an array, and then go back and serial print the array at your leisure

There was a post on here a few days ago, with a photograph of an oscilloscope screen - you could see that the sample rate was set to 500 Sa/s.

I couldn't turn down the sample rate on my oscilloscope to anywhere near his setting in order to replicate his problem.

I ended up making a crude oscilloscope from a second Arduino, sampling every 2ms to prove that the low sampling rate would give the results he got.

2 Likes

Can't find that scope photo. However the issue is very obvious.

500 Samples per second

He was trying to view a burst of 1kHz pulses.

It was in this topic: https://forum.arduino.cc/t/square-wave-with-1khz/1249249

2 Likes

Hi thanks for your replies, I read the thread you linked to with interest.

I just did an experiment based on the code of the OP in that thread:

int freqOut = 3;

void setup() {
  pinMode(freqOut, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  digitalWrite(freqOut, HIGH);
  Serial.println(0);
  delayMicroseconds(500);

  digitalWrite(freqOut, LOW);
  Serial.println(1);
  delayMicroseconds(500);
}

I wrote it out to a pin and the serial. I put my scope on the pin (and the other scope channel on the 1Khz reference) and got a perfect square wave.

Here's what the plotter shows:

plotter

I notice in the other thread the numbers on the X axis have gaps of 100 while mine are 12 apart, for the same input frequency (in your screengrabs).

It sounds like you're trying to visualize the output of your opto sensors as a digital signal using the Arduino Uno and the Serial Plotter. The issue you're encountering with the output not appearing as vertical lines but instead as slopes could be due to the sampling rate or how you're sending the data to the Serial Plotter.

To help you troubleshoot and improve your code for better visualization, could you please provide the code you're currently using? With the code, I can offer suggestions on how to adjust it to achieve the desired output.

Additionally, it's worth considering the following points:

  1. Sampling Rate: Ensure that your Arduino is sampling the opto sensor inputs at a sufficiently high rate. The Serial Plotter may not accurately represent fast transitions if the sampling rate is too low.

  2. Data Format: Make sure you're sending the data in the correct format to the Serial Plotter. For digital signals, you typically want to send 0s and 1s representing the low and high states, respectively. Sending analog values or incorrectly formatted data may result in unexpected visualizations.

  3. Serial Communication Speed: Check the baud rate of your Serial communication. Setting it too high may cause issues with data transmission. The default baud rate for Arduino Serial communication is usually 9600.

By addressing these points and sharing your code, we can work together to refine your project and achieve the desired output on the Serial Plotter.

1 Like

Hi many thanks for the detailed reply.

I just posted some code above your post.

I've not even connected the opto sensors yet. I'm just simulating them in software to get the plotter working. For the example above my data rate is 1 kHz and I'm sending this to my plotter (just a few lines copied from my Serial Monitor):

1
0
1
0

As to speed I tried from 9600 to 115200 and a few in between

Do you understand why? If not, please try the thought experiment proposed earlier in the thread:

Serial Plotter simply plots the data points your board prints and then connects those points with lines. So the plot you got is exactly what we expect from the sketch running on your board.

As already explained, you can get something closer to what you were hoping for by printing many data points:

int freqOut = 3;
unsigned long timestamp;
byte pinState = LOW;

void setup() {
  pinMode(freqOut, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  if (micros() - timestamp >= 500) {
    timestamp = micros();

    if (pinState == LOW) {
      pinState = HIGH;
    } else {
      pinState = LOW;
    }

    digitalWrite(freqOut, pinState);
  }

  Serial.println(pinState);
}

1 Like

Interpolate option on serial plotter is turned off?

Thank you!

I think I get it now. I did see the previous post about the dots, but didn't get that it was a thought experiment. Now it makes sense. The plotter seems to work differently to the graphing library I use in another language.

Tired now after a long day so will have a detailed look tomorrow!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.