Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Arduino and Sim800 Cellular Machine to Machine Commands Quickstart

Machine to Machine commands are the easiest way to get started with Programmable Wireless connectivity for your Internet of Things devices. This quickstart will show you how to send and receive text-based commands using an Arduino Uno or Arduino Nano and a SIMCom 800 GSM/GPRS module (other SIMCom GSM/GPRS modules should work, perhaps with slight modification).

In this Quickstart, you'll learn how to:

  1. Sign up for Twilio
  2. Purchase your first SIM cards for Twilio Programmable Wireless
  3. Insert your SIM card into a SIM800 module
  4. Wire the SIM800 for external power
  5. Connect the SIM800 to an Arduino Nano or Uno
  6. Power and flash the Arduino
  7. Send a Machine to Machine command from the Arduino and read it in the Wireless Console
  8. Receive a Machine to Machine command on the Arduino

Hardware Prerequisites

  • Arduino Nano or Arduino Uno
    • Other Arduino boards and clones will work but connectivity is beyond the scope of this article
  • SIM800 module localized for your country
    • For the United States, this should work well
    • If you are buying, try to get one with an included antenna
    • Note that many modules are not 5V compatible, if your board has a maximum you will need to find an alternative way to provide power
  • 5V 1.5+ Amp External Power Supply
  • USB Breakout Board
  • (Highly Recommended) Breadboard and Jumper Wires

Setup & Software Requirements

Not the quickstart you were looking for today? See our other Programmable Wireless quickstarts or see all Wireless resources.

While most of your code will be transferrable, it’s important to note: for new product in the United States development 2G isn’t a good choice.

American 2G networks are being deprecated. For new product development, we suggest researching alternatives or discussing your idea with the Twilio Programmable Wireless team.

I've gathered the hardware - let's get a SIM!

Already have a Twilio account and a SIM card? Skip ahead to the register and activate step by clicking the button at the end of the section.

Before you can connect with Twilio Programmable Wireless, you'll need a Twilio Account. Sign up for a free Twilio account here.

If you need a SIM card, visit the Programmable Wireless Console. Visit the Orders page, then purchase a set of 3 starter SIMs.

Order SIM Cards from Twilio

Note: Orders of 10+ SIMs will be delivered with the SIM cards already registered to your account.

I've got my SIM! How do I register and activate it?

Register, choose a rate plan, and activate the SIM

Once you receive your SIM shipment, choose your favorite and unpack it. Visit the Register a SIM page in the Wireless Consoleand enter the registration code as instructed:

SIM Card Registration Code

Next, pick a distinct Unique Name for this SIM.

Choose a rate plan

Select a rate plan from the options provided by Twilio.

We have created data metering quotas that optimize for both high and low usage devices. You can use our data usage estimator to find the right Quota for you. Rate Plans also let you control every aspect of what your device can do with data, voice, and messaging.

For this quickstart you can choose a low volume plan, but ensure the plan you choose includes messaging.

Activate your SIM card

When you're happy with the name and plan, 'Activate' the SIM card.

I'm active! Let's plug things in and wire it up.

Wire the SIM800 module and Arduino

We'll lead with the schematic of the connections you'll be making. Right after, we'll explain step by step the connections we'll be making (including inserting the SIM card and attaching the Antenna).

Here's what you'll be building at a high level:

Arduino M2M Schematic Connections

Insert the Antenna

If your module does not come with a PCB (onboard) antenna, you need to connect the antenna to the board. If you are unsure, check for an antenna included with your module or read the documentation included with your purchase.

Carefully attach the antenna to the board. Although your board is likely different, here is a picture of one module with antenna connected:

SIM800 Antenna Connection

Insert the SIM Card

Punch out your Twilio SIM card if necessary and insert it into the slot on the module. Be very careful to get the orientation correct with the SIM card insertion. If you are unsure of the correct side to insert, look for a diagram on the board showing the layout.

SIM Card Insert SIM800

Connect the Module to the Arduino and Power

We'll be identifying and wiring 5 of the connections from the module. If you are uncertain, take your time on these steps and read carefully.

Identify the Pins on the SIM800

Your Sim800 module most likely has at least 5 pins. Identify the following pins on the header:

  • 5V
  • Ground (Possibly with reference to the power supply)
  • Tx
  • Rx
  • Ground (Possibly with reference to UART or Serial)

It will also include a reset pin. We will be using a software reset in this quickstart, but for reliability in the field you can add a hardware reset. Here is a tutorial to read later detailing one method to wire reset.

If you scroll up to the SIM card picture, you can see how one board looks. Your board will vary.

Wire the Pins to the Arduino

First, you need to connect the module with your Arduino Nano or Uno. Orient yourself with the board identifying a ground pin, pin 2, and pin 3. They are labeled near the pin. You can make these connections either through a breadboard or directly to the pins using a jumper wire.

