Jump to content

SkySurveyBanner.jpg.21855908fce40597655603b6c9af720d.jpg

Arduino Based Weather Station


Gina

Recommended Posts

Thought the damping magnets could be causing the problem so removed them - now about 4m from the sensor chip.  Tried the axle magnet on top of the chip again but still no joy.  Wonder if it does need the FET and resistor from MISO.  I would have thought that if this was the problem the check would fail.

Link to comment
Share on other sites

I think the answer to the -1 display might be in here :-

/*
  MLX90316.cpp -  Library to use with Melexis MLX90316 rotary encoder chip.
  Created by Martin Nawrath KHM 2010.
  http://interface.khm.de
  Released into the public domain.
*/

#include "Arduino.h"
#include "MLX90316.h"


// constructor
MLX90316::MLX90316(){


};
// attach
void MLX90316::attach(int pinSS, int pinSCK, int pinMOSI )
{
	_pinSS = pinSS;
	_pinSCK = pinSCK;
	_pinMOSI = pinMOSI;

	pinMode(_pinSS , OUTPUT);
	pinMode(_pinSCK , OUTPUT);
	pinMode(_pinMOSI , OUTPUT);
}

//******************************************************
int MLX90316::readAngle() {
  byte bb;
  int ret=-1;
  unsigned int rr;
  long int lo;

  digitalWrite(_pinSS,0);


  bb=_spiByte(0x55);  // send sync byte ( AA reversed order = 55?)
  bb=_spiByte(0xFF);  // send sync byte FF)
 
   bb=_spiByte(0xFF); // receive 1. byte of mlx msb data
   rr= (bb << 8);   
   bb=_spiByte(0xFF); // receive 2. byte of lsb mlx data
   rr=rr+bb;
   
  if ((rr & 3) == 2) {
   
    if ( rr & (1 << 4)) ret=-2;  // signal to strong
    if ( rr & (1 << 5)) ret=-3;  // signal to weak
}
   
  if ((rr & 3) == 1) { // valid angle data ?
     rr = (rr >> 2);
     lo= rr ;
     lo=lo *  3600 / 16384;	// scale output to 360 deg, untit in tens of deg.	
    ret= lo;
  }

  digitalWrite(_pinSS,1);
  return(ret);


}

//*************************************************************
// send and receive SPI byte  to/from MLX90316 Chip
uint8_t  MLX90316::_spiByte(uint8_t  tx) {
  byte rxb=0;

  for (int ix = 0; ix < 8; ix++){  // receive/transmit 8 SPI bits

    digitalWrite(_pinSCK,1);    // clocksignal positive slope
    rxb = ( rxb << 1);           // shift received byte left

    pinMode(_pinMOSI,INPUT);    // switch MOSI pin to input
    digitalWrite(_pinMOSI,1);   // turn port internal pullup resistor on

    if (digitalRead(_pinMOSI) ==1) rxb = rxb | 1; // read respose bit from sensor
    digitalWrite(_pinMOSI,0);   // turn port internal pullup resistor off
    pinMode(_pinMOSI,OUTPUT);   // switch MOSI pin to output

    // write SPI transmit bit to sensor
    if ((tx & 1) != 0) digitalWrite(_pinMOSI,1);
    else digitalWrite(_pinMOSI,0);
    tx= ( tx >> 1);

    digitalWrite(_pinSCK,0);   // clocksignal negative slope
    digitalWrite(_pinMOSI,0);  // set MOSI databit 0
  }
 
  return(rxb);
 

}

 

Link to comment
Share on other sites

YEP!!  Returns angle if angle data is valid else -1.  If the magnetic field is too strong or too weak it returns -2 or -3 from these lines of code :-

    if ( rr & (1 << 4)) ret=-2;  // signal to strong
    if ( rr & (1 << 5)) ret=-3;  // signal to weak

SO a return of -1 indicates invalid angle data (from whatever cause other than magnetic field strength).

Edited by Gina
Link to comment
Share on other sites

The MLX90316.cpp reads the data from the device and parses the data to determine if the angle is valid or if there's an error in conjunction with information from the datasheet.  If the LSB 2 bits are 0 1 the data represents a valid angle otherwise there is an error condition as shown by the table in the second screenshot.

5a36b66cec3c0_SensorData04.thumb.png.46e889d69b9ca454d84fd17a34985beb.png5a36b66be60d4_SensorData05.thumb.png.4c1e9d4264c3e03b6ddca34ea7b752e2.png

Link to comment
Share on other sites

