Pages: [1] 2   Go Down
Author Topic: I2C Pressure Sensor  (Read 3593 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I have a Smartec Pressure sensor that i am trying to get values from and eventually display to a small LCD screen.
http://www.smartec-sensors.com/assets/files/pdf/Datasheets_pressure_sensors/SPD006LIhybN.pdf

I figured the first step would be to get proper data out to the serial monitor.

So far i have the following, but i believe i need some guidance on how to interpret/convert the data to something meaningful smiley

Thanks for your help !!
Pete


Code:
#include "Wire.h"
#define addrs 0x78 // I2C bus address


void setup()
{
Wire.begin();
Serial.begin(9600);
}

void loop()
{
 
   byte firstbyte;
   byte secbyte;
 
   Wire.beginTransmission(addrs);
   Wire.write(0);        // move your register pointer back to 0
   Wire.endTransmission();
   
   
   Wire.requestFrom(addrs, 2); // contents of your first two registers
   while(Wire.available())          // Check for data from slave
    {                               
      firstbyte = Wire.read();       // Read press high byte
      secbyte = Wire.read();      // Read press low byte
    }
 
  Serial.print("first byte  ");
  Serial.print(firstbyte, DEC);
 
  Serial.print("sec byte  ");
  Serial.println(secbyte, DEC);
 
  delay(500);
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12164
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The while loop is dodgy - you need to wait for each byte in turn, if data isn't available you'll skip the loop entirely and not wait.
Code:
  while (Wire.available() < 2)
  {}
  firstbyte = Wire.read();       // Read press high byte
  secbyte = Wire.read();      // Read press low byte

Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mark i appreciate the info..

I went ahead and modified the code as you suggested to read,
Code:
  while (Wire.available() <2 )
   {
    firstbyte = Wire.read();       // Read press high byte
    secbyte = Wire.read();      // Read press low byte
   }
 

It seems to me that it hangs around the While loop since the Serial Monitor doesn't show anything.

If i remove the "<2" i get data being spat out, although i don't think its correct since regardless of the pressure it shows the same numerical values ?

Ideas, Suggestions ?

Pete

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am still getting gibberish on the serial monitor, i think it has something to do with how i am declaring the stored data value/type and how long it is ?

Can someone look at the above sensor data sheet and offer some insight ?

I would really appreciate it smiley Happy New Years to Everyone !!

Pete
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anyone, some help would really be appreciated  smiley

Pete
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The way I read that datasheet is that to get the pressure byte back from the sensor, you need to send a READ request, which is a 1 after the address. You're sending 0.

Code:
   Wire.beginTransmission(addrs);
   Wire.write(0);        // move your register pointer back to 0
   Wire.endTransmission();
 
Change the second line to
Code:
   Wire.write(1);
and see what happens.
Found that in the Addressing section on page 3.
Hope this helps.
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what you made of it
Code:
while (Wire.available() <2 )
   {
    firstbyte = Wire.read();       // Read press high byte
    secbyte = Wire.read();      // Read press low byte
   }

is not what MarkT porposed
Code:
while (Wire.available() < 2)
  {}
  firstbyte = Wire.read();       // Read press high byte
  secbyte = Wire.read();      // Read press low byte

read carefully!


Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I appreciate everyone's responses !!
I have modified the code as follows and getting nothing on the Serial Monitor..

Code:
#include "Wire.h"
#define addrs 0x78 // I2C bus address


void setup()
{
Wire.begin();
Serial.begin(9600);
}

void loop()
{
 
   byte firstbyte;
   byte secbyte;
     
   Wire.beginTransmission(addrs);
   Wire.write(1);        // move your register pointer back to 0
   Wire.endTransmission();
   
   
   Wire.requestFrom(addrs, 2); // contents of your first two registers
   while (Wire.available() < 2)
   {}
   firstbyte = Wire.read();       // Read press high byte
   secbyte = Wire.read();      // Read press low byte
 
  Serial.print("first byte ");
  Serial.print(firstbyte, BIN);
 
  Serial.print("  sec byte ");
  Serial.println(secbyte, BIN);
 
  delay(500);
}

If i change the while loop to the following below, i get this on the serial monitor " first byte 11111111  sec byte 11111111", which still isn't right  ?

Code:
   Wire.requestFrom(addrs, 2); // contents of your first two registers
   while (Wire.available() )
   {}
   firstbyte = Wire.read();       // Read press high byte
   secbyte = Wire.read();      // Read press low byte

I appreciate the help guys a lot..

Pete


Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define addrs 0x78 // I2C bus address
is this right?

check it with :
Code:
// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011

#include <Wire.h>

void setup() {
  Serial.begin (115200);

  // Leonardo: wait for serial port to connect
  while (!Serial)
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
 
  Wire.begin();
  for (byte i = 1; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}
from - http://www.gammon.com.au/forum/?id=10896 - a well documented resource!
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I ran the below sketch Rob and  it gave me nothing on the serial monitor, but i ran a newer version of the scanner dated June2012 i found on the Arduino resource page and it gave me this..

Quote
I2C Scanner
Scanning...
I2C device found at address 0x78 !
done

Ideas ?

Thanks Much !!
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

broken device?
do you have other I2C devices working?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well i have access to their sensor acquisition board (see link below) and when i connect the same sensor i get meaningful data ?

http://www.smartec-sensors.com/assets/files/pdf/manuals/SMTAS02I2CN.PDF

Pete
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you try this sketch? and post its output?

Code:
#include "Wire.h"
#define addrs 0x78 // I2C bus address

void setup()
{
  Wire.begin();
  Serial.begin(9600);
}

void loop()
{
  byte b;
  
  Wire.beginTransmission(addrs);
  Wire.write(1);
  int x = Wire.endTransmission();

  Serial.println();
  Serial.println(x, DEC);
 
  while(Wire.available())
  {                                
    b = Wire.read();
    Serial.println(b, dec);
  }
 
  delay(1000);
}
« Last Edit: January 07, 2013, 01:10:05 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I ran the sketch you posted Rob and i get all Zeros on the serial monitor..

Quote
0

0

0
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The first 0  means success:   // int x = Wire.endTransmission(); so that part seems to work, however it does not give the 2 bytes.

new sketch, added some comments and missing request form

Code:
#include "Wire.h"
#define addrs 0x78 // I2C bus address

void setup()
{
  Wire.begin();
  Serial.begin(9600);
}

void loop()
{
  byte b;
 
  Wire.beginTransmission(addrs);
  Wire.write(1);
  int x = Wire.endTransmission();

  Serial.print("endTransmission: ");
  Serial.println(x, DEC);
 
  Wire.requestFrom(addrs, 2); // might be useful to request some bytes..
  while (Wire.available() < 2);

  Serial.print("byte 1: ");
  b = Wire.read();
  Serial.println(b, DEC);
  Serial.print("byte 2: ");
  b = Wire.read();
  Serial.println(b, DEC);
  Serial.println();

  delay(1000);
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1] 2   Go Up
Jump to: