Introduction: Step by Step Developing Controller for Relay With Your Laptop

Picture of Step by Step Developing Controller for Relay With Your Laptop

Guys,

In this experiment I wanna share the way on creating a relay controller from PC, I'm not using a relay directly but I'm using LED module as a replacemnet of a relay. If you connect it into your relay it will give the same result.
Please read :
https://www.instructables.com/id/Create-yourself-a-message-flasher-with-ATMEGA128/
For complete instruction on LCD side.

Step 1: Next Step Is Preparing the Part for This Experiment

Picture of Next Step Is Preparing the Part for This Experiment

In this experiment,

I used ATMEGA128, LCD 16x2, USBASP as a debugger and USB to TTL as line for sending command from my laptop.
It's simple and you can do it in your weekend....
circuit is available on
https://www.instructables.com/id/Create-yourself-a-message-flasher-with-ATMEGA128/

I'm using Visual Studio for laptop side and AVR Studio for ATMEGA128 side.

Step 2: Develope the Code for ATMEGA128

Picture of Develope the Code for ATMEGA128

For complete code, please refer the complete code to

https://www.instructables.com/id/Create-yourself-a-message-flasher-with-ATMEGA128/
this is the function for communicating with Laptop :
void usart_init( unsigned int ubrr )
{
/* Set baud rate */

UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1<<USBS)|(3<<UCSZ0);
}

void usart_transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR0 = data;
}



void usart_pstr(unsigned char *s) {

    // loop through entire string

    while (*s) {
        usart_transmit(*s);
        s++;
    }
}


unsigned char usart_receive( void )
{
/* Wait for data to be received */
while ( !(UCSR0A & (1<<RXC)) )
;
/* Get and return received data from buffer */
return UDR0;
}

.
.
.
.

on main()

please add :

do
   {
    c_byte = usart_receive();
   
           usart_transmit(c_byte);

         
   }
   while (c_byte==' ');
    
  _delay_ms (10);
     lcd_cmd(0x01);//Clear display
  lcd_data(c_byte);
  lcd_cmd(0xC0);//goto second row
  lcd_string("selected");
     _delay_ms(1000);

  switch(c_byte){
    case 'a'  :
       _delay_ms (10);
  lcd_cmd(0x80);//put the cursor into the first row
        _delay_ms (10);
        lcd_cmd(0x01);//Clear display
     _delay_ms (10);
        lcd_cmd(0xC0);//goto second row
        _delay_ms (10);
       lcd_cmd(0x01); //Clear display

    lcd_cmd(0x80);//put the cursor into the first row
        _delay_ms (10);
        lcd_cmd(0x01);//Clear display
     _delay_ms (10);
       lcd_string("Relay A");
    lcd_cmd(0xC0);//goto second row
       lcd_string("activated..");
    _delay_ms(1000);
    //next screen
       //activate relay1
    relay1 = 1;
    relay2 = 0;

       break; /* optional */
   
case 'b'  :
       _delay_ms (10);
  lcd_cmd(0x80);//put the cursor into the first row
        _delay_ms (10);
        lcd_cmd(0x01);//Clear display
     _delay_ms (10);
        lcd_cmd(0xC0);//goto second row
        _delay_ms (10);
       lcd_cmd(0x01); //Clear display

    lcd_cmd(0x80);//put the cursor into the first row
        _delay_ms (10);
        lcd_cmd(0x01);//Clear display
     _delay_ms (10);
       lcd_string("Relay B");
    lcd_cmd(0xC0);//goto second row
       lcd_string("Activated");
    _delay_ms(1000);
     //next screen
        //activate relay2
  relay1 = 0;
        relay2 = 1;

       break;
  
   case 'c' :
   
    //usart_pstr("HALLO FROM ATMEGA128\n");
   //  printf("HALLO RIKO");
  
  //_delay_ms(50000);

    _delay_ms (10);
  lcd_cmd(0x80);//put the cursor into the first row
        _delay_ms (10);
        lcd_cmd(0x01);//Clear display
     _delay_ms (10);
        lcd_cmd(0xC0);//goto second row
        _delay_ms (10);
       lcd_cmd(0x01); //Clear display

    lcd_cmd(0x80);//put the cursor into the first row
        _delay_ms (10);
        lcd_cmd(0x01);//Clear display
     _delay_ms (10);
       lcd_string("Message");
    lcd_cmd(0xC0);//goto second row
       lcd_string("Flasher");
    _delay_ms(1000);
     //next screen
      
    lcd_cmd(0x80);//put the cursor into the first row
        _delay_ms (10);
        lcd_cmd(0x01);//Clear display
     _delay_ms (10);
    lcd_string("With ATMEGA128");
    lcd_cmd(0xC0);//goto second row
       lcd_string("By arick");
    _delay_ms(1000);
    //next screen
   
    lcd_cmd(0x80);//put the cursor into the first row
        _delay_ms (10);
        lcd_cmd(0x01);//Clear display
     _delay_ms (10);
       lcd_string("Published on");
    lcd_cmd(0xC0);//goto second row
       lcd_string("Instructable.com");
    _delay_ms(1000);
    //next screen
    lcd_cmd(0x80);//put the cursor into the first row
        _delay_ms (10);
        lcd_cmd(0x01);//Clear display
     _delay_ms (10);
       lcd_string("Made in");
    lcd_cmd(0xC0);//goto second row
       lcd_string("AUSTRALIA");
    _delay_ms(1000);
    //next screen
    lcd_cmd(0x80);//put the cursor into the first row
        _delay_ms (10);
        lcd_cmd(0x01);//Clear display
     _delay_ms (10);
       lcd_string("Disable");
    lcd_cmd(0xC0);//goto second row
       lcd_string("All relays");
    _delay_ms(1000);
    //disable all relays
  relay1 = 0;
        relay2 = 0;

    break;
 
     }//end case

Step 3: Ask the Laptop to Comunicate With Your ATMEGA128

Picture of Ask the Laptop to Comunicate With Your ATMEGA128

The next step I created, is asking my Laptop to communicate with ATMEGA128,
I'm using C from Visual Studio in this case, you can use any language or developer you like....

I'll show you the function on visual C since I will not explain on how to use Visual Studio....hehehe
I assume you guys know about it...

Here I go with the function.....easy and simple.....always...

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {//begin button1
      //Initialize Communication Port
    SerialPort^ RelayPort;
    String^ portName;
    int baudRate=9600;
    portName ="COM13";
 
    RelayPort = gcnew SerialPort(portName, baudRate);
       /*try
               {*/
                  RelayPort->Open();
         RelayPort->WriteLine("a");
                     RelayPort->Close();
        //this->label1->Text = "Disconnected";
         /* }
                 catch (IO::IOException^ e  )
          {
             //this->label1->Text = "Not Connected";
          } */               
    }//end button1

Step 4: Test Your ATMEGA128 and Laptop

Picture of Test Your ATMEGA128 and Laptop

For testing the communication between ATMEGA128 and Laptop,
In this case I'm using "Access Port" and sending the character into my ATMEGA and see the responsse on LCD..
you guys can use Putty or other communication tool you have, everything can be used as long as it does the function.

Step 5: Enjoy Your Work !

Picture of Enjoy Your Work !
The last step is enjoying the result.....
In this video I haven't put the window application on connecting with my ATMEGA128.....
Thanks for reading....

Video ====>

Comments