USB Indoor/Outdoor Thermometer (or, 'My First USB Device')

82K9540

Intro: USB Indoor/Outdoor Thermometer (or, 'My First USB Device')

This is a simple design that demonstrates the USB peripheral on the PIC 18Fs. There are a bunch of examples for the 18F4550 40 pin chips online, this design demonstrates the smaller 18F2550 28 pin version.

The PCB uses surface mount parts, but all components are available in through hole versions. I use surface mount parts because its faster to make the board without drilling holes.

The end result is a small USB peripheral that measures indoor/outdoor temperatures. Data is available to the PC through a virtual serial port. Temperature sensors are used in the example, but other analog sensors can be added.

P.S.
See a live graph of the data from my logger here:

http://www.syndicit.com/stream/ian/nieuwemarkt/indoor_temperature/?format=graph

STEP 1: Design Overview

Required PIC components
This USB peripheral is based on the 28 pin PIC 18F2550. This chip has everything needed for a full speed USB interface.

A 0.1uf capacitor between power and ground decouples the PIC from the power supply.
A 20MHz crystal (used to generate a 48MHz internal clock) and two 27pf capacitors.
A 10K resistor from power to the MCLR pin. I added a 1n4148 equivalent diode between the resistor and power so that the 13+volts placed on MCLR during programming doesn’t damage other components on the circuit board.
An ICSP header (5 pins) is used to program and debug the device.

Required USB stuff
A USB female ‘B’ style socket.
220nf-440nf of capacitance is required for the internal USB voltage regulator. I use two 0.1uf capacitors (same as used for decoupling) without a problem.
A LED with 330ohm current limiting resistor is used to display USB connection status.

Required sensor stuff
I used the microchip TC1047A temperature sensor as the indoor temperature sensor. It is physically soldered to the board - this causes it to run a few degrees hotter than room temperature.
A header is used to connect an outdoor temperature probe (TO-92 version of TC1047a). An additional 0.1uf capacitor helps decouple a long wire run from the rest of the board.
USB voltage levels are not consistent enough to accurately measure and compute temperature from the analog sensors. An external voltage reference (MCP1525, 2.5volt) is used. The voltage reference requires a capacitor on the output of 1uf to 10uf. Without this capacitor the voltage reference will not work.

STEP 2: Firmware

The firmware uses the CDC USB class to emulate a serial port. The device will show up as a virtual com port on a PC. The firmware is broken into three sections:

1. The USB driver takes care of enumerating the device on a PC and emulating a COM port.
2. A service routine alternately reads the temperature values and converts them to a readable format. Doing both of these operations at once caused the USB device to loose connection the PC (time hogging). Converting the floating point values to a string in the service cycle following the conversion cleared up the connectivity problem.*
3. Finally, a main routine receives bytes sent from PC and returns the requested measurements. Multiple measures are separated by a comma, lines are terminated with a null character (hex 0x00)
The demonstration firmware accepts the follow commands:
Hex value Returns
0x00 Indoors temperature (on-board sensor)
0x01 Outdoor temperature (external header)
0x02 Both temperatures (comma separated)

Temperature is calculated based on the 2.5 volt reference and the Microchip TC1047A offset and slope. Other temperature sensors may have different characteristics. The TC1047A datasheet is available here:

http://ww1.microchip.com/downloads/en/DeviceDoc/21498c.pdf

The firmware is written in MPLAB with the demonstration PIC C18 compiler.

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en010014&part=SW006011

The actual USB firmware is provided by Microchip. The original environment is available here:

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en021631

To edit the firmware: extract the entire directory structure of the source to your root directory (c:\MCHPFSUSB). You can then load the workspace file in MPLAB.

*The float to string routine is a resource hog. To get around the connectivity problem the USB service routine is called at various points in the float2string routine. This would best be fixed by servicing the USB peripheral on an interrupt. I used the hack so that the firmware stayed as close to the reference version as possible.

STEP 3: Testing the Device

Instructions are for windows, but the device can be connected to anything that has CDC serial port emulation drivers.

Program the device with the firmware. All configuration bits should be set correctly in the firmware.

Plug the device into a USB port. Windows will detect it and prompt you to choose a driver. BE SURE TO CLICK ‘BROWSE FOR CUSTOM DRIVER’. Navigate to the location of the .inf file included with the project archive(driver-win2k_winxp). Windows will use this file to install the device.

*A note on the CDC driver – the CDC serial port emulation driver is included with Windows. The .inf file (provided by Microchip) simply tells Windows to connect these drivers to the device.

To find out what COM port the device was assigned do the following:
Go to the control panel (start->settings->control panel)
Double click ‘system’
Click the ‘hardware’ tab.
Click ‘device manager’
Click the ‘+’ next to ports
A list of COM ports on the system is shown. I have seen the device show up as COM 3 and 4, but it will vary by system.

If its not obvious which port is new, try this:
Unplug the device from the USB port.
One of the ports will disappear. This is the device’s COM port #.
Plug the device back in, the port will reappear.

The small VB application (source included) can be used to test the device. Enter the command to send, click 'Start'. The reply will be shown in the window.

