Friday, February 7, 2014

Simple Remote control mobile bot


Components RequiredATmega32 microcontroller

  • AVR Programmer Board
  • Crystal
  • Capacitors
  • Motors 
  • L293D IC 
  • +5V supply
  • Battery equivalent to voltage rating of motor 
  • Breadboard
  • Connecting Wires
  • HT12D and HT12E 
  • RF module

Circuit Diagram


Transmitter Circuit:

Receiver Circuit:



Description

  • Enable is directly connected to +5 volt.
  • PC0,PC1,PC2 and PC3 are connected to Input 1-4 of first L293d.
  • PD0,PD1,PD2 and PD3 are connected to Input 1-4 of second L293d.
  • OUT1 and OUT2 are connected to the terminals of first motor and OUT3 and OUT4 are connected to the terminals of the second motor.(L293D -1)
  • OUT1 and OUT2 are connected to the terminals of third motor and OUT3 and OUT4 are connected to the terminals of the fourth motor.(L293D-2)
  • Reset is connected to +5V.
Note:
  • A crystal needs to be connected to XTAL1 and XTAL2 pins to provide the clock pulse in real time.

Source Code


#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRD = 0xFF;         //All pins of PORT D as output
DDRC = 0xFF;        //All pins of PORT C as output 
DDRB = 0x00;         //All pins of PORT B as input 
PORTB = 0xFF;      //Initially all pins at high output
PORTD = 0x00;      //Initially all pins at low output
PORTC = 0x00;      //Initially all pins at low output
    while(1)                      //Infinite Loop
    {
PORTB = 0xFF;
        if(bit_is_clear(PINB,0))     //Forward Run
{
PORTC = 0b00001010;
PORTD = 0b00001010;
_delay_ms(100);
}
if(bit_is_clear(PINB,1))    //Reverse Run
{
PORTC = 0b00000101;
PORTD = 0b00000101;
_delay_ms(100);
}
if(bit_is_clear(PINB,2))    //Rotate on axis(clockwise)
{
PORTC = 0b00001010;
PORTD = 0b00000101;
_delay_ms(100);
}
if(bit_is_clear(PINB,3))      //Rotate on axis(anticlockwise)
{
PORTC = 0b00000101;
PORTD = 0b00001010;
_delay_ms(100);
}
PORTC = 0x00; //reset PORTC to output zero 
PORTD = 0x00;              //reset PORTD to output zero
    }
return 0;
}




The given circuit cannot be simulated in proteus because of the absence of RF modules and HT12D and HT12E. So, in order to check the program the following circuit can be useful.





Thank you for reading.
For more tutorial and information, like the Facebook Page. 

Friday, January 24, 2014

Creating Project simulations in Proteus


Proteus is software for microprocessor simulation, schematic capture, and printed circuit board (PCB) design. It is developed by Labcenter Electronics.

  • Go To Proteus

  • Go to File->>New Project or click on New Project in the Start on your home screen of proteus.

  • In the New Project Wizard give a name for your project with the extension “.pdsprj”.
  • Make sure the check on New Project is present. Click on next.
  • Select “Create a schematic from the selected template” and choose “DEFAULT” design template and click on next.
  • If you wish you can also create a PCB layout by selecting “Create a PCB layout from the selected template” and chose “DEFAULT” but if you don’t want, just select “Do not create a PCB layout.” and click on next.
  • If you wish to create a microcontroller based project then you need to select “Create Firmware Project”.
  • We shall be using microcontrollers of the AVR family, ATmega32 controller and WinAVR compiler.
  • Now click on Next and then Finish.
  • If you want to include the hex file of a prewritten program or if you want to write your program in some other compilers like "programmers notepad" or "avr studio" you can skip this option. Later right click on the microcontroller go to "edit properties" and give the path of your hex file in the "program file".   
  • You will get a window with a microcontroller.
  • Right click on the blank area of the window and select “Place->>Component->>From libraries

  • The “Pick Device” window will open up. Just search for the required device in the Keywords.e.g. to search for any type of motor(say dc motor) type “motor”   and press enter and select “Simple DC Motor model”.

  • Press ok once you are finished.
  • Move your mouse over the linkages of the components so that the mouse pointer changes into a pencil then click on it to start connecting the wire.
  • Move the mouse to the other linkage of the component and click to connect the two devices.
  • Complete the required circuit and go to “Source Code” tab to write your program.
  •  After writing the program go to “Build->>Build Project” or press F7 to create the “.hex” file of your program.
  • If everything goes write the “VSM Studio Output” will show “Compiled successfully” else check for errors in the program.

  • Now go to Schematic Capture and go to Debug->>Run Simulation. Alternatively you can also press F12 or run icon in the bottom left corner of the screen.
  • Don’t forget to save your project.


