Note: this page refers to a product that is retired.

RobotCompass

Plan a treasure hunt with this digital compass.

Compass

The robot has a compass module, which it uses to find its direction. This sketch will make sure the robot goes towards a certain direction.

When you hold the robot in your hands and rotate, you will the screen change, indicating direction.

NB : magnets will interfere with the compass. If you're getting unexpected results, check to make sure there are none around.

Hardware Required

  • Arduino Robot

Instruction

  1. Upload the example, unplug USB and turn on power.

  2. Place the robot on the ground.

  3. After the starting screen, a graph will appear on-screen, representing the compass

  4. The robot will start moving in a direction (in this example, it will start heading south). If you move it in a different direction, it will turn back to the way it wants to move.

  5. If you want to change the robot's heading, in the code, look for

    int direc=180
    at the beginning of the code. Change this value to another number, between 0 and 359. 0 represents north, 90 is east, 180 is south, and 270 is west. Where do you want to go?

Try it out

Check how the compass works
Check how the compass works

Code

1/* Robot Compass
2
3 The robot has an on-board compass module, with
4
5 which it can tell the direction the robot is
6
7 facing. This sketch will make sure the robot
8
9 goes towards a certain direction.
10
11 Beware, magnets will interfere with the compass
12
13 readings.
14
15 Circuit:
16
17 * Arduino Robot
18
19 created 1 May 2013
20
21 by X. Yang
22
23 modified 12 May 2013
24
25 by D. Cuartielles
26
27 This example is in the public domain
28
29 */
30
31// include the robot library
32#include <ArduinoRobot.h>
33#include <Wire.h>
34
35int speedLeft;
36int speedRight;
37int compassValue;
38int direc = 180; //Direction the robot is heading
39
40void setup() {
41
42 // initialize the modules
43
44 Robot.begin();
45
46 Robot.beginTFT();
47
48 Robot.beginSD();
49
50 Robot.displayLogos();
51}
52
53void loop() {
54
55 // read the compass orientation
56
57 compassValue = Robot.compassRead();
58
59 // how many degrees are we off
60
61 int diff = compassValue - direc;
62
63 // modify degrees
64
65 if (diff > 180) {
66
67 diff = -360 + diff;
68
69 } else if (diff < -180) {
70
71 diff = 360 + diff;
72
73 }
74
75 // Make the robot turn to its proper orientation
76
77 diff = map(diff, -180, 180, -255, 255);
78
79 if (diff > 0) {
80
81 // keep the right wheel spinning,
82
83 // change the speed of the left wheel
84
85 speedLeft = 255 - diff;
86
87 speedRight = 255;
88
89 } else {
90
91 // keep the right left spinning,
92
93 // change the speed of the left wheel
94
95 speedLeft = 255;
96
97 speedRight = 255 + diff;
98
99 }
100
101 // write out to the motors
102
103 Robot.motorsWrite(speedLeft, speedRight);
104
105 // draw the orientation on the screen
106
107 Robot.drawCompass(compassValue);
108}

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.