Hi! I’m very new in the Arduino scene and I don’t know much about coding.
I’m trying to log data from MPL115A1 (baromteric pressure sensor) to my SD card but I’m really having a hard time.
The data I get is trash so most likely I guess its about the SPI thing since I can log nicely with an I2C accelerometer.
Here is the default code (w/o the calculation part) of the pressure sensor. Any comments/help/advice would be really much be appreciated!
#include <SPI.h>
#define NWS_BARO 29.92
// Pin definitions
#define MPL115A1_ENABLE_PIN 9
#define MPL115A1_SELECT_PIN 10
// Masks for MPL115A1 SPI i/o
#define MPL115A1_READ_MASK 0x80
#define MPL115A1_WRITE_MASK 0x7F
void setup() {
// initialize serial i/o
Serial.begin(9600);
// initialize SPI interface
SPI.begin();
// these are the defaults
//SPI.setDataMode(SPI_MODE0);
//SPI.setClockDivider(SPI_CLOCK_DIV4); // MPL115A1 supports up to 8MHz
//SPI.setBitOrder(MSBFIRST);
// initialize the chip select and enable pins
pinMode(MPL115A1_SELECT_PIN, OUTPUT);
pinMode(MPL115A1_ENABLE_PIN, OUTPUT);
// sleep the MPL115A1
digitalWrite(MPL115A1_ENABLE_PIN, LOW);
// set the chip select inactive, select signal is CS LOW
digitalWrite(MPL115A1_SELECT_PIN, HIGH);
}
void loop() {
float pressure_pKa = 0;
float temperature_c= 0;
long altitude_ft = 0;
// wake the MPL115A1
digitalWrite(MPL115A1_ENABLE_PIN, HIGH);
delay(20); // give the chip a few ms to wake up
pressure_pKa = calculatePressurekPa();
temperature_c = calculateTemperatureC();
altitude_ft = calculateAltitudeFt(pressure_pKa);
// put the MPL115A1 to sleep, it has this feature why not use it
// while in shutdown the part draws ~1uA
digitalWrite(MPL115A1_ENABLE_PIN, LOW);
// print table of altitude, pressures, and temperatures to console
Serial.print(altitude_ft);
Serial.print(" ft | ");
Serial.print(FT_TO_M(altitude_ft));
Serial.print(" m | ");
Serial.print(KPA_TO_INHG(pressure_pKa), 2);
Serial.print(" in Hg | ");
Serial.print(KPA_TO_MMHG(pressure_pKa), 0);
Serial.print(" mm Hg | ");
Serial.print(KPA_TO_PSIA(pressure_pKa), 2);
Serial.print(" psia | ");
//Serial.print(KPA_TO_KGCM2(pressure_pKa), 3);
///Serial.print(" kg/cm2 | ");
Serial.print(pressure_pKa, 1);
Serial.print(" kPa | ");
// At a res of -5.35 counts/°C, digits lower than 0.1°C are not significant
Serial.print(temperature_c, 1);
Serial.print(" C | ");
Serial.print(DEGC_TO_DEGF(temperature_c), 1);
Serial.print(" F\n");
// wait a few seconds before looping
delay(5000);
}
unsigned int readRegister(byte thisRegister) {
byte result = 0;
// select the MPL115A1
digitalWrite(MPL115A1_SELECT_PIN, LOW);
// send the request
SPI.transfer(thisRegister | MPL115A1_READ_MASK);
result = SPI.transfer(0x00);
// deselect the MPL115A1
digitalWrite(MPL115A1_SELECT_PIN, HIGH);
return result;
}
void writeRegister(byte thisRegister, byte thisValue) {
// select the MPL115A1
digitalWrite(MPL115A1_SELECT_PIN, LOW);
// send the request
SPI.transfer(thisRegister & MPL115A1_WRITE_MASK);
SPI.transfer(thisValue);
// deselect the MPL115A1
digitalWrite(MPL115A1_SELECT_PIN, HIGH);
}