Thank you for reading.
For more tutorial and information, like the Facebook Page. 

Tuesday, November 26, 2013

Communicating two microcontrollers

Components Required




  • ATmega32 microcontroller x 2
  • AVR Programmer Board
  • Crystal
  • Capacitors
  • LEDs
  • +5V supply
  • Breadboard
  • Connecting Wires

Circuit Diagram

Description



The first microcontroller(at the top) takes the analog value of the voltage of the battery provided in the figure and shows its digital value on the given LEDs connected to the PORT B of the first microcontroller. For more information on the ANALOG TO DIGITAL conversion go through the blog analog-to-digital-conversion. Now the digital data is send to the second microcontroller and the LEDs corresponding to the digital value of the data in PORT B glows. If the data transfer is proper then the bulbs glowing through both the microcontrollers will be the same.

Connections

  • LEDs are connected to the Pins 0 to 7 of PORT B for both the microcontrollers.
  • The analog input is given to PIN A0 (ADC 0) of first microcontroller.
  • The Txd of the first microcontroller is connected to the Rxd of the second microcontroller.
  • A crystal is connected to the XTAL1 and XTAL2 pins to provide the clock pulse for both the microcontrollers.
  • Reset is connected to +5V.
NOTE:
  •  The crystal oscillator connection is not shown in the figure due to limited space. Kindly go through the previous blogs to check out the connection.
  • The ground of both the microcontrollers must be at the same potential.


Source Code 1/2

/*The first microcontroller converts the analog data to digital data, glows the corresponding LEDs and sends the same to the second microcontroller*/
#ifndef F_CPU
#define F_CPU 1000000UL               //frequency of the external crystal
#endif

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define USART_BAUDRATE 9600     
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
void send(uint8_t data);                          //function to send the data
int main()
 { 
DDRB = 0xFF;                             //All pins of PORT B as output
PORTB = 0xFF;                           //Initially all pins of PORTB as output low
UCSRB = (1 << RXEN ) | (1 << TXEN );            //Enabling the RXD and TXD 
UCSRC = (1 << URSEL ) | (1 << UCSZ0 ) | (1 << UCSZ1 );
UBRRH = ( BAUD_PRESCALE >> 8);
  UBRRL = BAUD_PRESCALE ; 
ADCSRA |= 1<<ADPS2;
ADMUX |=1<<REFS0 | 1<<REFS1;
ADMUX |=1<<ADLAR; 
ADCSRA |=1<<ADIE;
ADCSRA |=1<<ADEN;
sei();
ADCSRA |=1<<ADSC;
   while (1)                                                                      //Infinite while loop
   ;
   return 0;
 }
 ISR(ADC_vect)                                                            //Interrupt service routine 
 {
         //theLow stores the converted digital value
uint8_t theLow = ADCH;                                    
          //PORT B is configured according to theLow
PORTB = theLow;       
         //send theLow to the send function                                       
send(theLow);                                                       
 ADCSRA |=1<<ADSC;                                      
  } 
  void send(uint8_t data)
  {
 while (( UCSRA & (1 << UDRE )) == 0);
 _delay_ms(1);
 UDR = data;
  }

Source Code 2/2


/*The second microcontroller receives the data and configures the output of the PORT B according to the input received from the first microcontroller*/

