Electronics, programming & other stuff

Using a cellphone or GSM/GPRS modem with a microcontroller

It's possible to connect a microcontroller to a modem or GSM/GPRS cellphone, which enables the microcontroller to do stuff such as answer the phone, call a number, send and receive SMS as well as connect to the internet, even with small microcontrollers.

The first thing needed to know is that most cellphones and GSM modules work with AT commends, which are mostly universals and presents in any cellphone with a data cable, particularly old ones which could be used as modems through a virtual serial port.

We need to build the interface between the microcontroller and the module/cellphone. In most cases the cellphones work with 3.3 V, so the microcontroller needs to be supplied that voltage, or a level shifter or a simple resistive divider needs to be used.

Dedicated modules (such as the motorola G20 or G24) tend to use a complete RS232 interface (to keep compatibility with old modems), so an IC such as the MAX232 is needed to connect them together (se more information of the MAX232).

Once both parts can interface, it's possible to talk to the module via a standard serial communication, usually at 4800bps, 1 bit stop, no parity and no flow control. Commands can then be sent.

Everything sent will be echoed by the module, so that will have to be handled in software.

List of main commands:

  • AT\r\n : used to verify that the modem is connected and to try the link. It should reply OK.
  • ATE0\r\n : desables echo
  • ATE1\r\n : enables eco
  • AT+CPIN="1234"\r\n : if the SIM has PIN protection, sets the PIN to use
  • AT+CMGF=1\r\n : activates the text mode for SMS (makes easier routines software-wise)
  • AT+CMGS="+541155554444"\rMessage<^Z> : Sends "Message" to the specified 12-digit number.
  • AT+CNMI=,2\r\n : Activates the redirection of received messages to the microcontroller
    When a message arrives, the module outputs
    +CMT: “+541155554444,”10/9/22,14:12:34”
    It's necessary to tell the module that the message was received correctly, using AT+CNMA\r\n

\r is the control character corresponding to the carriage return (ASCII 13 or 0x0D)
\n is the line feed character (ASCII 10 or 0x0A)
^z is the character corresponding to "end of file" - EOF (ASCII 26 or 0x1A)

The routines to connect to the Internet, send and receive TCP and UDP packets aren't standard, and tend to vary according to the manufacturer. It's needed for the module to have a TCP/IP stack to use it (cheap ones don't have one). Otherwise, its needed to implement every protocol in the microcontroller firmware (from PPP up to UDP) . A sample code from an application note by Freescale (AN120) explains how to do it, including C code that can be ported to other microcontrollers.