Sunday, November 15, 2015

working on AVR SPI

SPI protocol:

Serial peripheral interface: it is a 4-wire interface while in a three wire protocol the SDI and SDO pins are tied together


SDI pin (DIN pin) (MOSI pin) for input data.

SDO pin (Dout pin) (MISO pin) for output data.

SCLK pin (shift clock) (SCk) pin to sync data transfer between two chips.

CE pin (chip enable) (SS) it is used to initiate and terminate data transfer.


Working:

SPI has one shift registers , one on Master side and one in slave side and a clock generator on master side.

the names MOSI(Master Out Slave In) is given to the master's Serial data out pin. while MISO(Master In Slave Out) is given to the master's Serial data input pin.

These registers are 8bit long.
SCLK synchronizes transfer one bit at a time And during CE must stay HIGH.
To distinguish between read and Write operation,

Address byte is followed immediately by the data byte.
D7 bit of address byte is always 1 for write operation.
D7 bit of address byte is always 0 for a read operation.

CPOL(clock polarity) :

value =0 means base value of clock is zero.

value =1 means base value of clock is one.

CPHA(clock phase) :

value =0 means sample on leading clock

value =1 means sample on falling clock


Single-byte write mode :

You make CE=0
Begin by sending the 8 bit address,
Always remember the MSB goes out first, and since you are writing data, the first bit is Address 7 bit should always be equal to 1. After the 8 bit address goes the 8 bit data from the Master to slave through MOSI pin.

The data is shifted 1 bit at a time with the SCLK edge and at the end CE is set to 1 to indicate end of write cycle


Single-byte Read mode :

You make CE=0
Begin by sending the 8 bit address,
Always remember the MSB goes out first, and since you are reading data, the first bit is Address 7 bit should always be equal to 0. After the 8 bit address is sent through the MOSI, the 8 bit data on the same address is sent back by Slave on MISO pin.

The data is shifted 1 bit at a time with the SCLK edge and at the end CE is set to 1 to indicate end of write cycle



Multibyte burst write:

You make CE=0
Begin by sending the 8 bit address,
Always remember the MSB goes out first, and since you are writing data, the first bit is Address 7 bit should always be equal to 1. After the 8 bit address goes the 8 bit data from the Master to slave through MOSI pin.

The data is shifted 1 bit at a time with the SCLK edge and the only difference here is that after the 8 bit DAta byte no. 1 you can send next data byte without giving an new address. The SCLK after the first databyte will automatically increment the address to +1 of previous address.
at the end CE is set to 1 to indicate end of write cycle.



Multibyte burst Read:

You make CE=0
Begin by sending the 8 bit address,
Always remember the MSB goes out first, and since you are reading data, the first bit is Address 7 bit should always be equal to 0. After the 8 bit address is sent through the MOSI, the 8 bit data on the same address is sent back by Slave on MISO pin.

The data is shifted 1 bit at a time with the SCLK edge and the only difference here is that after the 8 bit Data byte no. 1 you can send next data byte without giving an new address. The SCLK after the first databyte will automatically increment the address to +1 of previous address.
at the end CE is set to 1 to indicate end of write cycle.


SPSR: SPI STATUS REGISTER gives the current status of ongoing SPI exchange
SPCR: SPI CONTROL REGISTER lets you select

1.) a device as MASTER or SLAVE using MSTR bit
2) Clock Phase and polarity using CPHA and CPOL
3) data order using DORD
4) Clock Frequency using SPR1 SPR0 and SPI2X from SPSR register

SPDR: SPI DATA REGISTER holds data for reading and writing.


 The SS pin (SLAVE SELECT pin) has integral role to play in selecting the mode of operation of SPI

When in master mode, and SS as OUTPUT you can make it High or low.
When in master mode, and SS is INPUT, A high signal on SS pin will make it master and a low on SS pin will make it SLAVE
When in Slave mode, SS pin should always be INPUT


