Martha MigliacioAlex Wong
Published © GPL3+

Using the Pmod CMPS2 with Arduino Uno

Application notes for Pmod CMPS2 and Arduino Uno. The 3 components of the magnetic field, module, & angle are displayed in serial monitor

EasyProtip1 hour252
Using the Pmod CMPS2 with Arduino Uno

Things used in this project

Hardware components

Digilent Pmod CMPS2
×1
Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Pmod CMPS and Arduino Uno Fritzing file

Fritzing file displaying the connection of the Pmod CMPS to the Arduino Uno.

Pmod CMPS and Arduino Uno Fritzing Image

Fritzing image displaying the connection between the Pmod CMPS and Arduino Uno.
Pmodcmpsarduinouno bb boyvfvtoa8

Code

Pmod CMPS and Arduino Uno Code

Arduino
Using this code will display the 3 components of the magnetic field, its module, and then it angle in the serial monitor.
/************************************************************************
*
* Test of the Pmod
*
*************************************************************************
* Description: Pmod_CMPS
* The three components (X, Y, Z) of the magnetic field and then its module
* and its angle are displayed in the serial monitor
*
* Material
* 1. Arduino Uno
* 2. Pmod CMPS
*
************************************************************************/

#include <Wire.h> // call library
#define HMC5803L_Addresse 0x1E // adresse I2C of the modue

double composante_X;
double composante_Y;
double composante_Z;
double module;
double angle;

void setup()
{
 Serial.begin(9600);
 Wire.begin(); // initialization of I2C bus
 Init_HMC5803L(); // initialization ofthe compass
}

void loop()
{
 Wire.beginTransmission(HMC5803L_Addresse); // Launch of the measure
 Wire.write(0x02);
 Wire.write(0x01);
 Wire.endTransmission();
 delay(10);
 Wire.requestFrom(HMC5803L_Addresse, 6); // Recovery of components
 if(Wire.available() <=6)
 {
  composante_X = Wire.read() << 8 | Wire.read();
  composante_Y = Wire.read() << 8 | Wire.read();
  composante_Z = Wire.read() << 8 | Wire.read();
 }
 Serial.print("X=");
 Serial.print (composante_X);
 Serial.print('\t');
 Serial.print("Y=");
 Serial.print (composante_Y);
 Serial.print('\t');
 Serial.print("Z=");
 Serial.print (composante_Z);
 Serial.print('\t');
 // Calculation of module
 module = sqrt(composante_X * composante_X + composante_Y * composante_Y + composante_Z *
composante_Z);
 Serial.print("M=");
 Serial.print(module);
 Serial.print('\t');
 //Calculation of angle
 angle= atan2(composante_Y,composante_X) * (180 /
3.14159265); // angle en degrés
 if (angle<0) {angle=angle+360;}
 Serial.print("Angle=");
 Serial.println(angle);
 delay(1000);
}
// initialization of HMC5803L
void Init_HMC5803L(void)
{
 Wire.beginTransmission(HMC5803L_Addresse);
 Wire.write(0x00);
 Wire.write(0x70);
 Wire.write(0x01);
 Wire.write(0xA0);
 Wire.endTransmission();
}

Credits

Martha Migliacio

Martha Migliacio

5 projects • 5 followers
Alex Wong

Alex Wong

5 projects • 13 followers
I work in Digilent and like creating projects
Thanks to Lextronics.

Comments

Add projectSign up / Login