Re: creating alert when system crosses geofence

Hello Arduino family,
I am working on a geofencing project, where my motto is "Using a button to save current position and activate buzzer outside a 50 m cage." I am using Arduino mega 2560, neo 6m GPS, a buzzer.
As I am new to coding, I am finding little difficulty in writing the code for it, any help would be much appreciated. Thank you!!

What code have you tried so far ?

The TinyGPS++ library, and probably others, has a distanceBetween() function that will return the distance between 2 points

Save the current lat and long values when the button becomes pressed and use the distanceBetween() function to calculate how far the GPS has moved

@nikhil9398 Did you deliberately not follow the advice to use code tags when posting your code ?

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

What does your current code do that does not meet your requirements ?

While compiling I am getting a "gps_fix does not name a type" error. and I'm confused that is it the correct way to proceed and test my GPS module?

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
// The TinyGPS++ object
TinyGPSPlus gps;
gps_fix          fix; // the current GPS fix

const int PUSH_BUTTON = 2;
int buzzer = 12;
long a;
int status;

static const int RXPin = 4, TXPin = 3; //gps module connections
static const uint32_t GPSBaud = 9600;
float lat, lng;
float homeLat = 12.334455;
float homeLon = 05.112233;
#define Radius 0.0005 // Buzzer alert Radius = 50 m

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup(){
    pinMode(PUSH_BUTTON, INPUT_PULLUP); //push button input
    pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output

void loop(){
  unsigned char i;

  while (ss.available() > 0){
    if (gps.location.isUpdated()){
      Serial.print("Latitude= "); 
      Serial.print(, 6);
      Serial.print(" Longitude= "); 
      Serial.println(gps.location.lng(), 6);
    status = digitalRead(PUSH_BUTTON);
    if (status== HIGH){
          fix =; // save the latest

     if (fix.valid.location) {
    float dist = fix.location.DistanceKm( home );      
     if (dist >= Radius ){

        digitalWrite(buzzer, HIGH);
      digitalWrite(buzzer, LOW);


gps_fix fix; // the current GPS fix
What are you trying to do with this line of code ?

@UKHeliBob, declaring the fix mentioned fix =; // save the latest line.

That line of code is trying to declare a variable named fix of datatype gps_fix but that datatype does not exist

Where did you get the code ?

I see, in that case, how to save the location of the particular position upon pressing the push button, btw I have taken that line from one of the codes in forum.

Personally I think that I would save the latitude and longitude in separate variables as that then gives you the ability to calculate how far the GPS has moved

Is there any chance that you can provide a link to where you found that line if code ?

"Geofencing using Arduino and GPS - #3 by dev_1"

Your taking code from one library, NMEAGPS.h and using it with another library TinyGPS++.h

1 Like

A great place to start learning about using a code library is to study the examples that come with a particular library.

Then, when writing your own code, check that library's documentation carefully.

1 Like

Sure, Thank you for the sugesstion. :slight_smile:

Use the examples provided with the library to test your GPS module.

1 Like

Yes John, I have tried. Thank You :slight_smile:

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