A simple Example: I have chosen AtMega16
Make sure you use change the PIN numbers according to the controller you are using.
*****************************************************

# include <avr/io.h>

int main(void)
{
DDRA = 0xff;
DDRB = (1<<6)|(1<<7)
SPCR= (1<<MSTR)|(1<<SPE)|(1<<SPR0)
while(1)
{
SPDR= 'H';
while(!(SPSR & (1<<SPIF)));
PORTA=SPDR;
}
return 0;
}

******************************************************

Try this code combining two SPI controllers and twitch it according to your need.
For the slave device make MSTR==0 and SS pin as input and have fun with SPI.



And feel free to ask questions around the circuit or code or its scope of use.

keep reading :)
Abheerup

Tuesday, October 13, 2015

PROJECT 4a: GSM GPS based realtime Vehicle tracking/ collision detection with ARDUINO


Next we have a module for gathering real-time position tracking. GPS based tracking projects are fairly common. Their application can be seen in article, bus, vehicle, pet tracking etc. The GPS device we are going to use for this project is an atmega 328, a gps module , a gsm module and bunch of other stuff.
The module we are using is GPS Receiver with Antenna (5V TTL SERIAL) from rhydolabs. GPS TTL Mini is a high gain GPS Receiver . The on board 3V3 to 5V level convertor enable us to directly interface with normal 5V Microcontrollers. The module is made with ultra small High gain third generation POT (Patch Antenna On Top) GPS module. There is no prior configuration required; just power the module with 5V, and your data (NMEA 0183) is ready at TX pin. It has a very compact and easy to integrate design with the ultimate tracking performances. It can be directly connected to Microcontroller’s UART.

The GPS chipsets inside the module are designed by MediaTek Inc., which is the world’s leading digital media solution provider and largest fab-less IC company in Taiwan. The module can support up to 66 channels. The GPS solution enables small form factor devices. They deliver major advancements in GPS performances, accuracy, integration, computing power and flexibility. They are designed to simplify the embedded system integration process.
 Features:
MediaTek MT3329 Chipset, L1 Frequency, C/A code, 66 Channels
TTL asynchronous serial interface
5 VDC supply @ 55 mA (typical)
Data output Baud rate: 9600 bps(Default)
Standard NMEA0183 output format
Low Power Consumption: 55mA @ acquisition, 40mA @ tracking
High Sensitivity, -165 dBm, TCXO Design , superior urban performances
Position Accuracy: <3.0M 2D-RMS
DGPS (WAAS/EGNOS/MASA/GAGAN) Support
Multi-path Compensation ; E-GSM-900 Band Rejection
Cold Start is Under 36 seconds (Typical)
Warm Start is Under 34 seconds (Typical)
Hot Start is Under 1 second (Typical)
Max. Update Rate : 10Hz (Default: 1 Hz)
















here is a link to a good product: GPS Receiver with Antenna (5V TTL SERIAL) 































1. What is the NMEA 0183 Standard?
The National Marine Electronics Association (NMEA) is a non-profit association of manufacturers,
distributors, dealers, educational institutions, and others interested in peripheral marine electronics
occupations. The NMEA 0183 standard defines an electrical interface and data protocol for
communications between marine instrumentation.
NMEA 0183 devices are designated as either talkers or listeners (with some devices being both),
employing an asynchronous serial interface. All data is transmitted in the form of sentences. Only printable ASCII characters are allowed, plus CR (carriage return) and LF (line feed). Each sentence starts with a "$" sign and ends with <CR><LF>. There are three basic kinds of sentences: talker sentences, proprietary sentences and query sentences.

************************************************************
when  GPS is not connected to satellite or is in bad network:

$GPRMC,193856.000,V, , , , , 0.20,194.78,140215, , N*4F

