Saturday, May 7, 2011

Trip me once shame on you. Trip me twice shame on me.

I don't believe it. I just realized I got bogged down by the same bug twice. In both cases it took me hours to hunt down the cause. First time around it was with the PIC12F1822 last March while developing the toy traffic light.  And now with the PIC16F1827 for the kitchen timer. In both cases I was having a problem getting the MCU to sleep. And in both cases it was because I was clearing the interrupt-on-change interrupt flag bit IOCIF (which is read-only) in the INTCON register when what should be cleared is IOCxF register (where x = A or B depending on the PORT where IOC is present).

From the PIC12F1822 datasheet: "The IOCIF Flag bit is read-only and cleared when all the Interrupt-on-Change flags in the IOCAF register have been cleared by software." (DS41413B-page 91)

And from the PIC16F1827 datasheet: "The IOCIF Flag bit is read only and cleared when all the interrupt-on-change flags in the IOCBF register have been cleared by software." (DS41391D-page 86)

Because the IOCxF register remained non-zero, INTCON.IOCIF remained set and the MCU kept waking up immediately after the sleep command was executed.

This is just unbelievable. I actually completely forgot about the 12F1822 debugging nightmare I had just two months ago. And repeated the very same mistake and had to grope around for the cause all over again. My brain needs a tune up badly.

Monday, May 2, 2011

I guess I'm big on timers

This is a variant of the 99-minute timer. It has two keys for incrementing and decrementing the countdown time (green buttons). And it has a third key (the yellow one) for countdown time recall (and/or some other function which I have yet to come up with). Instead of a common anode, I've used a common-cathode two-digit seven-segment display. The reason is that I can use N-channel MOSFET transistors and dispense with transistor base current-limiting resistors. Three 2N7000 MOSFETs are used here--two for the display and one to switch the buzzer which is off-board. 

MCU is a PIC16F1827, a relatively new model in the enhanced mid-range line of PICs. All I/O pins are used. VPP pin is dedicated to ICSP. The green pushbuttons use the ICSPDAT and ICSPCLK pins. Since the buttons are normally open and use the MCU's weak pull ups, the pins are effectively unconnected when the buttons aren't pressed. Thus, serial programming is possible.  [May 3 edit: What the heck was I thinking when I wrote that?! The pushbuttons are NOT connected to the data and clock lines of the ICSP. Rather a 32.768kHz crystal and its two 18pF capacitors are, since those pins are also for timer 1 (pin labels T1OSI and T1OSO). I had previously tested whether there would be any problem with those programming pins being connected to a crystal and found that ICSP was unaffected.]

There are four jumper links (three are visible in the photo)--two for VDD and two for ground. I don't know if I could've eliminated the jumpers had I placed the components differently and routed differently. I tried. I really tried to minimize the number of jumpers.

As with the water heater timer board I've again left the photoresist on the traces. Only the photoresist on pads were cleaned off using acetone. And this was done immediately prior to soldering to prevent oxidation buildup. No further preparation of the pads was necessary. The freshly exposed copper took on solder easily.

The components of course were a different story. All leads which showed signs of tarnishing/oxidation were cleaned using the gray (abrasive) end of a Faber-Castel 7061B-50 rubber eraser. After which leads of all components were wiped down with isopronanol soaked cotton to remove oils and eraser debris.

Unfortunately I don't have any solder that's less than 1mm in diameter so the joints tend to have excess solder on them. The copper pour areas are the pads for the 78L05 voltage regulator. The larger copper area will aid in heat dissipation since the input voltage to the regulator is 12V and so there's a large headroom of 7 volts. Given approximately 50mA maximum current through the display, the 78L05 will be dissipating some 350mW. With a junction-to-ambient thermal resistance of 200C°/W for the TO-92, the regulator would reach a temperature of 0.35 x 200 = 70° above ambient. With an ambient of 30° the junction could reach 100° when "8" is being displayed. This is still below the absolute maximum of 125° but the lower the junction temperature the better.

Sunday, May 1, 2011

Not so classic electromagnet for kids

Promised my nephew we'd make an electromagnet. So I cobbled up one using the entire coil (6.5 meters) of enamel wire from the inductor of a burnt out compact fluorescent lamp, a 1/4" x 2" zinc-plated bolt, and a couple of 15ohm 3watt resistors from some old busted I don't remember what appliance. Juice provided by a power supply.

