Home › Forums › Forums › Technical Support for BerryIMU › Altitude & Temp
Tagged: BerryIMU LSM9DS1
- This topic has 8 replies, 2 voices, and was last updated 4 years, 7 months ago by jgreen.
- AuthorPosts
- April 23, 2019 at 11:15 am #7922jgreenParticipant
Where can I find sample code on the altitude and temp for an arduino nano?
I have guessed at some code for the temp but it is not working. This is always returning a temp of 77 degrees
float readTemp(){
uint8_t block[2];
//readFrom(LSM9DS1_MAG_ADDRESS, 0x80 | LSM9DS0_OUT_TEMP_L_XM, 2, block);
readFrom(LSM9DS1_MAG_ADDRESS, 0x80 | LSM9DS0_OUT_TEMP_H_XM, 2, block);
int16_t temp = (int16_t)(block[0] | block[1] << 8);
float temperature_c = (float)temp / 8.0 + 25;
float temperature_f = temperature_c * 1.8 + 32;
return temperature_f;
}April 23, 2019 at 11:26 am #7924Mark WilliamsKeymasterWhat version of BerryIMU do you have? 1 or 2?
Mark --OzzMaker.com --
April 23, 2019 at 11:52 am #7925jgreenParticipant2.3 When I run the gyro demo it states LSM9DS1 found
April 23, 2019 at 12:49 pm #7926Mark WilliamsKeymasterokay… try using this LSM9DS1_OUT_TEMP_H here
readFrom(LSM9DS1_MAG_ADDRESS, 0x80 | LSM9DS1_OUT_TEMP_H_XM, 2, block);
Mark --OzzMaker.com --
April 23, 2019 at 1:00 pm #7927jgreenParticipant‘LSM9DS1_OUT_TEMP_H_XM’ was not declared in this scope
I dont see the entry in LSM9DS1.h. File from https://github.com/ozzmaker/BerryIMU/blob/master/arduino-BerryIMU/LSM9DS1.h
April 23, 2019 at 1:02 pm #7928Mark WilliamsKeymastersorry… a copy and paste error. this is the correct one
LSM9DS1_OUT_TEMP_HMark --OzzMaker.com --
April 23, 2019 at 1:28 pm #7929jgreenParticipantthe function returns -3310
float readTemp(){
uint8_t block[2];
readFrom(LSM9DS1_MAG_ADDRESS, 0x80 | LSM9DS1_OUT_TEMP_H, 2, block);
int16_t temp = (int16_t)(block[0] | block[1] << 8);
float temperature_c = (float)temp / 8.0 + 25;
float temperature_f = temperature_c * 1.8 + 32;
return temperature_f;
}April 23, 2019 at 1:57 pm #7930Mark WilliamsKeymasterhave you enabled the accelerometer at the start of your code?
you could try and get the temperature from the BMP280 google “read temp bmp280” and you will find plenty of information.
We have python code
https://github.com/ozzmaker/BerryIMU/tree/master/python-BMP280-temperature-pressure
Mark --OzzMaker.com --
April 23, 2019 at 2:06 pm #7931jgreenParticipantyes,
void setup() {
// join i2c bus (address optional for master)
Serial.begin(115200); // start serial for outputdetectIMU();
enableIMU();delay(1000);
}void enableIMU(){
if (LSM9DS0){//For BerryIMUv1
//Enable accelerometer
writeTo(LSM9DS0_ACC_ADDRESS,LSM9DS0_CTRL_REG1_XM, 0b01100111); // z,y,x axis enabled, continuos update, 100Hz data rate
writeTo(LSM9DS0_ACC_ADDRESS,LSM9DS0_CTRL_REG2_XM, 0b00100000); // +/- 16G full scale//Enable the magnetometer
writeTo(LSM9DS0_MAG_ADDRESS,LSM9DS0_CTRL_REG5_XM, 0b11110000); // Temp enable, M data rate = 50Hz
writeTo(LSM9DS0_MAG_ADDRESS,LSM9DS0_CTRL_REG6_XM, 0b01100000); // +/-12gauss
writeTo(LSM9DS0_MAG_ADDRESS,LSM9DS0_CTRL_REG7_XM, 0b00000000); // Continuous-conversion mode// Enable Gyro
writeTo(LSM9DS0_GYR_ADDRESS, LSM9DS0_CTRL_REG1_G, 0b00001111); // Normal power mode, all axes enabled
writeTo(LSM9DS0_GYR_ADDRESS, LSM9DS0_CTRL_REG4_G, 0b00110000); // Continuos update, 2000 dps full scale
}
else if(LSM9DS1){//For BerryIMUv2
// Enable the gyroscope
writeTo(LSM9DS1_GYR_ADDRESS,LSM9DS1_CTRL_REG4,0b00111000); // z, y, x axis enabled for gyro
writeTo(LSM9DS1_GYR_ADDRESS,LSM9DS1_CTRL_REG1_G,0b10111000); // Gyro ODR = 476Hz, 2000 dps
writeTo(LSM9DS1_GYR_ADDRESS,LSM9DS1_ORIENT_CFG_G,0b10111000); // Swap orientation// Enable the accelerometer
writeTo(LSM9DS1_ACC_ADDRESS,LSM9DS1_CTRL_REG5_XL,0b00111000); // z, y, x axis enabled for accelerometer
writeTo(LSM9DS1_ACC_ADDRESS,LSM9DS1_CTRL_REG6_XL,0b00101000); // +/- 16g//Enable the magnetometer
writeTo(LSM9DS1_MAG_ADDRESS,LSM9DS1_CTRL_REG1_M, 0b10011100); // Temp compensation enabled,Low power mode mode,80Hz ODR
writeTo(LSM9DS1_MAG_ADDRESS,LSM9DS1_CTRL_REG2_M, 0b01000000); // +/-12gauss
writeTo(LSM9DS1_MAG_ADDRESS,LSM9DS1_CTRL_REG3_M, 0b00000000); // continuos update
writeTo(LSM9DS1_MAG_ADDRESS,LSM9DS1_CTRL_REG4_M, 0b00000000); // lower power mode for Z axis}
} - AuthorPosts
- You must be logged in to reply to this topic.