utc position=19hrs 38mins 56.000 seconds
status: A=data valid / V= data invalid
latitude: N/A
N/S indicator: N/A
Longitude : N/A
E/W: N/A
Speed over Ground: 0.20 knots
Course over Ground: 194.78 degrees
Date: 14/02/2015
Magnetic Variation: N/A
Checksum : 4F
End of line
************************************************************

when GPS is connected to satellite or is in network:

$GPRMC,193856.000,A ,3723.2475 ,N ,12158.3416 ,W , 0.20,194.78,140215, , N*4F

utc position=19hrs 38mins 56.000 seconds
status: A=data valid / V= data invalid
latitude: 37.232475
N/S indicator: N
Longitude : 12.1583416
E/W: W
Speed over Ground: 0.20 knots
Course over Ground: 194.78 degrees
Date: 14/02/2015
Magnetic Variation: N/A
Checksum : 4F
End of line
************************************************************


Along with that i am using a GSM MODULE:

The GSM/GPRS TTL UART Modem, is built with Dual Band GSM/GPRS engine- SIM900A, works on frequencies  900/ 1800 MHz. The Modem is coming with selectable interfacing voltage,which allows you to connect 5V & 3V3 microcontroller directly without any level conversion chips. The baud rate is configurable from 9600-115200 through AT command. The GSM/GPRS Modem is having internal TCP/IP stack to enable you to connect with internet via GPRS.

Here is a quick link to it: GSM/GPRSTTL UART MODEM-SIM900A

Modem Features:
High Quality Product (Not hobby grade)
Dual-Band GSM/GPRS   900/ 1800 MHz
3V3 & 5V  interface for direct communication with MCU kit
Configurable baud rate
SMA connector with GSM Antenna.
SIM Card holder.
Built in Network Status LED 
Inbuilt Powerful TCP/IP protocol stack for internet data transfer over GPRS.
Audio interface Connector
Normal operation temperature: -20 °C to +55 °C
Input Voltage: 5V-12V DC

Compatibility: AT cellular command interface




Code mentioned in this tutorial is for interfacing of a GSM module and a GPS module with Arduino simultaneously. I have written it in such a manner that there is a Master Number. (you should ideally put your own number there after copying the code.) which gets notified whenever there is an emergency of a collision detected by uc on pin D12(a high pulse on pin D12), a collision message (a custom string "GPS co-ordinates + There has been a collision here. Kindly contact immediately." is sent to master no. for help. You can edit this to something of your convenience as well.)




































