Built-in project · Arduino Uno R3

Send an SMS on an Arduino Uno

The SIM800L tutorial sketch: AT, text mode, AT+CMGS and Ctrl+Z, on a simulated network. Type sms hello in the modem’s serial box to text it back. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $35. On Mokxi they're free.

Step by step, with the wiring and the common mistakes: Arduino SIM800L: Send and Receive SMS, AT Commands

The circuit runs here once the page has loaded.

What you'll learn

  • Talking to a modem with AT commands
  • A second serial port made in software
  • Why a modem needs its own power supply
  • Relaying text between the serial monitor and a device
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The SIM800L modem talks in AT commands, plain text lines. The Uno cannot use its main serial port, because the serial monitor is on it, so the sketch makes a second port on pins 3 and 2 at 9600 baud.

In setup it sends AT to wake the modem, then text mode, then the setting for new messages. Then it gives a phone number, sends the message text, and finishes with Ctrl+Z, character 26, which makes the modem send it.

The modem has its own battery supply because it needs short bursts of high current. Here the network is simulated, and the message appears in the serial monitor about three seconds later.

Parts list

3 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
GSM modem, SIM800L pin GNDArduino Uno R3 pin GND
Battery: AA, coin cell, LiPo pin V-Arduino Uno R3 pin GND
GSM modem, SIM800L pin TXDArduino Uno R3 pin 3
GSM modem, SIM800L pin RXDArduino Uno R3 pin 2
Battery: AA, coin cell, LiPo pin V+GSM modem, SIM800L pin VCC

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Send an SMS: the SIM800L sketch every tutorial starts with.
//
//   SIM800L TXD  -> pin 3   (the sketch's RX)
//   SIM800L RXD  <- pin 2   (the sketch's TX)
//   SIM800L VCC  -> an 18650 cell's +, GND -> the cell's - and the Uno's GND
//
// The modem wants 3.4 to 4.4 V and 2 A peaks, so it has a Li-ion cell of its
// own. On the Uno's 5 V pin it switches itself off and back on, over and over,
// as the real one does.
//
// The Uno's one hardware serial port belongs to the serial monitor, so the
// modem gets a SoftwareSerial port at 9600 baud. A bare AT lets the modem's
// autobaud find that rate; then text mode, the number, the text, and Ctrl+Z
// (character 26) to send it.
//
// There is no real phone network here. The modem has its own line in the
// serial monitor, and that line is the simulated network: the SMS shows up
// there about three seconds after Ctrl+Z. Type sms hello in it to text the
// modem back, and the sketch prints what the modem says.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 2); // RX, TX: the SIM800L's TXD and RXD

void updateSerial() {
  delay(500);
  while (Serial.available()) {
    mySerial.write(Serial.read()); // the serial monitor to the modem
  }
  while (mySerial.available()) {
    Serial.write(mySerial.read()); // the modem to the serial monitor
  }
}

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("Initializing...");
  delay(1000);

  mySerial.println("AT");        // the handshake: the modem answers OK
  updateSerial();
  mySerial.println("AT+CMGF=1"); // text mode
  updateSerial();
  mySerial.println("AT+CNMI=1,2,0,0,0"); // print new messages as they come
  updateSerial();
  mySerial.println("AT+CMGS=\"+15551234567\""); // the number to text
  updateSerial();
  mySerial.print("Hello from Mokxi"); // the message
  updateSerial();
  mySerial.write(26); // Ctrl+Z sends it
}

void loop() {
  updateSerial();
}

Code walkthrough

The key functions of the sketch, and what each one does.

updateSerial sketch.ino

Waits half a second, then passes anything typed in the serial monitor to the modem, and anything the modem says back to the monitor. It lets you see each reply as the setup goes on.

void updateSerial() {
  delay(500);
  while (Serial.available()) {
    mySerial.write(Serial.read()); // the serial monitor to the modem
  }
  while (mySerial.available()) {
    Serial.write(mySerial.read()); // the modem to the serial monitor
  }
}

setup sketch.ino

Opens both serial ports at 9600 baud, then sends AT, AT+CMGF=1, AT+CNMI=1,2,0,0,0 and AT+CMGS with the number. It prints the message text and ends with write(26), the Ctrl+Z that sends it.

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("Initializing...");
  delay(1000);

  mySerial.println("AT");        // the handshake: the modem answers OK
  updateSerial();
  mySerial.println("AT+CMGF=1"); // text mode
  updateSerial();
  mySerial.println("AT+CNMI=1,2,0,0,0"); // print new messages as they come
  updateSerial();
  mySerial.println("AT+CMGS=\"+15551234567\""); // the number to text
  updateSerial();
  mySerial.print("Hello from Mokxi"); // the message
  updateSerial();
  mySerial.write(26); // Ctrl+Z sends it
}

loop sketch.ino

Just calls updateSerial over and over, so replies and texts typed in the monitor keep flowing.

void loop() {
  updateSerial();
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Nothing comes back from the modem This project
    The wires cross over: the modem's TXD goes to Uno pin 3 and its RXD to pin 2. Check the grounds are shared.
  • The modem keeps restarting This project
    Do not power it from the Uno's 5 V pin. It needs its own battery on VCC.
  • The message is never sent This project
    The message must end with Ctrl+Z, write(26). Without it the modem keeps waiting for more text.
  • The module resets or will not register when powered from 5 V or an Arduino pin. It needs 3.4 to 4.4 V and bursts of up to 2 A. GSM modem
    Power it from a single Li-ion cell or a good 4 V supply with a 1000 uF capacitor. Join the grounds. Do not power it from the 3.3 V pin or 5 V.
  • AT commands get no reply. The baud rate may not match, and the logic is 2.8 V, not 5 V. GSM modem
    Try 9600 baud and send AT, expecting OK. Use a level shifter or divider on the module's RX line from a 5 V board. Check for a SIM, PIN and network signal.
  • The battery is wired backwards, or the voltage is too low or too high for the circuit. A fresh AA is about 1.5 V, a LiPo is 3.7 V nominal and 4.2 V full. Battery: AA
    Match the plus and minus marks. Check the voltage your parts need. Two AAs make 3 V, four make 6 V. Never feed a LiPo to a 3.3 V chip directly at 4.2 V.
  • The circuit resets or browns out when a motor or servo starts. A battery has internal resistance, so the voltage sags under load. Battery: AA
    Use a fresh pack with enough current rating. Add a 100 uF or larger capacitor near the load. Power motors from their own pack and join the grounds.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.