Reading MS5535C using Teensy 3.1 problem

Status
Not open for further replies.
New library arrived from Adafruit, but for strange reasons can not use it with teensyduino... it does not recognise it. Tryed everything but no better.
The name of folder in library directory is correct. Names of files in the folder too..l. What else should I check?
 
Here, I've modified the files for you. Try this copy.

Are these published somewhere? If this works, maybe we could get whereever they're published to update?

Hi, I wanted to use this library you posted (MS55xx), I did compile the code you provided,
loaded into teensy with no problem but readings are wrong,
temp is ok but pressure shows some 20000 above readings instead od 0 or close to 0(zero)
on the beginning for water pressure.
I checked the original library on the Uno with two sensors, and it works ok.
Could you tell what have to be fixed to make it work with correct readings
on Teensy?
 
Last edited:
Hi, sorry for late answer, can you post you scetcher here so hopefuly I can see what makes problems.
Did you try to run the example in the library?
I get accurate readings.
 
Hi, yes I did use example from library,
here is a code I've used

Code:
#include <SPI.h>
#include "MS55xx.h"


MS55xx::MS55xx() {
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV32); //divide 16 MHz to communicate on 500 kHz
pinMode(9, OUTPUT);
getCalibrationWord();
}


void MS55xx::resetSensor() {
SPI.setDataMode(SPI_MODE0);
SPI.transfer(0x15);
SPI.transfer(0x55);
SPI.transfer(0x40);
}


void MS55xx::update() {
#ifdef __AVR__
TCCR1B = (TCCR1B & 0xF8) | 1 ; //generates the MCKL signal
#else
analogWriteFrequency(9, 31250);
#endif
analogWrite(9, 128);


//Pressure:
resetSensor();
SPI.transfer(0x0F); //send first byte of command to get pressure value
SPI.transfer(0x40); //send second byte of command to get pressure value
delay(35); //wait for conversion end
SPI.setDataMode(SPI_MODE1); //change mode in order to listen
presMSB = SPI.transfer(0x00); //send dummy byte to read first byte of value
presMSB = presMSB << 8; //shift first byte
presLSB = SPI.transfer(0x00); //send dummy byte to read second byte of value
D1 = presMSB | presLSB; //combine first and second byte of value

//Temperature
resetSensor();
SPI.transfer(0x0F); //send first byte of command to get temperature value
SPI.transfer(0x20); //send second byte of command to get temperature value
delay(35); //wait for conversion end
SPI.setDataMode(SPI_MODE1); //change mode in order to listen
tempMSB = SPI.transfer(0x00); //send dummy byte to read first byte of value
tempMSB = tempMSB << 8; //shift first byte
tempLSB = SPI.transfer(0x00); //send dummy byte to read second byte of value
D2 = tempMSB | tempLSB; //combine first and second byte of value


//calculate temp according to datasheet
dT = D2 - UT1;
if (dT < 0) {
dT2 = dT - (dT/128*dT/128)/2;
}
else {
dT2 = dT - (dT/128*dT/128)/8;
}


TEMP = (200 + dT*(C6+100)/pow(2,11))/10;
TEMPC = (200 + dT2*(C6+100)/pow(2,11))/10;


//calculate pressure according to datasheet
OFF = C2 + ((C4-250)*dT)/pow(2,12) + 10000;
SENS = C1/2 + ((C3+200)*dT)/pow(2,13) + 3000;
if (SensorType=='MS5535'){
P = (SENS * (D1-OFF))/pow(2,12) + 1000; //change neaded to calculate for MS5535C
}
else {
P = (SENS * (D1-OFF))/pow(2,11) + 1000; //normal calculation for MS5541
}
}


void MS55xx::setPressureSensorType(char PressureSensorType){
char SensorType=PressureSensorType;
}



float MS55xx::getPressureMBar() {
return P;
}


float MS55xx::getPressureBar() {
return P/1000.0;
}




float MS55xx::getTemperatureC() {
return TEMP;
}


float MS55xx::get2ndTemperatureC() {
return TEMPC;
}


float MS55xx::getTemperatureF() {
return (TEMP*1.8)+32.0;
}


float MS55xx::get2ndTemperatureF() {
return (TEMPC*1.8)+32.0;
}