The GSM and GPS modules used here are both UART TTL compatible devices that means you do not need to connect a voltage level converter chip like max232. You can directly connect the GSM and GPS to 2 serial ports. Since AtMega 328 has only 1 serial port , I am using GSM at the hardware serial port and GPS at the Software serial port (using the softwareserial library provided with Arduino package you can easily convert a pair of GPIO's to serial port. But these soft serials have a limitation. I advise you to always keep the BAUD Rate under 19200 on soft serials as higher baudrate might lead to scrambled or garbled serial o/p).

So co-ordinates are read in realtime from GPS using serial port and polling is done for any calls on GSM module. If a call is received on GSM module, it is Disconnected after 3 rings and the number is stored in the controller SRAM. Then the no. is sent back a message in format "latitude = xx.xx longitude = xx.xx ." As long as there is no new collision or call received by controller, the GPS coordinated are read and once a call is received or collision occurs, suitable action is taken.



below is the code , burn it onto your Arduino or make a custom PCB/ a prototype of the circuit and enjoy making minor changes :

***********************************************************
#include <SoftwareSerial.h>
SoftwareSerial mySerial_gsm(10, 11);
int incoming[100];
int msg_send_flag,msg_send_flag2;
String number, str,y, str2,str4;
int msg_read_flag=0;
int status_1;
int stat_1,val,last_val;

String master_number1="+91-xxxxxxxxxx";                                  // change it to a valid phone no. with an appropriate country code
  int Gpsdata;             // for incoming serial data
  unsigned int finish =0;  // indicate end of message
  unsigned int pos_cnt=0;  // position counter
  unsigned int lat_cnt=0;  // latitude data counter
  unsigned int log_cnt=0;  // longitude data counter
  unsigned int flg    =0;  // GPS flag
  unsigned int com_cnt=0;  // comma counter
  char lat[20];            // latitude array
  char lg[20];             // longitude array



void setup()
{ Serial.begin(9600);
  mySerial_gsm.begin(9600);
  pinMode(12, INPUT);
  mySerial_gsm.println("AT+CMGF=1");
  str2="hello...!";
   last_val=0;
   Receive_GPS_Data();
  
  
    String str5=" latitude = ";
    str5+=String(lat[1]);
    str5 +=String('.');
    for (int i=2;i<=3;i++)
    { str5+=String(lat[i]); }
    for (int i=1;i<=15;i++)
    {    str5+=String(lat[4+i]);   }
   
    str5=str5+" longitude = ";
    str5 +=String(lg[1]);
    str5 +=String(lg[2]);
    str5 +=String('.');
    for (int i=3;i<=4;i++)
    {    str5+=String(lg[i]);  }
    for (int i=1;i<=14;i++)
    {
    str5+=String(lg[5+i]);
    }   
    Serial.println(str5);
  delay(1000);
read_sms();
  }


void loop()
{  
    int incoming_1=0;
    int income=0;
    status_1=0;
    if (msg_read_flag==1)
    {if (mySerial_gsm.available()!=0)
    {for( incoming_1=0;incoming_1<=100;incoming_1++)
    { incoming[incoming_1]=mySerial_gsm.read();
      // Serial.print(char(incoming[incoming_1]));                   
    }
      for( incoming_1=0;incoming_1<=100;incoming_1++)

      {
      if( char(incoming[incoming_1])=='C'&& char(incoming[incoming_1+1])=='L' && char(incoming[incoming_1+2])=='I' && char(incoming[incoming_1+3])=='P' && char(incoming[incoming_1+4])==':' && char(incoming[incoming_1+5])==' ' && char(incoming[incoming_1+6])=='"' && char(incoming[incoming_1+7])=='+'  && char(incoming[incoming_1+8])=='9')
             {String numb;
               //Serial.println("here we are");
               for (int num=7;num<=19;num++)
              { numb +=String(char(incoming[incoming_1+num]));
               numb.trim();
                number=numb;
                delay(100);}
               status_1=1;} }}
             // Serial.println("number is:");
             // Serial.println(number1);
             // Serial.println(status_1);
              delay(2000);
               if (status_1==1)
              {Serial.println("One new message from :");
               Serial.println(number);
              delay(2000);
              msg_read_flag=0;
              delay(2000);
              status_1=0;
              str2+=" one new message from:";
              str2+=number;
              send_sms();
             
             
              } }
             
             
              while (msg_send_flag==1)
                                {  y="AT+CMGS=";
                                   delay(100);
                                   y+=String('"');
                                   y=y+number;
                                   delay(100);
                                   y+=String('"');
                                   delay(100);
                                   Serial.println(y);
                                   delay(100);
                                   mySerial_gsm.println(y);
                                  // Serial.println("entered the while loop for sms send");
                                   //stat_1=1;
                  delay(100);
                  while (char(income)!= '>' )
                         {
                           income=mySerial_gsm.read();
                          // Serial.println(char(income));
                                   if ((char(income))=='>')
                                                {
                                   //Serial.println("> string read");
                                   delay(400);
                                   mySerial_gsm.print(str4);
                                   mySerial_gsm.write(char(26));
                                   delay(300); 
                                   Serial.println("message sent successfully."); 
                                    status_1=0;  }
                    // Serial.println("sending...");
                         delay(100);
                       }
                                msg_send_flag=0;
            // Serial.print("exiting all loops");

                }
               

            while (msg_send_flag2==1)
                                {  String z= str4+ ". There has been a collision here. Kindly contact immediately.";
                   y="AT+CMGS=";
                                   delay(100);
                                   y+=String('"');
                                   y=y+master_number1;
                                   delay(100);
                                   y+=String('"');
                                   delay(100);
                                   Serial.println(y);
                                   delay(100);
                                   mySerial_gsm.println(y);
                                  // Serial.println("entered the while loop for sms send");
                                   //stat_1=1;
                  delay(100);
                  while (char(income)!= '>' )
                         {
                           income=mySerial_gsm.read();
                          // Serial.println(char(income));
                                   if ((char(income))=='>')
                                                {
                                   //Serial.println("> string read");
                                   delay(400);
                                   mySerial_gsm.print(z);
                                   mySerial_gsm.write(char(26));
                                   delay(300); 
                                   Serial.println("message sent successfully."); 
                                    status_1=0;  }
                    // Serial.println("sending...");
                         delay(100);
                       }
                                msg_send_flag2=0;
            // Serial.print("exiting all loops");

                }


collision_detection();
read_sms();

}

void read_sms()
{ mySerial_gsm.println("AT+CNMI=2,2,0,0,0");
  delay(1000);
  mySerial_gsm.println("AT+CLIP=1");
  msg_read_flag=1;
 
 
}



void send_sms()
                {int incoming_1=0;
         mySerial_gsm.println("ATH");
       for( incoming_1=0;incoming_1<=100;incoming_1++)
    { incoming[incoming_1]=mySerial_gsm.read();}
     // Serial.print(char(incoming[incoming_1]));                   
        
         mySerial_gsm.println("AT+CMGF=1");
                 delay(2000);
                 msg_send_flag=1;
                 delay(2000);
 
 
    finish = 0;pos_cnt = 0;
    Receive_GPS_Data();
    String str5=" latitude = ";
    str5+=String(lat[1]);
    str5 +=String('.');
    for (int i=2;i<=3;i++)
    { str5+=String(lat[i]); }
    for (int i=1;i<=15;i++)
    {    str5+=String(lat[4+i]);   }
   
    str5=str5+" longitude = ";
    str5 +=String(lg[1]);
    str5 +=String(lg[2]);
    str5 +=String('.');
    for (int i=3;i<=4;i++)
    {    str5+=String(lg[i]);  }
    for (int i=1;i<=14;i++)
    {
    str5+=String(lg[5+i]);
    }
   
    str4=str5;  
}
    //Serial.println(str5);
                // Serial.println("in send_sms func loop");

 void Receive_GPS_Data()
  {
    while(finish==0){
      while(Serial.available()>0){         // Check GPS data
        Gpsdata = Serial.read();
        flg = 1;
       if( Gpsdata=='$' && pos_cnt == 0)   // finding GPRMC header
         pos_cnt=1;
       if( Gpsdata=='G' && pos_cnt == 1)
         pos_cnt=2;
       if( Gpsdata=='P' && pos_cnt == 2)
         pos_cnt=3;
       if( Gpsdata=='R' && pos_cnt == 3)
         pos_cnt=4;
       if( Gpsdata=='M' && pos_cnt == 4)
         pos_cnt=5;
       if( Gpsdata=='C' && pos_cnt==5 )
         pos_cnt=6;
       if(pos_cnt==6 &&  Gpsdata ==','){   // count commas in message
         com_cnt++;
         flg=0;}

       if(com_cnt==3 && flg==1){
        lat[lat_cnt++] =  Gpsdata;         // latitude
        flg=0;}

       if(com_cnt==5 && flg==1){
         lg[log_cnt++] =  Gpsdata;         // Longitude
         flg=0;}

       if( Gpsdata == '*' && com_cnt >= 5){
         com_cnt = 0;                      // end of GPRMC message
         lat_cnt = 0;
         log_cnt = 0;
         flg     = 0;
         finish  = 1; }}}}



void collision_detection()
{
  val=0;
  val = digitalRead(12);       
      if (val != last_val)
          {   if (val==1)  
              {msg_send_flag2=1;;  
              Serial.println("collision detected");
              }        }   
             else {    }
      last_val=val;  }

  
************************************************************

Here is a handy link to the GPS module I have used you can refer to it or ask me to pen down a tutorial dedicated to 'GPS ONLY'.
http://www.rhydolabz.com/wiki/?p=658



The GSM module used here is a GSM TTL UART module.
And for the working and interfacing of GSM module to AVR/ ARDUINO i will make a separate comprehensive tutorial for you guys to easily understand that as well.

And feel free to ask questions around the circuit or code or its scope of use.

keep reading :)
Abheerup

