Serial and parallel ports are largely a thing of the past. As laptops have become commonplace, there is less room for serial connections; parallel ports have also disappeared from many desktop computers. Today, nearly everything connects through USB: mice, keyboards, multifunction printers, mobile phones, flash drives, portable hard drives, Blu-ray players, modems and routers. In this project, we’ll build a microcontroller circuit that lets you switch an LED from a laptop using an application you create yourself over USB. Let’s get started.
Older serial and parallel connections transferred data at up to 115 kbps. USB 1.1 reaches 1.2 Mbps, while USB 2.0 reaches 480 Mbps. Using one connector for different peripherals—and connecting or disconnecting equipment without restarting the computer—has helped make mobile computing practical. USB also suits portable field instruments, such as equipment used to monitor weather on site. It’s time to explore projects built around USB connectivity.
Setting up EasyHID
Proton Development Suite includes the EasyHID USB Wizard, which automatically generates the programs needed to work with PIC 18F2455, 18F2550, 18F4455 and 18F4550 microcontrollers. We’ll use the PIC 18F4550. The wizard can generate PIC code for Pic Basic Pro or Proton, and computer-side code for Borland Delphi, Visual C++ or Visual Basic. You can choose these options as you go. In just a few steps, you’ll have a HID USB application ready for your own code. Start the wizard from either Microcode Studio Plus or Proton Development Suite, then fill in the information it requests.
We’re preparing this system to operate the NeoTeo multiband receiver, so the example names shown in the images relate to that project. Here, the goal is simply to get familiar with the wizard and the details that help establish a successful connection. Enter whatever application name you prefer. Do not alter the Vendor ID or Product ID on the second screen. These numbers are assigned by the organization that regulates USB product identification. A yearly membership, which provides numbers reserved for your development, costs US$4,000. For hobby projects, we recommend keeping the sample numbers supplied by the application.
Next, set how often the computer checks whether the USB connection is present (polling). This matters: the computer queries the link and, if it does not find the device attached, it drops the connection and the device becomes detached. The check can run continuously through a microcontroller timer interrupt (TMR1), or through a suitably structured program in the PIC. Leave the input and output buffers at 8 bytes. Until you’re comfortable building this kind of application, stick with the default values; they’re more than sufficient for an introductory project like this one.
Choose where to save the files. The wizard creates two separate folders: one for the microcontroller’s base program and one for the computer application, which you’ll use on your computer. Pick a short path close to the drive’s root, since long names can cause compiler errors. Below the PIC selection, you can enable automatic generation of an interrupt-driven program to keep the USB connection alive while the device is connected. That means you don’t have to write a timing routine yourself. That’s it: the wizard will assemble the code. Wait for it to finish without errors.
Circuit and USB connection
After these five simple steps, the programs are ready to use. First decide what hardware you’ll build around the selected microcontroller, then plan the rest of the application accordingly. Here, we’ll add a push-button and an LED, along with a 16-character, two-line LCD. This is also where you decide what the software should do and how the computer will control it. We want the PIC-based device to work on its own as well as from a computer. When connected to a laptop, its local controls will stop working; the LCD will indicate the connection, and the hardware can then be operated over USB.
The circuit centers on the PIC 18F4550 and includes a 16×2 LCD display, a USB connector, an ICSP connector for programming the PIC in circuit, a 20 MHz crystal, a push-button, a green LED, and a 5-volt input supplied by the computer through USB. Many projects draw their power from USB. Examples include charging batteries in MP3, MP4 and MP5 players (MP3 players), GPS devices, PIC programmers and other equipment. Keep in mind that the port can supply at most 500 milliamps. Exceeding that limit can damage the port. Sometimes replacing a fuse resistor on the motherboard will fix the problem, but don’t take the risk.
By default, the third EasyHID USB Wizard screen limits the current to 50 mA. That small amount is enough to detect the USB connection and provide the PIC program with the information it needs. Before writing any application code, program the PIC with the firmware generated by EasyHID: compile it first to obtain the .HEX file. Once the PIC is programmed, connect it to the computer. The computer should recognize it as a new HID (Human Interface Device), confirming at least that the microcontroller code was generated correctly.
The product name entered on the first screen should appear when you connect the device and the computer detects new hardware. Next, you should see that it is a USB Human Interface Device. If installation succeeds, Windows will report that the device is installed and working properly. In Windows XP, you can check under Settings > Control Panel > System > Hardware > Device Manager; the device should appear as shown on the right. Important: don’t repeatedly unplug and reconnect the device. The computer will identify this device once; for it to recognize it again, restart the computer or shut it down, reconnect your project, and start it again. This behavior is due to the program structure, which we’ll examine as we learn how to work with its settings.
Making the PIC work with or without USB
The PIC program generated by EasyHID follows a simple but rigid logic that can be difficult to understand at first. Once you grasp how it works, though, you can bring your designs to life in minutes. The routine first checks whether the device is connected to USB. If it isn’t, the program stays in an endless loop until it detects a connection or the PIC is reset. This happens in the section marked AttachToUSB, where the loop repeats until the PIC’s internal register detects the USB connection. If you planned to run a flasher, blinker, MP3 player or anything else on the PIC before connecting it to USB, the program will remain stuck in that loop until you do.
A single added line of code can break this dependency. We’ll use the 5-volt input from USB, routed through R6 and R5 to RC0. If RC0 detects no input voltage, the program can leave the loop and jump to a section we add for standalone operation—no USB connection required. In that section, the PIC can perform its usual tasks, such as reading or writing I²C EEPROM, reading a keypad, driving an LCD or carrying out other familiar functions.
When the device powers up and reaches the instruction that checks for a USB connection, it will be sent out of the loop and run independently. We show that state on the LCD with an appropriate message. This is where you define what your project does on its own. In our example, a push-button switches an LED on and off. You could instead build a field data-acquisition system that stores measurements on an SD card or high-capacity EEPROM for later laboratory analysis. We’ll use this foundation to control our VHF-UHF receiver, which we’re publishing in installments each Saturday. You could even build a cardiac Holter monitor for patients with cardiovascular insufficiency—or pursue whatever your imagination suggests.
The dual-mode system works by placing the USB-connection check—whether RC0 has gone high—as early as possible in the program. Two things then matter. The program may or may not continue to accept input from its built-in controls; in our example, the single button stops working because control shifts to the computer. You’ll also need to prepare code for whichever PC software you chose. We use a VB6 application that confirms the device connects and disconnects properly, demonstrating that PORTC.0 is working as expected. It includes a button to switch the device’s LED and a colored circle that mirrors its state, just as it does when the equipment is controlled remotely. A short label changes between “On” and “Off” to match the LED.
As shown in the circuit, the LED is connected to PORTC.1 and the button to PORTC.2. The application is straightforward, but it’s designed to teach you the basics of USB port control. Understanding USB is a key step toward building practical, successful designs, especially for portable computers.
Understanding the buffers and the USB routine
The microcontroller code is the more complex part, so let’s look at another common stumbling block: buffers and how their positions or numbering inside the PIC correspond to those in the computer software. In Visual Basic, position 0 is reserved for the device report. The generated code says: “… first byte is the report ID, e.g. BufferIn(0) the other bytes are the data from the microcontrolller...” So the computer-side enumeration starts at 1 and runs through 8, while the corresponding PIC buffer positions run from 0 to 7. For example, PIC USBBuffer[2], when used as data to transmit, corresponds to VB6 BufferIn(3). Compare the program listings we provide, note this difference, then expand the project with more buttons, LEDs or other resources to add remote-control features.
There are also two closed loops that can leave the PIC program stuck until you reset it. These are the points where it sends data from the buffer to the bus (DoUSBOut) or requests data stored in the buffer (DoUSBIn). If it finds no values, it waits there for data to arrive, so watch for this condition and make sure your program can get out of it. We’re providing the complete programs so you can experiment from a working example. What you build from here is up to you. This is just the beginning: Proton and EasyHID make the process straightforward, and USB connectivity is now in your hands.
Download: Software for the PIC and computer