Hello everyone. I'm working on a project that incorporates a Measurement Specialties Barometric Sensor, MS5535C (http://www.meas-spec.com/product/t_product.aspx?id=5029).
There is a similar sensor, the MS5534C, that has some Arduino code which I've based my own sketch off of (http://blog.ulrichard.ch/?p=23). Unfortunately, I'm getting garbage from the outputs of sensor, leading me to believe that I'm putting garbage in. I'm not exactly sure if I'm addressing the MS5535C correctly, and as it's my first time with a sensor of this sort, I'm turning to the forums for help.
The sensor data sheet (http://www.meas-spec.com/downloads/MS5535C.pdf) says:
The MS5535C communicates with microprocessors and other digital systems via a 3-wire synchronous serial interface as shown in Fig. 1.
My sketch looks like this, and it complies, loads and runs without trouble:
/*
/*
Reading the pressure from an intersema barometric pressure sensor
By Richard Ulrich
*/
#include <Arduino.h>
// The circuit:
// * Intersema MS5534C connected to the following pins:.
static const uint8_t pinBaroSCLK = 8;
static const uint8_t pinBaroDOUT = 1; // in on the arduino
static const uint8_t pinBaroDIN = 0; // out on the arduino
static const uint8_t pinBaroMCLK = 9; // the tone function used to generate the 34kHz signal interferes with pin 3 and 11, so it's best to use it directly
unsigned int coefficients[6];
uint32_t temperature;
uint32_t pressure;
void setup()
{
// set the pin directions
pinMode(pinBaroMCLK, OUTPUT);
pinMode(pinBaroSCLK, OUTPUT);
pinMode(pinBaroDIN, OUTPUT);
pinMode(pinBaroDOUT, INPUT);
Serial.begin(9600);
// generate approx 34kHz square for pinBaroMCLK
tone(pinBaroMCLK, 31250);
}
void loop()
{
ReadCoefficients();
for(size_t i=0; i<6; ++i)
{
Serial.print("Coefficient ");
Serial.print(i + 1, DEC);
Serial.print(" : ");
Serial.println(coefficients[i], DEC);
}
temperature = ReadTemperature();
pressure = ReadPressure();
ConvertPressureTemperature(pressure, temperature);
Serial.print("Pressure: ");
Serial.println(pressure, DEC);
Serial.print("Temperature: ");
Serial.println(temperature, DEC);
Serial.println(" ");
delay(2000);
}
/*
* Interface to Intersema Barometric pressure sensor.
* For the moment, only the following sensors are supported:
* MS5534C though manual protocol
* MS5607B through I2C
* Others might be added in the future.
*
* Created 14 Jan 2010
* By Richard Ulrich
* Inspired by the work of Hari Nair
*
* Added support for the MS5607B I2C : 7 Jan 2011
*/
void ResetSensor()
{
SendCommand(0x155540, 21); // 1010101010101010 + 00000
}
void ReadCoefficients(void)
{
ResetSensor();
unsigned int wa = ReadCoefficient(0x15);
unsigned int wb = ReadCoefficient(0x16);
coefficients[0] = (unsigned int)((wa >> 1) & (unsigned int)0x7FFF);
coefficients[4] = (unsigned int)(((wa & 0x1) << 10) | ((wb >> 6) & (unsigned int)0x3FF));
coefficients[5] = (unsigned int)(wb & 0x3F);
wa = ReadCoefficient(0x19);
wb = ReadCoefficient(0x1A);
coefficients[3] = (unsigned int)((wa >> 6) & 0x3FF);
coefficients[1] = (unsigned int)(((wa & 0x3F) << 6) | (wb & 0x3F));
coefficients[2] = (unsigned int)((wb >> 6) & 0x3FF);
}
size_t ReadCoefficient(unsigned char addr)
{
// 111 + 6bit coeff addr + 000 + 1clk(send0)
unsigned long cmd = (unsigned long)0x1C00 | (((unsigned long)addr) << 4);
SendCommand(cmd,13);
return ReadWord();
}
void SendCommand(unsigned long cmd, size_t nbits)
{
while(nbits--)
{
if(cmd & (unsigned long)(1 << nbits))
digitalWrite(pinBaroDIN, HIGH);
else
digitalWrite(pinBaroDIN, LOW);
digitalWrite(pinBaroSCLK, HIGH);
digitalWrite(pinBaroSCLK, LOW);
}
}
unsigned int ReadWord(void)
{
unsigned int w;
unsigned int clk = 16;
w = 0;
while(clk--)
{
digitalWrite(pinBaroSCLK, HIGH);
digitalWrite(pinBaroSCLK, LOW);
w |= (digitalRead(pinBaroDOUT) << clk);
}
digitalWrite(pinBaroSCLK, HIGH);
digitalWrite(pinBaroSCLK, LOW);
return w;
}
uint32_t ReadTemperature(void)
{
// 111 + 1001 + 000 + 2clks(send 0)
ResetSensor();
SendCommand(0xF20, 12);
while(digitalRead(pinBaroDOUT));
const unsigned int temperature = ReadWord();
return temperature;
}
uint32_t ReadPressure(void)
{
// 111 + 1010 + 000 + 2clks(send 0)
ResetSensor();
SendCommand(0xF40, 12);
while(digitalRead(pinBaroDOUT));
const unsigned int pressure = ReadWord(); // read pressure
return pressure;
}
void ConvertPressureTemperature(uint32_t pressure, uint32_t temperature)
{
const long UT1 = (coefficients[4] << 3) + 20224;
const long dT = temperature - UT1;
const long TEMP = 200 + ((dT * (coefficients[5] + 50)) >> 10);
const long OFF = (coefficients[1] <<2) + (((coefficients[3] -512) * dT) >> 12);
const long SENS = coefficients[0] + ((coefficients[2] * dT) >> 10) + 24576;
const long X = ((SENS* ((long)pressure - 7168)) >> 14) - OFF;
pressure = ((X * 10) >> 5) + 2500;
temperature = TEMP;
long T2 = 0, P2 = 0;
if(TEMP < 200)
{
T2 = (11 * (coefficients[5] + 24) * (200 - TEMP) * (200 - TEMP)) >> 20;
P2 = (3 * T2 * (pressure - 3500)) >> 14;
pressure = pressure - P2;
temperature = temperature - T2;
}
}
The output that I'm getting from the serial monitor looks like this:
Coefficient 1 : 32767
Coefficient 2 : 4095
Coefficient 3 : 1023
Coefficient 4 : 1023
Coefficient 5 : 2047
Coefficient 6 : 63
Pressure: 8161
Temperature: 16256
Coefficient 1 : 32767
Coefficient 2 : 4095
Coefficient 3 : 1023
Coefficient 4 : 1023
Coefficient 5 : 2047
Coefficient 6 : 63
Pressure: 8161
Temperature: 16320
Coefficient 1 : 32767
Coefficient 2 : 4095
Coefficient 3 : 1023
Coefficient 4 : 1023
Coefficient 5 : 2047
Coefficient 6 : 63
Pressure: 8161
Temperature: 16256
My suspicion is that I've done something wrong with the baud rate, and I'm getting garbage from the sensor, but I have no way of knowing for sure. Perhaps someone with expertise could weigh in. Thanks in advance!