combining I2C sensors MPL3115A2 & MCP9808

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
allenmshapiro
 
Posts: 26
Joined: Tue Nov 11, 2014 6:37 pm

combining I2C sensors MPL3115A2 & MCP9808

Post by allenmshapiro »

I am protyping the combination of multiple I2C sensors, in this case, barometer (MPL3115A2) & temperature (MCP9808).

I tested each of these sensors individually by connecting them to an Arduino Uno I2C pins and using the downloaded test code. When I test them individually, they each appear to provide the correct sensor output as displayed on the Serial Monitor.

When I combine the two sensors and connect them to the I2C pins on the Arudino Uno (see the attached photo), and combine the .ino test codes, the temperature sensor appears to give the correct output on the Serial Monitor. However, the barometric sensor (MPL3115A2) does not yield the correct response. Am I missing something in the proper connection of these sensors to the I2C pins on the Arduino Uno when they are used together?

For you information, I have also attached a screen shot of the Serial Monitor output when the two sensors are attached to the Arduino Uno. The barometric output is 0.02 Inches (Hg), which is unrealistic. When I tested the barometric sensor by itself, the barometric output was 29.92 inches (Hg).
combine_MPL3115A2_MCP9808.JPG
combine_MPL3115A2_MCP9808.JPG (167.73 KiB) Viewed 1391 times
combined_MPL3115A2_MCP9808_test.png
combined_MPL3115A2_MCP9808_test.png (53.77 KiB) Viewed 1391 times

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by adafruit_support_mike »

Try adding a few milliseconds of delay between the MCP9808 reading and the MCP3115A reading. You may be seeing a timing issue.

User avatar
allenmshapiro
 
Posts: 26
Joined: Tue Nov 11, 2014 6:37 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by allenmshapiro »

Thanks for the suggestion.

I modified the .ino code to include multiple delays in the loop (at the start of the loop, after the part of the code calling the barometric sensor, and at the end of the loop). For your information, I have attached a listing of the .ino code that I modified.

Unfortunately, the barometric sensor reading continues to be erroneous (see the attached screen shot from the Serial Monitor) when I attach both the barometric sensor and the temperature sensor to the I2C pins on the Arduino Uno. In this case, the barometric sensor is reading ~77 Inches (Hg), where as the correct reading should be about 30 Inches (Hg). I have gone back again and tested the barometric sensor alone on the I2C pins, and I am able to get correct readings.

Do you have any further suggestions?

Thanks for your help
combined_MPL3115A2_MCP9808_test_1.png
combined_MPL3115A2_MCP9808_test_1.png (51.21 KiB) Viewed 1374 times
test_combined_MPL3115A2_MCP9808.txt
(2.4 KiB) Downloaded 147 times

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by adafruit_support_mike »

That's officially wierd..

When you tested the sensors independently, did you have them both wired as shown in the photo above? If not, try testing each sensor in that hardware configuration.. talking to one and ignoring the other. It's possible that something may be wonky in the signals.

User avatar
allenmshapiro
 
Posts: 26
Joined: Tue Nov 11, 2014 6:37 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by allenmshapiro »

I did the test as per your suggestions, that is, configuring the MPL3115A2 and MCP9808 so that both are connected to the I2C pins on the Arduino Uno and then run each test program; the test programs (from the Adafruit site) are written to access only one of the sensors. The temperature sensor (MCP9808) operates correctly, where as, the barometric sensor (MPL3115A2) is not operating properly and gives a sensor reading of approximately 0.14 Inches (Hg) - the real value should be approximately 30 Inches (Hg).

Thus, the only configuration where I can get the MPL3115A2 operating properly is if there are no other I2C sensors connected to the Arduino Uno.

To convince myself that the I2C pins are operating properly, I also connected a real time clock (RTC), the ChronoDot DS3231. I configured this to the I2C pins with both the MPL3115A2 and the MCP9808 and modified the test code to include parts of the RTC test code available on the Adafruit site. In this configuration both the RTC and the temperature sensor operate properly, but the barometric sensor continues to give erroneous readings.

The only thing I can perhaps conclude from this is that the MPL3115A2 is faulty. Are there any other options?

Thanks for your help & I look forward to your reply.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by adafruit_support_mike »

Okay, that tells us we're dealing with some kind of hardware problem.. let's try three more tests:

1) Using the MPL3115A2-only code, run once with the MCP9808 plugged into the breadboard and once with it removed. The presence/absence of that sensor should be the only change between runs.

