Send magnetometer data over 315mhz TX/RX

Hey everyone!
I’m a little stumped on my current project. Here’s a little background:
1] Ard Pro Mini 5V/16 + HMC5883L + 315Mhz Tx (all Sparkfun) + 3.3V battery for the sensor
2] Ard UnoV3 + 315Mhz Rx

I’m trying to get the Z axis number from one board to the other and Serialprint it on the reciever.
My testing process was:
1] play around with just the HMC - worked
2] play around with just the Tx/Rx, send LED signal - worked
3] combine HMC and Tx code on the Mini and serial write HMC on Mini, Rx code on the Uno - just recieve LED signal - worked
4] leave the LED signal to be sure Tx/Rx works, also send Z-axis to reveiver (Serial.println(z)) - stumped

I tried sending just the word “test”, but to no avail. I’m unsure how this part of the code for sending a text works. Also unsure how I should translate the Z-axis (Serial.println(raw.ZAxis); also works, just not sending over Rx/Tx) wirelessly.

Here’s the codes:

//simple Tx on pin D6
//transmitter

#include <VirtualWire.h>
#include <Wire.h> //I2C Arduino Library
#include <HMC5883L.h>
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
// Record any errors that may occur in the compass.
int error = 0;
// Store our compass as a variable.
HMC5883L compass;

char *controller;
char *controllerraw;
void setup() {
  //Initialize Serial and I2C communications
  Serial.begin(9600);
  Wire.begin();
  
  //Put the HMC5883 IC into the correct operating mode
  Wire.beginTransmission(address); //open communication with HMC5883
  Wire.write(0x02); //select mode register
  Wire.write(0x00); //continuous measurement mode
  Wire.endTransmission();
  
  pinMode(13,OUTPUT);
vw_set_ptt_inverted(true); //
vw_set_tx_pin(6);
vw_setup(4000);// speed of data transfer Kbps
}

void loop(){
  int x,y,z; //triple axis data
  // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.ReadRawAxis();  
  // Output the data via the serial port.
  Output(raw);

  //Tell the HMC5883 where to begin reading data
  Wire.beginTransmission(address);
  Wire.write(0x03); //select register 3, X MSB register
  Wire.endTransmission();
  
 
 //Read data from each axis, 2 registers per axis
  Wire.requestFrom(address, 6);
  if(6<=Wire.available()){
    x = Wire.read()<<8; //X msb
    x |= Wire.read(); //X lsb
    z = Wire.read()<<8; //Z msb
    z |= Wire.read(); //Z lsb
    y = Wire.read()<<8; //Y msb
    y |= Wire.read(); //Y lsb
  }  

  Serial.print("z: ");
  Serial.println(z);
  
  delay(1250);
  
controller="test";
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
delay(200);
controller="1";
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,1);
delay(200);
controller="0"  ;
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,0);
delay(200);

}

// Output the data down the serial port.
void Output(MagnetometerRaw raw)
{
//   Serial.print("Raw:\t");
//   Serial.print(raw.XAxis);
//   Serial.print("   ");   
//   Serial.print(raw.YAxis);
//   Serial.print("   ");   
//   Serial.println(raw.ZAxis);
//   Serial.print("   \tScaled:\t");
}
//simple Tx on pin D12
// reciever

#include <VirtualWire.h>
#include <Wire.h> //I2C Arduino Library
#include <HMC5883L.h>
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
// Record any errors that may occur in the compass.
int error = 0;
// Store our compass as a variable.
HMC5883L compass;

char *controller;
//char *controllerraw;
void setup(){
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_set_rx_pin(12);
    vw_setup(4000);  // Bits per sec
    pinMode(13, OUTPUT);

    vw_rx_start();       // Start the receiver PLL running
}

void loop(){
//  int x,y,z; //triple axis data
  // Retrive the raw values from the compass (not scaled).
  // MagnetometerRaw raw = compass.ReadRawAxis();  
  // Output the data via the serial port.
  
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
      if(buf[0]=='1'){
      digitalWrite(13,1);
      }  
      if(buf[0]=='0'){
      digitalWrite(13,0);
      }
      if(buf[0]=='test'){
        //Output(raw);
      Serial.println("test");
      }  
    }
}

// Output the data down the serial port.
void Output(MagnetometerRaw raw)
{
//   Serial.print("Raw:\t");
//   Serial.print(raw.XAxis);
//   Serial.print("   ");   
//   Serial.print(raw.YAxis);
//   Serial.print("   ");   
//   Serial.println(raw.ZAxis);
//   Serial.print("   \tScaled:\t");
}

There may be some junk in there, I was unsure so I left a bunch of stuff that may be unused, but byte size isn’t an issue.

Also I would like to know what you guys would recommend for “saving” the sensor number output form the last loop, then comparing it to the current one.
For example:

  • if difference between last number and current number is greater than 20 (positive or negative), high red LED, else high green LED
    1] first loop z-axis is 800
  • minute delay
    2] second loop z-axis is 700
  • minute delay
    = red LED

Edit: come to think of it, that should be just one loop, with a minute delay in the middle and end.

Any help is much appreciated :wink:

Got it working today. Only thing that’s wrong is that I have slightly erratic behavior from the sensor.
If I upload the HMC5833L code, there’s no erratic behavior. It is inly when transmitting.
Can anyone see anything in the previous code that would cause this? This is both on Tx and Rx side, tested on Serial Monitor so I’m pretty sure the fault would be in the transimitter code.