Tuesday, September 29, 2015

PROJECT 2b: Interfacing LCD 16x2 with AVR


Hey Guys,

Today lets discuss a basic output device. A LCD(Liquid Crystal Display) for your AVR's.
Its a nice and easy tutorial where you can learn to interface an LCD and display your bot status or any serially , Wireless received messages or anything from your ucontroller.

Soon i'll also share with you the files on how to do it on our 3 other favorite platforms:
1) Arduino
2) 805x series.
3) PIC controller


Lets look at the basic working of an LCD module. It receives data in ASCII format.


PIN DIAGRAM  and their uses:

VDD- +5v
VEE- power supply to contrast using a potentiometer
VSS- ground

RS-Register select (0=command code     1 = data word)

R/W-Read/Write    (0 = writing         1 = reading)

E- Enable (High to low pulse to latch data)

D0(lsb) to D7(msb) are data bits



Fig. A Basic LCD and its pins.




Some handy commands we will use:

0x01= clear screen
0x02= return home
0x04= cursor 1 place left
0x06= cursor 1 place right
0x05 shift display left
0x07 shift display right
0x0E= display on , cursor blinking
0x0f = display on , cursor blinking
0x80= force cursor to beginning of first line
0xC0= force cursor to beginning of second line
0x28= 2 lines 5x7 matrix(D4-D7, 4 bit data)
0x38= 2 lines 5x7 matrix(D0- D7, 8 bit data)