2) Swap the positions of the MPL3115A2 and the MCP9808 and see if that has any effect.

3) Try replacing all the jumpers that go to the MPL3115A2.. sometimes you'll get a broken wire inside the insulation, and those cause all sorts of bizarre problems.

User avatar
allenmshapiro
 
Posts: 26
Joined: Tue Nov 11, 2014 6:37 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by allenmshapiro »

I appreciate your patience and suggestions.

I tried the 3 physical tests that you suggested with the following results:

1. With both sensors wired to I2C pins and running the test code for the MPL3115A2 only, I DID get correct readings for the barometric sensor. This seems different from what I recall before, but I have tested it multiple times, and I get a barometric reading near 30 Inches (Hg). The barometric sensor also responds correctly when the temperature sensor is pulled out of the breadboard (while running the test code for the MPL3115A2).

2. I swapped locations of the sensors on the breadboard and the same result as above occurs.(note that in this operation, I needed to adjust locations of jumper wires because the location of SCL and SDA pins on the sensor boards differs between the two).

3. I replaced all of the jumpers and repeated the same tests above with the same results.

Based on this I went back and again ran the combined code that combines the test for the 2 sensors (see attached. . .please note that I added multiple delays in the code as per your earlier suggestions). Unfortunately, I continue to get the same response as earlier, that is, the temperature sensor appears to be operating correctly, but the barometric sensor continues to give erroneous readings (~0.14 Inches Hg). I went back to the product literature to check if there was a conflict in the I2C addresses for these sensors, and from my understanding there is not a conflict.

Thus, I am back to where I was previously, that is, the barometric sensor operates properly when it is the only I2C device called in the program; however, when multiple I2C devices are called in the program, the barometric sensor does not yield the correct response.

Thanks for your help, and I look forward to your reply.
Listing_combined_test_MPL3115A2_MCP9808.txt
(2.4 KiB) Downloaded 120 times

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by adafruit_support_mike »

Try this code:

Code: Select all

/**************************************************************************/
/*!
    @file     Adafruit_MPL3115A2.cpp
    @author   K.Townsend (Adafruit Industries)
    @license  BSD (see license.txt)

    Example for the MPL3115A2 barometric pressure sensor

    This is a library for the Adafruit MPL3115A2 breakout
    ----> https://www.adafruit.com/products/1893

    Adafruit invests time and resources providing this open source code,
    please support Adafruit and open-source hardware by purchasing
    products from Adafruit!

    @section  HISTORY

    v1.0 - First release
*/
//ams-2014_11_21 Combine test programs for MPL3115A2 & MCP9808

//This is a demo for the Adafruit MCP9808 breakout
//----> http://www.adafruit.com/products/1782
/**************************************************************************/

#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
#include "Adafruit_MCP9808.h"

// Power by connecting Vin to 3-5V, GND to GND
// Uses I2C - connect SCL to the SCL pin, SDA to SDA pin
// See the Wire tutorial for pinouts for each Arduino
// http://arduino.cc/en/reference/wire
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();

void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit_MPL3115A2 test!");
  
  if (! baro.begin()) {
    Serial.println("Couldnt find MPL3115A2");
    return;
  }
    Serial.println("MCP9808 demo");  
  // Make sure the sensor is found, you can also pass in a different i2c
  // address with tempsensor.begin(0x19) for example
  if (!tempsensor.begin()) {
    Serial.println("Couldn't find MCP9808!");
    while (1);
  }
}

void loop() {
  float pascals = baro.getPressure();
  // Our weather page presents pressure in Inches (Hg)
  // Use http://www.onlineconversion.com/pressure.htm for other units
  Serial.print(pascals/3377); Serial.println(" Inches (Hg)");

  float altm = baro.getAltitude();
  Serial.print(altm); Serial.println(" meters");
  
  delay(250);
  
  float tempC = baro.getTemperature();
  Serial.print(tempC); Serial.println("*C");

  // Read and print out the temperature, then convert to *F
  float c = tempsensor.readTempC();
  float f = c * 9.0 / 5.0 + 32;
  Serial.print("Temp: "); Serial.print(c); Serial.print("*C\t"); 
  Serial.print(f); Serial.println("*F");
  
  delay(1000);

}
It's more or less the same as the sketch you posted, but calls the MPL3115A2's .begin() method once in setup() rather than calling it every pass through loop().

User avatar
allenmshapiro
 
Posts: 26
Joined: Tue Nov 11, 2014 6:37 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by allenmshapiro »