I get data every 1000ms or so:

Heading: 271.99 st.
Heading: 358.37 st.
Heading: 358.37 st.
Heading: 358.37 st.
Heading: 272.01 st.
Heading: 271.99 st.
Heading: 271.99 st.
Heading: 271.56 st.
Heading: 271.99 st.
Heading: 271.54 st.
Heading: 271.56 st.
Heading: 6.32 st.
Heading: 271.54 st.
Heading: 271.99 st.
Heading: 271.56 st.
Heading: 271.56 st.
Heading: 358.37 st.
Heading: 271.54 st.
Heading: 272.01 st.
Heading: 271.99 st.
Heading: 272.01 st.
Heading: 272.46 st.
Heading: 272.01 st.
Heading: 272.01 st.
Heading: 87.53 st.
Heading: 272.02 st.
Heading: 271.99 st.
Heading: 271.99 st.
Heading: 272.01 st.
Heading: 358.37 st.
Heading: 271.99 st.
Heading: 272.01 st.
Heading: 271.99 st.

Oh and no, I’m not moving the magnetometer :slight_smile:

Here’s an example with more of the sensor data

Raw: -3 -126 -133 Scaled: -13.05 -548.10 -578.55 Heading: 4.76 Radians 272.45 Degrees
Raw: 24584 10556 -19198 Scaled: -17.40 -548.10 -574.20 Heading: 4.75 Radians 271.99 Degrees
Raw: -3 -126 -132 Scaled: -13.05 -548.10 -574.20 Heading: 4.76 Radians 272.45 Degrees
Raw: -2 -127 -131 Scaled: 5559.30 2644.80 9600.45 Heading: 0.51 Radians 29.25 Degrees
Raw: -4 -126 -133 Scaled: 106940.40 45918.60 -108010.50 Heading: 0.47 Radians 27.05 Degrees
Raw: -3 -126 -133 Scaled: 106940.40 45918.60 -108010.50 Heading: 0.47 Radians 27.05 Degrees
Raw: 1277 608 2229 Scaled: -13.05 -548.10 -574.20 Heading: 4.76 Radians 272.45 Degrees
Raw: -2 -126 -131 Scaled: -8.70 -548.10 -569.85 Heading: 4.76 Radians 272.90 Degrees
Raw: -3 -127 -132 Scaled: -13.05 -552.45 -574.20 Heading: 4.76 Radians 272.46 Degrees
Raw: -3 -127 -133 Scaled: -13.05 -552.45 -578.55 Heading: 4.76 Radians 272.46 Degrees
Raw: -3 -127 -132 Scaled: -13.05 -552.45 -574.20 Heading: 4.76 Radians 272.46 Degrees
Raw: -3 -126 -132 Scaled: -13.05 -548.10 -574.20 Heading: 4.76 Radians 272.45 Degrees
Raw: -3 -126 -133 Scaled: -13.05 -548.10 -578.55 Heading: 4.76 Radians 272.45 Degrees
Raw: -3 -126 -132 Scaled: -13.05 -548.10 -574.20 Heading: 4.76 Radians 272.45 Degrees
Raw: -3 -126 -132 Scaled: -13.05 -548.10 -574.20 Heading: 4.76 Radians 272.45 Degrees
Raw: 258 2464 -32256 Scaled: -8.70 -548.10 -574.20 Heading: 4.76 Radians 272.90 Degrees
Raw: -3 -127 -132 Scaled: 1122.30 10718.40 -140313.59 Heading: 1.53 Radians 87.83 Degrees
Raw: -3 -126 -132 Scaled: -13.05 -548.10 -574.20 Heading: 4.76 Radians 272.45 Degrees
Raw: 24584 10556 -19198 Scaled: -13.05 -548.10 -578.55 Heading: 4.76 Radians 272.45 Degrees
Raw: -3 -127 -132 Scaled: -13.05 -552.45 -574.20 Heading: 4.76 Radians 272.46 Degrees
Raw: -3 -127 -131 Scaled: -13.05 -552.45 -569.85 Heading: 4.76 Radians 272.46 Degrees
Raw: -3 -126 -132 Scaled: -13.05 -548.10 -574.20 Heading: 4.76 Radians 272.45 Degrees
Raw: -4 -126 -132 Scaled: -17.40 -548.10 -574.20 Heading: 4.75 Radians 271.99 Degrees
Raw: -2 -126 -132 Scaled: -8.70 -548.10 -574.20 Heading: 4.76 Radians 272.90 Degrees
Raw: 24584 10556 -19198 Scaled: -8.70 -552.45 -574.20 Heading: 4.76 Radians 272.91 Degrees
Raw: -3 -126 -132 Scaled: -13.05 -548.10 -574.20 Heading: 4.76 Radians 272.45 Degrees
Raw: -2 -126 -132 Scaled: -8.70 -548.10 -574.20 Heading: 4.76 Radians 272.90 Degrees
Raw: -3 -127 -131 Scaled: -13.05 -552.45 -569.85 Heading: 4.76 Radians 272.46 Degrees
Raw: 258 2464 -32256 Scaled: -13.05 -548.10 -574.20 Heading: 4.76 Radians 272.45 Degrees
Raw: -3 -127 -133 Scaled: -13.05 -552.45 -578.55 Heading: 4.76 Radians 272.46 Degrees

Ahh... datatransfer was too high at 4000bps, tuned it down to 1000 on both Tx and Rx and all is good. Hope this helps someone in the future.