Microcontroller

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.

The post Interfacing GSM Module with Atmega32 AVR microcontroller appeared first on Gadgetronicx.

Source: gadgetronicx.com – Microcontroller

Leave a Reply

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