I will show you how to take your Arduino projects to the next level by having the Arduino interact with the Python programming language. Python is a free program you can download. Since you have already learned the fundamentals of programming of Arduino lessons, learning Python will be a snap!
Make sure you have python 2.7, vPython and pySerial installed from Python. Make sure you have installed matplotlib, and install drawnow.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Download Python, Pyserial, Vpython
1)Download and Install Python 2.7.8.(I personally prefer this). Also
download 32 bit version of the software, even if you have a 64 bit windows machine.
2) Download and Install Pyserial version 2.7. Again, download the 32 bit version of the software.
3) Download and install the Vpython library for 32 bit windows.
Step 2: Install Matplotlib
go to this 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 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. These links will be useful. You can download pip at this link:
Right mouse click on get-pip.py and download to your desktop. You will then want to run the program in python. . 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. Add the two elements to your path. Adjust the parameters to reflect where your python installation is and where your python script folder.there are the two things added to my path:
Once you have these in your system path, you can test your PIP as follows.
Open a CMD box.
pip install -U pip
Step 3: BMP180 Sensor Measuring Pressure and Temperature
Hook up the circuit, program the arduino, and stream the temperature and pressure data over the serial port. Connecting Up the BMP180 Pressure and Temperature Sensor
BMP180 Pin Arduino Pin
Vin ----------> 5V
The first thing you will need to do is to download and install the adafruit library for this component.
You can download the library for this part here:
Click the “Download the Adafruit_BMP085 Arduino Library” large green box. This will download as a zip folder. Open the zip folder, and then drag and drop the contents on your desktop. You want the contents of the zip folder, not the zip folder itself. Rename the folder you dropped to your desktop “adafruitBMP180”.Now you need to drag and drop this folder into your arduino library folder. To find your arduino library folder, in the arduino IDE window, look in file, preferences. A window should pop open, and it should show you where your arduino sketchbook folder is. Drop your adafruitBMP180 folder into the Library folder of your arduino sketchbook folder.Once your adafruit_BMP180 folder is in your arduino library folder, you are ready to start writing your code. You need to kill your arduino IDE window and reopen it for it to find your new library. Now, to get this sensor to work, you just need a few lines of code. To begin with, you must load the Wire.h library and the Adafruit_BMP085.h library.Then in void setup you will need to start the sensor, and then in void
loop begin making measurements. The code below is a nice example of how to do this.
#include "Wire.h" // imports the wire library for talking over I2C
#include "Adafruit_BMP085.h" // import the Pressure Sensor Library
AdafruitBMP085 mySensor; // create sensor object called mySensor
float tempC; // Variable for holding temp in C
float tempF; // Variable for holding temp in F
float pressure; //Variable for holding pressure reading
Serial.begin(115200); //turn on serial monitor
mySensor.begin(); //initialize mySensor
tempC = mySensor.readTemperature(); // Read Temperature
tempF = tempC*1.8 + 32.; // Convert degrees C to F
pressure=mySensor.readPressure(); //Read Pressure
Serial.print("The Temp is: "); //Print Your results
Serial.println(" degrees F");
Serial.print("The Barometric Pressure is: ");
delay(250); //Pause between readings.
Now run the program and check your serial monitor and you should see measurements of temperature and pressure.
If everything is working good in serial monitor. now slight change in code in print for python interact. change the last serial.print and serial.println to---------------->
Serial.print(" , ");
by doing this the serial monitor shows
Now its time to unleash the power of python.
Step 4: Python Interact
Do not simply cut and paste this code, but make sure that you understand it so you are able to create your own live graphing programs from scratch.
#step 1: import the Library
import serial # import Serial Library
import numpy # Import numpy
import matplotlib.pyplot as plt #import matplotlib library
from drawnow import *
#step2: Setup the variables and create serial object
arduinoData = serial.Serial('com11', 115200) #Creating our serial object named arduinoData
plt.ion() #Tell matplotlib you want interactive mode to plot live data
note:- always check the com port. for my case i connect the arduino in com11. your is deffer from me. and baud rate use same baud rate in arduino sketch and python otherwise python fail to interact with arduino.
#step3:create a function for plot:-
def makeFig(): #Create a function that makes our desired plot
plt.ylim(80,90) #Set y min and max values
plt.title('My Live Streaming Sensor Data') #Plot the title
plt.grid(True) #Turn the grid on
plt.ylabel('Temp F') #Set ylabels
plt.plot(tempF, 'ro-', label='Degrees F') #plot the temperature
plt.legend(loc='upper left') #plot the legend
plt2=plt.twinx() #Create a second y axis
plt.ylim(93450,93525) #Set limits of second y axis- adjust to readings you are getting
plt2.plot(pressure, 'b^-', label='Pressure (Pa)') #plot pressure data
plt2.set_ylabel('Pressrue (Pa)') #label second y axis
plt2.ticklabel_format(useOffset=False) #Force matplotlib to NOT autoscale y axis
plt2.legend(loc='upper right') #plot the legend
step4:loops that forever :-
while True: # While loop that loops forever
while (arduinoData.inWaiting()==0): #Wait here until there is data
pass #do nothing
arduinoString = arduinoData.readline() #read the line of text from the serial port
dataArray = arduinoString.split(',') #Split it into an array called dataArray
temp = float( dataArray) #Convert first element to floating number and put in temp
P = float( dataArray) #Convert second element to floating number and put in P tempF.append(temp) #Build our tempF array by appending temp readings pressure.append(P) #Building our pressure array by appending P readings drawnow(makeFig) #Call drawnow to update our live graph
plt.pause(.000001) #Pause Briefly. Important to keep drawnow from crashing
if(cnt>50): #If you have 50 or more points, delete the first one from the array tempF.pop(0) #This allows us to just see the last 50 data points
note: here is the trick cnt. when count cross the 50 or more, delete the first one from the array.
You should be seeing data like the graph. You will probably need to adjust your y-axis scale parameters in Python to ensure the scale is suitable for the data you are taking. If your chart is blank, likely your y-scales are not right for your data measurements.