Interfacing GSM Module with Atmega32 AVR microcontroller

interfacing-gsm-module-with-atmega32-avr-microcontroller

GSM (Global System for Mobile Communication) technology lets user to communicate with others across mobile networks hence it offers a vast area of coverage. Interfacing GSM technology with microcontroller will enable us to extend the communication to cover large area. This tutorial will teach you about interfacing GSM module with atmega32 AVR microcontroller and program it to communicate it with another user.

GSM MODEM:

GSM modem is nothing but a special hardware that modulates and demodulates GSM signals. There are plenty of GSM modems specially designed for interfacing and working with Microcontroller, you can find it online. We are using GSM SIM900A model in this tutorial to explain the interfacing.

PARTS REQUIRED:

We have added “Buy here” links in the below table to buy the listed components with ease. Since we are a sales partner of Amazon you could do a favor by buying parts from below link and this will earn some referral fee for us. By this way you could be a indirect contributor to our site.

NAME

SPECIFICATION

NUMBER

LINK TO BUY

GSM/GPRS Modem SIM900A 1 Buy here
Microcontroller AVR 1
LCD HD44780 1 Buy here

AT COMMANDS:

These are the set of commands that are predefined in order to establish a communication between controller and modem. Also with the help of these commands a SMS or call has been made out. Take a look at the ” Commands used in a GSM modem ” for detailed description about it.

DESIGN:

gsm-interface-avr-controller-schematic-design

STEPS TO INTERFACE GSM MODEM WITH MICROCONTROLLER:

STEP 1: MODEM TESTING:

The Modem consists of two indicating LED’s Green and Red to indicate the availability of the network. Green indicates the availability of the network whereas red indicates its absence. Turn the modem ON and wait for sometime to register itself in GSM network.

STEP 2: INTERFACING WITH AVR MICROCONTROLLER:

The Communication between AVR and modem takes place through USART protocol. GSM Modem SIM900 works on TTL level hence we can interface directly to RXD and TXD pins of AVR microcontroller. There is no need of using Voltage or level converter between them. However if you ever happen to buy a SIM300 or other module which operates above TTL level, you may want to use MAX232 level converter IC to make the communication possible.

STEP 3: INITIALIZING MODEM:

The modem must be Initialized using the commands and then the process you are about to carry out must be selected. In this tutorial am going to initialize the modem and them make it to send a message “Hi Murugan” to my mobile using the GSM modem. And then i will reply to the message which will be displayed in the LCD interfaced to the microcontroller.

CODE:

#include<avr/io.h>
#define F_CPU 8000000UL
#include<util/delay.h>
#define ctrl PORTB
#define en PB2
#define rw PB1
#define rs PB0

void lcd_cmd(unsigned char cmd);
void init_lcd(void);
void usartinit();
void lcd_write(unsigned char data);

unsigned char rxvalue(void);
unsigned char rxdata,a,cmd,b;
unsigned int z;
unsigned char message[15];
unsigned char cmd1[]={"AT"};
unsigned char cmd2[]={"AT+CMGF=1"};
unsigned char cmd3[]={"AT+CMGS="};
unsigned char cmd4[]={"Hi Murugan"};
unsigned char cmd5[]={"9092*******"};

int main ()
{
DDRC=0xFF;
DDRB=0x07;
init_lcd();
_delay_ms(50);
usartinit();
lcd_cmd(0x80);
lcd_write(0x47);// G
lcd_write(0x53);// S
lcd_write(0x4d);// M

///**** GSM send function****///////

for(z=0;cmd1[z]!='';z++)
{
UDR = cmd1[z];
_delay_ms(100);
}

UDR = ('\r');
_delay_ms(500);

for(z=0;cmd2[z]!='';z++)
{
UDR = cmd2[z];
_delay_ms(100);
}

UDR = ('\r');
_delay_ms(500);

for(z=0;cmd3[z]!='';z++)
{
UDR = cmd3[z];
_delay_ms(100);
}

UDR = ('"');
_delay_ms(100);

for(z=0;cmd5[z]!='';z++)
{
UDR = cmd5[z];
_delay_ms(100);
}

UDR = ('"');
_delay_ms(100);

UDR = ('\r');
_delay_ms(500);

for(z=0;cmd4[z]!='';z++)
{
UDR = cmd4[z];
_delay_ms(100);
}

UDR = (26);// ctrlZ-> to send the messge
_delay_ms(100);

///**** GSM receive function****///////
while(1)
{

b=rxvalue();

while (b!='+') // infinite loop when + equal to +. ortherwise untill the loop infinte
{

b=rxvalue();

}
b=rxvalue();
if(b=='C')
{ b=rxvalue();
if(b=='M')
{
b=rxvalue();
if(b=='T')
{
b=rxvalue();
if(b==':')
{

b=rxvalue();
while (b!=0x0a) // waiting upto next line if no means till loop infinte
{

b=rxvalue();

}
b=rxvalue();

message[0]=b;
b=rxvalue();
message[1]=b;
b=rxvalue();
message[2]=b;
b=rxvalue();
message[3]=b;
b=rxvalue();
message[4]=b;
b=rxvalue();
message[5]=b;
//b=rxvalue();
lcd_cmd(0x80);
for(z=0;z<6;z++)
{
a=message[z];
lcd_write(a); // PRINT IN lcd
_delay_ms(100);
}

}
}
}
}

}
}
void init_lcd (void)
{
lcd_cmd(0x38);
_delay_ms(1);
lcd_cmd(0x38);
_delay_ms(1);
lcd_cmd(0x38);
_delay_ms(1);
lcd_cmd(0x38);
_delay_ms(1);
lcd_cmd(0x06);
_delay_ms(1);
lcd_cmd(0x0c);
_delay_ms(1);
lcd_cmd(0x01);
_delay_ms(1);
lcd_cmd(0x02);
_delay_ms(1);
return;
}
void lcd_cmd(unsigned char cmd)
{
PORTA=cmd;
ctrl=(0<ctrl=(0<<rs)|(0<<rw)|(1<<en); 
_delay_ms(1); 
ctrl=(0<<rs)|(0<<rw)|(0<<en); 
_delay_ms(50); return; 
} 
void lcd_write(unsigned char data) 
{ 
PORTC=data; 
ctrl=(1<<rs)|(0<<rw)|(1<<en); 
_delay_ms(1); 
ctrl=(1<<rs)|(0<<rw)|(0<<en); 
_delay_ms(50); 
return; 
} 
void usartinit() 
{ 
UBRRH=00; 
UBRRL=77; 
UCSRB|=(1<<RXEN)|(1<<TXEN); 
UCSRC|=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1); 
} 
unsigned char rxvalue(void) 
{ 
while(!(UCSRA&(1<<RXC))); 
{ 
rxdata=UDR; 
return rxdata;
}
}