void MS55xx::getCalibrationWord() {
#ifdef __AVR__
TCCR1B = (TCCR1B & 0xF8) | 1 ; //generates the MCKL signal
#else
analogWriteFrequency(9, 31250);
#endif
analogWrite(9, 128) ;

resetSensor();
SPI.transfer(0x1D); //send first byte of command to get calibration word 1
SPI.transfer(0x50); //send second byte of command to get calibration word 1
SPI.setDataMode(SPI_MODE1); //change mode in order to listen
result1 = SPI.transfer(0x00); //send dummy byte to read first byte of word
result1 = result1 << 8; //shift returned byte
inbyte1 = SPI.transfer(0x00); //send dummy byte to read second byte of word
result1 = result1 | inbyte1; //combine first and second byte of word


resetSensor();
SPI.transfer(0x1D);
SPI.transfer(0x60);
SPI.setDataMode(SPI_MODE1);
result2 = SPI.transfer(0x00);
result2 = result2 <<8;
inbyte2 = SPI.transfer(0x00);
result2 = result2 | inbyte2;


resetSensor();
SPI.transfer(0x1D);
SPI.transfer(0x90);
SPI.setDataMode(SPI_MODE1);
result3 = SPI.transfer(0x00);
result3 = result3 <<8;
inbyte3 = SPI.transfer(0x00);
result3 = result3 | inbyte3;


resetSensor();
SPI.transfer(0x1D);
SPI.transfer(0xA0);
SPI.setDataMode(SPI_MODE1);
result4 = SPI.transfer(0x00);
result4 = result4 <<8;
inbyte4 = SPI.transfer(0x00);
result4 = result4 | inbyte4;


C1 = result1 >> 3 & 0x1FFF;
C2 = ((result1 & 0x07) << 10) | ((result2 >> 6) & 0x03FF);
C3 = (result3 >> 6) & 0x03FF;
C4 = (result4 >> 7) & 0x07FF;
C5 = ((result2 & 0x003F) << 6) | (result3 & 0x003F);
C6 = result4 & 0x007F;
UT1 = 8*C5 + 10000;
}
 
Last edited:
here is the sketch
Code:
#include "MS55xx.h"
#include <SPI.h>

/*********************************
* Please respect pin wiring:
* MOSI: pin 11 arduino or pin 7 on sensor
* MISO: pin 12 arduino or pin 8 on sensor
* SCK: pin 13 arduino or pin 1 on sensor
* MCLK: pin 9 arduino or pin 6 on sensor
*********************************/

//creating the sensor
MS55xx PressSensor;
float airPressure;
float waterPressure;

void setup() {
    Serial.begin(9600); 
    PressSensor.setPressureSensorType('MS5535');
    //start arduino before diving
    //otherwise a 1.0 bar is used as surface pressure
    PressSensor.update();
    airPressure = PressSensor.getPressureBar();
    if (airPressure > 1.2) {
      airPressure = 1.0;
    }
}

void loop() {
  //Before getting the values you need to update the sensor
  PressSensor.update();
  
  //get pressure
  waterPressure = PressSensor.getPressureBar() - airPressure;
  Serial.print("Deep (meters): ");
  Serial.println(abs(waterPressure)*10.0,1);
   
  //get 2nd temperature --> see datasheet for details
  Serial.print("Temperatre 2nd (celsius): ");
  Serial.println(PressSensor.get2ndTemperatureC());
  
  Serial.println();
  
  delay(2000);
}

it generates me waterPressure above 20000 on the start,
same code on Uno works well
 
Last edited:
Hi,

there is a problem in library that I discovered later and did not update here. Honestly I did take this library much further, but for other purposes.
The ptoblem making is the calculation of pressure here:
Code:
if (SensorType==5535){
	P = (SENS * (D1-OFF))/pow(2,12) + 1000; //change neaded to calculate for MS5535C
	}
	else {
	P = (SENS * (D1-OFF))/pow(2,11) + 1000; //normal calculation for MS5541
	}
just change it to this and you will get correct results.:

Code:
if (SensorType==5535) {
      P = (SENS * (D1-OFF));
      P = P/pow(2,12) + 1000; //calculate for MS5535C
  }
  else {
      P = (SENS * (D1-OFF));
      P = P/pow(2,11) + 1000; //calculate for MS5541
  }

Seem calculation of P is having prblems when not split in two parts.

Let me know.

Igor P
 
Last edited:
Status
Not open for further replies.
Back
Top