In today’s interconnected world, knowing the location of your vehicle can be incredibly useful. Whether it’s for security, fleet management, or simply keeping track of your car, a GPS tracker is a valuable tool. This guide will walk you through How To Program A Car Gps tracker using an Arduino, a SIM800L GSM module, and a GPS module. This project combines the power of Arduino programming with readily available hardware to create a functional car tracking system that can send location updates via SMS.
This tutorial is designed for hobbyists and makers who are interested in DIY electronics and basic Arduino programming. By following these steps, you’ll learn how to interface a GPS module and a GSM module with an Arduino to build a system that not only tracks your car’s location but also allows for remote control of a relay, which can be used for various applications like remotely switching off the engine in emergencies or controlling other car accessories.
Let’s dive into the components you’ll need and the code that makes it all work.
Components Needed
To get started with programming your car GPS tracker, you’ll need the following components:
- Arduino Board: An Arduino Uno or similar microcontroller board. This will be the brain of your GPS tracking system.
- SIM800L GSM Module: This module will enable your Arduino to communicate over the cellular network, allowing you to send and receive SMS messages for location updates and remote control.
- GPS Module (e.g., TinyGPS): This module will determine the car’s geographical location (latitude and longitude).
- SoftwareSerial Library: This Arduino library allows serial communication on other digital pins of the Arduino, used for communicating with the GPS and GSM modules.
- TinyGPS Library: A lightweight Arduino library for parsing NMEA GPS data.
- Relay Module: A relay to control external circuits, in this example, used for remote control via SMS.
- Connecting Wires: To connect all the modules to the Arduino.
- Power Supply: To power the Arduino and modules.
- SIM Card: A SIM card with SMS capability for the GSM module.
Setting up the Hardware
Connect the components as follows:
- SIM800L GSM Module:
- Connect the SIM800L’s RX pin to Arduino pin 7.
- Connect the SIM800L’s TX pin to Arduino pin 8.
- Connect the SIM800L’s VCC and GND to a 5V power supply and Ground respectively. Ensure the SIM800L has adequate power, sometimes a separate power supply is recommended.
- Insert the SIM card into the SIM800L module.
- GPS Module:
- Connect the GPS module’s TX pin to Arduino pin 4.
- Connect the GPS module’s RX pin to Arduino pin 5.
- Connect the GPS module’s VCC and GND to 5V and Ground.
- Relay Module:
- Connect the relay control pin to Arduino pin 12.
- Connect the relay module’s VCC and GND to 5V and Ground.
- LED (Optional):
- Connect an LED (with a resistor) to Arduino pin 9. This is used in the code for GSM module power control.
Arduino Code Explanation: Programming the GPS Tracker
Now, let’s examine the Arduino code that brings our car GPS tracker to life. The code is designed to:
- Initialize Modules: Set up serial communication for both the GSM and GPS modules and initialize the relay pin.
- Read GPS Data: Continuously read data from the GPS module and parse it to get latitude and longitude coordinates.
- Send SMS on Trigger: When a digital input pin (pin 9 in this code, can be connected to a car sensor or switch) goes HIGH, the system sends an SMS with the current GPS coordinates to a predefined phone number.
- Remote Control via SMS: The system also listens for incoming SMS messages. It can recognize commands “ON”, “OFF”, and “STATE” to control the relay and query the relay’s state.
Here’s a breakdown of the code sections:
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <String.h>
SoftwareSerial Sim800L(7, 8); // RX, TX for GSM module
int state = 0;
const int pin = 9; // Pin to trigger SMS location update
float gpslat, gpslon; // Variables to store latitude and longitude
TinyGPS gps; // GPS parser object
SoftwareSerial sgps(4, 5); // RX, TX for GPS module
String textMessage; // Variable to store incoming SMS
String Engine = "HIGH"; // Variable to store relay state
const int relay = 12; // Relay control pin
This section includes necessary libraries, defines serial ports for GSM and GPS communication using SoftwareSerial
, initializes variables for GPS coordinates, SMS messages, and relay state, and sets up pin assignments.
void setup() {
// Power control for GSM module (can be adapted based on your 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 state
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
}
The setup()
function initializes the digital pin 9 for potential GSM module power cycling (specific to some modules and might not be needed), sets the relay pin as an output and turns the relay OFF initially, and starts serial communication for debugging and with the GSM and GPS modules.
void loop() {
while (sgps.available()) { // Read GPS data if available
int c = sgps.read();
if (gps.encode(c)) { // Parse GPS data
gps.f_get_position(&gpslat, &gpslon); // Get latitude and longitude
}
}
if (digitalRead(pin) == HIGH && state == 0) { // Trigger SMS on pin HIGH
sendLocationSMS(); // Function to send location SMS
state = 1;
}
if (digitalRead(pin) == LOW) {
state = 0;
}
// GSM module initialization and SMS handling
delay(20000); // Wait for GSM module to connect to network
Serial.println("SIM900 ready...");
Sim800L.print("AT+CMGF=1r"); // Set SMS mode to text
delay(100);
Sim800L.print("AT+CNMI=2,2,0,0,0r"); // Configure SMS indication to serial
if (Sim800L.available() > 0) { // Check for incoming SMS
textMessage = Sim800L.readString(); // Read incoming SMS
Serial.print(textMessage);
if (textMessage.indexOf("ON") >= 0) { // Check for "ON" command
digitalWrite(relay, LOW); // Turn relay ON
Engine = "on";
Serial.println("Relay set to ON");
textMessage = "";
}
if (textMessage.indexOf("OFF") >= 0) { // Check for "OFF" command
digitalWrite(relay, HIGH); // Turn relay OFF
Engine = "off";
Serial.println("Relay set to OFF");
textMessage = "";
}
if (textMessage.indexOf("STATE") >= 0) { // Check for "STATE" command
String message = "Engine is " + Engine;
sendSMS(message); // Send relay state via SMS
Serial.println("Engine state request");
textMessage = "";
}
}
}
void sendLocationSMS() {
String message = "Latitude: " + String(gpslat, 6) + ", Longitude: " + String(gpslon, 6);
sendSMS(message);
}
void sendSMS(String message) {
Sim800L.print("AT+CMGF=1r"); // Set SMS mode to text
delay(100);
Sim800L.println("AT + CMGS = "XXXXXXXXXXXXXX""); // Replace with recipient phone number
delay(100);
Sim800L.println(message); // Send SMS message
delay(100);
Sim800L.println((char)26); // End SMS command with CTRL+Z (ASCII 26)
delay(100);
Sim800L.println();
delay(5000); // Wait for SMS to be sent
}
The loop()
function is the heart of the program. It continuously checks for GPS data, parses it, and triggers an SMS location update when the digital pin 9 goes HIGH. It also handles GSM module initialization, waits for incoming SMS messages, and processes commands (“ON”, “OFF”, “STATE”) to control the relay remotely. The sendLocationSMS()
and sendSMS()
functions encapsulate the SMS sending logic, making the main loop cleaner and more readable. Remember to replace "XXXXXXXXXXXXXX"
with the actual phone number you want to send SMS messages to.
Step-by-step Programming Guide
- Install Libraries: In your Arduino IDE, install the “TinyGPSPlus” and “SoftwareSerial” libraries if you haven’t already. (Although
SoftwareSerial
is usually included, ensure it’s available). - Copy and Paste Code: Copy the complete Arduino code provided above into your Arduino IDE.
- Modify Phone Number: Replace
"XXXXXXXXXXXXXX"
in thesendSMS
function with the recipient’s phone number, including the country code. - Upload Code: Connect your Arduino to your computer and upload the code to your Arduino board.
- Power Up: Power up your Arduino and the connected modules. Ensure the GSM module has a valid SIM card and sufficient signal.
- Testing:
- GPS Location: Monitor the serial monitor for “Latitude” and “Longitude” readings. Ensure the GPS module has a clear sky view to get a GPS fix.
- SMS Location Update: Trigger the SMS location update by briefly connecting pin 9 to 5V. You should receive an SMS with the car’s GPS coordinates on the specified phone number.
- Remote Relay Control: Send SMS messages “ON”, “OFF”, and “STATE” to the SIM card number in the GSM module. Observe the relay’s state and the serial monitor output.
Conclusion
You have now learned how to program a car GPS tracker using Arduino! This project provides a foundation for building more advanced car tracking and remote control systems. You can expand upon this by adding features like:
- Real-time GPS Tracking: Send location updates at regular intervals to a server or web platform.
- Geofencing: Set virtual boundaries and receive alerts when the car enters or exits these areas.
- Enhanced Security Features: Integrate with car alarm systems or immobilizers.
- Data Logging: Store GPS data on an SD card for later analysis.
By understanding the principles of GPS tracking, GSM communication, and Arduino programming, you can tailor this project to meet specific needs and create a sophisticated car management system. Remember to always operate within legal and ethical boundaries when implementing such technology.