I could edit the code in MLX90316.cpp to take the errors and convert to a negative number to show what's wrong.  I don't think it's anything to do with the MISO and FET etc. though it wouldn't take much to add the components birds-nest fashion :D  I guess that would eliminate that possibility.

Edited by Gina
Link to comment
Share on other sites

Yes and that's probably the easiest thing to do.  I only have to take this code section and produce a general negative number from the error code.  I can interpret it later.

  if ((rr & 3) == 2) {
   
    if ( rr & (1 << 4)) ret=-2;  // signal to strong
    if ( rr & (1 << 5)) ret=-3;  // signal to weak
}

I propose this code :-

  if ((rr & 3) == 2) { 
    ret=-rr;  // error code as negative number
}

 

Link to comment
Share on other sites

That still produced -1 so the data is neither a valid angle nor a valid error code.  Changing the code to return -rr if not a valid angle produced a code of 1 meaning rr was -1 ie. all 1s - a data transfer error.  The Arduino is not reading the MLX90316 output register or the register is stuck on all ones.

Next stage then is to add the FET and resistor to see if that cures it but if not I shall have to assume the chip is damaged.  I did buy a spare.

Edited by Gina
Link to comment
Share on other sites

4 hours ago, Gina said:

Read carefully through the MLX90316 datasheet and can see no mention of error output.  But generally where the device gives a positive number to show a measurement an output of -1 indicates an error and therefore I assume that I am getting an error from the sensor chip.  I have Googled till I'm blue in the face for any information about MXL90316 errors.  The datasheet says an error will occur if the magnetic field is to weak or too strong.  So this is one possibility.

Unless it's sending and endless stream of 0xFF because something is stuck high?

 

  • Like 1
Link to comment
Share on other sites

Data is read in to the Arduino (master) from the MLX90316 (slave) therefore the data channel is MISO.  The MOSI line sends data to the slave to make it do something, in this case read the angle.  The way I have it connected it couldn't possibly work! :eek:   It might work without MOSI as the SS (Slave Select) may trigger the output though that isn't the way it's described in the datasheet.  But at least one person seems to have it working without MOSI.

Link to comment
Share on other sites

Used a transistor instead of a FET with a resistor in the base line.  Still not working :(  Guess I'll connect my little digital oscilloscope to it tomorrow and see if I can tell what's happening.

Link to comment
Share on other sites

Gina, I'm sorry to hear you're having issues with the chip. I re-tested mine this morning with the same wiring as before and works perfectly fine. This is where I got the code and the diagram from. Wiring the chip exactly as illustrated in the diagram on the site works for me. Make sure your Baud rate is the same on the Serial Monitor as it is on the sketch. I am testing with an Arduino Mega, but another thing you could try is adding a pullup resistor as mentioned here.

I never received an output of -1 so don't know what could be causing that. Another possibility is that Mouser may not have shipped the SPI version? On my order it says MLX90316EDC-BDG-100-SP, maybe worth checking.

This is how I have mine setup on the breadboard

20171218_122221.thumb.jpg.5d71d532cc11f89ac40e5d9fb6bfc167.jpg

I'm also in the process of uploading a video showing the range of motion from 0 to 360 degrees via Serial Monitor.

  • Thanks 1
Link to comment
Share on other sites

22 hours ago, Gina said:

Testing...  Have Arduino Nano connected to Win 7 laptop and displaying Serial Monitor but just getting a high speed run of strange characters which I don't think is right :(

5a367b6ce4081_SensorData01.png.7f1b5ded5afe19c9bdd20eac0abf2d55.png

This may be the issue as I get the same output when setting the Baud rate to 9600 instead of 115200 as in the sketch!

  • Like 1
Link to comment
Share on other sites

Thanks :)  I have the baud rates on both set to 115200 as per the sketch.  I'm using different pins on the Arduino Nano though.  I was going by the Nano connection diagram which shows SPI as D10-D13 rather than D3-D5.  I'll try D3-D5 then.  From your pic you are indeed connecting to D3-D5 on the Mega.  I must be missing something - I'll examine the MLX90316.cpp code again.

nano.png

Edited by Gina
  • Like 1
Link to comment
Share on other sites

OK - I've got it :)  The MLX90316.cpp code is independent of the standard Arduino SPI system and by way of the main sketch reassigns these functions to D3-D5 and also switches between MOSI & MISO by switching the pin between output and input respectively.  I'll change my Arduino Nano connections and try again.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. By using this site, you agree to our Terms of Use.