All posts by admin

Python with Arduino LESSON 13: Calculating Height from Pressure measurements from BMP180 Pressure Sensor.

It is time to bring together a lot of things we have learned in our earlier lessons to create a Height-O-Meter, which will plot how high our BMP180 pressure sensor is above the floor. For this lesson we make simplifying assumption of constant temperature. When we use the sensor for our space probe or other high altitude experiments we will need to derive the equation again to take into account changing temperature. We went through the math of calculating height from changing pressure in LESSON 12.

In this lesson, we start with the software we developed in LESSON 11 for measuring, streaming, and plotting pressure and temperature data from the BMP180 sensor.

Remember, we connect the sensor to the Arduino as follows:

Connecting Up the BMP180 Pressure and Temperature Sensor
BMP180 Pin Arduino Pin
Vin 5V
GND GND
SCL A5
SDA A4

 

The software we are using on the arduino side is shown below, from LESSON 11.

We modify the Python code from LESSON 11 as explained in the video above to get this code for the Python side.

 Please go through video for complete description of this software. Remember this is only valid for small changes in height over which temperature is constant.

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.

Calculate Changes in Height from Changes in Pressure

We can rearrange the equation to solve for height as a function of 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.

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 9: Measuring Pressure and Temperature with the BMP180 Sensor

One of our goals with this series of lessons is to learn how to plot live data in Python. To do that, we need some interesting streaming data from the Arduino. In this lesson we will provide a live stream of temperature and pressure data. We will hook up the circuit, program the arduino, and stream the temperature and pressure data over the serial port. Then in the next lesson, we will read the data stream into Python, and provide a live plot of the incoming data. We will be using the Adafruit BMP180 Pressure Sensor.

This is the most excellent BMP180 Pressure Sensor from adafruit.

This is a really simple sensor to get set up. To connect it up, use the following connections:

Connecting Up the BMP180 Pressure and Temperature Sensor
BMP180 Pin Arduino Pin
Vin 5V
GND GND
SCL A5
SDA A4

 

With the circuit hooked up, you are ready to start coding. The first thing you will need to do is to download and install the adafruit library for this component. I prefer the API V1 version of the library, so we will download that one. Do not worry that the documentation lists a different part number. This is an upgraded version of the sensor, and the documentation still references the old part number. You can download the library for this part here:

https://learn.adafruit.com/bmp085/using-the-bmp085

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. If this is not perfectly clear, watch the video above and you can watch me do it step-by-step. Once your adafruitBMP180 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 (again, do not worry that the library is named after an earlier model of this sensor).  After loading the libraries, you will need to create a sensor object. 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.

Now run the program and check your serial monitor and you should see measurements of temperature and pressure. Pressure in Pascals is a big number. To convert Pascals to inches of Mercury, or in Hg, which is what the weather sites usually report, take the Pascal reading, and divide by 3386.389. Then you should be in Inches of Mercury and you can check your reading against a weather report for your area. The numbers should be close.