so suppose the LCD data bits D0 to D7 are connected to port C.1 to C.7

RS= PORTB.0

R/W= PORTB.1

Enable pin=PORTB.2

VDD- +5v

VEE- power supply to contrast using a potentiometer

VSS- ground


Below is the  basic circuit and different states of LCD on running the below program.




burn the demo code below to your AVR USING PROGRAMMER's Notepad and enjoy making minor twitches and interfacing your lcd with AVR.

*****************************************************************************************************


void delay_ms(unsigned int c)
{
_delay_ms(c);
}



void comm_write (unsigned char a)
{
   PORTD=0x01;
PORTC=a;
PORTB &= ~(0x03);
PORTB|= (1<<2);
delay_ms(2);
PORTB &= ~(1<<2);
delay_ms(100);
PORTD&=~(0X01);
}

void data_write(char* str )
{
unsigned char i=0;
while (str[i] !=0)
{
PORTC=str[i];
PORTB = PORTB & ~(0X02);
PORTB = PORTB |(0x01);
PORTB|=(1<<2);
delay_ms(200);
PORTB= PORTB & ~(1<<2);
delay_ms(200);
PORTB = PORTB & ~(0X03);
i++;
if (i>15)
{
comm_write(0x07);
}
}
}

void init_lcd()
{
DDRB=0Xff;
DDRC=0Xff;
DDRD=0x03;
PORTB &= ~(1<<2);
PORTD|=(1<<2);
delay_ms(2000);
PORTD&=~(1<<2);
comm_write(0x38);
comm_write(0x0E);
comm_write(0x01);
delay_ms(2000);
comm_write(0x06);
data_write("Abhirup.");
comm_write(0xC0);
data_write("welcome to my tutorials");

}

