Professional Documents
Culture Documents
ESP32 GPS Tracker - IoT Based Vehicle Tracking System
ESP32 GPS Tracker - IoT Based Vehicle Tracking System
Menu
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 1/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Table of Contents
1. Prerequisites
2. GPS Vehicle Tracking System
3. Components Required for GPS Tracker
3.1. NEO 6M GPS Module
3.2. SSD1306 0.96-inch OLED Display
4. ESP32 GPS Tracker Circuit Diagram
5. Setting up Blynk Application for ESP32 Vehicle Tracker
5.1. Installing and Getting Ready
5.2. Creating Project
6. Installing Required Arduino Libraries
6.1. Installing Blynk Library
6.2. Installing TinyGPS++ Library
6.3. Installing OLED Display Libraries
7. Arduino Sketch ESP32 GPS Tracker
7.1. How the Code Works?
7.2. Demonstration
Prerequisites
We will use Arduino IDE to program our ESP32 development board. Thus, you
should have the latest version of Arduino IDE. Additionally, you also need to
install the ESP32 plugin.
If your IDE does not have the plugin installed you can visit the link
below: Installing ESP32 library in Arduino IDE and upload code.
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 2/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Type GPS
For more information regarding the NEO-6M module refer to its datasheet given
here.
Pinout of NEO 6M Module
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 3/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
The diagram below shows the pin out of the NEO 6M module. It consists of 4 pins
named GND, TX, RX and VCC.
This is the ground pin that will be connected with the ground of the
GND
ESP32 board.
This is the VCC pin used to power up the GPS module. Connect it with
VCC
the 3.3V of the ESP32 board
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 4/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Although there are several types of OLED displays available in the market the
one which we will be using is the SSD1306 0.96-inch OLED display. The main
component of all different types of OLED displays is an SSD1306 controller which
uses I2C or SPI protocol to communicate with the microcontrollers. The OLED
performs faster in SPI communication but it is popular with I2C communication.
The reason for the popularity is the lower number of pins. The OLED displays can
vary in size, colour, and shape but are primarily programmed in a similar way.
Let us take a look at the OLED display which we will be using in this article. It is
called SSD 1306 0.96-inch OLED display which has 128×64 pixels and
communicates only via I2C protocol with the ESP development boards. It is cheap
and readily available in the market.
Pinout of OLED Display (I2C only)
Below you can see the pinout of this OLED Display.
The OLED display has 4 terminals which we will connect with the ESP32 board. As
the OLED display requires an operating voltage in the range of 3.3-5V hence we
will connect the VCC terminal with 3.3V which will be in common with the ESP32
board. SCL of the display will be connected with the SCL pin of the module and
the SDA of the display will be connected with the SDA of the module. By default,
the I2C pin in ESP32 for SDA is GPIO21, and for SCL is GPIO22. The connections
between the two devices can be seen in the table below.
VCC=3.3V VCC
GND GND
VCC=3.3V VCC
RX2 TX
TX2 RX
GND GND
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 6/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 7/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
You will have to create an account to proceed further. You can also log in with
your Facebook account if you possess that.
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 8/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Creating Project
After you have successfully signed in the following window will appear. Click ‘New
Project.’
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 9/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Now specify the name of your project, device and connection type. After that
press the ‘Create’ button.
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 10/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
You will receive a notification regarding your Authorization token. This can be
accessed from the email account you signed in with and also through Project
Settings.
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 11/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Now the Blynk canvas will open. Press on the screen. The widget box will appear.
Press the ‘Map’ widget. You can view information regarding it by pressing the
icon present in the far right.
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 12/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Now click on this widget and the map settings appear. Choose the virtual pin V0
as an input pin. Press ‘OK’.
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 13/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 14/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
After you alter the settings of the map the canvas will look something like this:
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 15/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 16/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
You have to go to Sketch > Include Library > Add .zip Library inside the IDE to add
the library as well. After installation of the library, restart your IDE.
Installing TinyGPS++ Library
To make our project easier we will install the TinyGPS++ library to easily handle
the receiving data from the GPS module. To download the library, click here. Click
on ‘Code’ and then ‘Download Zip’.
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 17/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
You have to go to Sketch > Include Library > Add .zip Library inside the IDE to add
the library as well.
Installing OLED Display Libraries
To use the OLED display in our project, we have to install the Adafruit SSD 1306
library and Adafruit GFX library in Arduino IDE. Follow the steps below to
successfully install them.
Open Arduino IDE and click on Sketch > Library > Manage Libraries
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 18/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Type ‘SSD1306’ in the search tab and install the Adafruit SSD1306 OLED library.
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 19/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
We will also require the Adafruit GFX library which is a dependency for SSD1306.
Type ‘Adafruit GFX’ in the search tab and install it as well.
#include <TinyGPS++.h>
#include <HardwareSerial.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <BlynkSimpleEsp32.h>
WidgetMap myMap(V0);
WiFiClient client;
TinyGPSPlus gps;
HardwareSerial SerialGPS(2);
void setup()
{
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected!");
{
while (SerialGPS.available() > 0) {
if (gps.encode(SerialGPS.read()))
{
if (gps.location.isValid())
{
latitude = gps.location.lat();
latitude_string = String(latitude , 6);
longitude = gps.location.lng();
longitiude_string = String(longitude , 6);
Serial.print("Latitude = ");
Serial.println(latitude_string);
Serial.print("Longitude = ");
Serial.println(longitiude_string);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 20);
display.println("Latitude: ");
display.setCursor(45, 20);
display.print(latitude_string);
display.setCursor(0, 40);
display.print("Longitude: ");
display.setCursor(45, 40);
display.print(longitiude_string);
libraries are the ones which we previously installed and are required for the
proper functionality of the OLED display, the GPS module and the Blynk
application.
#include <TinyGPS++.h>
#include <HardwareSerial.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <BlynkSimpleEsp32.h>
Defining Variables
Next we will define some string and float variable that we will use later on in the
code. These will save the latitude and longitude values of the vehicle.
network credentials.
Creating Instances
Create the following instances for the widget map, wifi client, TinyGPSPlus library
and HardwareSerial.
WidgetMap myMap(V0);
WiFiClient client;
TinyGPSPlus gps;
HardwareSerial SerialGPS(2);
setup()
Inside the setup() function, we will open a serial connection at a baud rate of
115200.
Serial.begin(115200);
Moreover, we will also initialize the OLED display by using display.begin(). Make
sure you specify the correct address of your display. In our case, it is 0X3C.
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
The following section of code will connect our ESP32 board with the local
network whose network credentials we already specified above. We will use the
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 24/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
WiFi.begin() function. The arguments will be the SSID and the password which we
defined earlier in the code. After a successful connection is established,
“Connected to the WiFi network!” will get displayed on the serial monitor.
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected!");
Now, the GPS module and the Blynk appliation is also initialized.
loop()
Inside the loop() function, we will obtain data from the GPS module and check
whether the location parameters are valid or not. If the parameters are valid
then we save the values of longitude and latitude in their respective variables
after converting them in readable format.
Serial.print("Latitude = ");
Serial.println(latitude_string);
Serial.print("Longitude = ");
Serial.println(longitiude_string);
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 25/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Additionally, we will display the location on the OLED display as well. This is done
by the following lines of code.
First, we will clear the buffer by using clearDisplay() on the Adafruit_SSD1306
object. Next, we will control the colour of the text by using the setTextColor()
function and passing WHITE as an argument. We will set the size of the text using
setTextSize() and pass the size as a parameter inside it. We have set the font size
as default which is 1.We will use the setCursor() function to denote the x and the
y axis position from where the text should start. Then by using print() we will
pass the text which we want to display on the OLED. We will set the cursor again
at different positions to display the all the values correctly. We will call the
display() function on the display object so that the text displays on the OLED.
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 20);
display.println("Latitude: ");
display.setCursor(45, 20);
display.print(latitude_string);
display.setCursor(0, 40);
display.print("Longitude: ");
display.setCursor(45, 40);
display.print(longitiude_string);
display.display();
This GPS data is also sent to the Blynk application. The map will display the
current position of the vehicle/person by accessing the latitude and longitude
values.
Inside the loop function, we will use the Blynk.run() command to keep the
connection running.
Blynk.run();
Demonstration
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 26/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Choose the correct board and COM port before uploading your code to the
board.
Go to Tools > Board and select ESP32 Dev Module.
Next, go to Tools > Port and select the appropriate port through which your
board is connected.
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 27/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Click on the upload button to upload the code to your ESP32 development
board.
After you have uploaded your code to the development board, press its ENABLE
button.
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 28/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Open your Blynk application. Open the project you created and press the ‘play’
button. You will be able to view your current location in the map.
The OLED display will also start displaying your location in latitude and longitude
values.
To read more GPS related articles, follow the links below:
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 29/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Email Address
SUBSCRIBE
ohn than
September 13, 2021 at 9:56 am
Danny
May 10, 2022 at 11:38 pm
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 30/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
I like this project. But, this project needs a continuous WIFI connection,
doesn’t it?
Reply
Microcontrollers Lab
May 14, 2022 at 1:14 pm
Yes, it requires. But you can use an LTE module instead to get internet
connectivity.
Reply
Leave a Comment
Name *
Email *
Website
POST COMMENT
ESP32 MAX30100
ESP32 MAX30102
MLX90614 ESP32
K-Type Thermocouple
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 33/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Preferences Library
Reconnect to WiFi
ESP32 Andriod App
ESP32 MultiWiFi
ESP32 Rest API
ESP32 CAM
Image Classification ML
IoT CCTV Camera
ESP32 Motors Interfacing
A4988 Driver
SUBSCRIBE
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 34/35
7/23/22, 8:40 PM ESP32 GPS tracker - IoT based Vehicle Tracking System
Categories
Select Category
Recent Posts
Install Node-RED on Windows and Getting Started with Dashboard
Copyright © 2013-2022
Microcontrollerslab.com All Rights Reserved
https://microcontrollerslab.com/esp32-gps-tracker-iot-based-vehicle-tracking-system/ 35/35