Pololu Robotics & Electronics
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Compass sketch does not work & other soft problems


I have a pre-assembled brand new Zumo 32U4 Robot and I am very happy with it as it works perfectly, once the necessary software is uploaded.
I tested & work perfectly:

  • Demo.
  • FaceUphill.
  • LineFollower.
    -MazeSolver. With this you must use GridMovement.h /.cpp, but then you must remove them from the Libraries folder, otherwise they will not allow other sketches to work.

Unfortunately Compass DOES NOT WORK: it compliles and loads normally: you even see the LED blinking at the end of upload, but then the robot stays dead and does not do anything.

Please investigate.


#include <Zumo32U4Motors.h>
#include <Pushbutton.h>
#include <Wire.h>
#include <LSM303.h>


Hello, Glovisol.

You should not have to remove GridMovement from the libraries for other sketches to work. What kind of error did it give you?

Which sketch you are using to test the magnetometer (compass)? Are you using the InertialSensors example? If so, are you looking at the readings on the Arduino serial monitor?



Hi Grant, thanks for the quick reply. I am using the “Compass” example:

/* This example uses the magnetometer in the Zumo Shield’s onboard

  • LSM303DLHC to help the Zumo make precise 90-degree turns and drive
  • in squares. It uses the ZumoMotors, Pushbutton, and LSM303
  • (compass) libraries. The LSM303 library is not included in the Zumo
  • Shield libraries;

Of course I have included the LSM303 library, but still no operation/activity/readingg whatsoever after uploading.

Further I went over the sketch and find that the “BUTTON A” command is missing: e.g. provisions for button action are written, but the actual button command is missing…any ideas?




The example sketch you are using is for the Zumo shield, and some of the pins used in the Zumo shield library are not the same as the Zumo 32U4 library. If you just want to test the magnetometer, I recommend using the InertialSensors example and viewing the sensor readings in the Arduino serial monitor.



Dear Grant, sorry to contradict you. Here below I copy the first 40 lines of the “COMPASS” sketch which show the sketch IS FOR THE ZUMO 32U4. Please check and fix this example!!

#include <Zumo32U4Motors.h>
#include <Pushbutton.h>
#include <Wire.h>
#include <LSM303.h>

/* This example uses the magnetometer in the Zumo Shield's onboard
 * LSM303DLHC to help the Zumo make precise 90-degree turns and drive
 * in squares. It uses the ZumoMotors, Pushbutton, and LSM303
 * (compass) libraries. The LSM303 library is not included in the Zumo
 * Shield libraries; it can be downloaded from GitHub at:
 *   https://github.com/pololu/LSM303
 * This program first calibrates the compass to account for offsets in
 *  its output. Calibration is accomplished in setup().
 * In loop(), The driving angle then changes its offset by 90 degrees
 * from the heading every second. Essentially, this navigates the
 * Zumo to drive in square patterns.
 * It is important to note that stray magnetic fields from electric
 * current (including from the Zumo's own motors) and the environment
 * (for example, steel rebar in a concrete floor) might adversely
 * affect readings from the LSM303 compass and make them less
 * reliable.

#define SPEED           200 // Maximum motor speed when going straight; variable speed when turning
#define TURN_BASE_SPEED 100 // Base speed when turning (added to variable speed)

#define CALIBRATION_SAMPLES 70  // Number of compass readings to take when calibrating
#define CRB_REG_M_2_5GAUSS 0x60 // CRB_REG_M value for magnetometer +/-2.5 gauss full scale
#define CRA_REG_M_220HZ    0x1C // CRA_REG_M value for magnetometer 220 Hz update rate

// Allowed deviation (in degrees) relative to target angle that must be achieved before driving straight

Zumo32U4Motors motors;
Pushbutton button(ZUMO_BUTTON);
LSM303 compass;




Dear Grant,

Sorry about my last message. I checked again and the “Commpass” sketch is suitable only for Arduino equipped robots. I got confused because I forgot I had modified this sketch myself! So you were right and I have to modify the sketch to make it work with 32U4.




I am glad you figured out what was going on. We might be able to help you modify the compass sketch to work with the Zumo32U4 or write some similar code, but first I recommend trying the InertialSensors example to verify that your compass is working.



Hi Grant,

Yes, I have already tried the Inertial Sensors sketch and the compass works O.K. Concerning the compass sketch it would be great if you could modify it to work with the 32U4. The modification mailly entails the re-write /adapt of the ZumoMotor.cpp, which seems a rather heavy job.

I have modified the Border Detect sketch and now the robot is able to follow rectilinear paths on a black surface delimited by white bands OR on a white surface delimited by black bands. If several paths are set side by side with alternate south / north openings, the robot will cover all the area by going thru all the paths.

I can share the sketch if you think it could be of interest.




Making a version of the Compass demo that is compatible with the Zumo 32U4 is not high on our priority list, but if you want to write a program like that for your own purposes and run into trouble, you can post your code and I would be happy to take a look.

If you want to share the code you already have, I am sure there are other user’s who would be interested in it.