0% found this document useful (0 votes)
5 views9 pages

Code 1

The document provides a complete diagnostic code for checking the status of a GSM module using AT commands, including checks for module communication, SIM card detection, signal strength, and network registration. It also includes wiring diagrams, power supply requirements, and integration testing code for connecting an ultrasonic sensor to the GSM module. Additionally, it offers troubleshooting tips and expected responses for successful communication with the GSM module.

Uploaded by

girish_ugh5138
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Code 1

The document provides a complete diagnostic code for checking the status of a GSM module using AT commands, including checks for module communication, SIM card detection, signal strength, and network registration. It also includes wiring diagrams, power supply requirements, and integration testing code for connecting an ultrasonic sensor to the GSM module. Additionally, it offers troubleshooting tips and expected responses for successful communication with the GSM module.

Uploaded by

girish_ugh5138
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Here is the complete diagnostic code to check the status of your GSM module.

It runs
a series of AT commands automatically to check if the module is active, reading the SIM
card, and connected to a network.

Diagnostic Testing Code


cpp
#include <SoftwareSerial.h>

// Pins: 10 is RX (connect to TX of GSM), 11 is TX (connect to RX of GSM)


SoftwareSerial gsmSerial(10, 11);

void setup() {
[Link](9600);
[Link](9600);

[Link]("--- Starting GSM Module Diagnostic Check ---");


delay(3000); // Wait for the module to fully power up

checkModuleResponse();
checkSIMCard();
checkSignalStrength();
checkNetworkRegistration();

[Link]("--- Diagnostic Check Complete ---");


[Link]("You can now type manual AT commands below:");
}

void loop() {
// Pass-through loop so you can manually test commands
if ([Link]()) {
[Link]([Link]());
}
if ([Link]()) {
[Link]([Link]());
}
}

// 1. Check basic communication


void checkModuleResponse() {
[Link]("\nChecking module communication...");
[Link]("AT");
delay(1000);
printGSMResponse();
}

// 2. Check if SIM card is detected


void checkSIMCard() {
[Link]("\nChecking SIM Card status...");
[Link]("AT+CPIN?");
delay(1000);
printGSMResponse();
}
// 3. Check signal quality
void checkSignalStrength() {
[Link]("\nChecking Signal Strength (RSSI)...");
[Link]("AT+CSQ");
delay(1000);
printGSMResponse();
}

// 4. Check cellular network registration status


void checkNetworkRegistration() {
[Link]("\nChecking Network Registration...");
[Link]("AT+CREG?");
delay(1000);
printGSMResponse();
}

// Helper function to read and print responses from the GSM module
void printGSMResponse() {
while([Link]()) {
char c = [Link]();
[Link](c);
}
[Link]();
}
Use code with caution.
How to Understand the Results
Open your Serial Monitor at 9600 baud with Both NL & CR enabled. Look for these
specific outputs to verify your hardware status:
 Communication Check (AT)
o Expected Response: OK
o If missing: Check your TX/RX wire connections or swap them. Ensure your baud rates
match.
 SIM Card Check (AT+CPIN?)
o Expected Response: +CPIN: READY
o If it says ERROR or CME ERROR: Your SIM card is inserted backward, loose, or needs
a PIN code removed.
 Signal Strength Check (AT+CSQ)
o Expected Response: +CSQ: [value],99 (e.g., +CSQ: 18,99)
o Values breakdown: Less than 10 means the signal is too weak. Values between 15 and
31 represent good to excellent network signal.
 Network Registration (AT+CREG?)
o Expected Response: +CREG: 0,1 (Registered home network) or +CREG: 0,5
(Registered roaming network)
o If it says +CREG: 0,0 or 0,2: The module is searching for network signal. Check your
external power source, as a weak power supply causes the module to constantly reset
while registering.
Tell me what results you see in your Serial Monitor, or let me know if you are getting
an error message so we can fix it!
You said: hoe to module communication
To make your Arduino and GSM module communicate smoothly, you must align three
critical components: physical wiring, shared power, and software serial pins.
Here is exactly how to set up and troubleshoot that core communication link.

1. The Correct Wiring Diagram

To prevent damaging your Arduino, we cross the communication lines (TX goes to RX
and vice versa) and avoid the Arduino's main hardware serial pins (0 and 1).
GSM Module Pin Arduino Pin Why?
TXD (Transmit) Pin 10 (Software RX) The GSM module talks, Arduino listens.
RXD (Receive) Pin 11 (Software TX) Arduino talks, the GSM module listens.
GND (Ground) GND Mandatory. Without shared ground, data gets corrupted.
Note on Logic Levels: If you are using a SIM800L, its RX pin expects 3.3V logic. If
connecting directly to a 5V Arduino Uno, it is highly recommended to place a simple
voltage divider (a 1kΩ and 2kΩ resistor) on the Arduino's Pin 11 to drop the voltage
down.