Again, thanks for your prompt reply.

I applied the code that you attached to your last message (moving the baro.begin() command to setup rather than having it executed continually in loop).

The results, unfortunately were the same. The temperature sensor gives accurate results; however, the barometric sensor yields a barometric pressure ~0 Inches Hg, whereas it should be reading about 30 Inches Hg.

With the wiring setup the same, I again tested the sensors using the test program written for only the barometric sensor, and as before, the results appear to be correct. Thus, problem continues to be getting an incorrect reading when the code attempts to access multiple sensors on the I2C pins.

Any further suggestions in trying to get the barometric sensor working with other I2C sensors?

User avatar
gary_n
 
Posts: 3
Joined: Wed Jan 15, 2014 8:42 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by gary_n »

Hey Allen,
I am getting the same results as you with the Barometric pressure reading. I am using the Adafruit example code for the MPL3115A2. Using this sensor, all the readings are correct, MPL3115A2 pressure and temp as long as the Delay (250); is not changed in the last line of code. I wanted to use a longer delay but changing this number to anything else gets me a false 0.22 Inches (Hg) reading. Changing code back to Delay(250); everything works fine again.
Adjust your delay to 250 and see if it works correctly.
Any ideas on how to correct this?
Thanks!

User avatar
allenmshapiro
 
Posts: 26
Joined: Tue Nov 11, 2014 6:37 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by allenmshapiro »

Wow!! This is really random!!

I changed delay to 250 ms at the end of the test code, and I now obtain a correct reading for the barometric sensor in combination with other I2C sensors hooked up to the I2C pins on the Arduino Uno, in particular, the temperature sensor (MPC9808) and the ChronoDot RTC (DS3231).

I tried other values of the delay at the end of the loop. I was able to get correct readings for 500 ms, but when I tried 1000 ms or greater, again I would get incorrect readings for the barometric sensor, but the other sensors hooked up to the I2C pins were correct.

Is there any explanation for this, or is there a work around to enable longer delays?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by adafruit_support_mike »

I have no explanation of what's going on, but now that we have a working configuration we can poke at it and see what happens.

Try adding 1k pull-up resistors to the SDA and SCK lines and see if the sensor is any more reliable that way.

User avatar
allenmshapiro
 
Posts: 26
Joined: Tue Nov 11, 2014 6:37 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by allenmshapiro »

I wired 1k pull up resistors to the SCL and SDA lines connecting to the I2C pins on the Arduino Uno. I have the barometric sensor (MPL3115A2), a temperature sensor (MCP9808) and a RTC (ChronoDot DS3231) connected to the I2C pins.

I uploaded the code with the 500 ms delay. The results for all sensors with the wiring for the pull up resistors appear accurate, including the barometric sensor.

Unfortunately, when I modify the code for 1000 ms delay, the results for the barometric sensor are ~0 Inches Hg, but the temperature sensor and the RTC give appropriate values.

I also tried installing 10k pull up resistors with no luck. Again, with 500 ms delay, everything operates properly, but when I increase the delay to 1000 ms, the barometer gives erroneous readings.

Is there another move to try to provide for longer delays in the use of the barometric sensor.

User avatar
gary_n
 
Posts: 3
Joined: Wed Jan 15, 2014 8:42 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by gary_n »

I have another quirk to add to this discussion. When the Delay 250; is in the sketch, it is uploaded to my UNO, the first time the sketch runs the reading is Barometric Pressure = 0.34 Inches (Hg) but on the next reading 250ms later the sensor has the correct Barometric Pressure = 29.01 Inches (Hg)

I have not tried the pull-up resistors that Allen did but I am sure my results will be the same. Have tried multiples of 250 in the sketch but so far no other value has worked.

User avatar
allenmshapiro
 
Posts: 26
Joined: Tue Nov 11, 2014 6:37 pm

Re: combining I2C sensors MPL3115A2 & MCP9808

Post by allenmshapiro »

I also suffer the same "quirk" of the first reading from the loop for the barometric sensor being erroneous (~ 0 In Hg). Even with the 1k or 10k pull up resistors, that "quirk" continues. As long as the delay statement at the end of the loop is 250 ms, 500 ms, or 750 ms, subsequent barometric readings appear accurate.

I have still not come up with a fix to enable longer delays. I have tried adding a delay statement earlier in the loop in addition to the delay statement at the end of the loop. The result is the same. . . barometric sensor yields erroneous readings, whereas the other I2C devices appear to be correct.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”