#ifndef F_CPU
#define F_CPU 1000000UL               //frequency of the external crystal
#endif

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define USART_BAUDRATE 9600 
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
uint8_t receive(void);
uint8_t msg;
int main()
 { 
DDRB = 0xFF;
PORTB = 0xFF;
UCSRB = (1 << RXEN ) | (1 << TXEN );
UCSRC = (1 << URSEL ) | (1 << UCSZ0 ) | (1 << UCSZ1 );
UBRRH = ( BAUD_PRESCALE >> 8);
  UBRRL = BAUD_PRESCALE ; 
sei();                                                      //to enable the interrupt
   while (1)                                                      //Infinite while loop
   {
  msg = receive();                                   //to receive the data from function
           //configure the PORT B according to the received data
  PORTB = msg;                                   
   }
   return 0;
 }

uint8_t receive(void)
  {
 uint8_t data;
 while (!( UCSRA & (1 << RXC )));
      return UDR;                                          //return the received data to the calling function 
  }  


Thank you for reading.

For more tutorial and information, like the Facebook Page. 


Wednesday, October 30, 2013

Analog to Digital Conversion

Components Required



  • ATmega32 microcontroller
  • AVR Programmer Board
  • Crystal
  • Capacitors
  • LEDs
  • +5V supply
  • Breadboard
  • Connecting Wires

Circuit Diagram


Description

The digital value of the analog input at PA0 will be shown in the 8 leds PC0 to PC7 from LSB to MSB.

