Pages

Thursday, April 12, 2012

Controlling LED lights from your PC by using Visual Basic.



Wouldn't it be nice if you could just turn on and off a set of LED lights at your own will, from your computer?? Though this project is for LED lights, however, when you get the idea you can control anything, 
such as a robot, remote car, or even build a fully automated home! But to give the idea I will demonstrate using LED lights.

NOTE : This project uses the Parallel port of your PC. It is an essential component of the motherboard, and rough handling will result in burning of the port or even the motherboard. I take no responsibility under such circumstances.


So let's get started!

Things you will require-

>A computer with parallel port. (If your motherboard does not have the port you can buy a USB to parallel port converter).
>Visual Basic.
>Breadboard.
>LED Lights.
>Connecting wires.
>Resistors of suitable value.
>Tools (wire stripper, soldering gun).

What is parallel port??

It is a connector at the back of your PC. It has a 8 bit data bus, which we will be using to control our LEDs. The pins of the data bus can be made High or Low by programming.


It is also called as LPT port. Hereafter we will refer to it as LPT port.The pins are numbered. Pins 2-8 are for carrying data and we will use them.

Rough idea of the project-

Through programming, we can make an individual pin HIGH or LOW, thus causing the corresponding LED to turn ON or OFF.

How can we implement this using VB??

The actual coding to control the LPT port is very complex. We need not go into that. Experts have developed a DLL file, which contains the code to do that. All we have to do is include the DLL file in our project and then simply call the functions to control the port. Simple??! Yes it is!

Hardware Assembly : 
Take a suitable sized breadboard. Assemble the LED lights on it such that they same ground wire (All Cathodes (-) connected together) but different inputs (Anodes (+)). You can connect a maximum of 8 




The circles are our LEDs
 
LEDs. Take out n different input wires for n LEDs and one ground wire. Connect resistiors between Cathode and the wire which will come from LPT port. If you don't understand this, refer the diagram.
That's it ! you have assembled the hardware!

Now, programming-

Download the DLL file inpout32.dll from http://www.logix4u.net/. Once you download it, paste it in your C:\Windows\System folder. Don't worry, this is harmless if you do it properly.

Now open up Visual Basic .NET. I assume that you have a basic idea of VB .NET programming. However, if you don't have, just copy the code given below!

We will write a code to switch ON ALL the lights when user clicks a button and SWITCH OFF ALL the LEDs when anothe button is clicked.

First, write this code on the topmost:


Option Strict Off
Option Explicit On
Module InpOut32_Declarations
Public Declare Function Inp Lib "inpout32.dll" Alias "Inp32" (ByVal PortAddress As Short) As Short
Public Declare Sub Out Lib "inpout32.dll" Alias "Out32" (ByVal PortAddress As Short, ByVal Value As Short)
End Module


Thus we have declared that we will be using our DLL to control the port.

Now, place two buttons, one for switching ON (ON button) and another OFF button.

To turn ON all LEDs, place the following code in the Click Event of ON button.

Out(&H378S, &HFFS)

This is a function used in DLL file named OUT. The first argument is Port Address and second one is the VALUE to send to port. We want to turn ON ALL LIGHTS so the 8 bit binary word is 11111111 which is 
equal to 255 in DECIMAL and FF in HEXADECIMAL. So we have given '&HFFS' as arguement. The port address is &H378S for LPT, common on all computers.

To turn OFF all LEDs, place the following code in the Click Event of OFF button.

Out(&H378S, 0)
This will turn OFF all LEDs.


TESTING YOUR PROJECT : 

Connect the pins 2-8 of LPT to corresponding 8 LEDs. Connect the pins 18-25 to common ground wire.
Start your project. Test by clicking both buttons..nice effect, isn't it??
I tried this for many times but didn't work, but later I achieved success. Note that it runs on XP only.

This is only for turning ON and OFF the LEDs. You can experiment with folloeing things- sequence lighting, flicker lighting using timers..the possibilities are endless. 


If you encounter ANY problem, do tell me through comments and I will do my best to solve them.
If your project runs successfully, tell me that also!.
This is perfect for a mini project and we ourselves did it in our mini project for Digital Techniques in Second Year.

If you like this article do tell others.
Thanks and Cheers.

SSD

SSD (Solid State Drive) is a type of hard disk (Storage Device) which offers a great speed about 6 GB Per Second. But the stuff that maters is it's price.

Startup programs

If you want to open a program when Windows starts, just add it's shortcut to startup menu in start menu.

YouTube Downloader HD


Download YouTube Downloader HD and Download HD videos in seconds.

Haiiiiiiiiiiiiiiiiiiiiiiii!