# Plotting and Graphing Live Data From Arduino Using the Power of Python

4,872

41

2

Published

## Introduction: Plotting and Graphing Live Data From Arduino Using the Power of Python

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.

download 32 bit version of the software, even if you have a 64 bit windows machine.

## Step 2: Install Matplotlib

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:

https://pip.pypa.io/en/latest/installing.html

C:\Python27

and

C:\Python27\Scripts

Once you have these in your system path, you can test your PIP as follows.

Open a CMD box.

Type:

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

GND--------> GND

SCL---------> A5

SDA---------> A4

The first thing you will need to do is to download and install the adafruit library for this component.

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

void setup()

{

Serial.begin(115200); //turn on serial monitor

mySensor.begin(); //initialize mySensor

}

void loop()

{

tempF = tempC*1.8 + 32.; // Convert degrees C to F

Serial.print("The Temp is: "); //Print Your results

Serial.print(tempF);

Serial.println(" degrees F");

Serial.print("The Barometric Pressure is: ");

Serial.print(pressure);

Serial.println(" Pa.");

Serial.println("");

}

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(tempF);

Serial.print(" , ");

Serial.println(pressure);

delay(250);

--------------------------------------------------------------------------------------------------------------

by doing this the serial monitor shows

tempF,pressure

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

tempF= []

pressure=[]

arduinoData = serial.Serial('com11', 115200) #Creating our serial object named arduinoData

plt.ion() #Tell matplotlib you want interactive mode to plot live data

cnt=0

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[0]) #Convert first element to floating number and put in temp

P = float( dataArray[1]) #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

cnt=cnt+1

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

pressure.pop(0)

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.

## Recommendations

• ### Arduino Class

70,607 Enrolled

## 2 Discussions

Nice instruction! One question. How many stream may use on this python applicatio ? If I want use many sensors. Thanck you.

Thank you!!

for many sensor case. first sketch your arduino. such as:-

--------------------------------------------------------------------------------------------------

sensor1,sensor2,sensor3,sensor4 //com serial output

--------------------------------------------------------------------------------------------------

and
in case of python set up the variables for each sensors. read the line
of text from the serial port. Split it into an array called dataArray
.Convert first element to floating number and put in sensor1. Convert
second element to floating number and put in sensor2 and so on for the
remaining sensors.. Build our sensors array by appending there readings.
and create plot functions for each sensors.