MS5540C Drucksensor

Hallo,

ich habe ein problem :frowning:

mein aufbau:

arduino uno

lcd 20x4

und ein MS5540C Water Depth Sensor

mein problem ist das ich eine falsche anzeige habe.

Temparatur zeitgt -405.00°C
und 8700 mbar sollte ca 11mbar sein.

wass mache ich falsch?

ich bitte um Hilfe

lg

m4xx

sketch_drucksensor_6.ino (5.42 KB)

Hallo,
Du könntest z.B. die 5397 Zeichen in CodeTag´s (Editor </>) setzen.
Gruß und Spaß
Andreas

/*-----( Import needed libraries )-----*/
#include <Wire.h>  // Comes with Arduino IDE
// Get the LCD I2C Library here: 
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>

/*-----( Declare Constants )-----*/
/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 16 chars 2 line display
// A FEW use address 0x3F
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address


#include <SPI.h>
 int clock = 9;
void resetsensor() //this function keeps the sketch a little shorter
{
 SPI.setDataMode(SPI_MODE0);
 SPI.transfer(0x15);
 SPI.transfer(0x55);
 SPI.transfer(0x40);
}
void setup() {
 lcd.begin(20,4); //Im Setup wird der LCD gestartet (anders als beim einfachen LCD Modul ohne 16,2 in den Klammern denn das wurde vorher festgelegt
 Serial.begin(9600);
 SPI.begin(); //see SPI library details on arduino.cc for details
  SPI.setBitOrder(MSBFIRST);
 SPI.setClockDivider(SPI_CLOCK_DIV32); //divide 16 MHz to communicate on 500 kHz
 pinMode(clock, OUTPUT);
 delay(100);
}
void loop()
{
 TCCR1B = (TCCR1B & 0xF8) | 1 ; //generates the MCKL signal
 analogWrite (clock, 128) ;
 resetsensor();//resets the sensor - caution: afterwards mode = SPI_MODE0!
 //Calibration word 1
 unsigned int word1 = 0;
 unsigned int word11 = 0;
 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
 word1 = SPI.transfer(0x00); //send dummy byte to read first byte of word
 word1 = word1 << 8; //shift returned byte
 word11 = SPI.transfer(0x00); //send dummy byte to read second byte of word
 word1 = word1 | word11; //combine first and second byte of word
 resetsensor();//resets the sensor
 //Calibration word 2; see comments on calibration word 1
 unsigned int word2 = 0;
 byte word22 = 0;
 SPI.transfer(0x1D);
 SPI.transfer(0x60);
 SPI.setDataMode(SPI_MODE1);
 word2 = SPI.transfer(0x00);
 word2 = word2 <<8;
 word22 = SPI.transfer(0x00);
 word2 = word2 | word22;
 resetsensor();//resets the sensor
 //Calibration word 3; see comments on calibration word 1
 unsigned int word3 = 0;
 byte word33 = 0;
 SPI.transfer(0x1D);
 SPI.transfer(0x90);
 SPI.setDataMode(SPI_MODE1);
 word3 = SPI.transfer(0x00);
 word3 = word3 <<8;
 word33 = SPI.transfer(0x00);
 word3 = word3 | word33;
 resetsensor();//resets the sensor
 //Calibration word 4; see comments on calibration word 1
 unsigned int word4 = 0;
 byte word44 = 0;
 SPI.transfer(0x1D);
 SPI.transfer(0xA0);
 SPI.setDataMode(SPI_MODE1);
 word4 = SPI.transfer(0x00);
 word4 = word4 <<8;
 word44 = SPI.transfer(0x00);
 word4 = word4 | word44;

 long c1 = word1 << 1;
 long c2 = ((word3 & 0x3F) >> 6) | ((word4 & 0x3F));
 long c3 = (word4 << 6) ;
 long c4 = (word3 << 6);
 long c5 = (word2 << 6) | ((word1 & 0x1) >> 10);
 long c6 = word2 & 0x3F;
 resetsensor();//resets the sensor
 //Temperature:
 unsigned int tempMSB = 0; //first byte of value
 unsigned int tempLSB = 0; //last byte of value
 unsigned int D2 = 0;
 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
 resetsensor();//resets the sensor
 //Pressure:
 unsigned int presMSB = 0; //first byte of value
 unsigned int presLSB =0; //last byte of value
 unsigned int D1 = 0;
 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;

 const long UT1 = (c5 * 8) + 20224;
 const long dT =(D2 - UT1);
 const long TEMP = 200 + ((dT * (c6 + 50))/1024);
 const long OFF = (c2*4) + (((c4 - 512) * dT)/4096);
 const long SENS = c1 + ((c3 * dT)/1024) + 24576;
 
 long PCOMP = ((((SENS * (D1 - 7168))/16384)- OFF)/32)+250;
 float TEMPREAL = TEMP/10;
  Serial.print("pressure = ");
 Serial.print(PCOMP);
 Serial.println(" mbar");
 const long dT2 = dT - ((dT >> 7 * dT >> 7) >> 3);
 const float TEMPCOMP = (200 + (dT2*(c6+100) >>11))/10;
 Serial.print("temperature = ");
 Serial.print(TEMPCOMP);
 Serial.println(" °C");
 Serial.println("************************************");
lcd.setCursor(0,0);
lcd.print("Druck");
lcd.setCursor(0,1);  // lcd.setCursor um Zeichen und Zeile anzugeben
lcd.print(PCOMP); // lcd.print um etwas auf dem Display anzeigen zu lassen.
lcd.print(" mbar");
lcd.setCursor(0,2);
lcd.print("Temp");
lcd.setCursor(0,3 );  // lcd.setCursor um Zeichen und Zeile anzugeben
lcd.print(TEMPCOMP); // lcd.print um etwas auf dem Display anzeigen zu lassen.
lcd.print(" C");

 delay(1000);
 }

Hallo,
hast Du den auch einmal probiert.
Lief der vor der LCD ausgabe?
Gruß und Spaß
Andreas

Hallo Andreas habe ich noch nicht probiert

Und ohne LCD hat es auch nicht funktioniert.

Ich habe das sketch eingespielt

und das Ergebnis:

Temperature raw =28570
Pressure raw =16872
Real Temperature in °C=54.00
Compensated pressure in mbar =214
2nd order compensated temperature in °C =54.00
Calibration word 1 =50335
Calibration word 2 =1177
Calibration word 3 =32091
Calibration word 4 =48282
6291
7186
501
377
1627
26

kommt schon besser hin aber 54°c hat es auch nicht :slight_smile:

Hallo,
wenn der RAW-Werte liefert, dann müssen die umgerechnet werden. Aber frage mich mal...
Hier gibt es das Datenblatt dazu.
Da gibt es jede Menge Info zu dem Ding.
Hier kannst Du Dich auch durchwühlen.
Gruß und Glück
Andreas