Speed Alert with buzzer and LED

Hi guys i am currently building a project where if a speed of 5MPH is reached a 12v siren and a 12v Light will flash. i have reduced the speed from 5mph down to 0.01mph to test the siren and light and this works well however my issue is that the GPS module will not give me the correct speed when moving, what i have found is that if i unplug the unit from the USB and plug it back in again it gives the correct speed. Below is the code

#include <TinyGPS++.h>
#include <AltSoftSerial.h>

TinyGPSPlus gps;
AltSoftSerial gpsSerial;

const int sirenPin = 13; // Pin controlling the siren relay (IN2)
const int ledPin = 7; // Pin controlling the LED relay (IN1)
float speedThreshold = 5.00; // Speed threshold to activate siren and LED

void setup() {
Serial.begin(9600);
gpsSerial.begin(9600);

pinMode(sirenPin, OUTPUT); // Pin controlling the siren relay
pinMode(ledPin, OUTPUT); // Pin controlling the LED relay

digitalWrite(sirenPin, LOW); // Initially turn off relay (siren off)
digitalWrite(ledPin, LOW); // Initially turn off relay (LED off)

Serial.println("Waiting for GPS fix...");
}

void loop() {
while (gpsSerial.available() > 0) {
gps.encode(gpsSerial.read());

if (gps.location.isValid() && gps.speed.isValid()) {
  float speed = gps.speed.mph();
  
  if (speed >= speedThreshold) {
    digitalWrite(sirenPin, HIGH);  // Activate the siren relay
    digitalWrite(ledPin, HIGH);    // Activate the LED relay
  } else {
    digitalWrite(sirenPin, LOW);   // Deactivate the siren relay
    digitalWrite(ledPin, LOW);     // Deactivate the LED relay
  }
  
  Serial.print("Speed mph: ");
  Serial.println(speed);
} else {
  Serial.println("Waiting for valid GPS fix and speed data...");
}

}
}

How far have you moved from fix to fix? What is the minimum distance that your GPS shows movement?

This would be a fast walk... were you walking fast?

Hi thanks for the reply... I have only tested by driving 1 mile round trip. If the GPS doesn't have a fix then the speed will not show and a warning text will be displayed saying 'waiting for GPS' only once it has a fix will the speed show... This is all visible via the serial port to laptop. Even when stood I will get a response of something like 0.06 MPH but that's neither here nor there. I have tried 2 different GPS modules and getting the same results.

I am wondering if a different method of reading the speed is the way to go

It will be used for a moving product is the end game

In your test, were you walking quickly (>5mph)?

i placed the project in a box (cardboard) and used a folk truck for speed testing in the yard. I have also used my car on a mile round trip to see if i could get anything whether that be a true reading or an alert and neither happened. I just wanted to confirm that the code is correct and i haven't missed something silly. If the GPS isn't the best way to monitor the speed i will have to try another method.

How do the results compare to using your smart phone and a GPS application that shows your speed?

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