I measured the enamel wire's resistance at 2.5ohms. I could've used a single dry cell (say, a D battery) to power the EM but at 600mA current draw it would run out pretty quickly. So I decided on using a power supply. Unfortunately the PS's minimum output is several volts which meant I had to add some resistance to decrease the current flow or the EM would get too hot to hold!

To build the EM I just coiled the wire around the bolt, going back and forth its length a couple of times till the wire ran out, leaving the two ends of the wire sticking out to make electrical connection with the resistors and power supply. The two 15ohms were paralleled (to decrease resistance and increase power dissipation capacity) and the pair connected in series with the enamel wire. With wire resistance of 2.5ohms and the paralleled resistor value of 7.5 ohms, the total resistance was a nice round figure of 10ohms.

I soldered the resistors on a piece of stripboard and added two PCB terminal blocks on both ends to connect wires to so that when this electromagnet circuit is scrapped I'll still be able to use those porcelain-encased resistors. The resistors are paralleled with jumper wires on the terminal blocks.

I initially powered the circuit using 3.3VDC from a regulated supply. That's gives 330mA of current. Magnetic flux was fine but not at all strong. Once nothing blew up or got fried I increased the supply to 5VDC. With half an amp the resistors were dissipating almost a watt each [(0.5A/2)^2*15ohms] and the electromagnet (the enamel wire), 625mW.

Increasing the current through the coil would increase the magnetic flux but will proportionally increase the heat dissipation of the circuit as well.

Optimally of course I shouldn't have to resort to adding resistors. As the above computations show I was throwing away 2W as heat while less than a watt was doing useful work. Well I do have more dead CFLs whose circuit boards still have to be salvaged from the lamps. If the 5-year old brat doesn't get bored with the EM after just a few minutes and ends up wanting to play with it some more in the coming days I'll crack open (literally) those lamps and splice more enamel wire to the EM to bring the total wire resistance to 10 or more ohms. May have to use a larger bolt. And have to check that it doesn't become too warm to handle comfortably!

Wednesday, April 27, 2011

Populated PCB

So here's the board with all the components soldered on. Very few parts indeed. I could've soldered the ICs directly onto the board but I make it a policy to always use sockets. If ever I decommission the board I can always recover the ICs instantly and reuse them without the hassle of desoldering and without having solder all over the pins.


As you can see I didn't remove the photoresist from the tracks. I used a Q-tip (cotton on a stick) dipped in acetone and dabbed the pads with it. It wasn't even necessary to rub the pads with the Q-tip. The cotton soaked up the photoresist, leaving clean unoxidized copper ready for soldering. No further preparation of the pads was necessary. Soldering was a cinch--given that I cleaned the leads of the components with isopropanol (ordinary rubbing alcohol) just prior to soldering. Small pads connected to  narrow tracks are actually easy to solder onto because there is less copper to dissipate the heat.

The only part that I goofed was the triac (TO-92 package). It was an old part and was heavily oxidized. I cut corners and didn't clean it sufficiently and so paid for with leads that wouldn't take on solder. After snipping off the excess leads I dabbed solder on the freshly exposed metal thereby finally making good contact. That's the reason for the blobs of solder on them.

The board is now installed and the water heater timer is working as designed. It's controlling how long a 2500-watt heater is kept on. Just a brief description of the operation:

Countdown time depends on how many times the user presses the momentary contact switches.

Initially, momentarily pressing switch A sets time to 2 minutes. Thereafter each momentary press of A increments time by 2 minutes.

Initially, pressing switches A and B at the same time sets time to 5 minutes. Thereafter, each press of A will increment the time by 5 minutes.

If the user makes an error pressing B cancels the countdown and s/he can begin anew. When time has been set and after around 3 seconds with no button presses the heater is turned on and countdown commences.

Initially, keeping A depressed for several seconds (until the "heater started" beep tone sounds) will set time to 30 minutes and immediately turn on the heater.

Once countdown begins pressing either switch A or B will abort countdown and heating.

Because there are no display readouts, a sonalert buzzer and an LED are used as feedback to the user. 2-min and 5-min increments, 30-min set time, abort/cancel all have different beep patterns so the user doesn't get lost, so to speak. The LED flash pattern is in sync with the buzzer.  Once countdown starts the LED blinks once every second.

