Tag Archives: Arduino

Arduino LESSON 21: Log Sensor Data to an SD Card

In most of our work so far, we have just watched our data go by on the Serial Monitor. In most cases, you will want to have some means to store your data. The easiest way to do this is to use a simple SD card reader. For this example, we use the Virtuabotix SD Card Reader.

Arduino connected to a BMP180 pressure sensor and an SD Card Reader

In this tutorial, we will need to have some sensor hooked up so we will have some data to store. We will be using the BMP 180 Pressuer and Temperature sensor from adafruit.  We have a complete tutorial on this sensor  HERE.  You will need to go to that lesson and get the sensor hooked up, the library installed, and the software done. All this is explained step-by-step in the LESSON.

The BMP180 is connected 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

Once you have the BMP180 connected, test and make sure your code is working, and you are getting good pressure and temperature readings. Once that is working, you are ready to connect your SD card Reader/Writer.

The SD card reader should be connected as follows:

Connecting the SD Card Reader
Sd Card Reader Pin Arduino Pin Details
GND GND Common Ground
3.3 V – (NOT USED)
+5 5V Power
CS 4 Chip Select
MOSI 11 SPI Data
SCK 13 Clock
MISO 12 SPI Data
GND GND Common Ground

In the video we will show step-by-step how to develop the software. You should follow along in the video, and not copy and paste the code below. You will never learn to program if you do not write your own code. The code below is to help you in case you get stuck.

If you have the BMP180 and the SD card connected correctly,  this should create a file called PTData.txt on the card, and write comma delimited data to the file. Note that if the file does not exist on the card, the command:

will create the file. If the file already exists, this command will append data to the existing file. If you want to start with a clean new data set, erase the old PTData file.

When you run the program, you end up with a PTData.txt file on the SD card. When you have finished logging your data, you can pop the card out, put it into your PC, and then import the data into excel. You should now be able to plot, graph or analyze the data using all the powerful features of Excel.

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 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.

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.

 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.

Python with Arduino LESSON 4: Expanding your Virtual World

In this lesson we will expand the virtual world we created in Python with Arduino LESSON 3. We will be creating a virtual world that will track a simple scene in the real world. In this project, the virtual world will track both the position and the color of a target in the real world. This lesson requires that you have the Python software and libraries installed, which we explained in LESSON 2.

This is our circuit with the HC-SR04 ultrasonic sensor and the TCS230 Color Sensor

This Lesson will be a bit more involved, and I will take you through it step-by-step. I will need to break things into two parts. In today’s lesson we will cover the Arduino side. We will develop the software that will measure distance and color, and then send those numbers over the serial port. Then in tomorrows lesson, we will develop the Python software to create a really cool virtual graphic to display the data in a virtual world.

For this project you will need the HC-SR04 ultrasonic sensor, the TCS230 Color Sensor, the Arduino Microcontroller, and some male/female jumper wires to connect to the color sensor.

The Ultrasonic Sensor can be attached per the schematic below:

Simple Circuit for Measuring Distance

Detailed tutorial on using this sensor was described in Arduino LESSON 18, so we will not go through all the details of using the sensor here. Review that lesson if you need more help. Key point here is to connect it as seen in diagram above.

You will also need to connect up the Color Sensor.

Connecting the Color Sensor to the Arduino

Color Sensor Pin
Arduino Pin
S0 GND
S1 5V
S2 pin 7
S3 pin 8
OUT pin 4
VCC 5V
GND GND

Use of the color sensor was described in detail in Arduino LESSON 15.  You should be able to develop to write the software yourself based on earlier lessons to make measurements from both the Color Sensor, and Ultrasonic Sensor, but if you get stuck, you can glance at my code below. Again, it is important for you to write your own code and not copy and paste mine. Mine is just a reference if you get stuck.

The key point to notice with this code is the print statements, summarized below:

 Notice that we are printing  our color strengths and distance on one line separated by commas. It is important to note the order of the data. When we read this in Python, we will read it in as one line of text, and then we will parse it into its individual values. So, we must make note and remember the order the data is arranged in in this line.

Remember when you have your python program reading this data, you must have your serial monitor closed. For now though, run your program and look at the serial monitor to verify you are getting correct data in the expected format.

In the next Lesson, LESSON 5, we will build the Python program to create a virtual world from this data.