Connections

  • LEDs are connected to the Pins 0 to 7 of PORT C.
  • The analog input is given to PIN A0 (ADC 0).
  • A crystal is connected to the XTAL1 and XTAL2 pins to provide the clock pulse.
  • Reset is connected to +5V.

    Source Code

    /*The following code will keep on converting the analog value applied at the pin PA0, convert it into digital value and store the most significant 8 bits in ADCH register and the lesser two bits in the ADCL register.
    Since the lesser two values can be ignored the 8 bits present in the ADCH are only taken care of and the LEDs are glown according to the ADCH register*/

    /* Go through the blog http://rickruling.blogspot.in/2013/10/adc-control-and-status-registers.html for more details on adc control and status registers*/

    #include <avr/io.h>
    #include <avr/interrupt.h>                     //For interrupt handling

    #ifndef F_CPU
    #define F_CPU 1000000UL               //frequency of the external crystal
    #endif

    int main(void)
    {
    DDRC = 0xFF;                     //All ports of port C as output
    PORTB = 0x00;                            //Initially all pins as output low
    //configure the ADC
    ADCSRA |=1<<ADPS2;       //ADC Prescaler Select Bits (Division Factor is 16 for 100)
    ADMUX |=1<<ADLAR;      //MSB in ADCH
    ADMUX |=1<<REFS0;        // AREF pin
    ADCSRA |=1<<ADIE;         //ADC Interrupt Enable
    ADCSRA |=1<<ADEN;        //ADC Enable
    sei();                                      //set global interrupt enable
    ADCSRA |=1<<ADSC;        //ADC Start Conversion
        while(1)                                      //Infinite loop
              ;
    }

    ISR(ADC_vect)
    {
    PORTB = ADCH;                     //Result of conversion to PORTB
    ADCSRA |=1<<ADSC;            //Restart the conversion
    }


    Thank you for reading.

    For more tutorial and information, like the Facebook Page 

    ADC Control and Status Registers

    Features

    • 10-bit Resolution
    • 0.5 LSB Integral Non-linearity
    • ±2 LSB Absolute Accuracy
    • 13 μs - 260 μs Conversion Time
    • Up to 15 kSPS at Maximum Resolution
    • 8 Multiplexed Single Ended Input Channels
    • 7 Differential Input Channels
    • 2 Differential Input Channels with Optional Gain of 10x and 200x
    • Optional Left adjustment for ADC Result Readout
    • 0 - VCC ADC Input Voltage Range
    • Selectable 2.56V ADC Reference Voltage
    • Free Running or Single Conversion Mode
    • ADC Start Conversion by Auto Triggering on Interrupt Sources
    • Interrupt on ADC Conversion Complete
    • Sleep Mode Noise Canceler

    The ATmega32 features a 10-bit successive approximation ADC. The ADC is connected to an 8-channel Analog Multiplexer which allows 8 single-ended voltage inputs constructed from the pins of Port A.The device also supports 16 differential voltage input combinations. Two of the differential inputs(ADC1, ADC0 and ADC3, ADC2) are equipped with a programmable gain stage, providing amplification steps of 0 dB (1x), 20 dB (10x), or 46 dB (200x) on the differential input voltage before the A/D conversion. Seven differential analog input channels share a common negative terminal (ADC1), while any other ADC input can be selected as the positive input terminal. If 1x or 10x gain is used, 8-bit resolution can be expected. If 200x gain is used, 7-bit resolution can be expected.The ADC contains a Sample and Hold circuit which ensures that the input voltage to the ADC is held at a constant level during conversion.The ADC has a separate analog supply voltage pin, AVCC. AVCC must not differ more than ±0.3V from VCC. Internal reference voltages of nominally 2.56V or AVCC are provided On-chip. The voltage reference may be externally decoupled at the AREF pin by a capacitor for better noise performance.


    ADC Multiplexer Selection Register - ADMUX



    Bit 7:6 – REFS1:0: Reference Selection Bits
    These bits select the voltage reference for the ADC, as shown in Table 83. If these bits are
    changed during a conversion, the change will not go in effect until this conversion is complete
    (ADIF in ADCSRA is set). The internal voltage reference options may not be used if an external
    reference voltage is being applied to the AREF pin.

    Bit 5 – ADLAR: ADC Left Adjust Result
    The ADLAR bit affects the presentation of the ADC conversion result in the ADC Data Register.
    Write one to ADLAR to left adjust the result. Otherwise, the result is right adjusted. Changing the
    ADLAR bit will affect the ADC Data Register immediately, regardless of any ongoing conversions.

    Bits 4:0 – MUX4:0: Analog Channel and Gain Selection Bits
    The value of these bits selects which combination of analog inputs are connected to the ADC.
    These bits also select the gain for the differential channels.  If these bits
    are changed during a conversion, t
    he change will not go in effect until this conversion is
    complete (ADIF in ADCSRA is set).


    ADC Control and  Status Regiter A - ADCSRA


    Bit 7 – ADEN: ADC Enable
    Writing this bit to one enables the ADC. By writing it to zero, the ADC is turned off. Turning the
    ADC off while a conversion is in progress, will terminate this conversion.

    Bit 6 – ADSC: ADC Start Conversion
    In Single Conversion mode, write this bit to one to start each conversion. In Free Running Mode,
    write this bit to one to start the first conversion. The first conversion after ADSC has been written
    after the ADC has been enabled, or if ADSC is written at the same time as the ADC is enabled,
    will take 25 ADC clock cycles instead of the normal 13. This first conversion performs initialization
    of the ADC.
    ADSC will read as one as long as a conversion is in progress. When the conversion is complete,
    it returns to zero. Writing zero to this bit has no effect.

    Bit 5 – ADATE: ADC Auto Trigger Enable
    When this bit is written to one, Auto Triggering of the ADC is enabled. The ADC will start a 
    conversion on 
    a positive edge of the selected trigger signal. The trigger source is selected by setting
    the ADC Trigger Select bits, ADTS in SFIOR.


    Bit 4 – ADIF: ADC Interrupt Flag
    This bit is set when an ADC conversion completes and the Data Registers are updated. The
    ADC Conversion Complete Interrupt is executed if the ADIE bit and the I-bit in SREG are set.
    ADIF is cleared by hardware when executing the corresponding interrupt handling vector. 
    Alternatively, ADIF
    is cleared by writing a logical one to the flag. Beware that if doing a Read-Modify-
    Write on ADCSRA, a pending interrupt can be disabled. This also applies if the SBI and CBI
    instructions are used.

    Bit 3 – ADIE: ADC Interrupt Enable
    When this bit is written to one and the I-bit in SREG is set, the ADC Conversion Complete Interrupt
    is activated.

    Bits 2:0 – ADPS2:0: ADC Prescaler Select Bits
    These bits determine the division factor between the XTAL frequency and the input clock to the
    ADC.

    The ADC Data Registers - ADCL and ADCH


    ADLAR = 0





    ADLAR = 1





    When an ADC conversion is complete, the result is found in these two registers. If differential
    channels are used, the result is presented in two’s complement form.
    When ADCL is read, the ADC Data Register is not updated until ADCH is read. Consequently, if
    the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read
    ADCH. Otherwise, ADCL must be read first, then ADCH.
    The ADLAR bit in ADMUX, and the MUXn bits in ADMUX affect the way the result is read from
    the registers. If ADLAR is set, the result is left adjusted. If ADLAR is cleared (default), the result
    is right adjusted.

    For more tutorial and information, like the Facebook Page 

    https://www.facebook.com/Robowares

    Sunday, October 27, 2013

    Interfacing LCD with the ATmega32

    Components Required


    • ATmega32 microcontroller
    • AVR Programmer Board
    • Crystal
    • Capacitors
    • 16x2 LCD
    • +5V supply
    • Breadboard
    • Connecting Wires

    Circuit Diagram






    Description


      DB0-DB7: Data Pins( use DB4-DB7 in 4 bit mode) 

      RS(Register Select) : 1 - Data is sent , 0 - Command is sent . 

      R/W(Read/Write) :1 - Read the LCD , 0 - Write to LCD. 

      E(Enable): to enable an operation . first make low(0) to send data and then set the other two control lines and when they are configured, bring E high (1) and wait for the minimum amount of time required by the LCD and bring it low (0) again. 

      VEE: Contrast Adjust Pin.

      Connections 

    • PC0 to PC7 are connected to the data pins D0 to D7 of the LCD.
    • VDD and VEE are connected to the +5 volts directly.
    • VEE can be connected to a potentiometer to adjust the contrast.
    • VSS is connected to ground.
    • RS, RW and E are connected to PD5, PD6 and PD7 respectively.
    • A crystal is connected to XTAL1 and XTAL2 pins to provide the clock pulse.
    • Reset is connected to +5V.

    Source Code

    /*
    The following code will display the string defined in the main function.
     */ 

    #include <avr/io.h>
    #include <util/delay.h>

    #ifndef F_CPU
    #define F_CPU 1000000UL //Frequency of the external crystal
    #endif

    /*CWR - Command Word Register*/
    #define ENABLE_LCD PORTD |= 0x80 //CWR state  to enable LCD
    #define DISABLE_LCD PORTD &= ~0x80//CWR state to disable LCD
    #define SET_LCD_DATA PORTD |= 0x20 //CWR state to send data
    #define SET_LCD_CMD PORTD &= ~0x20 //CWR state to send command

    void LCD_WriteCommand (unsigned char CMD);
    void LCD_WriteData (unsigned char Data);
    void LCD_DisplayString_F(char row, char column,char *string);
    void LCD_Cursor(char row, char column);

    void init_devices(void)
    {
    DDRC = 0xFF; //All pins of PORTC declared as Output
    PORTC = 0x00;//Initially all pins declared as output low
    DDRD = 0xF0;//Pins D4 to D7 declared as input,remaining as o/p
    PORTD = 0x00;//All output pins as low
    _delay_ms(100); // wait for 100ms
    LCD_WriteCommand (0x38); // 8 data lines
    LCD_WriteCommand (0x06); // cursor setting
    LCD_WriteCommand (0x0f); // display ON
    LCD_WriteCommand (0x01); // clear LCD memory
    _delay_ms (10); // 10ms delay after clearing LCD
    }
    int main(void)
    {
    init_devices();
            //Text to be displayed in first row
    LCD_DisplayString_F(1,1,"    ROBOWARES ");
            //Text to be displayed in second row
    LCD_DisplayString_F(2,1,"rickruling.blogspot.in");
    return 0;
    void LCD_WriteCommand (unsigned char Command)
    {
    SET_LCD_CMD; // Set LCD in command mode
    PORTC = Command; // Load data to port
    ENABLE_LCD; // Write data to LCD
    _delay_ms(1);
    DISABLE_LCD; // Disable LCD
    _delay_ms(1); // wait for 1ms
    }
    void LCD_WriteData (unsigned char Data)
    {
    SET_LCD_DATA; // Set LCD in data mode
    PORTC = Data; // Load data to port
    ENABLE_LCD; // Write data to LCD
    _delay_ms(1);
    DISABLE_LCD; // Disable LCD
    _delay_ms(1); // wait for 1ms
    }

    void LCD_DisplayString_F (char row, char column ,char *string)
    {
             /*Break the string into characters and send them for the display*/
    LCD_Cursor (row, column);
    while (*string)//Loop till the string is not null
    LCD_WriteData(*string++);
    }
    void LCD_Cursor (char row, char column)
    {
    switch (row)
    {
    case 1:     //Set the position in the first row
                     LCD_WriteCommand (0x80 + column - 1);
    break;
    case 2:     //Set the position in the second row
                     LCD_WriteCommand (0xC0 + column - 1);
    break;
    default: break;
    }
    }


    For more tutorial and information, like the Facebook Page 

    https://www.facebook.com/Robowares


    Interfacing Unipolar Stepper motor using ULN2803

    Unipolar Stepper Motor

    A unipolar stepper motor has one winding with center tap per phase. Each section of windings is switched on for each direction of magnetic field. Since in this arrangement a magnetic pole can be reversed without switching the direction of current, the commutation circuit can be made very simple (e.g., a single transistor) for each winding. Typically, given a phase, the center tap of each winding is made common: giving three leads per phase and six leads for a typical two phase motor. Often, these two phase commons are internally joined, so the motor has only five leads.

    Components Required

    • ATmega32 microcontroller
    • AVR programmer board
    • Crystal
    • Capacitors
    • Unipolar Stepper Motor
    • ULN2803
    • +5 V Supply
    • Battery Equivalent to the voltage rating of the motor
    • Breadboard
    • Connecting wires 

    Circuit Diagram



    Description

    • PC0 to PC3 are connected to the four inputs of the ULN2803 IC.
    • Both the enable pins are separately connected to Vcc.
    • A crystal is connected to the XTAL1 and XTAL2 pins to provide the clock pulse.
    • Reset is connected to +5 volt.

    Control Logic

    To control a unipolar stepper, you use a Darlington Transistor Array. The stepping sequence is as shown below. Wires 5 and 6 are wired to the supply voltage.


    Source Code

    /*The following code will make the stepper motor to run continously*/
    #include <avr/io.h>
    #include<util/delay.h>

    #ifndef F_CPU
    #define F_CPU 1000000UL
    #endif

    int main()
     { 
             DDRC = 0xFF;                        //All pins of PORT C as output
    PORTC = 0x00;                      //Initially all pins as output low
       while (1)                                         //infinite loop
       {
    PORTC = 0x03;                       //0011
    _delay_ms(100);
    PORTC = 0x06;                      //0110
    _delay_ms(100);
    PORTC = 0x0C;                      //1100
    _delay_ms(100);
    PORTC = 0x09;                       //1001
    _delay_ms(100);
       }
       return 0;
     }

    Source Code for controlled Rotation


    /* This code will make the stepper motor to run for a specific angle */ 

    #include <avr/io.h>
    #include <util/delay.h>

    #ifndef F_CPU
    #define F_CPU 1000000UL         // frequency of external crystal
    #endif

    int main()
     {
             /*Calculate x by the minimum step angle of your motor and required rotation angle*/
              int x=20;
              DDRC = 0xFF;                         //All pins of PORT C as output
     PORTC = 0x00;                        //Initially all pins as output low
       while (x)                                           //loop till x is not equal to zero
       {
    PORTC = 0x03;                         //0011
    _delay_ms(100);
    PORTC = 0x06;                         //0110
    _delay_ms(100);
    PORTC = 0x0C;                        //1100
    _delay_ms(100);
    PORTC = 0x09;                         //1001
    _delay_ms(100);
      x--;                                          //decrement x by 1
       }
       return 0;
     }


    For more tutorial and information, like the Facebook Page