In the following order:

  1. Connect the Ground for the Serial or UART to a Ground pin on your Arduino
  2. Connect the Tx pin to Pin 2 on the Arduino
  3. Connect the Rx pin to Pin 3 on the Arduino

You're done with the connections on the Arduino side. Now we'll wire the power.

Wire the Power to the SIM800

Before you begin, make sure your adapter is unplugged from the wall. In order, make the following connections (using either a breadboard or jumpers where able):

  1. Connect a USB cable between the unplugged adapter and the breakout
  2. Connect the ground on the breakout to the Ground for power pin on the SIM800
  3. Connect the 5V pin on the breakout to the 5V pin on the SIM800

It's okay to power the module now, even though the Arduino is unpowered. Plug power in.

Your setup will vary, but one possible setup will look like this:

Arduino Nano SIM800

Whew, that was a rush. Let's program the Arduino.

Connect and Program the Arduino

You can download the code directly from this directory on Github. Either load that into Arduino, or copy paste it into a new project (from the File menu in Arduino.

The Copy/Paste Method

If you choose to copy/paste, here is how to do it:

Not sure how to add new files? Using the down arrow menu on the right side of the IDE, if you click there is an option for a 'New Tab'. If you select that you'll be able to name a new file then paste and edit code inside:

New File in the Arduino IDE

Power and Flash the Arduino

Power your Arduino by connecting it to your computer using a USB cable.

Wait a beat, then select your board and the serial port it enumerates on inside the 'Tools' menu. For an example with an Arduino Nano:

Select Arduino Nano

Arduino Nano Serial Port

Now, 'Upload' the code to the board. One way is to use the buttons on the toolbar, another is to select 'Upload' from the 'Sketch' Menu.

While the board receives your code, open the 'Serial Monitor' from the 'Tools' menu.

And that's all you need to do on the Arduino side! Watch the monitor and you should see it churn away and send an M2M command:

Send M2M Command

(If you don't see the above, make sure all power is connected and you are also set to 115,200 baud. Power cycle or hit the 'reset' button on the Arduino to have it spin through again).

Let's look at how the message was sent in the next session then prepare to send a response from our computer.

How did you send the M2M command?

Send a Machine to Machine Command from an Arduino

We're included a very small helper library to assist you sending M2M commands. Here's the method to send a machine to machine command:

        
        
        
        
        Sending an M2M Command with a SIM800 and an Arduino Nano using the helper functions provided.

        Send a Machine to Machine Command from an Arduino

        Sending an M2M Command with a SIM800 and an Arduino Nano using the helper functions provided.

        Behind the scenes, we send a SMS to the shortcode 2936.

        Verify the Machine to Machine Command was sent

        The easiest way to check the command was successful is through the Programmable Wireless Console. Visit that link now then click the 'SIMs' link in the sidebar. Click the SIM you are using for this project, then navigate to the 'Commands' tab:

        Copy a SIM ID

        While there, copy the SIM Sid from the highlighted box. As you might guess, we will implement the reverse - you'll receive an inbound SMS from 2936 on the Arduino triggered by an API call using the SIM's Sid. Let's look at that next.

        Cool, show me how to receive a M2M command.

        Receive a Machine to Machine Command on the Arduino

        Sending (or in this case, manually responding to) a Machine to Machine command is very straightforward from your development machine. The fastest way to respond to a command is to use cURL if available. If you can, send a command (under 160 ASCII characters) back to the SIM using cURL:

        curl -X POST https://wireless.twilio.com/v1/Commands -d 'Sim=DEXXXXXXXXXXXXXXXXXXXXXXXXXXXX' -d 'Command=Testing return' -u 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXX:YOUR_API_KEY'

        Otherwise, commands can be sent from our helper library:

              
              
              
              

              Receive the Command on the Arduino

              Again, we've included code to help you receive the M2M command easily.

                    
                    
                    
                    
                    Receive a M2M command by searching for the 2936 short code in incoming SMS messages to the Arduino.

                    Receive a Machine to Machine Command on an Arduino

                    Receive a M2M command by searching for the 2936 short code in incoming SMS messages to the Arduino.

                    To test this out, return to the Arduino IDE and make sure the Serial Monitor is still connected.

                    Cycle power or 'reset' the Arduino. If all goes well, the Arduino should once again send a command, then after a short wait you should see...

                    Receive an M2M Command Arduino

                    ... magic!

                    And that's all there is to Machine to Machine commands. You now know how to send them from an Arduino and check their status in the console. You can also return a message from your machine and have seen how to receive it on your Arduino. With those basic building blocks you're ready to build the next big IoT Thing!

                    What's Next?

                    Now that you have seen how to send and receive Machine to Machine commands on both your Arduino and development machine, you're ready for your custom application. Here are some possible ideas to take it to the next level:

                    Need some help?

                    We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.