Tag Archives: Python

Python with Arduino LESSON 12: Approximating Changes in Height from Changes in Pressure

In LESSON 9 we learned how to hook up a BMP180 Pressure Sensor and make pressure and temperature readings. Then in LESSON 11 we learned how to stream that data to Matplotlib and create live graphs and charts of our data that update in real time. We could see that as we moved the pressure sensor up and down, we could see the pressure change, as the pressure decreases with increasing elevation.

This leads to the interesting question of whether we can use our circuit developed in LESSON 9 to create a Height-O-Meter . . . a simple device that will measure the height above the floor.

The math to calculate altitude vs. pressure turns out to be very complex. Particularly, if we wanted something for our high altitude balloon flights, or for model rocketry. It turns out that for the case of measuring height inside and for relatively small changes in height we can make simplifying assumptions that make things much easier. The assumption we will make is that temperature does not change much over the range of our experiment. With this assumption, we can create our own Height-O-Meter. To do this though, we do need to to through some math. I show my math below, and go through it step-by-step in the video. Remember, this simplified approach is only valid for playing around with small changes in height. We will have to do the more complicated math when we make our high altitude balloon probe. For now though, this math will work pretty well.

Height and Pressure
Calculate Changes in Height from Changes in Pressure

We can rearrange the equation to solve for height as a function of pressure.

Height Pressure
Calculating Height from Pressure Changes

Python with Arduino LESSON 11: Plotting and Graphing Live Data from Arduino with Matplotlib

We now have all the pieces put together to allow us to plot live data from the Arduino. If you have kept up with the earlier lessons, you will now have everything you need. If you have not done the earlier lessons, make sure you have python 2.7, vPython and pySerial installed from Python with Arduino LESSON 2.  Make sure you have installed matplotlib (Python with Arduino LESSON 7), and install drawnow (Python with Arduino LESSON 10). Also, you need to build the BMP180 circuit and get the arduino programmed up as explained in Python with Arduino LESSON 9. With this business taken care of, you are now ready to start plotting live data.

Pressure Data
This chart shows live pressure and temperature data being plotted in real time

We are using the Adafruit BMP180 pressure sensor.  We showed how to hook it up and program it in LESSON 9. As a reminder, we are using this code for the arduino. LESSON 9 explained in detail how the code works.

The video in this lesson above explains step-by-step how to develop the code on the Python side, and how matplotlib and drawnow work together to make live graphs and plots of data streaming from the arduino in real time. The code below is what we developed in the video. 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. If you are in my class, you will be required to be able to develop live graphing code like this from scratch, so don’t take a shortcut and copy and paste.

You should be seeing data like the graph on the top of this lesson. 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.

Python with Arduino LESSON 10: Installing Drawnow to Allow Live Plotting with Matplotlib

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.

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.

 

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.