Smart Compass

Published May 26, 2024
 90 hours to build

A smart compass that points to nearest emergency service (example - Petrol Pump, Hospital, Police Station etc) and locks itself in that direction.

display image

Components Used

Raspberry Pi 4 - 4GB
Single Board Computers Raspberry Pi 4 4GB
28BYJ-48 Stepper Motor
Power Management IC Development Tools Gear Stepper Motor Driver Pack
5V 2A Power bank
Adafruit Accessories USB Li-Ion Power Bank with 2 x 5V Outputs @ 2.1A - 5000mAh
ULN2003 Motor Driver
ULN2003A is a high-voltage, high-current Darlington transistor array.
4x4 Matrix Keypad
4x4 Matrix Keypad
Android Phone
An android phone

Smart Compass



A normal compass is boring (just points north), lets make something smarter that points to any required place and locks itself in that direction.




Imagine yourself in a situation when somehow you can't use Google Maps App on your phone and want to navigate to nearest petrol pump as you don't have enough fuel. Our general compass just points towards north. I decided to make a smart compass, that will point to the nearest desired place selected using keypad. This device will not only point to the location but also lock the needle in that direction, which will still show correct direction even if we take turns.




Set HyperIMU app on android phone to send UDP packets to the PI. Select orientation and GPS values in packet settings. Attach the phone to the device, such that the phone points towards the tip of the needle. The pi uses the orientation sensors inside the phone to determine its orientation. This values are streamed to the PI using HyperIMU.


Press the button on keypad for desired nearest location.


The buttons correspond to this nearest different places ->


1 - Automobile Service

2 - Hospital

3 - Police Station

4 - Petrol Pump

A - Mall

5 - Pizza

6 - Bank

B - Post Office

7 - Stadium

8 - Railway Station

C - Airport

*  - Hotel

0 - Restaurant

# - School

D - Reset

( Note - For the needle to return to 0 degree mark, press the Reset button )




The whole Python application is packaged in a Docker image. So Docker installation is required.

Docker installation - 

$ sudo curl -fsSL -o
$ sh
$ sudo usermod -aG docker pi
$ sudo reboot


Pulling Docker image from Docker Hub -

$ sudo docker pull --platform linux/arm64/v8


Running the container -

$ sudo docker run -it --platform linux/arm64/v8 --name smartcompass-con.$(date "+%Y.%m.%d-%H.%M.%S") --net=host --device /dev/gpiomem yashindane/smartcompass:v2 --ip="<IPV4-OF-PI>" --port=<UDP-PORT> --key="<GOOGLE-MAPS-API-KEY>" --keypad_rows="16,20,21,5" --keypad_cols="6,13,19,26" --motor_pins="17,18,27,22"

( Note - One needs to have a active Google Maps API Key )




The PI continuously scans the keypad for a button press. Once a button is pressed, using Google Maps API nearest corresponding location place coordinates and name are fetched.


Sample of code from that does the coordinates fetching -

loc_string = ",".join(loc)

gmaps_client = googlemaps.Client(key=api_key)
place_result = gmaps_client.places_nearby(location=loc_string, keyword=place, rank_by="distance")

nearest_place = place_result["results"][0]
nearest_place_lat = nearest_place["geometry"]["location"]["lat"]
nearest_place_lng = nearest_place["geometry"]["location"]["lng"]
nearest_place_name = nearest_place["name"]
nearest_place_vicinity = nearest_place["vicinity"]


Next the Pi calculates the Bearing Angle, using the coordinates fetched and the device coordinates received from HyperIMU stream. 


What is Bearing Angle?



It is the angle between the North-South direction line and the line connecting the current position point and destination position point.



Sample Code from computing the Bearing angle -

a = [math.radians(x) for x in a]
b = [math.radians(x) for x in b]
diffLong = b[1]-a[1]
X = (math.cos(b[0]))*(math.sin(diffLong))
Y = math.cos(a[0])*math.sin(b[0])-math.sin(a[0])*math.cos(b[0])*math.cos(diffLong)
B = math.atan2(X, Y)
BEARING_ANGLE = math.degrees(B)


Then according to the device orientation, the final angle is calculated. This final angle is fed to stepper driver and the needle moves.

After the needle points to the required direction, the needle gets locked in that direction. To return back to 0 degree mark, press reset button.




This device can be mainly used in emergency cases for quick navigation to nearest required emergency services.

This can be a good substitute to Google Maps App, although it only points towards the direction.


Future Developments


We can add a lot of more features to this-


  1. Adding a LCD Display to show name of the place we are looking for
  2. Adding a Mechanical keypad for robustness
  3. Making a more good design using 3D printing




This device can be used to quickly get the direction of a required place so that we can navigate to it, in case of emergency situation.



Keypad-Interfacing Download
Stepper-Interfacing Download