GSM Scan Networks

Check for available networks in your area.

This example prints out the IMEI number of the modem, then checks to see if it's connected to a carrier and prints out its signal strength. It also scans for all nearby networks.

Hardware Required

Circuit

image of the Arduino GSM Shield on top of an Arduino board
image of the Arduino GSM Shield on top of an Arduino board

Code

First, import the GSM library

#include <GSM.h>

SIM cards may have a PIN number that enables their functionality. Define the PIN for your SIM. If your SIM has no PIN, you can leave it blank :

#define PINNUMBER ""

Initialize instances of the classes you're going to use. You're going to need the GSM, GSMScanner, and GSMModem classes.

1GSM gsmAccess;
2
3GSMScanner scannerNetworks;
4
5GSMModem modemTest;

Create a variable to hold the IMEI number, and a status messages to send to the serial monitor:

1String IMEI = "";
2
3String errortext = "ERROR";

In

setup
, open a serial connection to the computer. After opening the connection, send a message to the Serial Monitor indicating the sketch has started. Call @scannerNetworks.begin()@@ to reset the modem.

1void setup(){
2
3 Serial.begin(9600);
4
5 Serial.println("GSM networks scanner");
6
7 scannerNetworks.begin();

Create a local variable to track the connection status. You'll use this to keep the sketch from starting until the SIM is connected to the network :

1boolean notConnected = true;

Connect to the network by calling

gsmAccess.begin()
. It takes the SIM card's PIN as an argument. By placing this inside a
while()
loop, you can continually check the status of the connection. When the modem does connect,
gsmAccess()
will return
GSM_READY
. Use this as a flag to set the
notConnected
variable to
true
or
false
. Once connected, the remainder of
setup
will run.

1while(notConnected)
2
3 {
4
5 if(gsmAccess.begin(PINNUMBER)==GSM_READY)
6
7 notConnected = false;
8
9 else
10
11 {
12
13 Serial.println("Not connected");
14
15 delay(1000);
16
17 }
18
19 }

Get the IMEI of the modem with

modemTest.getIMEI()
and print it out to the serial monitor.

1Serial.print("Modem IMEI: ");
2
3 IMEI = modemTest.getIMEI();
4
5 IMEI.replace("\n","");
6
7 if(IMEI != NULL)
8
9 Serial.println(IMEI);

In

loop()
, scan and print out all available networks. This may take some time

1Serial.println("Scanning available networks. May take some seconds.");
2
3 Serial.println(scannerNetworks.readNetworks());

Print out the current connected carrier, and the strength of the signal. Signal strength is on a scale of 0-31, where 0 is the lowest, and 31 is the highest. close the

loop()
.

1Serial.print("Current carrier: ");
2
3 Serial.println(scannerNetworks.getCurrentCarrier());
4
5 Serial.print("Signal Strength: ");
6
7 Serial.print(scannerNetworks.getSignalStrength());
8
9 Serial.println(" [0-31]");

Once your code is uploaded, open the serial monitor to see the status of the connection.

Complete Sketch

The complete sketch is below.

1/*
2
3 GSM Scan Networks
4
5 This example prints out the IMEI number of the modem,
6
7 then checks to see if it's connected to a carrier. If so,
8
9 it prints the phone number associated with the card.
10
11 Then it scans for nearby networks and prints out their signal strengths.
12
13 Circuit:
14
15 * GSM shield
16
17 * SIM card
18
19 Created 8 Mar 2012
20
21 by Tom Igoe, implemented by Javier Carazo
22
23 Modified 4 Feb 2013
24
25 by Scott Fitzgerald
26
27 http://www.arduino.cc/en/Tutorial/GSMToolsGsmScanNetworks
28
29 This example code is part of the public domain
30
31 */
32
33// libraries
34#include <GSM.h>
35
36// PIN Number
37#define PINNUMBER ""
38
39// initialize the library instance
40
41GSM gsmAccess; // include a 'true' parameter to enable debugging
42
43GSMScanner scannerNetworks;
44
45GSMModem modemTest;
46
47// Save data variables
48
49String IMEI = "";
50
51// serial monitor result messages
52
53String errortext = "ERROR";
54
55void setup() {
56
57 // initialize serial communications and wait for port to open:
58
59 Serial.begin(9600);
60
61 while (!Serial) {
62
63 ; // wait for serial port to connect. Needed for Leonardo only
64
65 }
66
67 Serial.println("GSM networks scanner");
68
69 scannerNetworks.begin();
70
71 // connection state
72
73 bool notConnected = true;
74
75 // Start GSM shield
76
77 // If your SIM has PIN, pass it as a parameter of begin() in quotes
78
79 while (notConnected) {
80
81 if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
82
83 notConnected = false;
84
85 } else {
86
87 Serial.println("Not connected");
88
89 delay(1000);
90
91 }
92
93 }
94
95 // get modem parameters
96
97 // IMEI, modem unique identifier
98
99 Serial.print("Modem IMEI: ");
100
101 IMEI = modemTest.getIMEI();
102
103 IMEI.replace("\n", "");
104
105 if (IMEI != NULL) {
106
107 Serial.println(IMEI);
108
109 }
110}
111
112void loop() {
113
114 // scan for existing networks, displays a list of networks
115
116 Serial.println("Scanning available networks. May take some seconds.");
117
118 Serial.println(scannerNetworks.readNetworks());
119
120 // currently connected carrier
121
122 Serial.print("Current carrier: ");
123
124 Serial.println(scannerNetworks.getCurrentCarrier());
125
126 // returns strength and ber
127
128 // signal strength in 0-31 scale. 31 means power > 51dBm
129
130 // BER is the Bit Error Rate. 0-7 scale. 99=not detectable
131
132 Serial.print("Signal Strength: ");
133
134 Serial.print(scannerNetworks.getSignalStrength());
135
136 Serial.println(" [0-31]");
137
138}

Last revision 2018/08/23 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.