MMC5883MC Demonstration Project

I have tested this on a Nano and an UNO. This was a bit of a learning curve I would like to share the results.
The GY801 knockoffs are being shipped with the MMC588MC from MEMSIC. The i2c address is 0x30. I put together a magnetic compass program using the chip.
I put lots of comments for beginners. The schematic is very easy to connect. It is typical i2c directly from the Nano to the GY-801.

MMC5883_test.ino (9.04 KB)

Wiring diagram

Spec Sheet
There is a thermometer in the chip too. Program to read it is similar to reading the magnetometer values.

  1. Write 2 to the control register 0
  2. watch the second bit in the status register.
    3 read the temperature register.

MMC5883MA-RevC-1219541.pdf (436 KB)

This section of the forum is for questions. A moderator might move this topic elsewhere.

I wrote the common mistakes and my alternative explanation of the Wire library.

Please remove the line with the while-statement after a Wire.requestFrom(). There is no such thing as waiting after a Wire.requestFrom().
Also remove the Wire.endTransmission() after the Wire.requestFrom(). It should only be used together with Wire.beginTransmission().

Can you add a few extra '(' and ')' in the lines where x, y, z are calculated ?
At first glance, I do not know what this is doing:

x = 2.0 * a / b - 1.0;

We prefer that you show you sketch in the post between code-tags. The </> button is for code-tags.

The GY801 has a onboard level shifter for SDA and SCL and a voltage regulator. Since a Arduino Nano is a 5V Arduino board, you need that level shifter to connect the 5V I2C bus (Arduino Nano) to the 3.3V I2C bus (sensors). To make that level shifter work, the module has to be powered with 5V to VCC_IN. Leave the VCC_3.3V pin open.

I am sorry if this ended up in the wrong category. I can't move it.
As far as the Wire library is concerned I was winging that part. Easy enough to modify.
AS far as the parentheses are concerned this is a moment for me to educate.
Rules of Operator Precedence
C++ applies the operators in arithmetic expressions in a precise order determined by these rules of operator precedence, which are generally the same as those in algebra:

  • Operators in expressions contained within pairs of parentheses are evaluated first. Parentheses are said to be at the "highest level of precedence." In cases of nested, or embedded, parentheses, such as the operators in the innermost pair of parentheses are applied first.
  • Multiplication, division and modulus operations are applied next. If an expression contains several multiplication, division and modulus operations, operators are applied from left to right. Multiplication, division and modulus are said to be on the same level of precedence.
  • Addition and subtraction operations are applied last. If an expression contains several addition and subtraction operations, operators are applied from left to right. Addition and subtraction also have the same level of precedence.

Oh and the code tags. The introduction to posting made no mention of that for downloads. Copying and pasting out of a code window is a pain in the rear. I absolutely despise people who want to share and only do that. It is much easier to put a link to github but I read that the practice of linking to offsite code was frowned.
If I were needing help and wanted to ask a question about a snippet then I would use the code tags.
The post is limited to 9000 characters. The project wouldn't fit.
Needless to say I haven't ever asked a coding question on a forum. I am the one who usually answers. But being a consultant I usually sell answers. and don't have time to answer questions on forums. Occasionally if there is something I see many people frustrated with such as why the i2c scanner returns 0x30 for the magnetometer instead of 0x0d (QMS) or 0x1e (HMS) on the GY801, then I don't mind doing a little digging and sharing.

Rules of operator presence ? Then I have to use my brain. That is hard. I want to know the calculation with a first glance :stuck_out_tongue:

So something like this

// Simple linear interpretation
// given an input that has a range of Min_0 to Max_0
// and wanting an output of Min_1 and Max_1
// we first calculate some ranges
Range_0 = Max_0 - Min_0;
Range_1 = Max_1 - Min_1;
// we then zero out the input
z_input = input - Min_0;
// we then devide by the range to get a percent of full scale
percent = z_input / Range_0;
// we then multiply the percent by the second range
scaled = percent * Range_1;
// we then slide the scale to the new minimum by adding the Min_1
output = scaled + Min_1;
// Max_1, Min_1, Max_0 and Min_0 can be any number as long as they are different else you only get one answer.
// Range output is 2*a/b - 1  Range_1 = 2 Min_1 = -1 input = a Range_0 = b

I meant with extra '(' and ')'. They seem to be called "parentheses" and sometimes "brackets" but I would call them "round brackets" :confused:

x = ( 2.0 * a / b ) - 1.0;   // <---- this one
x = 2.0 * a / ( b - 1.0 );
x = 2.0 * ( ( a / b ) - 1.0 );

Completely unnecessary because of the rules. Multiplication and division first, left to right and then addition and subtraction left to right. You certainly need to memorize this. Every programming language uses this rule.
When you were in school did you have trouble with y = mx + b?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.