Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: Help on connecting an I2C MPRLS pressure sensor from Adafruit

  1. #1
    Senior Member
    Join Date
    Apr 2015
    Posts
    125

    Help on connecting an I2C MPRLS pressure sensor from Adafruit

    https://www.adafruit.com/product/3965

    So I went and bought a pressure sensor board from Adafruit thinking that it would run fine on the Teensy 3.6 over I2C. So far, I am so wrong...

    Adafruit has their own dead simple library which base off of the Wire.h library but I know that's not going to be compatible with the Teensy. So I went to the i2c_t3 library to see if I could get that to work, and so far I am not able to communicate with the board at all, let alone get any data.

    I am using Pin 3 for SCL, Pin 4 for SDA on the Teensy 3.6.

    I tried all manner of these examples libraries but I can't get any of them to work on this board.

    Here's the code I have.

    Code:
    // -------------------------------------------------------------------------------------------
    // Basic Master
    // -------------------------------------------------------------------------------------------
    //
    // This creates a simple I2C Master device which when triggered will send/receive a text 
    // string to/from a Slave device.  It is intended to pair with a Slave device running the 
    // basic_slave sketch.
    //
    // Pull pin12 input low to send.
    // Pull pin11 input low to receive.
    //
    // This example code is in the public domain.
    //
    // -------------------------------------------------------------------------------------------
    
    #include <i2c_t3.h>
    
    // Memory
    #define MEM_LEN 256
    char databuf[MEM_LEN];
    int count;
    
    void setup()
    {
        pinMode(LED_BUILTIN,OUTPUT);    // LED
        digitalWrite(LED_BUILTIN,LOW);  // LED off
        pinMode(3,INPUT_PULLUP);       // Control for Send
        pinMode(4,INPUT_PULLUP);       // Control for Receive
    
        // Setup for Master mode, pins 18/19, external pullups, 400kHz, 200ms default timeout
        Wire.begin(I2C_MASTER, 0x00, I2C_PINS_3_4, I2C_PULLUP_EXT, 400000);
        Wire.setDefaultTimeout(200000); // 200ms
    
        // Data init
        memset(databuf, 0, sizeof(databuf));
        count = 0;
    
        Serial.begin(9600);
    }
    
    void loop()
    {
        uint8_t target = 0x18; // target Slave address
     
        // Send string to Slave
        //
        if(digitalRead(12) == LOW)
        {
            digitalWrite(LED_BUILTIN,HIGH);   // LED on
    
            // Construct data message
            sprintf(databuf, "Data Message #%d", count++);
    
            // Print message
            Serial.printf("Sending to Slave: '%s' ", databuf);
            
            // Transmit to Slave
            Wire.beginTransmission(target);   // Slave address
            Wire.write(databuf,strlen(databuf)+1); // Write string to I2C Tx buffer (incl. string null at end)
            Wire.endTransmission();           // Transmit to Slave
    
            // Check if error occured
            if(Wire.getError())
                Serial.print("FAIL\n");
            else
                Serial.print("OK\n");
    
            digitalWrite(LED_BUILTIN,LOW);    // LED off
            delay(100);                       // Delay to space out tests
        }
    
        // Read string from Slave
        //
        if(digitalRead(11) == LOW)
        {
            digitalWrite(LED_BUILTIN,HIGH);   // LED on
    
            // Print message
            Serial.print("Reading from Slave: ");
            
            // Read from Slave
            Wire.requestFrom(target, (size_t)MEM_LEN); // Read from Slave (string len unknown, request full buffer)
    
            // Check if error occured
            if(Wire.getError())
                Serial.print("FAIL\n");
            else
            {
                // If no error then read Rx data into buffer and print
                Wire.read(databuf, Wire.available());
                Serial.printf("'%s' OK\n",databuf);
            }
    
            digitalWrite(LED_BUILTIN,LOW);    // LED off
            delay(100);                       // Delay to space out tests
        }
    }
    I am assuming that in this configuration, I am wanting a master to slave (MPRLS) configuration. The board is said to only listen on 0x18 address and, hopefully, that doesn't conflict with anything else I need on the Teensy (how could I figure that out?). I am using external 10k pullup resistors on both the SCL and SDA pins.

    Can anyone help with this?

    If I can't get this to work, I'm going to need to abandon the Teensy for this project.

    Thanks

  2. #2
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,093
    Quote Originally Posted by ceremona View Post
    https://www.adafruit.com/product/3965


    Adafruit has their own dead simple library which base off of the Wire.h library but I know that's not going to be compatible with the Teensy. So I went to the i2c_t3 library to see if I could get that to work, and so far I am not able to communicate with the board at all, let alone get any data.
    The Adafruit library and Teensy's Wire lib should work -- did you even bother to try and run the lib's example? I just confirmed it compiles on T3.6. I don't have the device so I can't test execution. Schematic shows breakout boards already has pullup resistors on SCL and SDA. I'd use pins 18 and 19 for testing the supplied Adafruit lib example.

    If you are going to use pins 3 and 4 you need to use Wire2

    https://learn.adafruit.com/adafruit-...ensor-breakout

  3. #3
    Senior Member
    Join Date
    Apr 2015
    Posts
    125
    Thanks Manitou.

    Yes, I "even bothered" the lib examples. As stated clearly in my first post. I was not clear about using the wire2 constructor, I didn't see it documented clearly what I should do.

  4. #4
    Did you try invoking begin() with Wire2?
    Here's an example, taken from the Adafruit Github repo, modified to use pins 3&4 (Wire2) on a T3.6:

    Code:
    #include <Wire.h>
    #include "Adafruit_MPRLS.h"
    
    // You dont *need* a reset and EOC pin for most uses, so we set to -1 and don't connect
    #define RESET_PIN  -1  // set to any GPIO pin # to hard-reset on begin()
    #define EOC_PIN    -1  // set to any GPIO pin to read end-of-conversion by pin
    Adafruit_MPRLS mpr = Adafruit_MPRLS(RESET_PIN, EOC_PIN);
    
    void setup() {
      Serial.begin(115200);
      Serial.println("MPRLS Simple Test");
      if (! mpr.begin(MPRLS_DEFAULT_ADDR, &Wire2)) {
        Serial.println("Failed to communicate with MPRLS sensor, check wiring?");
        while (1) {
          delay(10);
        }
      }
      Serial.println("Found MPRLS sensor");
    }
    
    
    void loop() {
      float pressure_hPa = mpr.readPressure();
      Serial.print("Pressure (hPa): "); Serial.println(pressure_hPa);
      Serial.print("Pressure (PSI): "); Serial.println(pressure_hPa / 68.947572932);
      delay(1000);
    }
    Marc

  5. #5
    Senior Member
    Join Date
    Apr 2015
    Posts
    125
    Nice catch. I just tried that but it doesn't seem to change anything. Its always been the case that, with the Adafruit library, the code would claim to "see" the board, but couldn't retrieve any data. Same as now, when I changed to using the Wire2 in the begin line.


    Output:

    MPRLS Simple Test
    Failed to communicate with MPRLS sensor, check wiring?
    Found MPRLS sensor

  6. #6
    Senior Member
    Join Date
    Feb 2017
    Posts
    299
    Might be time to put a scope on the SDA and SCL lines.

  7. #7
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,138
    Can you post a diagram of how you have it wired, including the power? And/or post a photo.

    Pete

  8. #8
    Senior Member
    Join Date
    Apr 2015
    Posts
    125
    Quote Originally Posted by gfvalvo View Post
    Might be time to put a scope on the SDA and SCL lines.
    If only. Don't have one at hand. One thing I can say for sure is that the I2C pressure sensor board works because I tested it on another generic arduino. Will post a picture tomorrow when I am at work. Thanks.

  9. #9
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,093
    The T3.6 is not 5v tolerant so be sure to power the sensor board with 3.3v or you may damage the T3.6. Both the Wire and i2c_t3 libs have an I2C scanner example sketch that you can run to see if the T3.6 detects any I2C devices. The scanner examples assume pins 18 and 19 for SDA and SCL. With power to the sensor board a voltmeter should see 3.3v on SCL and SDA since the sensor board includes pullup resistors.

  10. #10
    Senior Member
    Join Date
    Apr 2015
    Posts
    125
    Well. Getting out the meter really did help me sort this out. It's now solved. I was not jumpering tracks across the two sections of hot and ground on my long breadboard. Gah! And yes, as maitou said, no external resistors are needed for this board, it turns out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •