Microchip PIC18F27J53 Generic HID USB Project

For various projects I have settled on using the PIC18F27J53 microprocessor. It is an 8 bit PIC microprocessor which runs at 48 MHz (12 MIPS), has 128 kB of memory and has a built in USB transceiver. This chip should provide more than enough flexibility for all of the projects that I have in mind.

The code here should give you a start in getting a PIC18 device talking to a Mac OS X computer via USB.

My first goal was to get the USB interface working. For the microprocessor code, I am using the Microchip Application Library, which can be downloaded directly from microchip.com. The microprocessor code is based on the PIC18F demo code found at waitingforfriday.com/index.php/Building_a_PIC18F_USB_device and the USB demo files included with the Microchip Application Library. I have reworked a lot of the code in these demo to work with the PIC18F27J53 microprocessor, simply the code down to the bare essentials, and to use ascii characters for the control characters.

Download the Generic USB HID PIC18F27J53 Code

To get the code to compile you will need to have installed: MPLab, C18 compiler, and the Microchip Application Library. When you first load the MPLab project you will need to add the Microchip Application Library to the include search path. In MPLab 8, this is found under the Project->Build Options->Project file menu item. Under the directories tab, select ‘Include Search Path’ and add the directory for the include files for the application library. In my case, this is E:\Microchip Solutions v2011-12-05\Microchip\Include. You will also need to link in the usb_device.c and usb_function_hid.c files. You may also want to change the compiler mode to use the large memory model, otherwise you may get warnings when using string literals if you don’t cast them as  (const rom far char *).

For the host side, in this case Mac OS X, you will need to install Xcode before getting started, a free download from the Mac App Store. The code here is based off the code found here, again with several changes and simplifications.  To get the code running, simply add the two files to a new Xcode project and add the IOKit framework. The program presents you with a command prompt. At this prompt pressing ‘a’ will read the LED status (defined in hardwareProfile.h as LATA<3>), ‘A’ will toggle the LED, and ‘d’ will return the debug buffer.

Download the Generic HID Mac code


A useful tool included with Xcode is the USB prober, use spotlight to find it. This tool details all of the connected USB devices. I typically have it open, just to make sure that the device is properly enumerating.

Generic HID Mac

Generic HID PIC18F27J53

Temperature Controlled Motorized Furnace Vent

During the winter, our bedroom can get very warm. Inevitably, we end up closing both vents in the room and then several hours later open the vents because we are freezing. I decided to automate this process.

Originally I had envisioned having a small temperature sensor across the room which would relay open and close commands wirelessly to the motorized vent. Given the unreliable results from the light switch controlled outlet project, I abandoned that idea. Instead, I decided to put the temperature sensor on the same board as the vent motor controller.

I purchased a Vent-Miser programmable vent. These vents have a programmable timer and will open and close the vent depending on the time of day. All I cared was that it was a motor controlled vent.

The motor is controlled with a PIC16F1824 micro-controller and the temperature is read using one of the analog to digital inputs fed by a TC1047A temperature to voltage converter. The PIC spends most of the time asleep allowing it to conserve energy. Once a minute it wakes from sleep, takes a temperature reading and decides whether or not to open the vent. With the temperature sensor inside of the furnace register, there are some wild temperature swings when the furnace is on. To compensate for this, the PIC looks for the minimum temperature over a ten minute period. Then, depending on the minimum temperature, it makes a decision about whether or not the vent should be open. Currently, if the temperature is over 73 degrees it will close the vent and if it is under 70 degrees it will open the vent. This was done to prevent the vent from opening and closing sporadically due to noise on the temperature sensor.

The entire circuit is powered by a 9V battery. Using very imprecise measurements, I estimate a lifetime of just over two years. This of course depends highly on how often the vent opens and closes.


If you would like detailed schematics or code, please let me know and I will post them as well.

Revisiting Light switch controlled plug wireless link

My first attempt at wirelessly controlling outlets could be considered a failure, even if technically it works. My solution was simply too unreliable and was rather awkward to assemble. The main failure was my decision to use the cheapest wireless receiver and transmitter pair that I could find. Unfortunately they proved to be completely sporadic in anything but ideal conditions. With a nice pair of antennas they probably work fine, bu with the small enclosures that I was using there simply wasn’t room for a quarter wave antenna (just over six inches). Instead of buying a helical antenna which would fit in the enclosure looped the wire antenna around the inside. It worked, but not well enough. When the lighting in the room depends on a wireless connection, a light randomly turning off and back on makes it very obvious that my solution was not good enough.


The other problem was how difficult it was to put the boxes together. I decided early on that I would simply use old cell phone charges that we had laying around for the AC/DC converter to power the micro controller. What I failed to consider was that these boards take up a lot of space and the extra wires would make it a spaghetti bowl of wires.


I really really want to walk into our living room, flip the light switch and have all of the lamps turn on and off together. I also like being able to use devices how I want to, and I haven’t seen anything commercially available that I like.


At this point, I have decided to put more time into my wireless plug control boxes and make them work. To avoid a lot of the wireless link issues I have decided to use the MRF89XAM9A 915MHz transceiver module from Microchip. It has a properly designed antenna (with impedance matching), has a tx/rx buffer, and is even FCC certified.


For powering the board I have decided to integrate an AC/DC converter onto the circuit board, which will save a lot of space and aggravation. Dealing with main voltages is always dangerous, so be careful. I ultimately decided against using a transformer free design because of the power levels needed for the transceiver (27mA at 3.3V). I would need a huge capacitor rated for use at 120 VAC, and they would have cost more than a transformer and rectifier.


The transceiver module is backordered until March 2012, so I may not get a chance to do much more until then.

GPS Puzzle Box – Reverse Geocache Box

For a good friend, I decided to make a GPS puzzle box, also known as a reverse geocache box. We have both shared an interest in electronics, how things work, and challenges. Not only is the box a puzzle to get open, but once it is open, he has a PIC based board with a GPS module, LCD screen, button, and an RGB LED that he can hack and make his own project, in addition to the contents of the box.

The GPS puzzle box is much like a treasure box, except that you are carrying the treasure inside of a locked box. The box only unlocks once a series of waypoints have been visited. The box looks like any other box, except it has an LED display and a single button. When the single button is pushed, a message displays greeting the treasure hunter and then proceeds to give a hint to the next waypoint. In my case, the box gives the distance from the waypoint. If the person is close enough to the waypoint, the waypoint is marked as visited and a clue to the next waypoint is given. Once all of the waypoints have been visited, the box unlocks itself, allowing the person to open the box and receive its contents.

In theory, the box is simple. It consists of a mechanized locking mechanism, a microprocessor, and a GPS module. However, in practice, as usual, the implementation is much more complicated. For this box power conservation is key, once the battery goes dead, it could be locked forever. When the button is pushed, it activates a small circuit which then powers the microprocessor. Then microprocessor then powers each of the other modules, as they are needed, and cuts their power, and eventually power to itself, as appropriate. In the power downed state, the circuit draws an extremely small amount of power.

If you would like more details about my implementation, please let me know in the comments below.

Below are pictures of the nearly completed box.



Wood Box Construction