Help interfacing Parallax altimeter MS5607 and Arduino using SPI

Hello, I recently buy a Altimeter Module MS5607 (http://www.parallax.com/Store/Sensors/AccelerationTilt/tabid/172/ProductID/780/List/0/Default.aspx?SortField=ProductName,ProductName) and I need to interface it with my Arduino UNO with SPI protocol.

In the Parallax site there is a I2C example (http://www.parallax.com/Portals/0/Downloads/docs/prod/sens/29124-AltimeterModule-v1.0.pdf) but I need to work in SPI mode.

I'm still trying and I made this code, that when a "0" is recieved in the serial, the arduino sends the conversion and read commands to the altimeter and stores the 24 bit data of the altitude. Till now, I get the data in three bytes, but for some reason I only get 3 bytes of 11111111, 11111111, 11111111.
I post the code:

/*
Circuit:
 
 SCK: pin 13
 MISO: pin 12
 MOSI: pin 11
 SS: pin 9
 */


const int ss = 9;
#include <SPI.h>


//3 bytes to get the 24 bit adc read
byte byte1; 
byte byte2; 
byte byte3;


void setup() {
  Serial.begin(9600);


  SPI.begin();
  SPI.setBitOrder(MSBFIRST);


  pinMode(ss, OUTPUT);
}


void loop() {


  if (Serial.available() > 0) {


    byte incomingByte = Serial.read();


    switch (incomingByte){
    case '0':
      read_alt();
      break;
    } //endswitch


  }//enif


}//endloop


void read_alt()
{
  //READ Altimeter
  digitalWrite(ss, LOW);
  SPI.transfer(0x50); //ADC conversion command
  delay(1);


  // and store read data into three bytes
  byte1 = SPI.transfer(0x00);//ADC conversion read command, first 8 bits
  byte2 = SPI.transfer(0x00);//ADC conversion read command, second 8 bits
  byte3 = SPI.transfer(0x00);//ADC conversion read command, third 8 bits


  //print the 3 bytes in serial
  Serial.println(byte1, BIN);
  Serial.println(byte2, BIN);
  Serial.println(byte3, BIN);
  Serial.println();


  //release chip, signal end transfer
  digitalWrite(ss, HIGH); 
}

When using SPI, you must either use pin 10 as the chip select or, at the very least, you must configure it as an output. Otherwise the SPI library will start up in slave mode instead of master.
So if you want to keep using Pin 9 as chip select, you must also add this statement in your setup function even though you don't use pin 10:

pinMode(10, OUTPUT);

Or you can wire up CS to pin 10 and then just change "const int ss = 9;" to "const int ss = 10;"

The other thing to make sure of is that you have MOSI and MISO wired to the correct pins on the device.
In the case of the altimeter, I presume that SDA should be wired to MOSI, SDO goes to MISO.

Pete

Hello Pete, first of all thank you for answering, I check the MISO and MOSI and they are ok, and include the "pinMode(10, OUTPUT);" in my setup(); but no success on the bytes recieved, I'm still getting 11111111, 11111111, 11111111. Any ideas?
The way to recieve 24 bits in SPI is correct in the code?

On page 9 and 10 of the datasheet is the read and conversion sequence (http://www.amsys.de/sheets/amsys.de.ms5607b.pdf)

Thank you again,
Daniel

It does look like it should work.
I would put "digitalWrite(ss, HIGH);" immediately after you've read byte3, but that shouldn't make any difference. The fact you are getting all ones means that the MS5607 isn't seeing you set the chip select.
Oh, the "pinMode(10, OUTPUT);" must be before "SPI.begin();".

Other than that, I can only suggest you check the wiring again :slight_smile:

Pete

Hello Pete, I've made those changes but no success, still getting the 11111111, 11111111, 11111111 :~
Here is the new code:

/*
Circuit:
 
 SCK: pin 13
 MISO: pin 12
 MOSI: pin 11
 SS: pin 10
 */

const int ss = 10;
#include <SPI.h>

//3 bytes to get the 24 bit adc read
byte byte1; 
byte byte2; 
byte byte3;

void setup() {
  Serial.begin(9600);

  pinMode(ss, OUTPUT);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);


}

void loop() {

  if (Serial.available() > 0) {

    byte incomingByte = Serial.read();

    switch (incomingByte){
    case '0':
      read_alt();
      break;
    } //endswitch

  }//enif

}//endloop

void read_alt()
{
  //READ Altimeter
  digitalWrite(ss, LOW);
  SPI.transfer(0x50); //ADC conversion command
  delay(1);

  // and store read data into three bytes
  byte1 = SPI.transfer(0x00);//ADC conversion read command, first 8 bits
  byte2 = SPI.transfer(0x00);//ADC conversion read command, second 8 bits
  byte3 = SPI.transfer(0x00);//ADC conversion read command, third 8 bits

  //release chip, signal end transfer
  digitalWrite(ss, HIGH); 

  //print the 3 bytes in serial
  Serial.println(byte1, BIN);
  Serial.println(byte2, BIN);
  Serial.println(byte3, BIN);
  Serial.println();


}