By James S.
Introduction
There are many ways that hardware, such as computers, can interact with other hardware, with software keeping the interfacing under control. On this page I will show you many ways to hook up your own circuits to a computer as well as how to use standalone (not connected to a computer) processor devices to interface with lights, switchs and more. In this case, microcontrollers, are a type of microprocessor that is a simple computer on a chip and are used in countless devices.
Interfacing to a Computer
As much as technology has progressed in a good way, for some, older computers are better for connecting you own circuits to, as you will soon see. The most popular way for hobbyists to interface with a 'modern' computer was by use of the parallel port, which was originally designed to communicate with printers (and then later was used to connect scanners and other devices to). Because of the parallel port being used to interface with more complicated peripherals, the parallel port's capabilities were expanded, such as to add bi-directional control of data. This resulted in several parallel port modes that could be selected from the computer's BIOS settings, to emulate the different modes, to maintain backwards compatibility to devices connected to the parallel port.
The parallel port is very useful for interfacing because it contains eight data lines that can be configured as either all inputs or all outputs, as well as several other signals that are always input and further signals that are always output. To use the parallel port, there are a number of control structures in memory which can be accessed through software.
To give you an idea or what is possible with the parallel port, you can connect switches which software can detect and respond to (which would be handy, for example, to trigger an alarm). You can also connect lights to the parallel port which software can turn on and off, which would be good, for example, to warn a user of a problem. And then you can get more advanced such as to update LCD and LED displays by using the parallel port.
You may be thinking that a type of interface that was once used only for printers but can actually be used for so much more, seems too good to be true. Well, it kind of is depending on what computer you have and this brings me back to the first paragraph of this section where I mentioned that older computers were better for interfacing. The problem is that some operating systems, including Windows XP, Windows Vista and Windows 7 prevent direct access to hardware ports such as the parallel port. However, there are ways to get around that, by using a special parallel port driver, for example. But that's if you even have a parallel port on your computer as especially with laptops, most computers nowadays don't even have a parallel port, replaced with USB which is not as straightforward for interfacing with. There are, however, USB to parallel port adapters but using them for interfacing with your own projects may not work.
One way to interface with a computer that does not have a parallel port (or cannot be accessed through software) is to use a USB experiment interface board. This gives you a number of inputs and outputs to which you can connect switches, lights, etc. and communicate with them using a driver.
Introduction to the Arduino
The Arduino is a very popular interface board especially designed for those new to microcontrollers and is very much a rival of the PIC microcontrollers. Although I started off with PIC microcontrollers, for a university project I was introduced to the Arduino and I quickly saw the advantages of using it. For one thing, even one of the most basic of the Arduino boards, the Arduino Uno, features plenty of digital and analogue I/O connections for interfacing with LED's, motors, and so on. This board also features an on-board USB port for easy connection to a computer.
The programming language used with the Arduino is called Wiring and is similar to C/C++ with facilities for setting or reading the ports and the like. After a program is written, it can be uploaded to an attached Arduino and then the program will run automatically. Since an Arduino board can be programmed 100,000 times you can make changes as often as needed. Like with most of the PIC microcontrollers, the program code for the Arduino is stored in flash memory (like what is used in SD cards) on board the microcontroller on the Arduino board; you get 32KB on the Arduino Uno. The Arduino, by the way, is the board itself including the microcontroller, but the actual microcontroller chip is not an Arduino, it's an ATmega328 made by Atmel.
The Arduino Uno has two rows of connectors which provide power, and interface in the form of digital I/O lines that can be programmed as either inputs or outputs, and a number of analogue inputs. You can plug wires into theses sockets, although it's best to use the type with pins either end so that the wires stay in place. You can also plug in a circuit board known as a shield which has a number of headers for connecting with the Arduino. You can buy these shields ready made designed for a set task (for example wireless communications) or there are ones available with a matrix of holes for soldering your own components. You could make up a shield yourself using stripboard although I found that some connections didn't quite line up so I had to bend the header pins somewhat.
Since the Arduino is so popular there is a wealth of information on how to use the Arduino along with details about the projects that have been made with the Arduino. On this page you will find my own projects that I've created using the Arduino.
While the Arduino is great for taking away some of the difficulty of interfacing microcontrollers with input and output devices, you pay a price for that convenience; I'll explain why. Microcontrollers, like other processors run on machine language, which are the binary values (0's and 1's) that make up the low-level instructions that form the microcontroller's program. You can program a microcontroller in assembly language, which consists of a number of instructions that represent the machine language values, and are more readable than a bunch of 0's and 1's, or even the hexadecimal equivalents (i.e. 5F, CE, etc.). Assembly language gives you great control over the microcontroller as well as optimum speed but assembly language is more difficult to use and not recommended for beginners.
So, the Arduino team went with a more high level programming language (Wiring) that is more easier to use than assembly language but the Wiring code gets converted into machine language before being uploaded to the Arduino. It's this converting that can cause programs written for the Arduino to run slower than if the chip had been programmed directly in assembly language. The conversion process is optimised, that is, the assembly language instructions are chosen to give as high speed as possible. What introduces slow down is that some Wiring commands, such as the digitalWrite function, have to do error checking, which means more assembly language instructions are required, slowing down how fast the program runs.
Fortunately, the Wiring programming language provides a way to more directly access the Arduino ports, giving greater speed at the expense of getting closer to the hardware level of the Arduino which may be confusing to those new to the Arduino or microcontrollers in general. In summary, if you are doing something simple such as flashing an LED, digitalWrite and similar functions will give you all the speed you need. But if you need greater speed, such as to update a large display frequently, if may be that you will have to use a more low-levels means of accessing the Arduino's ports. If you do use the low level commands your program will likely take up less space than if you had used the high level functions.
Introduction to PIC Microcontrollers
The family of PIC microcontrollers were first made by the company Microchip (there are now clones by other companies) and have since become very popular by both the electronics industry and hobbyists because they offer so much on one chip which makes complex circuits much easier. A good example is my traffic light project (which is on this page); to create a set of traffic lights using logic and timing components alone is quite tricky, especially if you was to realistically emulate a typical traffic light sequence. But by using a PIC microcontroller, the task becomes much simpler and the number of components and the complexity of the circuit greatly drops, as the PIC does a lot of the hard work.
The PIC microcontrollers range from basic to very complex devices yet even the most basic PIC's offer a lot for your money. A typical PIC contains on-board flash memory for the programming code (there are one time programmable and UV erasable versions too), EEPROM for storing settings and other useful data, a section of RAM (referred to as a collection of file registers), timers, analog comparators and bi-directional input/output ports.
A PIC microcontroller is programmed using a programmer connected to a PC, using either a low level programming language (assembly) or a high level language, such as C or BASIC. The advantage of high level programming languages is that they are much easier for beginners to learn, however, especially with the more basic PIC's, there is little space for code so using assembly has the advantage of making better use of the limited memory. Either way, the code you write gets converted to machine code which is then 'burned' to your PIC. Most programmers allow the PIC to then be tested by providing a number of switches and lights but depending on what you want the PIC to do and what testing features the programmer offers will determine how useful the programmer is for testing the PIC. You can also debug your code on your computer which will at least lets you check that your code is running as it should. MPLAB is a free PIC development IDE by Microchip which you can use for writing the PIC code, testing it and to convert to PIC code ready to be written to your PIC.
When you are happy the PIC is behaving as it should, you can then take the PIC out of the programmer and place it in your own circuit. If need be, you can put the PIC back into the programmer and update its code, perhaps to fix a bug that you have found or to change a timing value. The PIC's that use flash memory to store its code can be written to about 1000 times so that is a big bonus for flash PIC's. A good idea is to write the version number of your code on a sticker placed on the PIC along with a series of characters and numbers to reference your source code and project. Or, you could output the version number using LED's or a display (see the PIC Traffic Lights Project as an example of how you would do that).
If you do program a PIC in assembly language, there are a few things to watch as there is a limited number of instructions. While there are some very useful instructions, such as to clear or set individual bits, there are also missing some instructions you would expect to see. For example, to compare two values you have to subtract one from another and then test if the Zero flag has been set (although the development software you are using may provide additional instructions which are then converted to the individual instructions to perform the task like the one I just mentioned). Another thing to keep in mind is that there is only one working register (the W register) which is like an Accumulator register found in other processors and certain instructions make use of the W register. However, you do still have a number of general purpose registers which act as the RAM for storing your variables.
Because a typical PIC has so much built into it, a lot of its signals are multiplexed which means that a signal can function in many ways depending on a selected mode. For example, one signal may act as a reset if configured that way, but can also act as an input/output port if the software selects it to. Then, if the signal is in input/output mode, it can further be set as either an input or output signal. These considerations are very important when testing the PIC in a programmer or using the PIC in your own circuit as depending on what you want the PIC to do, you may have fewer input/output signals (for example) if you use certain features.
Another useful feature of a PIC is that it can either use a built in oscillator for timing (which cuts down on the number of components needed in your circuit) or it can use external components connected to the PIC for timing. There is one thing you must watch and that is if you select to use a PIC's internal oscillator, the programmer may no longer work with that PIC (the Velleman PIC programmer K8048/VM111 is an example of a PIC programmer that has that flaw).
The PIC programmer I use is the Velleman VM111 which I bought from Maplin; it comes in two forms which is the kit version in which you have to solder the components yourself, or the ready made version (which is the one I got) which comes with an RS232 cable. Yes, the programmer uses the old style serial port and it is advised by Velleman to use only a real serial port on-board a computer. However, the programmer does work on my laptop with a USB-to-RS232 converter as well as my desktop computer which has an on-board serial port. The PIC programmer is designed for computers running any Windows operating system from Windows 95 to Windows XP, however, the programmer works fine on my Windows Vista 32-bit desktop and my Windows 7 laptop by slowing down the speed of the programmer using the supplied software that flashes the program to the PIC (PIcProg2009).
The power supply is not supplied, which is needed for both flashing the PIC with your code and for testing the PIC while still in the programmer. An unregulated, 12V DC wall adapter rated at 300mA is what is needed for the PIC programmer and I happened to have one of those variable types with voltage settings; make sure the polarity is correct. There is no switch on the programmer to turn the power on or off but instead a switch for putting the programmer in standby, programming (or read) mode, or run mode for testing the PIC.
The PIC prgrammer supports many different types of PIC's that come in 8-pin, 14-pin, 18-pin and 28-pin sockets, with a chip socket for each type on board the programmer. You can only have one PIC in the programmer at once and you have to set the appropriate on board switch settings to connect the oscillator to the PIC. There are also four push buttons and six LED's for testing the PIC while in the programmer, although if any of them will actually be useful depends on the way you have the PIC set up (which connections are inputs and which are outputs).
The programmer also supports what is known as In-Circuit Serial Programmer(ICSP) which allows you to connect your PIC while in its own circuit to the programmer to update its code. If for example you had made your PIC project but wanted the option to be able to update the PIC's code at a later date, you can provide in the circuit of your project a connector so that it can be connected to the programmer. You can then re-program the PIC as if it were in the programmer but without having to take it out its own circuit (assuming that it is even in a chip holder). The cable between the programmer and your PIC circuit must not be longer than 15cm otherwise the programmer won't work.
The Velleman PIC programmer is not without its oddities; when a certain type of PIC is inserted in the chip holder of the programmer the warning light (LD9) will flash faint. According to Velleman this will do no harm to the PIC, but I can see how (like it did me) it could cause concern. Another quirk is with the programming software, PicProg2009, it will often say that the settings don't match the PIC in the programmer (even though they do). Sometimes the PicProg2009 will say there is no programmer connected and this happened to me when using an USB-to-RS232 converter so I uninstalled the converter from the device manager, unplugged the converter and when I plugged it back in again and the driver had installed I was able to program using the PicProg2009.
For writing the PIC program I use MPLAB IDE, which can be downloaded online for free, and has many useful features including a debugger. I write in assembly language, which is the PIC's native code, which gives me full control (and greatest speed) of the PIC, allows me to make full use of the available memory (the code space and RAM) and gives me an understanding of how the PIC works at low level. I then use PicProg2009 (which came with the PIC programmer-but you should download the latest version) to convert my programming to machine code and then store to the PIC. However, depending on whether you want to use assembly language and certainly for beginners, you can use high level programming languages, such as C and BASIC, which then get converted to PIC machine code. These high level languages may not make the best use of the PIC's program space, which is especially important with PIC's that have little memory, but will give you a chance to use your PIC without going low level.
PIC Traffic Lights Project
To be added soon...
PIC LCD Character Module Driver
The aim of this project is to use a PIC microcontroller to communicate with a standard character only LCD module based on the HD44780 chip. These LCD modules are found in many devices, including fax machines and alarm systems, and use a standard 14-pin interface (some have one or more additional connections for a back light) and can support up to 80 characters. These displays come in different sizes as some support only a few characters, others the full amount, but another variation is in the number of lines. For example, an LCD module that supports 16 characters could display all characters on just one line or on two lines of 8 characters.
These modules are very flexible in that they can be operated in either 8-bit or 4-bit mode with the 4-bit mode offering the advantage of few connections to the display that are needed. When it comes to microcontrollers, such as the PIC, it may be that there are only a few control lines to interface with the display. Especially in a system where you may have lights and switches in addition to a display you soon run out of interface connections on the microcontroller. This is where the display's 4-bit mode gives such an advantage over the 8-bit mode, with a little extra work from the PIC's software (having to break up a byte into 2 4-bit nibbles).
All content of this and related pages is copyright (c) James S. 2011-2012