Arduino oscilloscope with 7 lines of code

The Updated Arduino OscilloscopeIn the video below I will show you how to build an Arduino oscilloscope in 2 minutes using only 7 lines of code.

This is possible because of an update in the Arduino software.

New to Arduino? Check out What Is Arduino?

In the newest version of the Arduino software, there’s a tool that makes it super easy to build a simple oscilloscope!

It’s called Serial Plotter and it works like this:

It listens on a serial port, and it plots every number that it sees.

That means, to create an oscilloscope, all you need to do is to write code that prints out the voltage value from an analog input, and the arduino plotter will take care of the rest.

I only used 7 lines of code to make this work!

The analog input of the Arduino UNO can only take up to 5V. Make sure you don’t go over this limit as you may damage your Arduino.

If you need to measure a higher-volt signal, you can use a voltage divider.

Measurement Rate Issue

I chose the baud rate of 115200. This is the number of bits per second. To send a character over the serial port, you usually need 10 bits. 8 bits for the character in addition to one start bit and one stop bit.

So, that gives me a speed of 11520 characters per second.

The numbers to plot are written in text. So, the number 800 is sent as an “8”, then “0”, then “0”. That is three characters.

Let’s simplify and say that all numbers are between 100 and 999. Then, the maximum number of measurements this oscilloscope can do per second is 11520 divided by 3, which is 3840. This is called the sampling rate.

You can only measure frequencies that are up to half of the sampling rate. Half of 3840 is 1920. Which means you can measure frequencies up to about 1.9 kHz with this oscilloscope.

Another problem with this oscilloscope is that for measurements that give a number of below 100 or above 999, you have fewer or more than 3 characters to send. This will cause the sampling rate to increase or decrease, and mess up the signal in the Serial Plotter.

You can fix this by making sure you always send 4 characters. Ex “0013” instead of “13”. “0390” instead of “390”.

Comments, questions and improvements

The oscilloscope above is the quickest and easiest way of setting it up. But there are many ways to improve it.

Let me know your questions, or which improvements you would do to make it work for your application in the comment field below!

More Arduino Tutorials

20 thoughts on “Arduino oscilloscope with 7 lines of code”

  1. Oyvind, this is awesome!

    On your original project, I added some code to use the microcontroller’s built-in PWM generator to generate three different PWM outputs on pins 9, 10 and 11. It makes for a great tutorial on PWM.

    I’ve used it to teach kids about PWM and how it can be generated without a bit-banging code loop.

    The code is in the last comment of your original post.

    I’ll try it out on this new design and add a comment when I get it working.

    Reply
  2. Won’t AnalogRead() turn analog signal to digital? I am still new to this so, sorry if I am wrong. But if I am right, then you are turning analog to digital and the digital back to analog to show on screen, which is not efficient or even accurate. But if you do can show the real analog signal, the one that is not processed by ADC (before he turns it into digital) than this is something awesome. :)

    Reply
    • Adrien, to display the signal you need to digitise it. The whole point is to take some analogue signal, and digitise it with the Arduino so that we can analyse it.

      Reply
  3. Wow oyvind….
    I am eager to try this.
    Wht is the circuit which is connected to breadboard and mobile? Do you have video or tutorial on how you made that connection. Please let me know.
    Thanks

    Reply
  4. Your blog has been useful. It took me less than 10 minutes to understand how to build an Arduino oscilloscope. Thanks for posting this!

    Reply
  5. I added some of this to your 1st generation oscilloscope comments, and improved it some more. I’m using it as part of an Arduino class to illustrate the various input and output modes without having to do any wiring.

    Add the following code to the setup loop to illustrate the various voltage levels by programming some of the pins with PWM, low, high, and LED 13 on.

    Plug into the pins (7,8,9,10,11,13 and GND) to see what’s happening. DO NOT PLUG INTO 5V!!! THAT COULD BE CONSIDERED A SHORT CIRCUIT!

    The waveform happening when it’s just the wire plugged into A0 is the ambient electrical noise. A proper oscilloscope would probably show it operating at about 60 cycles (in North America).

    There’s probably an additional lesson to add here for PULLUP resistor settings.

    void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600); //Set up serial connection
    // Set up PWM output on pins 9,10 and 11 for different levels.
    // You do it here because once it’s set up, it keeps running.
    // You don’t need it in the main loop so it doesn’t interfere
    // with the measurements.
    // Once it’s running, attached the wire from A0 to each pin to
    // see the square waveform generated by each value,

    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(9, OUTPUT);
    analogWrite(9,25);
    analogWrite(10,128);
    analogWrite(11,200);

    // Now let’s set up a couple of basic digital pins
    pinMode(8, OUTPUT);
    digitalWrite(8, HIGH);
    pinMode(7, OUTPUT);
    digitalWrite(7, LOW);
    }

    // Turn on Pin 13 LED so we see what’s happening there.

    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);

    }

    Reply
  6. Awesome tutorial! I tried it and it worked great, but I don’t know how to calibrate it. A 1.5V battery I plugged in gave a wave reaching above 321 on the graph. If you could tell me the conversion, I would really appreciate it. Thank you!

    Reply
    • The adc gives you a value between 0 and 1023.

      0 means 0V and 1023 means 5V.

      That’s 1024 steps in total.

      So to convert take your reading, 321, and divide by 1024. Then multiply this with 5V:

      321/1024 = 0.313
      0.313 * 5V = 1.565V

      Reply
  7. Hello,

    Tried out the oscilloscope but only got a flat line for the monitor although the serial monitor kept displaying many numbers in one long line.

    What i am really trying to do is to visualize the bouncing of a switch using the oscilloscope. If you could add a youtube lesson to show that, and then show how it can be debounced with a resistor-capacitor circuit — and explain why exactly this works — that would be great?

    (i couldn’t for example figure out how the electrons flow once the capacitor is full and the switch is open)

    Reply

Leave a Comment