int main(void)
{
init_lcd();
return 0;
}



*************************************************************************************************

Have fun playing around. And feel free to ask questions around the circuit or code or its scope of use.

keep reading :)
Abheerup

PROJECT 8a: Interfacing RF433 MODULE with ARDUINO

Hey guys, 
Today we'll looking at another cool project. Its a very basic wireless transmission project.
We'll be using it to create wireless channel between two micro-controllers.

Applications:
  • Car security system
  • Wireless security systems
  • Sensor reporting
  • Automation system
  • Remote Keyless entry 
  • Burglar alarm system
  • Smoke and fire alarm system
  • Garage door controllers
  • Car door controllers
  • Car alarm system
  • Security system
  • Cordless telephones
  • Other remote control systems


TX and RX are low cost ASK transmitter modules Pair which are based on RFIC . The transmitter modules work in very wide voltage range so they are very suitable for battery-driven applications. RX is a type of high sensitive ASK receiver which works at 3.6~5.5V. It can be used together with TX to construct simple and short range wireless control system.


The transmitted signal of the wireless receiver, remote control distance, can pass through walls, no direction. Supporting the use of any of the same frequency remote control. The Super-regenerative receiver module LC oscillator circuit includes amplifier shaping output data signals to TTL level directly to the decoder, extremely easy, and inexpensive. Small in size & high in sensitivity. 


This is an PLL based ASK Hybrid  433Mhz RF receiver module and is ideal for short-range wireless control applications where quality is a primary concern.The receiver module requires no external RF components except for the antenna.

Specs for receiver:
Static receiver module operating current is 4mA.
Operation voltage: DC5V
Static Current: 4MA
Receiver frequency: 433.92MHZ; 315 Mhz


Transmitter Module Specification:
Operating voltage :under 6V
Operating frequency: 433.92MHz; 315Mhz
Standby current: 0mA
Operating current :20-28mA
Transmission distance:> 500m
Transfer rate: <10Kbps


You have to use the MCU to simulate decode and encode process.


For the same we'll be using a commonly available decoder encoder pair from holtek
HT12d(decoder) and HT12E(encoder). the pair is a 4 bit data encoder-decoder and  has 8 address bits able to operate in 2^8 =256 different channels. And its decoder output works in active low logic.

Key: for successful operation , always use same address on encoder and decoder. SAFE FOR beginners -> keep all address bits grounded on both 12D & 12E.

The encoder decoders are a series of CMOS LSIs for remote control system applications. For proper operation, a pair of encoder/decoder with the same number of addresses and data format should be chosen.

The decoders receive serial addresses and data from a programmed series of encoders that are transmitted by a carrier using an RF 433 mhz radio.

They compare the serial input data three times continuously with their local addresses. If no error or unmatched codes are found, the input data codes are decoded and then transferred to the output pins. 
The VT pin also goes high to indicate a valid transmission. The HT12E & HT12D are arranged to provide 8 address bits and 4 data bits. 





The data flow is explained below.


The transmitter side circuit is shown below:







The receiver side circuit is shown below:


The code for the following is shown below. Copy the following Arduino code and burn it to your AVR and have fun. ;)

*************************************************************************
copy code below

// TRANSMITTER SIDE



int dat1=3;
int dat2=4;
int dat3=5;
int dat4=6;                                //    dat1 is triggered by inp1
int inp1=A0;                               //    dat2 by inp2
int inp2=A1;                               //    dat3 by inp3
int inp3=A2;                               //    dat4 by inp4            for more see the void loop function
int inp4=A4;