STEP 4: What Else Can It Do?

This demonstration board is a good starting point for a small home weather station:
Add a HIH3160 humidity sensor, or replace the analog temperature sensor with the Sensiron HT74 high accuracy serial temperature and humidity sensor.
Add a Freescale MPX(a)4115 pressure sensor to measure barometric pressure.

Want to syndicate this data to web? Include it in your own website, view it in google earth, or get updates in your RSS reed? See this instructable on integrating this device with the syndicIT.com web backend:

https://www.instructables.com/id/E25ZKI1NDCEQZJJOTC/

41 Comments

could you mod this to sence temp in a fridge/freezer and activate a relay at a certain temp while showing temp while a pc is off?
sure, but you could also use a chip without USB if that feature wasn't required. I think that would be cheaper.
how would i make that with a usb and without (so i can use one or the other)
What skills do you already have? It's probably impossible to simply tell you these kinds of modifications. Can you make the changes to the cct and PCB in eagle? It's not hard, check out some of the tutorials here at instructables. The 'uC group is a good place to start. Good luck, and let me know how it goes.
i know about all the components but i just don't know how to use microchips in circuits
Any idea how I could do high temp monitoring for a woodstove. I assume I need a high temp capable thermocouple - but I'm not sure how I can interface that with a device like this?
I'd find the thermocouple first, its datasheet will tell you how to read it. Then its just a matter of writing the code - always easier than you think!
Sweet work! Really inspiring, even if I'm in a bit over my head. I too, am wondering how difficult it is to get Mac support for the usb - would it require writing a driver, or are there any pic drivers out there? Or would it just work as a standard serial port???

As to making surface mount boards, the best I've seen is the skillet method. Here's a link to some great lead free solder paste that doesn't even need refrigeration - it's about 35 bucks a syringe and that's including shipping.

http://www.celeritous.com/estore/index.php?main_page=index&cPath=6_17
The device uses the CDC driver (virtual com port). There are already drivers in windows, so I bet there is something similar for mac. I make all my surface mount boards with a hand iron (though I have tried the skillet method). The stuff on this board is huge (0805/SOIC) so there is no problem soldering with an iron. I hand solder all the way down to 0402 and TQFP (QFN requires a hot air rework station, but still very doable). No need for soldering paste, regular solder is fine. I know it would be great to use the lead free stuff, but its really not as nice as 60/40 lead solder. At the quantities that the hobbyist uses the tiny amount of lead is not a problem.
This is perfect for what I need. Great instructable, but one question; How crucial are the values of the caps for the crystal. Somewhere I saw that you should match the impedance of the crystal. You say 27pf somewhere on this page, but the eagle file says 30pf. I don't think that it really matters that much. Great instructable!
Or will a 3 terminal crystal resonator work?
Sorry, Ceramic Resonator.
sure, it will work, just run the grounding wire through a rectifier matrix use .7 less volts. should work fine. (lol jk, if you know anything about electronics at all you'll figure out that theres no such thing as a "rectifier matrix" and .7 less volts wont do anything. I dont know if your idea will work but its sounds ok.)
Ok, I've looked into this a little, and it seems to me like a 20Mhz resonator would work, but it wouldn't be as stable/exact/accurate. I don't think that this needs to be overly stable/exact/accurate so I think it would work fine in this.
Does anybody know if this will work with a Macintosh computer?
I just finished re-laying out the microdot before writing this email. A little cross-eyed at the moment, I'll sleep dreaming of red and blue lines in my head. I couldn't fit any more than the LEDs and switches though for extra functionality as we've previously discussed...trying to keep it small. Was a little disappointed I had to add an extra 5mm to the dimensions...but it should still fit on a wrist. Currently 1.3x1.1inchs. I'll keep the icd in mind, though eventually I went and bought a meLabs USB programmer rather than fiddle around trying to make one. Nice little unit but no ICD as far as I can tell. Fortunately I've got a 18F2550 hanging around, so along with etching up a microdot and a couple of paying jobs, I'll etch up the thermometer, burn your code into it and see if I get a virtual comm port....that'll be the final test to see if it works with USB1.1
please get me some examples of using the 18F4550...using usb incircuit progamming...please...it would be of great help
Great! I hope the firmware/source is helpful. If you build the thermometer, consider pushing a little data to a syndicit.com feed. You can read more in the other instructable I posted today. 1.3x1.1 inches is really quite small! I've been working on another wristwatch since finishing the 01/\/atch, called the Digitwatch. I've made half-a-dozen prototypes, but this seems to be a project plagued with difficulties. When I get the watch set in clear acrylic resin the touch switch goes nuts and stops working. I made the traces too long and created an antenna, thats my theory anyways. The dimensions of this watch are about 2x3 inches, and its still wearable.
BTW: pin headers are not the greatest medium for a wrist watch touch switch - I wore the prototype to a culture night at the Amsterdam modern art museum. I tore up myself (and some around me) with the pin headers....dangerous fashion.
Great project. Would you please write down which software do you use for 3D design board in picture above.
More Comments