Implementing GPS tracking in your car can offer a range of benefits, from enhancing security to monitoring vehicle usage. This guide will walk you through the fundamentals of setting up a DIY GPS tracking system in your car using Arduino, a GPS module, and a GSM module. This project combines hardware and software programming to pinpoint your car’s location and even control a relay remotely via SMS commands.
Understanding the Components
Before diving into the code, let’s break down the essential components we’ll be using:
- Arduino Board: The brain of our operation. Arduino is a microcontroller platform that allows us to program and control electronic components.
- GPS Module (e.g., TinyGPS): This module receives signals from GPS satellites to determine the precise location (latitude and longitude).
- GSM Module (e.g., Sim800L): This module enables communication over a cellular network. We’ll use it to send SMS messages containing the GPS coordinates and receive SMS commands to control a relay.
- SoftwareSerial Library: Since we’re using multiple serial devices (GPS and GSM) with Arduino, we’ll utilize the SoftwareSerial library to create additional serial ports using digital pins.
- Relay Module: An electromagnetic switch that allows us to control high-voltage circuits with a low-voltage signal from the Arduino. In this example, it’s used for demonstration purposes and could potentially control car accessories in more advanced applications.
Setting Up Your Arduino Environment
- Install Arduino IDE: If you haven’t already, download and install the Arduino Integrated Development Environment (IDE) from the official Arduino website.
- Install Libraries:
- TinyGPS++: This library helps parse the raw NMEA data from the GPS module and extract usable latitude and longitude values. You can install it through the Arduino Library Manager (Sketch > Include Library > Manage Libraries…). Search for “TinyGPS++” and install it.
- SoftwareSerial: This library is usually included with the Arduino IDE, but ensure it’s available in your environment.
Wiring the Components
Connect the components to your Arduino board as follows:
-
GPS Module (sgps):
- GPS RX pin to Arduino pin 4 (SoftwareSerial TX)
- GPS TX pin to Arduino pin 5 (SoftwareSerial RX)
- GPS VCC to Arduino 5V
- GPS GND to Arduino GND
-
GSM Module (Sim800L):
- Sim800L RX pin to Arduino pin 7 (SoftwareSerial TX)
- Sim800L TX pin to Arduino pin 8 (SoftwareSerial RX)
- Sim800L VCC to Arduino 5V (ensure sufficient power supply for Sim800L, sometimes external power is recommended)
- Sim800L GND to Arduino GND
- Connect the Sim800L’s power key (if applicable) to Arduino digital pin 9 through a simple transistor circuit or directly if your Sim800L module allows direct digital control for power ON/OFF. This part of the code in the original extract is for automatically turning on the shield.
-
Relay Module:
- Relay IN pin to Arduino pin 12
- Relay VCC to Arduino 5V
- Relay GND to Arduino GND
- Connect the relay’s normally open (NO) or normally closed (NC) contacts to control an external circuit as needed for your application. For this example, it’s simply toggled by SMS commands.
-
Digital Input Pin (pin):
- Connect a button or sensor to Arduino digital pin 9. When this pin goes HIGH, it will trigger sending the GPS coordinates via SMS.
Arduino Code Explanation
Below is the Arduino code, broken down for clarity:
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
SoftwareSerial Sim800L(7, 8); // RX, TX for GSM module
int state = 0;
const int pin = 9; // Digital pin for trigger input
float gpslat, gpslon; // Variables to store latitude and longitude
TinyGPSPlus gps; // GPS object
SoftwareSerial sgps(4, 5); // RX, TX for GPS module
String textMessage; // Variable to store received SMS messages
String Engine = "HIGH"; // Variable to track relay state
const int relay = 12; // Relay control pin
void setup() {
// Power ON/OFF sequence for Sim800L module
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(5000);
pinMode(relay, OUTPUT); // Set relay pin as output
digitalWrite(relay, HIGH); // Initialize relay to OFF
Serial.begin(9600); // Initialize serial monitor for debugging
Sim800L.begin(9600); // Initialize serial communication with GSM module
sgps.begin(9600); // Initialize serial communication with GPS module
}
void loop() {
// Read GPS data
while (sgps.available()) {
int c = sgps.read();
if (gps.encode(c)) { // Encode GPS data
gps.f_get_position(&gpslat, &gpslon); // Get latitude and longitude
}
}
// Trigger SMS sending when digital pin 9 goes HIGH
if (digitalRead(pin) == HIGH && state == 0) {
Sim800L.print("r");
delay(1000);
Sim800L.print("AT+CMGF=1r"); // Set SMS mode to text
delay(1000);
Sim800L.print("AT+CMGS=""xxxxxxxxxxxxxx""r"); // Replace with recipient phone number
delay(1000);
Sim800L.print("Latitude :");
Sim800L.println(gpslat, 6); // Send latitude with 6 decimal places
Sim800L.print("Longitude:");
Sim800L.println(gpslon, 6); // Send longitude with 6 decimal places
delay(1000);
Sim800L.write(0x1A); // End SMS message with Ctrl+Z
delay(1000);
state = 1; // Update state to prevent repeated SMS sending
}
if (digitalRead(pin) == LOW) {
state = 0; // Reset state when pin goes LOW
}
delay(20000); // Wait for network registration (adjust as needed)
Serial.println("SIM800 ready...");
Sim800L.print("AT+CMGF=1r"); // Set SMS mode to text again (ensure mode)
delay(100);
Sim800L.print("AT+CNMI=2,2,0,0,0r"); // Configure GSM to send SMS data to serial
delay(100);
// Check for incoming SMS messages
if (Sim800L.available() > 0) {
textMessage = Sim800L.readString(); // Read incoming SMS
Serial.print(textMessage);
// Control relay based on SMS commands
if (textMessage.indexOf("ON") >= 0) {
digitalWrite(relay, LOW); // Turn relay ON
Engine = "on";
Serial.println("Relay set to ON");
textMessage = "";
}
if (textMessage.indexOf("OFF") >= 0) {
digitalWrite(relay, HIGH); // Turn relay OFF
Engine = "off";
Serial.println("Relay set to OFF");
textMessage = "";
}
if (textMessage.indexOf("STATE") >= 0) {
String message = "Engine is " + Engine;
sendSMS(message); // Send SMS with relay state
Serial.println("Engine state request");
textMessage = "";
}
}
}
// Function to send SMS messages
void sendSMS(String message) {
Sim800L.print("AT+CMGF=1r"); // Set SMS mode to text
delay(100);
Sim800L.print("AT+CMGS=""xxxxxxxxxxxxxxxxxx""r"); // Replace with your phone number
delay(100);
Sim800L.println(message); // Send the message
delay(100);
Sim800L.println((char)26); // End SMS with Ctrl+Z
delay(100);
Sim800L.println();
delay(5000); // Wait for SMS sending
}
Code Breakdown:
- Includes: Includes necessary libraries for GPS and SoftwareSerial communication.
- SoftwareSerial Instances: Creates software serial ports for both GPS (
sgps
) and GSM (Sim800L
) modules. - Variables: Declares variables to store GPS coordinates, SMS messages, relay state, and pin assignments.
setup()
function:- Initializes serial communication for debugging, GPS, and GSM modules.
- Configures the relay pin as an output and turns the relay OFF initially.
- Includes a power ON/OFF pulse for the Sim800L module, which might be necessary for some modules to start correctly.
loop()
function:- GPS Data Reading: Continuously reads data from the GPS module and decodes it using the
TinyGPSPlus
library to get latitude and longitude. - SMS Trigger: Checks if digital pin 9 is HIGH and the
state
is 0. If both conditions are met, it sends an SMS message containing the current GPS latitude and longitude. Thestate
variable prevents repeated SMS sending for a single button press. - GSM Initialization and SMS Reception: After a delay for network registration, it configures the GSM module to SMS text mode and sets it up to forward incoming SMS messages to the serial port.
- SMS Command Processing: Checks for incoming SMS messages. If a message containing “ON”, “OFF”, or “STATE” is received, it controls the relay accordingly and sends back an SMS with the engine state if requested.
- GPS Data Reading: Continuously reads data from the GPS module and decodes it using the
sendSMS()
function: A separate function to encapsulate the SMS sending process, making the main loop cleaner.
Getting Started and Testing
- Upload the Code: Copy the code to your Arduino IDE, replace the placeholder phone numbers (
xxxxxxxxxxxxxx
) with your actual phone numbers (recipient for GPS location SMS and your number for SMS commands). Select the correct Arduino board and port, and upload the code. - Insert SIM Card: Insert an active SIM card into the Sim800L module.
- Power Up: Power your Arduino and connected modules. Ensure the Sim800L module has sufficient power.
- GPS Fix: Place the GPS module in an open area with a clear view of the sky to acquire a GPS fix. This may take a few minutes initially. You can monitor the serial monitor for debugging information and to see if GPS data is being received.
- Trigger SMS: Activate the trigger connected to digital pin 9 (e.g., press a button). You should receive an SMS message on the recipient phone number with the car’s GPS coordinates.
- Send SMS Commands: Send SMS messages “ON”, “OFF”, or “STATE” to the SIM card number in the Sim800L module. Observe the relay’s state change and the serial monitor output. You can also request the state via SMS and receive a reply.
Potential Applications and Further Improvements
This project provides a fundamental framework for GPS tracking in a car. You can expand upon this for more advanced applications:
- Vehicle Security: Implement geofencing to receive alerts if the car moves outside a defined area.
- Fleet Management: Track multiple vehicles for business or logistical purposes.
- Remote Immobilization: Incorporate a more robust relay control system for remote vehicle immobilization (use with caution and ethical considerations).
- Data Logging: Store GPS data on an SD card for historical tracking and analysis.
- Real-time Tracking Platforms: Integrate with online platforms or build your own web interface to visualize car location on a map in real-time.
This guide offers a starting point for programming GPS tracking in your car. By understanding the code, components, and potential applications, you can tailor this project to meet your specific needs and explore the exciting possibilities of DIY car electronics.