/*
******************************************************************************************************
I have connected simple 4 SPST push buttons , to trigger each output digitally,                      *
You can use these 4 push buttons to control a bot using a 4 button wireless joystick via rf 433mhz   *
Or you can replace the SPST with accelerometer inputs (and use analogWrite instead of digitalWrite)  *
and using a calibration and thresholding function as given in my accelerometer bot tutorial, you can *
make a simple hand gesture based robot.                                                              *
******************************************************************************************************
*/

void setup()
{
pinMode(dat1, OUTPUT);                    //declaring outputs
pinMode(dat2, OUTPUT);
pinMode(dat3, OUTPUT);
pinMode(dat4, OUTPUT);

pinMode(inp1, INPUT);
pinMode(inp2, INPUT);                    // declaring respective inputs
pinMode(inp3, INPUT);
pinMode(inp4, INPUT);

}

void loop()
{
  if (digitalRead(inp1)==1)
      {
         digitalWrite(dat1,HIGH);                        //whenever push button is pressed, turn the respective o/p on for 100ms
         delay(100);
       
      }
  if (digitalRead(inp2)==1)
      {
         digitalWrite(dat2,HIGH);
         delay(100);
      }
  if (digitalRead(inp3)==1)
      {
         digitalWrite(dat3,HIGH);
         delay(100);
      }
  if (digitalRead(inp4)==1)
      {
         digitalWrite(dat4,HIGH);
         delay(100);
      }      

digitalWrite(dat1,LOW);
digitalWrite(dat2,LOW);
digitalWrite(dat3,LOW);
digitalWrite(dat4,LOW);

delay(100);
}



**********************************************************************


//Rx side


int dat1=3;
int dat2=4;
int dat3=5;
int dat4=6;                                //    dat1 is triggered by inp1
int inp1=A0;                               //    dat2 by inp2
int inp2=A1;                               //    dat3 by inp3
int inp3=A2;                               //    dat4 by inp4            for more see the void loop function
int inp4=A4;


/*
******************************************************************************************************
I have connected simple 4 simple LED's to show each output digitally,                                *
You can use these motors to control a bot using a 4 button wireless joystick via rf 433mhz           *
Use forward= Motor_1_positive_pin (HIGH) & forward= Motor_2_positive_pin (HIGH) rest of the pins low *
   Backward= Motor_1_negative_pin (HIGH) & forward= Motor_2_negative_pin (HIGH) rest of the pins low *
    Left = Motor_1_positive_pin (HIGH) and rest of the pins low                                      *
   Right = Motor_2_positive_pin (HIGH) and rest of the pins low                                      *
and using digitalWrite or if you want PWM use analogWrite as given in my accelerometer bot tutorial, *
you can make a simple hand gesture based robot.                                                      *
******************************************************************************************************
*/

void setup()
{
pinMode(dat1, OUTPUT);                    //declaring outputs
pinMode(dat2, OUTPUT);
pinMode(dat3, OUTPUT);
pinMode(dat4, OUTPUT);

pinMode(inp1, INPUT);
pinMode(inp2, INPUT);                    // declaring respective inputs which are connected to DOUT pins of HT12D
pinMode(inp3, INPUT);
pinMode(inp4, INPUT);

}

void loop()
{
  if (digitalRead(inp1)==1)
      {
         digitalWrite(dat1,HIGH);                        //whenever push button is pressed, turn the respective o/p on for 100ms
         delay(100);
       
      }
  if (digitalRead(inp2)==1)
      {
         digitalWrite(dat2,HIGH);
         delay(100);
      }
  if (digitalRead(inp3)==1)
      {
         digitalWrite(dat3,HIGH);
         delay(100);
      }
  if (digitalRead(inp4)==1)
      {
         digitalWrite(dat4,HIGH);
         delay(100);
      }      

digitalWrite(dat1,LOW);
digitalWrite(dat2,LOW);
digitalWrite(dat3,LOW);
digitalWrite(dat4,LOW);

delay(100);
}



*****************************************************************

Have fun playing around. And feel free to ask questions around the circuit or code or its scope of use.

keep reading :)
Abheerup