NOTE:

  • You may want to add up MAX232 IC if your modem output is above TTL logic level.
  • You also want to alter the commands fed to modem based on your requirements.

45 Comments

  1. jordan

    bonjour!
    j’ai un module GSM sim900A et un atmega32. j’aimerai pouvoir interfacer les deux afin de pouvoir appeler mon numéro.mais je ne sais pas comment m’y prendre. je sollicite votre aide merci d’avance.
    cordialement Jordan

    Reply
  2. kali

    I need to interface SIM900 GSM module with atmega32 in order to send a text message and get a reply from the phone in the other end. So can i go with the same code which you have given here? if not what sort of changes do i need to apply for the above code?

    Reply
    1. Frank DonaldFrank Donald

      Kali,
      The above given code should work for your requirement. Kindly try using and let me know how it came out

      Reply
  3. carlson

    for(z=0;cmd1[z]!=’?????’;z++)
    ??? = error help me?

    Reply
    1. Frank DonaldFrank Donald

      Carlson,
      What’s the error message?

      Reply
    2. Anonymous

      use
      cmd1[z] != ‘\0’

      Reply
  4. Anonymous

    Which editor did u use

    Reply
    1. Frank DonaldFrank Donald

      AVR studio

      Reply
  5. Anonymous

    Hi this code can be use to atmega 8-pu? Or there are codes to be replace or edit? It is possible to use this code directly without editing? Thanks

    Reply
    1. Frank DonaldFrank Donald

      I don’t think so. You should check for the pins in Atmega8 first. Then based on that you should rewrite a portion of the code like usage of ports for interfacing LCD module and UART configuration of TX and RX. But the overall code logic and structure will remain the same.

      Reply
  6. Shayan

    Hi, I am using sim900A, Atmega32 with Atmel studio7. I have received Build succeed. but no message received. please assist. thanks indeed!!

    Reply
    1. Brijesh Pratap Singh

      Bro can you please send this code with library to my mail brijeshpratap716@gmail.com
      Because it’s showing error of library when I copied it
      Thank you so much in advance

      Reply
    2. daniel

      Mr. shayan , can you send me the code for atmega32 with sim900A for my email info.danielxcd@gmail.com
      Thankyou !

      Reply
  7. adam

    infinite loops without timeout are not so good. when something goes wrong, program will hang up forever 🙁
    implement timeouts.

    Reply
  8. Melvin

    how to interface ATmega 128 with GSM simm900??

    Reply
  9. hari

    while compiling i got these errors.
    “empty character constant
    recipe for target ‘main.o’failed”
    can you correct it please?

    in line 33 40 47 53 (ie in for loops of send sms)

    Reply
    1. Frank DonaldFrank Donald

      Hari,

      Not sure about this error, after going through forums it seems like this error is associated with Atmel AVR Studio check this forum out http://www.avrfreaks.net/forum/recipe-target-maino-failed-atmel-studio-7

      Reply
    2. adam

      this comparison is nothing else like checking string end in variable
      as far as I know numerically it is 0
      so try to compare to 0 instead of ”(empty char)

      another idea is to compare it to char 0 by using ‘#0’

      last idea is to check string length and compare z counter to that number

      Reply
  10. Rifky

    while compiling i got these errors.
    “empty character constant
    empty character constant
    empty character constant
    empty character constant
    empty character constant
    recipe for target ‘main.o’failed”
    can you correct it please?

    Reply
    1. Frank DonaldFrank Donald

      Rifky,
      In which lines you are getting these errors?

      Reply
    2. Anonymous

      use ‘\\0’ instead of ‘ ‘

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *