Modifying code to run multiple MLX90333 sensors with Arduino Micro

Hey guys, I am working on a project in which I need my Arduino Mirco to talk to and receive data from multiple MLX90333 Sensors. Below is my working code for sending and receiving data between one sensor and my arduino.

#include <SPI.h>
#include <stdint.h>

//*******Arduino MICRO SPI pins********
#define SS 17   //brown
#define SCK 15  //blue
#define MOSI 16  //SDI MOSI green
#define MISO 14  //SDO MISO yellow

byte dataBuffer[8];

void setup() {
  Serial.begin(9600); 
  SPI.begin;
  pinMode(SS, OUTPUT);
  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);
}

void loop() {
  delay(20); //MLX90333 startup takes 16ms
  SPI.beginTransaction(SPISettings(160000, MSBFIRST, SPI_MODE1)); //320000 is about max
  delay(5);
  int j;
  for (j=0; j<10; j++){
    digitalWrite(SS, LOW);
    delay(20);  //Short delay necessary here
    int i;
    for (i=0; i<8; i++){ 
      dataBuffer[i] = SPI.transfer(255);  // Must transfer 1's, 0's don't work
      }
    digitalWrite(SS, HIGH);
    
    SPI.endTransaction();
    Serial.print(dataBuffer[2],8); //Print 3rd byte, MSB for Alpha
    Serial.print(" ,");
    Serial.print(dataBuffer[4],8); //Print 5th byte, MSB for Beta
    Serial.println(" ,");
    }
  }

The circuit I am using is the recommended Serial Protocol ckt schematic for the MLX90333 (8-soic packaging.) The link below has the MLX90333 data sheet; page 42 holds the schematic I used.

https://www.melexis.com/-/media/files/documents/.../mlx90333-datasheet-melexis.pdf

The method I have chose to connect multiple slave is pictured below.

I have tried to connecting two sensors using the same serial protocol in combination with the multiple slave select pin method I show above. Then I tried to modify my code to talk to both, but I was unable to get it to compile although I thought it seemed trivial after getting one sensor’s code to work.

So my questions are:

  1. Is there anything wrong with what I have so far?
  2. How do I modify my code to handle the multiple sensors?
  3. What pins can be used as Slave Select pins other than the already designated SS/ pin?

Any information, leads, or hand holding would be much appreciated!

Thanks!

What is the point today showing your working code? You don't need help with that.

That code only works with one sensors. How do I modify it to work with multiple sensors.

Add more select lines.

Which pins on arduino Micro can be slave select?

Do I have to define them differently in my code?

For example I have the designated slave select defined like:

#define SS 17

Would I define my next one, say pin 2 assuming it would work as a SS/, like:

#define SS1 2

Yes, that would be one approach. You could then put those values in an array.

So something like this?

//*******Arduino MICRO SPI pins********
#define SS 17   //brown
#define SCK 15  //blue
#define MOSI 16  //SDI MOSI green
#define MISO 14  //SDO MISO yellow
#define SS1 2

int SSPins[] = {17 2};

byte dataBuffer[8];

void setup() {
  Serial.begin(9600); 
  SPI.begin;
  pinMode(SSPins, OUTPUT);
  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);
}

Notice the pin mode part especially, I am not sure if that would be the right way to set the mode of all those two pins.

  pinMode(SSPins, OUTPUT);There is no overload of pinMode that accepts an array. You have to do each pin explicitly.

Okay this is what I have so far:

#include <SPI.h>
#include <stdint.h>

//*******Arduino MICRO SPI pins********
#define SS 17   
#define SCK 15  
#define MOSI 16  
#define MISO 14  
#define SS1 2

byte dataBuffer[8];

void setup() {
  Serial.begin(9600); 
  SPI.begin;
  pinMode(SS, OUTPUT);
  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);
  pinMode(SS1, OUTPUT);

 
}

void loop() {
  delay(20); //MLX90333 startup takes 16ms
  SPI.beginTransaction(SPISettings(160000, MSBFIRST, SPI_MODE1)); //320000 is about max
  delay(5);
  int j;
  
  for (j=0; j<10; j++){
    digitalWrite(SS, LOW);
    delay(20);  //Short delay necessary here
    int i;
    for (i=0; i<8; i++){ 
      dataBuffer[i] = SPI.transfer(255);  // Must transfer 1's, 0's don't work
      }
    digitalWrite(SS, HIGH);
    
    SPI.endTransaction();
    Serial.print(dataBuffer[2],8); //Print 3rd byte, MSB for Alpha
    Serial.print(" ,");
    Serial.print(dataBuffer[4],8); //Print 5th byte, MSB for Beta
    Serial.println(" ,");
    }

   for (j=0; j<10; j++){
    digitalWrite(SS1, LOW);
    delay(20);  //Short delay necessary here
    int i;
    for (i=0; i<8; i++){ 
      dataBuffer[i] = SPI.transfer(255);  // Must transfer 1's, 0's don't work
      }
    digitalWrite(SS1, HIGH);
    
    SPI.endTransaction();
    Serial.print(dataBuffer[2],8); //Print 3rd byte, MSB for Alpha
    Serial.print(" ,");
    Serial.print(dataBuffer[4],8); //Print 5th byte, MSB for Beta
    Serial.println(" ,");
    }
  }

I wasn’t sure how to use the array, although I can see where it may be useful. Until I understand how to use it, I’ll leave it out.

The code does compile and uploads, but when view the serial monitor it only shows two columns where I would like to get four (two for each sensor displaying the two angles of the magnetic field each of them senses.) Furthermore, I the readings I see are all zeroes accept when I make a really rapid movement over one of the sensor- then I see a very short few lines of non-zeros before it goes back to all zeros.

I looked walked through the code a couple times and everything seems like it should work… any suggestions?

Why do you have these? Doesn't SPI.begin() take care of those for you?

#define SCK 15  
#define MOSI 16  
#define MISO 14

and

pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);

I just tried my single sensor code with those lines commented out, and I found that you are correct that they are unneeded. My two sensor circuit still doesn’t work though. When using SPI.begin, I should still need to define additional Slave select pins right? Here is that chunk of code:

//*******Arduino MICRO SPI pins********
//#define SS 17   
//#define SCK 15  
//#define MOSI 16  
//#define MISO 14  
#define SS1 2

byte dataBuffer[8];

void setup() {
  Serial.begin(9600); 
  SPI.begin;
  //pinMode(SS, OUTPUT);
  //pinMode(SCK, OUTPUT);
  //pinMode(MOSI, OUTPUT);
  //pinMode(MISO, INPUT);
  pinMode(SS1, OUTPUT);

}