I'm working programming a Leonardo board and I have a MCP9801 attached to a shield I have been given for this project.
#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);
}
I know what register the ambient temperature is on on the sensor but where in this code do I need to use this information
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?
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?
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.
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.
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.
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
My completed code if it's of interest / use to other people looking at this topic for help.
// 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);
}