Tuesday, April 26, 2011

Freshly etched PCB

This phenolic paper board measures 3x2 inches. It was cut using the score-and-snap method from a 6x10 inch board. The presensitized board was exposed for just 60 seconds. Ten more seconds wouldn't have hurt. Not sure how much less I could've shaved off without compromising the exposure quality. Copper has already been etched away but the photoresist is still on it (it's really metallic blue-green in color but it looks black in this pic). I've already drilled out the holes but this image was taken right before drilling.

As I said in my previous entry, as a substitute to silkscreen on the component side and in lieu of just painting the empty spaces with copper or laying a ground plane (which isn't necessary here or is it that rational on a single sided board), I try to cram as much textual info as I can on the copper side since that doesn't cost extra at all. I would've wanted to label most of the components (put labels adjacent to them) and their pads but there just isn't enough space. So I just listed the major components at the bottom of the board. Hardly standard or industry practice. But these boards are for my own use and I'm tailoring them to my needs.

The connectors (2 x 2-pin and 1 x 4-pin) are all on the right side and the labels refer to what each pin of those connectors are for. There's an ICSP header for the PIC MCU in the middle of the board. You might be able to make out the "VPP" label marking pin 1 of the header. That label will come in handy if I ever re-program the MCU in the (far) future since the PICkit 2 or 3 will go either way into the header. Plugging the programmer the wrong way around is, needless to say, a no-no.

You'll probably be able to make out the "MOC3021" near the top left hand side. That's a 6-pin triac optoisolator. There's going to be 220VAC on this board so I've placed all 5VDC components and tracks on the DC side of the optoisolator. The left side is dedicated to high voltage power circuitry. Nice clean, copious electrical isolation, eh?

The circles with crosshairs on each of the corners of the board are of course for mounting holes. The traces for those crosshairs are 10mils wide. And they're still very distinct and without any visible breaks. So I can probably get away with tracks as narrow as this if I don't bungle anything all the way to etching.

Given my breathtaking shoddiness in soldering I try to keep as large a space around pads as I can so as not to short anything. The smallest pitch I specify on my board layout is 100mils. So for TO-92s I have to physically spread the three legs apart before dropping them into the board. With my now extremely poor vision and unsteady hands I doubt I'll be able to solder anything with a smaller pitch. Right now the board looks pristine and immaculate. But the copper side won't be quite as pretty when I'm through soldering the components.

Making PCBs using presensitized boards

Here's my current procedure for making single-sided printed circuit boards. 

1. Draw the schematic and PCB layout. I use DesignSpark PCB since it's full-featured and free without any limitations whatsoever. Getting the layout right is the most time-consuming part. But it's all on computer so I really have no problem with this part of the work. In fact I kind of like it--optimizing a design on the drawing board is my kind of game.

Instead of (just) pouring copper all over the empty parts of the board, I put in as much relevant text including what it is for, creation date, what major ICs populate it, etc. Since I don't have the luxury of having a silkscreen on the component side, placing information on the copper side is a fairly good substitute.


2. Print PCB layout on transparency film. Since I'm using an inkjet printer, the transparency is one made specially for inkjet printers--one face of the sheet is rough to take on the ink. Scale of printout is of course is 1:1. This is very important. And to make sure that component lead pitch on the printout is correct it may be wise to print a draft copy on plain paper and check.

The printer is set to black-only printing. Contrast and other controls (eg. gamma) are set to maximize ink deposition and darkness. I'm using an Epson T10 and the print quality leaves much to be desired. It isn't solid black--there are minute cracks, gaps, lines which allow light to seep through. Because of this exposure time has to be at its minimum else copper traces will be compromised.


3. Cut the presensitized board to size if necessary. I use the score and snap method: Score both sides of the board where the cut will be made using an X-Acto type of knife. Use a ruler/straightedge as a guide for the knife. Run the knife several times to deepen the groove. Insert the board into or lay it underneath a thick book (I use a telephone directory) all the way until the score line. With one hand pushing down on the book, use the other hand to push down on the part of the board that's sticking out. If the scoring is deep enough, the cut should be fairly clean.

Peel off the protective backing. Center and secure the transparency film to the board. Do this under subdued lighting and as quickly as possible. There are various ways of securing the film to the board. Taping the transparency and board to a glass sheet is one, albeit crude, way of doing it. With my current setup I have a large foam sponge (approx 15" x 24") I use as a base on which the presensitized board is mounted (photoresist side facing up). I then lay the transparency on top of the board and align it. Printed side of film touches the presensitized side of the board. Next a large glass pane (3mm thick) goes on top. Finally the fluorescent lamp is placed on top of the glass. The lamp doubles as a weight to compresses the board-film-glass sandwich. The foam at the base evens out the pressure. Placing additional weights on the glass may be necessary because it's important that the transparency is firmly touching the photoresist for the traces/tracks and pads to be distinct.

4. Expose to UV light or fluorescent light or sunlight. Exposure time depends on light source, its intensity, and its distance from the board. It will usually be anywhere from a minute to ten minutes. With Kinsten phenolic paper single-sided boards and the 2 x 11-watt Toshiba daylight compact fluorescent lamp set up that I use with the lamps approximately 2 inches from the board, it takes between 1 to 2 minutes for boards less than 4x4 inches.

5. Develop the PCB until light-exposed photoresist is washed away. Agitate the developer and board while developing. If developer isn't included with the presensitized board when purchased, then one can make it by using a sodium hydroxide (NaOH) solution or better yet sodium metasilicate solution. If the board isn't underexposed, developing time will usually be under a minute.

6. Rinse developer off the board under running water.

7. Etch the board in a mixture of (by volume):
1 part tap water
1 part 6% (20 volumes) hydrogen peroxide (H2O2)
1 part 29% hydrochloric acid (HCl).
Etching time will depend on the thickness of the copper cladding. Etching will usually be complete within  three minutes. The etchant will turn green as the acid reacts with the copper and eats it away. The mixture is exothermic so expect it to get warm. Bubbles may also form. It's probably oxygen being liberated from the peroxide. 

8. Rinse etchant off the board under running water.

9. Drill holes into pads.

10. Remove photoresist from the the pads using Q-tips (cotton swabs) dipped in acetone. Photoresist on tracks and any copper pour area and anywhere where there won't be any soldering can be left on. After using a solvent to remove the photoresist the copper cladding may have to be further cleaned using a scouring pad to prepare the surface for soldering. 

11. Solder components to the board.


The great part about the etchant chemicals used above is that they're readily available in supermarkets, hardware stores and drugstores. I've chosen not to use ferric chloride not only because it can only be obtained from electronic parts and chemical suppliers but also because it's dark opaque color detracts from visually inspecting the progress of the etching process. The HCl+H2O2 mixture on the other hand is readily available, transparent, quick-acting, and cheap. I even use the spent mixture as toilet cleaner.

Safety notes: Sodium hydroxide and particularly hydrochloric acid are nasty stuff. You don't want them on your skin, your eyes, or making their way into your respiratory tract. Needless to say they are toxic when ingested. Here are safety guidelines that must be observed when using these substances:

* Always don protective eyewear, face mask, and gloves when using the chemicals
* Use only plastic (eg. polypropylene, polyethylene) containers for the chemicals and any implement that will come in contact with them
* Always pour water and H2O2 into a container first before adding HCl
* Always mix the chemicals outdoors or in well-ventilated area
* Dispose of them properly.


Some references which I found useful for PCB making:
http://www.youtube.com/watch?v=VtmWaxzgoK8
http://www.youtube.com/watch?v=RfelrrZyCYQ
http://www.rcexplorer.se/page14/page15/page15.html
http://www.foxhunt.com.au/misc/pcb/making_pcbs.htm
http://www.electricstuff.co.uk/pcbs.html

Saturday, April 23, 2011

Using the ICSP and T1OSx pins of the 16F1827

Am going to use the PIC16F1827 and the circuit needs a 32.768kHz crystal on the T1OSI and T1OSO pins. As it turns out the same pins are used for the clock and data lines of the In-Circuit Serial Programmer (ICSP) which will be needed as well. So I had to know whether I can have a crystal on the pins and still program the MCU via ICSP. I breadboarded a circuit and test shows there's no problem using the ICSP with a crystal and its capacitors on the pins.

However, I discovered that the crystal and timer1 will not work if the PICkit 2 programmer is left connected. Once removed the timer works fine.