Pages: [1]   Go Down
Author Topic: Using a temperature sensor I2C. Where do registers come into code?  (Read 1648 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I'm working programming a Leonardo board and I have a MCP9801 attached to a shield I have been given for this project.

Code:
#include <Wire.h>
int I2C_address = 0b1001000 // define address of bus

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
 
   Wire.beginTransmission(I2C_address); // transmit to I2C
    Wire.write(0b00000000);              // sends one byte 
  Wire.endTransmission();    // stop transmitting
 
 
  Wire.requestFrom(I2C_address, 2);    // request 2 bytes from address.
  while(Wire.available())    // slave may send less than requested
  {
    int temp1 = Wire.read(); // receive a byte as character
    int temp2 = Wire.read(); // receive a byte as character
   
    Serial.println(temp);         // print the character
  }

  delay(500);
}


1) I know what register the ambient temperature is on on the sensor  but where in this code do I need to use this information
2) My lecturer talked about taking two bytes from the sensor and bit shifting to get 9 bit from the sensor, Why would I do that? and indeed what's the best way?
3) Finally there's some simple maths to perform on the output to convert it from Raw data to a temperature as defined in the data sheet for the temp sensor, What would be the best way to do this?

Thank you for your help, I hope this is clear
Logged

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

First step, get the data sheet for the hardware, it'll list the location of each register.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


1) I know what register the ambient temperature is on on the sensor  but where in this code do I need to use this information
That all depends on what you want to do with the data.
Quote
2) My lecturer talked about taking two bytes from the sensor and bit shifting to get 9 bit from the sensor, Why would I do that? and indeed what's the best way?
You'd do that because the sensor provides 9 bits of data, and I²C is an 8 bit protocol.  The best way would be to use bit shifting - as you have already said.
Quote
3) Finally there's some simple maths to perform on the output to convert it from Raw data to a temperature as defined in the data sheet for the temp sensor, What would be the best way to do this?
By reading the formula from the data sheet and typing it into the program, using relevant variables.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

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

I have the data sheet I know the register pointer I need, I'm just not clear on where this goes into the code. I want to read from the register for ambient temperature and the end result at this stage only needs to be printing the temp in degrees celsisu on the serial monitor.

My second question may not have been clear enough:
Do you have any guidance on using bit shift, is the bit in the examples in the IDE entitled strings relevant?

Should my order be something like this:
I2C transmits two bytes

then I do the bitshifting ( I want the first byte and the first bit of the second byte?)

the the maths to get it from raw data into celsius

then I print?
Logged

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

put the regs here
    int temp1 = Wire.read(0x00); // receive a byte as character
    int temp2 = Wire.read(0x00); // receive a byte as character
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 625
Posts: 34099
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do you have any guidance on using bit shift,
http://arduino.cc/en/Reference/Bitshift

Quote
is the bit in the examples in the IDE entitled strings relevant?
No.
Logged

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

The other dude pretty much explained bitshifting, here is the ref page
http://arduino.cc/en/Reference/Bitshift

the temp is received in raw data, you need to convert the raw value into Celsius

writing some variables that hold the temperature value might be helpful
Logged

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

Ok then two more things to clarify from your repsonses, but I think it's becoming clear.

Where you've put 0x00 I'll put 0000000 (the register pointer for ambient temp register?)

I want all of the first byte and to "add" the first bit of the second byte on the end?

What's the function called to join the two pieces of data after I've done the bit shifting?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 625
Posts: 34099
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Where you've put 0x00 I'll put 0000000 (the register pointer for ambient temp register?)
Yes.
Quote
I want all of the first byte and to "add" the first bit of the second byte on the end?
Yes.

Quote
What's the function called to join the two pieces of data after I've done the bit shifting?
The full name is the inclusive OR function the operation symbol is a single | ( that is not an i or an l )
Logged

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

Thanks very much I'll post back with my finished code later on.

I appreciate your patience with a useless mechanical engineer currently getting lost in the world of programming
Logged

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

My completed code if it's of interest / use to other people looking at this topic for help.
Code:
// This code reads a temperature sensor using an I2C bus. Presents data in serial monitor.
// Uses wire library
// Uses arduino, I2C bus and temperature sensor
// RICHARD NASH 13/12/12

#include <Wire.h>
int I2C_address = 0b1001000; // define I2C Address


void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
 
   Wire.beginTransmission(I2C_address); // transmit to I2C
    Wire.write(0b00000000);              // sends one byte.   
  Wire.endTransmission();    // stop transmitting
 
 
  Wire.requestFrom(I2C_address, 2);    // request 2 bytes from address. Because sensor produces 9 bit
  while(Wire.available())    // slave may send less than requested
  {
    int temp1 = Wire.read(); // receive a byte as integer
    int temp2 = Wire.read(); // receive a byte as integer Can change resolution of by chaning 2nd and 3rd bit of the register pointers.
   
    int a = temp1 <<1; // bitshift 1 to the left to allow for 9bit data from temp sensor
    int b = temp2 >>7; //bitshift 7 to the right to allow for 9 bit data from temp sensor.
   
    int tempRaw = a|b;  // define int rawtemp as the combination of integer a and b. We now have 9 bits of data.
   
    int tempC = tempRaw * 0.5 ;// 0.5 = 2^-1 since T = C x 2^n where c is the binary code value, and n is dependent on resolution used. tempC is temperature in celsius
 
   
    Serial.println(tempC);         // print the character
  }
  delay(500);
}
Logged

Offline Offline
Edison Member
*
Karma: 29
Posts: 2449
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Waymon - Look at your breadboard, see the 2 ceramic caps and your crystal, GND is not connected!

only 1 side of the crystal is connected to GND via the caps, the other is not..  - fix that and it should upload fine.
Logged

Pages: [1]   Go Up
Jump to: