Embedded Electronics Tutorials, Coding and lot more

Controlling your Electrical appliances via PC/Laptop (8051 Microcontroller)

The whole simulation video is available at youtube link below:

 You can download hex file and proteus simulation at end of the post.

This device can be used to control your home’s all 230V appliances directly from your PC. We can do it via the serial port of our PC. We have used the 8051 microcontroller here.  We are communicating via the RS-232 port.

The circuit is designed in the proteus ISIS software. I have first simulated the circuit in the proteus so that all correction and debugging process is done in the PC itself. So we don’t needto modify hardware later. After confirming the design in proteus I created the hardware.

We are communicating via the serial port so we must need to know about the UART interfacing of 8051 with PC. Here the code was written in C language using the uVision Keil IDE. Then the created hex file was dumped in the proteus simulation. Also later on made GUI app for that.

VB

I have connected 8 leds to the Port 1 of microcontroller. Here I assigned some codes to these LEDs, so that every LED will have 2 unique codes to turn on and turn off. For example

‘0’ key in keyboard means 0x30 in HEX, The whole table is shown below.

 ascii-to-hex_chart

We have used it at 11.0592 MHz frequency. For demo purpose I have connected 8 LEDs at Port1 pins.

ISIS

Here they are switching means we can connect relays via relay driver circuit and also switch the relays. That thing I implemented in the harwdware directly, as I was sure to be working for LEDs in the simulation. I can use NPN transistors 2N2222 for driving the relay.

relay

But as I needed to interface multiple relays, for reliability and compactness I preferred to use ULN 2803 IC as a relay driver.

 The program code for the microcontroller is shown below.

#include <reg51.h>
sfr acc=0xe0;
//sfr P3=0xb0;
sbit light1=P1^0;
sbit light2=P1^1;
sbit light3=P1^2;
sbit light4=P1^3;
sbit plug1=P1^4;
sbit plug2=P1^5;
sbit plug3=P1^6;
sbit plug4=P1^7;
unsigned char byte;

void receive() // function for data receiving event
{
if(RI==1)

{
acc=SBUF;
RI=0;
if(acc==0x30) // Light ON
{
light1=1;
}
if(acc==0x31) // Light OFF
{
light1=0;
}

if(acc==0x32) // Lights ON
{
light2=1;
}
if(acc==0x33) // Lights OFF
{
light2=0;
}
if(acc==0x34) // Lights ON
{
light3=1;
}
if(acc==0x35) // Lights OFF
{
light3=0;
}
if(acc==0x36) // Lights ON
{
light4=1;
}
if(acc==0x37) // Lights OFF
{
light4=0;
}

if(acc==0x61) // Plug ON
{
plug1=1;
}
if(acc==0x62) // Plug OFF
{
plug1=0;
}

if(acc==0x63) // Plug ON
{
plug2=1;
}
if(acc==0x64) // Plug OFF
{
plug2=0;
}

if(acc==0x65) // Plug ON
{
plug3=1;
}
if(acc==0x66) // Plug OFF
{
plug3=0;
}

if(acc==0x67) // Plug ON
{
plug4=1;
}
if(acc==0x68) // Plug OFF
{
plug4=0;
}

if(acc==’A’) // Lights ON
{
light1=1;
light2=1;
light3=1;
light4=1;
}
if(acc==0x42) // Lights OFF
{
light1=0;
light2=0;
light3=0;
light4=0;
}

if(acc==0x43) // Plug ON
{
plug1=1;
plug2=1;
plug3=1;
plug4=1;
}
if(acc==0x44) // Plug OFF
{
plug1=0;
plug2=0;
plug3=0;
plug4=0;
}
if(acc==0x45) // Fan speed 0
{
P2=0x0f;
}
if(acc==0x46) // Fan speed 1
{
P2=0x0b;
}

if(acc==0x47) // Fan speed 2
{
P2=0x07;
}
if(acc==0x48) // Fan speed 3
{
P2=0x03;
}
if(acc==0x49) // Fan speed 4
{
P2=0x00;
}
return;
}
else
{
return;

}

}
void main()
{

TMOD = 0x20; // 9600 baudrate
SCON = 0x50;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
P1=0x00;
P2=0xff;
while(1)
{

receive();
}
}

I am showing the simulation in proteus and Real Term software. Both are connected via virtual serial port. You can see my post on VSPE tutorial if you don’t know about this virtual ports and all. In the video my proteus circuit is acting as actul hardware and Real Term app in PC as user input from PC to hardware.

Create COM Port 1 & 2 pairs in VSPE software. Then make sure you assing different ports to Proteus and Real Term. I am assigning Port 1 to RealTerm and Port 2 to Proteus.  Set baud rates 9600. Then run the simulation. When you press Numeric Keys on the keyboard, the corresponding LED in proteus circuit will be turning ON or OFF. For example, as per our program here

