We are now going to use the GPS to build a first geofencing project. In this first project, we will continuously check the location of the project, and see if we exceeded a given distance that we set in the sketch as well.

If that's the case, we will make the LED blink, and also make the buzzer emit a sound. As this sketch shares a lot with the previous one, I will only talk about what I added here, and you will find the complete sketch inside the GitHub repository of the project.

We first declare some constants & variables for the alarm:

// LED & Buzzer pins
const int ledPin = 6;
const int buzzerPin = 9;

// Alarm
int counter = 0;
bool alarm = false;

Then, we set the maximum distance that the project can go without raising the alarm. Note that the precision of a civilian GPS like this one is around 10 meters, so I really suggest setting a value which is superior to 20 meters or so:

const float maxDistance = 100;

We also declare two variables that will contain the initial location of the project:

float initialLatitude;
float initialLongitude;

By default, we set the alarm to be false:

alarm = false;

In the setup() function of the sketch, we get a GPS fix to set the initial location of the project:

bool gpsFix = fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);
initialLatitude = latitude;
initialLongitude = longitude;

In the loop() function of the sketch, we constantly get the current GPS location, and then calculate the difference between this & the initial location:

float distance = distanceCoordinates(latitude, longitude, initialLatitude, initialLongitude);

This function, that I won't detail here, is using the Haversine formula:


This is a well-known formula used to calculate the distance between two GPS coordinates. You can check the details of the implementation inside the sketch.

After that, we also print this distance inside the Serial monitor:

Serial.print("Distance: ");
printFloat(distance, 5);

If the measured distance exceeded the maximum distance we allowed, we also set the alarm on:

if (distance > maxDistance) {
  alarm = true;

After that, we check if we are in alarm mode or not, and act on the LED & Piezo buzzer accordingly:

if (alarm == false) {

  if (millis() - counter > 5000) {
    digitalWrite(ledPin, HIGH);

  if (millis() - counter > 5100) {
    digitalWrite(ledPin, LOW);
    counter = millis();

else {
  if (millis() - counter > 100) {
    digitalWrite(ledPin, HIGH);

  if (millis() - counter > 200) {
    digitalWrite(ledPin, LOW);
    counter = millis();
  tone(buzzerPin, 1000);

You can find all the code for this part inside the GitHub repository of the project:

It's now time to test the project! Upload the code to the Arduino board, and open the Serial monitor. You should see that initially, the distance is equal to zero, or at a small value.

Indeed, note that the precision of the GPS is around 10 meters, so between two measurements the position will change slightly. You can also take the project for a short walk, and see if it raises the alarm!

This guide was first published on Nov 05, 2015. It was last updated on Nov 05, 2015.

This page (Geofencing Alerts) was last updated on May 18, 2021.

Text editor powered by tinymce.