2. The Power Supply Fix (The #1 Cause of Failure)

If your wiring is perfect but you still get no response, power is the issue.
 The Problem: A GSM module requires up to 2 Amps of current when searching for a
network. The Arduino's 5V pin can only provide around 0.5 Amps.
 The Symptom: The module's onboard LED blinks rapidly, or it shuts down and restarts
the second you try to communicate.
 The Solution: Use an external power adapter (e.g., 5V 2A or 12V 2A depending on
your specific shield) or a high-discharge lithium battery to power the GSM module's
VCC/GND pins directly.
3. Basic Code to Start Communication

Upload this short sketch to bridge your computer to the module. It allows you to
manually type into the Arduino Serial Monitor and see if the module replies.

cpp

#include <SoftwareSerial.h>

// Set up a virtual serial port: Arduino RX = Pin 10, Arduino TX = Pin 11


SoftwareSerial myGSM(10, 11);

void setup() {
// Open communication with your Computer
[Link](9600);

// Open communication with your GSM Module (most default to 9600)


[Link](9600);

[Link]("Communication established. Type 'AT' to test the


module.");
}

void loop() {
// If the GSM module sends data, print it to the computer
if ([Link]()) {
[Link]([Link]());
}

// If you type data into the computer, send it to the GSM module
if ([Link]()) {
[Link]([Link]());
}
}
Use code with caution.

4. How to Test It

1. Open your Arduino IDE Serial Monitor.


2. Look at the bottom right dropdown menus and set the baud rate to 9600.
3. Set the line ending dropdown to Both NL & CR (Newline and Carriage Return).
4. Type AT in the top bar and press Enter.
5. If the communication is working, the GSM module will instantly reply with:
text
OK
Use code with caution.
If you type AT and nothing happens, let me know:
 Is your module's status LED blinking fast (searching), blinking slowly (connected), or
completely off?
To connect an Ultrasonic Sensor (HC-SR04) and a GSM Module to an Arduino Uno,
you must use separate digital pins for each component and ensure they all share a
common ground.
Because GSM modules draw large spikes of current (up to 2A), you cannot power the
GSM module from the Arduino. It requires a dedicated external power supply.

🏢 System Architecture & Wiring Diagram


+---------------------------------------------+

| EXTERNAL POWER SUPPLY |


| (e.g., 5V-12V 2A Adapter) |
+----------------------+--------------+-------+

| (+) | (-)
v |
+-----------------------+ +------------+ |

| GSM MODULE | | | |
| | | | |
| VCC / PWR <------------+ | |
| GND <-------------------------+---+

| TXD ------------> Pin 10 |


| RXD <------------ Pin 11 |
+-----------------------+ |

|
+-----------------------+ |
| ARDUINO UNO | |
| | |
| Pin 10 (Soft RX) | |
| Pin 11 (Soft TX) | |
| Pin 6 (Trigger) | |
| Pin 7 (Echo) | |
| 5V Pin ------+ | |
| GND Pin ----+ | | |
+------------------|-|--+ |

| | |
| +-----------------------+
v
+-----------------------+

| ULTRASONIC SENSOR |
| (HC-SR04) |
| |
| VCC <--------- (From Arduino 5V)

| Trig <--------- Pin 6


| Echo ---------> Pin 7
| GND <--------- (From Arduino GND)
+-----------------------+

📌 Pin Connection Reference Table

Component
Component Arduino Uno Pin Power Connection
Pin
GSM Pin 10 (Software
TXD
Module RX)
GSM Pin 11 (Software
RXD
Module TX)
GSM Connected to External Supply Ground and
GND GND
Module Arduino GND
GSM
VCC / PWR Do Not Connect Connected to External Power Supply (+)
Module
Ultrasonic VCC Connected to Arduino 5V Pin
Ultrasonic Trig Pin 6
Ultrasonic Echo Pin 7
Ultrasonic GND GND Connected to Arduino GND Pin
Safety Check: If you are using a SIM800L module, remember that its RX line prefers
3.3V logic. It is highly recommended to place a 1kΩ / 2kΩ voltage divider between
Arduino Pin 11 and the SIM800L RX pin to keep the hardware safe.

💻 Integration Testing Code

This sketch reads the distance from the ultrasonic sensor. If an object gets closer than
20 centimeters, it triggers the GSM module to send an alert text message.

cpp

#include <SoftwareSerial.h>

// Pin configurations
const int TRIG_PIN = 6;
const int ECHO_PIN = 7;
const int DISTANCE_THRESHOLD = 20; // Threshold in centimeters
// Setup Software Serial for GSM (RX, TX)
SoftwareSerial gsmSerial(10, 11);

void setup() {
// Initialize physical pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);

// Start communications
[Link](9600);
[Link](9600);

[Link]("System Initializing... Waiting 3 seconds for GSM.");


delay(3000);
}

void loop() {
long distance = getDistance();
[Link]("Current Distance: ");
[Link](distance);
[Link](" cm");

// Check if an object crossed the threshold


if (distance > 0 && distance < DISTANCE_THRESHOLD) {
[Link]("Threshold breached! Sending SMS alert...");
sendAlertSMS(distance);

// Cool-down delay to avoid sending infinite spam texts


delay(30000);
}

delay(500); // Small pause between readings


}

// Function to calculate ultrasonic sensor distance


long getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

long duration = pulseIn(ECHO_PIN, HIGH);


long cm = duration * 0.034 / 2; // Convert time to distance
return cm;
}

// Function to interface with GSM and send text


void sendAlertSMS(long objectDistance) {
[Link]("AT+CMGF=1"); // Set text mode
delay(1000);

// CHANGE THIS to your actual receiving mobile number


[Link]("AT+CMGS=\"+1234567890\"");
delay(1000);

// Construct the text message payload


[Link]("ALERT: Intruder detected! Object distance is ");
[Link](objectDistance);
[Link](" cm.");
delay(1000);

[Link](26); // Sends Ctrl+Z command to dispatch the SMS


delay(5000);
[Link]("SMS Process Completed.");
}

You might also like