Our objective with this series of lessons is to plot live data coming from arduino using Python and Matplotlib. We have taken a few lessons to get familiar with Matplotlib, and we have built a circuit to stream live data from arduino to python. We need to install one more library to enable Matplotlib to plot live sensor data in real time. The magic library is called drawnow. The bad news is that this library is hard to install on windows. The good news it that PIP installs it very easy. So, if you have not done so already, you need to go to Python with Arduino LESSON 6 and install PIP. PIP makes it very easy to install drawnow.
Tag Archives: Python
Python with Arduino LESSON 8: Introduction to Graphing with Matplotlib
While it is cool to create 3D visuals using vPython to represent our data coming from arduino, sometimes we want to make more quantitative graphs and charts from the data. To do this, we need to learn how to create graphs in Python. We do this using the library Matplotlib. We learned how to install and download this library in Python with Arduino LESSON 7: If you have not installed the library yet, make sure to go back and do in as shown in LESSON 7.
With the library installed, we are ready to learn Matplotlib. The video takes you through an introductory tutorial with step-by-step instructions. The code below is a sample of how to plot a sin and cos wave. You can watch the video, and then play around with the parameters to become familiar with this library.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import numpy as np #import numpy library import matplotlib.pyplot as plt #import matplotlib x= np.linspace( 0, 2*np.pi, 50)#create your x array y= np.sin(x) #create y array z= np.cos(x) #create z array plt.plot(x,y, 'b-d', linewidth=2, label='sinx') #plot y plt.plot(x,z, 'r-o', linewidth=2, label='cosx') #plot z plt.grid(True) #display background grid plt.axis([0,2*np.pi,-1.5,1.5]) #set range on axis plt.title('My Sin and Cos Waves') #chart title plt.xlabel('Time in Seconds') #label x axis plt.ylabel('My Waves') #label y axis plt.legend() #show legend plt.show() #show the plot |
Python with Arduino LESSON 7: Installing Matplotlib for Graphing
In this video we provide step-by-step instructions on how to install Matplotlib. Hopefully you have been following our lessons, and have already installed Python 2.7, the 32 bit version. If you stay on the same path as me as far as libraries and software versions, all my tutorials should work easily for you. In order for matplotlib to work, you need to have the numpy library installed. In Python with Arduino LESSON 2 we installed the vPython library. If you installed the vPython library, you already have numpy, as that was part of the vPython installation. If you have not already installed vPython, you should review that lesson now and do that.
Now, to install matplotlib, go to this page:
https://github.com/matplotlib/matplotlib/downloads/
If you are following my lessons and software versions, you will want to install this version, which should be towards the top of the page:
matplotlib-1.2.0.win32-py2.7.exe — Binary installer for 32-bit Windows, built using python.org’s 2.7 and Numpy 1.6.2
When the file downloads, then click on it and then follow the simple installation instructions.
Once it is installed you can test things with this simple program, which should plot a sine wave.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import numpy as np import matplotlib.pyplot as plt x=[] y=[] for i in np.arange(0,2*np.pi,2*np.pi/1000): x.append(i) y.append(np.sin(i)) plt.plot(x,y, 'b-', linewidth=2) plt.grid(True) plt.axis([0,2*np.pi,-1.5,1.5]) plt.title('My Sine Wave') plt.xlabel('Time in Seconds') plt.ylabel('Sin(t)') plt.show() |
Python with Arduino LESSON 6: Installing PIP on Windows
In this series of lessons you have learned to send data from Arduino to Python, and then do some pretty cool things in Python. We have created little virtual worlds, and have done neat dynamic graphics. Unfortunately not all Python libraries are as easy to install as vPython and pySerial. Some are next to impossible to install. The good news is that there is a free program called PIP that will install just about any Python library very easily. We need to pause and install PIP. Many of the future lessons will require you to have PIP installed on your machine. Please follow along with me on the video, which shows you how to install PIP on your windows machine. These links will be useful. You can download pip at this link:
https://pip.pypa.io/en/latest/installing.html
Go to the section on PIP install as seen here:
Right mouse click on get-pip.py and download to your desktop. You will then want to run the program in python. We show you how to do this in the video above. Then, you need to edit your system path file by going to the control panel, select system, select advanced settings, and under environmental parameters select the path. Update your path file to show your system where your python folder is and where your python script folder is. If you are unsure of this, watch the video where I show you exactly how to do it. If you are adept with computers, you can just do it from this description. Add the two elements to your path. Adjust the parameters to reflect where your python installation is and where your python script folder is. For me, these are the two things added to my path:
C:\Python27
and
C:\Python27\Scripts
If you do not know exactly what I am saying, then watch the video for more detail.
Once you have these in your system path, you can test your PIP as follows.
Open a CMD box.
Type:
pip install -U pip
This asks pip to update itself. You should see it come up and indicate it is either up to date or is updating. This will tell you that you have the PIP installed correctly.
Your life with Python will now be much easier because your system now knows the path to both your Python program and your PIP installer.
Python with Arduino LESSON 5: Finishing our Virtual Reality Example
This Lesson finishes the work that was begun in Python with Arduino LESSON 4. In that lesson we built the circuit and programmed the arduino to measure the distance to a target and the color of the target. The program then output that data to the serial port. In today’s lesson we will use python to read that data stream, and use the data to dynamically update a virtual world we create.
You will need to start with the work in LESSON 4 to get your circuit working, and your arduino programmed up. Once you have done that, you are ready to use Python to program up your virtual world. Remember you will need to have the pyserial and the vPython libraries loaded. We showed how to install the software in Python with Arduino LESSON 2.
In the video we will go through the process step-by-step to create a virtual world. The code we end up with is posted below. You should not copy and paste the code, but just glance at it if you get stuck. In the end, you should develop your own virtual world and just use mine as a guide if you need more help.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
## This program reads data over the serial port ## from that arduino. You have to read an entire line of data ## and then you have to parse it into different number values ## Then those R, G, B numbers are used to make the color of ## a visual object in python change. import serial #import serial library from visual import * #import vPython library MyScene=display(title='My Virtual World') #Create your scene and give it a title. MyScene.width=800 #We can set the dimension of your visual box. 800X800 pixels works well on my screen MyScene.height= 800 MyScene.autoscale=False #We want to set the range of the scene manually for better control. Turn autoscale off MyScene.range = (12,12,12) #Set range of your scene to be 12 inches by 12 inches by 12 inches. target=box(length=.1, width=10,height=5, pos=(-6,0,0)) #Create the object that will represent your target (which is a colored card for our project) myBoxEnd=box(length=.1, width=10,height=5, pos=(-8.5,0,0)) #This object is the little square that is the back of the ultrasonic sensor myTube2=cylinder(color=color.blue, pos=(-8.5,0,-2.5), radius=1.5,length=2.5 ) #One of the 'tubes' in the front of the ultrasonic sensor myTube3=cylinder(color=color.blue, pos=(-8.5,0,2.5), radius=1.5,length=2.5 ) #Other tube myBall=sphere(color=color.red, radius=.3) sensorData= serial.Serial('com11',115200) # Create senorData object to read serial port data coming from arduino while True: #This is a while loop that will loop forever, since True is always True. rate(25) #We need to tell Vpython how fast to go through the loop. 25 times a second works pretty well while(sensorData.inWaiting()==0): # Wait here untill there is data on the Serial Port pass # Do nothing, just loop until data arrives textline = sensorData.readline() # read the entire line of text dataNums=textline.split(',') #Remember to split the line of text into an array at the commas red=float(dataNums[0]) # Make variables for Red, Blue, Green. Remember green=float(dataNums[1]) # the array was read as text, so must be converted blue=float(dataNums[2]) # to numbers with float command dist=float(dataNums[3]) # last number in the list is the distance blue=blue*.7 #On my sensor, blue is always a little too strong, so I tone it down a little if (dist>=1.5 and dist<=2.25): #only change color or target if target is between 1.5 and 2.25 inches from sensor target.color=(red/255., green/255., blue/255.) #This keeps color from flickering. target.pos=vector(-6 + dist,0,0) #Adjust the position of the target object to match the distance of the real target from the real sensor |
The video explains each line of the code. Play around and tweak the values and see the effect on your virtual scene. Now your assignment is to take what you have learned here, and continue to expand your virtual world. Add objects to your virtual scene. Perhaps build an object for the breadboard, color sensor and arduino. I will give you several days to do this, and then when I come around for a project grade, I will want to see who has built the most impressive virtual scene. You should go well beyond the simple demonstration I have done here.
