How to use HMC5883L with Arduino RC Car?

Hello Everyone,
I have made the RC Car to work but I am trying to incorporate the HMC5883L with the RC Car which is not working. In my code for now, I am trying the steer the wheels in left or right direction to correct the direction of the car first and then move onto forward-direction. These directions are nothing but the angles. Here is the sample of my code:

#include <Arduino.h>
#include <Wire.h>
#include <HMC5883L_Simple.h>

// Create a compass
HMC5883L_Simple Compass;
int D;
void setup()
{
  pinMode(13,OUTPUT);
  pinMode(10,OUTPUT);
  Serial.begin(9600);
  Wire.begin();

  Compass.SetDeclination(10, 17, 'W');  

  Compass.SetSamplingMode(COMPASS_SINGLE);

  Compass.SetScale(COMPASS_SCALE_130);
  
  Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);
  
}

// Our main program loop.
void loop()
{
  if(Serial.available()){
    D = Serial.read();
    Serial.println(D);
  }
   int heading = Compass.GetHeadingDegrees();
   
   Serial.print("Heading: \t");
   Serial.println( heading );   
   delay(1000);

   do{
    if(heading == D){
      digitalWrite(13,HIGH); //Left motors, help steer to right
      digitalWrite(10,HIGH); //right motors, help steer to left
    }
    else{
      digitalWrite(10,HIGH);
    }
    }while (heading-D <= 180);

    do{
   if(heading == D){
      digitalWrite(13,HIGH);
      digitalWrite(10,HIGH);
    }
    else{
      digitalWrite(13,HIGH);
    }
    }while (heading-D > 180);

}

Thank you

Start by making a list of the features you want and how they will be controlled. Then check for the various hardware devices and there data sheets so you understand how they work and or interface. Then generate a block diagram of how it goes together. Select the appropriate microcontroller. Acquire the parts and start to assemble and write your code or hire it done. As you progress through this feel free to ask questions we are here to help, but not to design your project.

Hey gilshultz,

Thank you very much. I will surely get on it asap.

The compass is the last thing you should add to the project.

A number of tutorials have been posted on the web for robot cars steered by Arduino/Bluetooth/smart phone apps. They will get you started.

Hello jremington,
I have made the RC Car successfully. I just want to steer the RC Car by giving out the directions like North, South etc. now which is why I was thinking of using the HMC5883L.

No problem. If properly calibrated, the compass indicates the direction in which the vehicle is currently pointed (headed).

To drive in another direction, calculate the angle from the difference, turn by that amount, and go.

Or, just turn until the compass indicates that the heading is correct, and go.

Keep the compass well away from batteries and motors.

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