Plotting Real-time Data From Arduino Using Python (matplotlib)

143,006

50

65

Introduction: Plotting Real-time Data From Arduino Using Python (matplotlib)

About: Open Source Hardware Projects - Microcontrollers, Programming, and new prototyping technologies.

Arduino is fantastic as an intermediary between your computer and a raw electronic circuit. Using the serial interface, you can retrieve information from sensors attached to your Arduino. (You can also send information via the serial interface to actuate circuits and devices (LEDs, relays, servos, etc.) connected to your Arduino.) Once you have the data in your computer, you can do all sorts of things with it – analyze it, display it, or share it on the internet, for instance.

In this instructable, I will be reading and displaying analog data from a pair of LDRs connected to an Arduino. Attached is the schematic.

The Arduino sketch is very simple – it just reads the values from analog pins A0 and A1 (in the range [0, 1023]) and prints it to the serial port.

Here is the code:

https://gist.github.com/electronut/5641938

The serial port sends values in the format:

512 300
513 280
400 200
...

On the computer side, I need to read these values, and plot them as a function of time. I am using Python and the Matplotlib library for this. I wanted to display this as a scrolling graph that moves to the right as data keeps coming in. For that, I am using the Python deque class to keep and update a fixed number of data points for each time frame.

You can see the full implementation here:

https://gist.github.com/electronut/d5e5f68c610821e311b0

3 People Made This Project!

Recommendations

  • Go Big Challenge

    Go Big Challenge
  • Colors of the Rainbow Contest

    Colors of the Rainbow Contest
  • Make It Modular: Student Design Challenge

    Make It Modular: Student Design Challenge

65 Comments

0
brnstin
brnstin

11 months ago

Universal Real-Time Software Oscilloscope GUI DLL Library
PYTHON READY

"The library makes it possible to show on the beams of the oscilloscope
over a million quantization steps of signal per second – less than one
micro-second is sufficient for one signal sample. This software can be
used for linking to real-time controlling programs as longer delays in
relay of data to the oscilloscope don’t occur."

https://www.oscilloscope-lib.com

Regards

0
nuwan621
nuwan621

2 years ago

nice work!!!!

0
fmarengo
fmarengo

4 years ago

Dear electronut,

I tried to implement your code in my PC using Spyder editor, but unfortunately it gives me an error (see at the bottom). I also replaced

strPort = args.port

by

strPort = "COM10"

since COM10 is the port I am using in my PC (Windows7 64bits). Even in this case, the error is the same. How can I solve it from the editor, please? Thanks!

usage: ldr.py [-h] --port PORT
ldr.py: error: argument --port is required
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
0
carloszoom3000
carloszoom3000

Reply 3 years ago

I'm having the same error.
Any update?
Regards,

0
RitwikaM
RitwikaM

6 years ago

Hello Elecronut,

Thank you for the code. I am very new to this, so I have been facing quite a few issues with running the code. I keep getting errors while running the argparse command. I replaced port with COM3 like below, but keep getting errors!

def main():

# create parser

parser = argparse.ArgumentParser(description="LDR serial")

# add expected arguments

parser.add_argument('--COM3', dest='COM3', required=True)

Error in python:

usage: test_python.py [-h] --COM3 COM3

test_python.py: error: argument --COM3 is required

My problem is I am not sure where to replace strPort or port with COM3.

Please help!

Thanks!

Ritwika

0
electronut
electronut

Reply 6 years ago

Without changing the code, run it as:

>python ldr.py --port COM3

What is the output?

0
KartikS50
KartikS50

Reply 3 years ago

Could you please elaborate how to run code
Without changing the code, run it as:
>python ldr.py --port COM3
Where do i need to make the changes

0
RitwikaM
RitwikaM

Reply 6 years ago

Thanks, it works!!! I am trying now to add the data to a csv file. Thank you for all the help again!

0
fmarengo
fmarengo

Reply 4 years ago

Hi RitwikaM

Did you happen to execute the python script from a python editor (e.g Spyder)? If your answer is yes, could you please send me your code?
I would like to modify the code so as to plot ONE data series from port COM10. Also, I would like to save the data series on the local HDD.
Thanks!

0
electronut
electronut

Reply 6 years ago

Awesome!

0
aghosca
aghosca

5 years ago

Great! How to reverse the flow of the graph from right to left?

0
StefanoR30
StefanoR30

Reply 4 years ago

@line 33 change it with:

buf.popleft()

buf.append(val)

0
Dylan TaylorA
Dylan TaylorA

5 years ago

Works 'out of the box' which is seriously just fantastic in the world of analog and digital communication. Serial port can be found by looking in your arduino IDE under tools-> serial port. for me it was /dev/ttyACM0

Great work!

0
cristhianc12
cristhianc12

5 years ago

good day apologize for my English, I'm from Colombia I am currently developing my thesis project and have been almost impossible to create the code acquisition 4 sensors, I wonder if I can use some of their code in my project. and I wonder if there is any way to predefine the port COM6 not run the program from the console. thank you very much for contribution.

0
Guddi11
Guddi11

5 years ago

Hi,

I also tried your code and I also get a nice Tk Window, BUT no plot…
apparently analogPlot.update is not called or at least does not do
anything. I already tried some things to fix it, but without success.
Any hint?

Could it be the backend, that doesn’t want to show me anything?

Otherwise, a nice, easy and understandable code!!! Well done!

Thanks in advance.

Robert

0
JordiG2
JordiG2

6 years ago on Introduction

Hello elecronut,

Thank you for sharing. I like your project and I will use with my robot.

I have a ultrasonic attached to a servo to mapping obstacles. I send to serial port to numbers: angle of the servo and detected distance. Now, I want to graphic the obstacles.

I try your code but I need a XY praph. I mean, use the first number (angle) for X and the second number (distance) as Y.

I tried to make some modification but without any success.

Could you give me any suggestions?

Thank you in advance!

0
electronut
electronut

Reply 6 years ago on Introduction

Hello,

You need to specify the axes data differently. Take a look at the matplotlib tutorial below:

http://matplotlib.org/users/pyplot_tutorial.html

Regards

0
JordiG2
JordiG2

Reply 6 years ago on Introduction

Thank you!

Now I can see the problem. I have made some changes and... it works :)!