Key        LED         State

0              D1              ON

1               D1             OFF

2              D2              ON

3              D2              OFF

And so on..

For interfacing actual hardware you can use your desktop PC’s DB9 port. Else if you have a laptop, you must use a USB to serial converter for this.

ULT40315-vendor-sp

This Is very simple way to use this device. But if you know some softwares like Visual Basic, Java, Visual Studio etc then you can also create a nice GUI app for this device.

The final hardware pics are shown below:

   IMG_20140403_190505               IMG_20140403_190426

IMG_20140329_171323

Here in the video one demo application is shown.I have created a Graphical User Interface app for that. The software is made using Microsoft Visual BASIC 6.0.

VB

The code for setting com port and baudrate is as follows:

Private Sub Form_Load() MSComm1.CommPort = 1 ‘ 9600 baud, no parity, 8 data bits, 1 stop bit MSComm1.Settings = “9600,N,8,1” ‘ Disable DTR MSComm1.DTREnable = False ‘open the port MSComm1.PortOpen = True End Sub() Dont forget to close com port at closing form: Private Sub Form_Unload(Cancel As Integer) MSComm1.PortOpen = False End Sub You can configure each option button shown in the software as below: Private Sub Option1_Click() MSComm1.Output = Chr$(48) End Sub Here ’48’ means ASCII key ‘0’ or 0x30 in HEX. In program for PIC controller Corrosponding hex code is decoded for particular pins.

Finally all the steps are summarized as below:HA

Visual BASIC provides us utility to access the PC serial port. It automatically initializes COM Port number, Baud rate and all settings while loading the application. User just need to click directly to turn ON or OFF any device. Means “Everything at just 1 touch”

You can downloard VB project source files from here

To Download simulations files click here

You can also view the same project usign PIC microcontroller here.

Sharing is caring

12 Comments

  1. LucyCBandt

    Hi friends, how is everything, and what you wish for to say
    on the topic of this article, in my view its truly awesome for me.

  2. sdorttuiiplmnr

    Fantastic website. Plenty of useful info here. I am sending it to some friends ans also sharing in delicious. And certainly, thank you in your effort!

  3. Agnibha Ghosh

    It will be very kind of you if you can upload the whole VB project folder from My Documents including the bin, my project and all other files in google drive or dropbox.

  4. Agnibha Ghosh

    Sir, the hardware has been made and the hex file has also been burnt successfully..also under Ports(COM&LPT) under device manager is showing the device being connected to port no. But when I open the VB application it gives an error saying A device attached is not functioning properly. Highly the VB code is generating an error as i had to copy some of your codes as your .exe application was not running. what should i do now?

    • Taral Shah

      I already uploaded whole folder which i was working on. I was storing in custom location. So there no meaning of bin folder files. when i run the same files, they are perfect working. May be some libraries not properly included when you installed VB. And even if you make new file, then just copy pasting code doesnt make sense. You have to make form exactly as previous one, just every component name and id exactly same. and have to insert the hidden MSCcomm port. And code is burned to 8051 successfully doesn’t guarantee 100% output. Because you are just uploading the hex file that is already built. But your work after that to make hardware and testing it, will make surety of output.
      You should have test hardware as working by making some test cases manually.
      Just because this hardware is made atleast 10 times and worked like charm every time.

  5. Agnibha Ghosh

    As given from your link the hardware serial port of my computer is accessible and as of now and has been set to COM1.. So I’ll not be needing any driver right?

  6. Agnibha Ghosh

    As given from your link the hardware serial port of my computer is accessible and as of now has been set to COM1.. So I’ll not be needing any driver right?

  7. Agnibha Ghosh

    Sir, I am using a PC and the driver given to me for the RS232 to USB is only Windows 98 compatible. So i dont thing i cant make it work. What should I do in that case? But my PC has default COM port no as 1 already set.. But is the driver really neccesary and needs to be installed?

    • Taral Shah

      If PC has hardware serial port then no driver needed.. Else use a usb serial converter compatible with your OS

  8. Taral Shah

    If you have made hardware excatly as i specified in article, then it will work 100% sure. you can also see simulation as a proof.

  9. Taral Shah

    After you do all setup at hardware as well as software side, then if you use Desktop PC with inbuilt serial port then you can directly use it, but if you use laptop, you need to install drivers and change assigned COM port number to COM1. For that you see the all step mentioned very well here.
    http://plugable.com/2011/07/04/how-to-change-the-com-port-for-a-usb-serial-adapter-on-windows-7

Leave a Reply

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

© 2023 EMBEDDED TWEAKS

Theme by Anders NorenUp ↑

Follow Me

Get the latest posts right delivered in you mailbox

%d bloggers like this: