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.
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:
Is there anything wrong with what I have so far?
How do I modify my code to handle the multiple sensors?
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!
#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?
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: