Monday, January 25, 2010

Switching 120VAC With 5V Digital Logic

After building my glorified 2 outlet power strip for my switchless desk lamp, the natural next step is to build a switched outlet that can be electronically controlled. I separated the two outlets electrically, and connected them to opposite poles of the relay, so one of them is normally open, and one of them is normally closed, and they reverse when the relay is energized.

NOTE: It is important for me to be exceedingly clear that this is primarily meant as a prototyping device.  The gang box is used as an enclosure, not because it's meant to be installed in a wall, but because it's the perfect sized enclosure.  You are dealing with mixed high and low voltages here, so implement at your own risk.
Video:

How it works is that it has a double-throw relay inside, which is controlled by the 5V logic connected to the terminal strip on the side of the junction box, and switches power between two separated outlets, so you can have either a positive output, or an inverted one, depending on what you're doing with it.
The three terminals are power, ground, and control.  This is because the relay draws 65mA, which is much more than TTL logic is designed to drive, so I used a transistor, which only draws 4mA, to switch the higher current, which is sourced directly from the TTL power supply.  The diode is called a freewheel or flyback diode, and is used to suppress the voltage spikes caused by the inductive nature of magnetic relays.


Parts list:
  • Extra deep single gang junction box (approx 65 cents at hardware store)
  • Duplex 120V outlet (Less than $1 at hardware store)
  • Single gang cover plate, matching color of outlet (Less than $1 at hardware store)
  • 2 6-32 screws to assemble outlet
  • 4 contact terminal strip ($1.10 at Halted)
  • 2 8-32 screws + nuts to attach terminal strip to junction box
  • Old 3 conductor power cable (spare computer cables are perfect for this).
  • Relay with a 5V coil, and 250V contacts. ($1.22 at Digikey)
  • 1N4002 diode ($0.26 at Digikey). This is not a critical value; Feel free to use any value diode.
  • 2N2222 NPN transistor ($0.49 at Digikey). Note that I used one from inventory, which happened to be in a TO-18 case, instead of the more modern TO-92. Don't worry about it.  Even a 2N3904 would work.
  • 1k resistor ($0.064 at Digikey)
  • 2 pieces heavy gauge (at least AWG14) wire.


First is preparing the outlet and junction box.

Normally, both plugs in an outlet are tied together, since they're all on the same circuit.  I wanted one of the two outlets to be the inverse of the other, so this had to be fixed.  Outlets are designed so that you can just cut a tab between the two screws to electrically separate them.  We only need to cut the tab on the hot (brass) side, since they're still going to share the same neutral.

For the junction box, drill two holes to attach the terminal strip.  I used a terminal strip because this is primarily meant for prototyping, so I want strain relief while still being able to easily connect and disconnect it.
And remove one of the plastic tabs on the bottom for the three TTL wires and the 120VAC power cable to come into the box.  You can punch it out with a screw driver, or your bare hands, if there is an attractive female watching.

Now fire up your soldering iron and start making some magic.  This was my first attempt at dead bug construction, where you glue the parts down upside down and solder them in the air.  Using hot-melt glue was probably a bad idea, and I probably should have covered the 5V stuff in something to protect it from the high voltage lines running on top of them... Food for thought...

Cut off a few inches of insulation from the power cable, and wire up the outlet. Be sure to feed the power cable through the junction box before soldering it to the relay!!! (Not that I would do something incredibly stupid like that... honestly...)

Once it's all wired up, feed it all into the junction box, and screw together the outlet and face plate (Of course, you tested it all before tightening down the screws, right? That'd be pretty silly to close it all up, and then have it not work...).  Wire up the terminal strip, and you're done!

There is quite a few possible uses for this thing.  We've come up with using it to automatically turn on appliances like a water heater right before your alarm, so that you can wake up to hot water for tea; or flash the lights a few times before turning them on to wake you up in the morning; or even using it to turn off a fan a few hours after you go to sleep during the summer so it doesn't run all night.

Thursday, January 21, 2010

Programming Arduino AVRs on the Cheap

I've been wanting a few extra Arduino ATmegas lately, so that I can use them in my projects instead of the entire Arduino board, and have the projects be a little more permanent.  The problem is that when you buy ATmegas from somewhere like Digikey, they don't have the Arduino bootloader burned on them, and buying them anywhere else tends to be a little more expensive.

I've been putting a lot of thought into how to fix this, since AVR programmers aren't cheap (cheapest is the USBtinyISP at $22).  The thing to do is borrow someone else's, since it's only needed to burn the bootloader, which handles reprogramming it from there on.  The problem is that I only know one other person who has any kind of an interest in microcontrollers, and he just bought his first Arduino over winter break, so borrowing a programmer isn't going to happen.

Another one of my friends then gave me a miniPOV 3 kit that he had been given and no longer wanted, and that gave me an idea.  The only difference between programming the ATtiny2313V on the miniPOV, and programming an ATmega168 from an Arduino is the pinout.  They both use the same SPI protocol to appear as a slave flash storage device to the programmer.

The miniPOV has a DASA programmer built into it, which means it hooks up to a serial port, and then in software on the computer you bit-bang out the SPI protocol to talk to the AVR on the POV board.  Retargeting this programmer for an ATmega would be relatively easy: Connect the correct six pins for SPI, and change the Makefile that comes with the bootloader source to use DASA instead of the STK500 programmer that it's expecting.

Hardware


Figuring out the correct pinout is simply a matter of reading the datasheet for the ATtiny2313 and the ATmega168, and jumper the six pins needed to program it.  It's six pins because, beyond the three needed for the SPI protocol itself, you also need a forth line for slave select, since SPI has no addressing system like I2C, and two more lines for power and ground.  For the following list, connect the first number's pin from the IC socket on the miniPOV to the second number's pin on the ATmega, so the 20th pin on the 2313 is power, where it is pin 7 on the 168.
  • VCC 20 → 7
  • GND 10 → 22
  • SCL (Serial Clock) 19 → 19
  • MISO (Master In Slave Out) 18 → 18
  • MOSI (Master Out Slave In) 17 → 17
  • RESET (Which acts as the Slave Select pin for accessing the flash) 1 → 1
 You're also going to need a 16MHz crystal on pins 9 & 10.  Without it, once it finishes burning the fuses to not use the internal oscillator, the chip will stop responding, which is unfortunate.

Software

The majority of the software battle is just getting the environment setup (AVRDUDE, GCC, Make, etc).  If I remember correctly, this was relatively simple, following the directions on the miniPOV site.

The source for the bootloader is shipped with the Arduino environment under arduino\hardware\bootloaders\atmega.  Open the Makefile in a text editor, and change the following lines (which are about 20 lines into the file).

# enter the parameters for the avrdude isp tool
ISPTOOL       = dasa
ISPPORT       = com6
ISPSPEED   =
DASA is the type of programmer which we are using (Serial bit-bang), and com6 is the serial port that my USB → Serial adapter happened to show up as.  You'll need to figure out which com port you're using and change accordingly.  I didn't know what to put for the speed, so I left it blank, and it defaulted to something functional, if painfully slow.

Once everything is hooked up, you're ready to play with fire.  Open a command prompt in the atmega bootloader folder, type make diecimila_isp, and sit back and enjoy the next TWO HOURS (!!!) as it ever so carefully writes the 16kBs of flash on the ATmega.

But hey, two hours is two hours I would have wasted otherwise doing something silly like try and hang out with real life people or do homework, and you only need to do this once before you can start reprogramming it just using the Arduino board, so in the end, it's not that bad.

In the end, I now have a second Arduino for the cost of an ATmega 168 ($4.32), a crystal (50 cents), a few caps, and whatever other parts I need for the current project, without having to buy a real AVR programmer.  Happy hacking.

Sunday, January 17, 2010

Three Transistor Ring Oscillator

I don't remember how it came up tonight, but somehow I ended up looking at the pendant Drew built back in October out of UV LEDs and a uranium glass marble.  It's a very cool project, and I urge you to go check out his photos and video of it in action (and schematic!).

The electronics in it form what is called a ring oscillator.  A ring oscillator is an odd number of inverters connected in a circle.  Each inverter outputs the opposite of its input, but since there is an odd number, it's never stable, and will oscillate.  Its frequency depends on how much you slow down the signal by using resistors and capacitors.  If there was an even number of inverters, note that it would be stable, which isn't useful for an oscillator, but can be used to store information, and is known as SRAM.

Looking at Drew's schematic, I realized that with a little digging, I probably had all the parts to build the same oscillator, though with only three stages instead of seven.  I also don't have any uranium marbles or UV LEDs sitting around, so we'll have to settle with the significantly less awesome bright red.  I've tried to build this circuit before using a 7404 inverter instead of discrete components, but didn't quite grasp the concept at the time enough to get it to work.

The keen eye will note that I used IRF630 MOSFETs, instead of the 2N7000s he did. This is because I tried to use the last of my 2N7000s to build it, but managed to smoke them in the process (first time using 7000s; turns out the pinout wasn't quite what I expected.). A keener reader will note that IRF630s are rated for 9 amps, which means using them to switch 15mA LEDs is some kind of silly.  They were all I had in my junk box; don't mock me.

And yes, my hand drawn schematics are unfortunate, but I'm still not happy with any of the CAD programs I've been experimenting with, so it's either this or nothing tonight.

Video

Monday, January 11, 2010

Design Project: Zombie Tag Repeller

During my daily browse-the-internet-because-I'm-bored session a few days ago, a question came up in the electronics subredditBovingdonBug is trying to build a "zombie repeller" for his (?) kids.  This question caught my eye for a few reasons: He didn't tell us to solve his problem in broken English, it's an actually reasonable first project in electronics, and he posted a beautiful info-graphic with it.  Click to enlarge:

Since he is building five or six of these, he asked to keep the budget in control.  Considering that I've been pounding electrical nails in with the sledgehammer that is the Arduino all quarter, I figured some down-to-the-metal digital logic would be a good change of pace.

Changing the design for the different feature sets (random length cycles vs fixed, single lit light vs flashers) is pretty trivial, so I'll give you a solution for each one.  There is certainly other ways to do each step, and the component values I'm giving you are just what I stuck in my breadboard that worked.  I know that they could at least be optimized to improve battery life, at the cost of wave symmetry, which I find unacceptable, but you may be less hellbent on symmetry1.


Power Supply

Many of these stages only use a 555 timer IC, which can take up to ~15 volts from the supply.  If this ends up being the only IC you use, you could freakin hook this thing up to a car battery (with the right LED resistor values).  In any case, my personal choice would be to use three or probably four AA batteries in a battery holder.  With NiMH batteries, this will get you 4.8 volts, which is perfect for TTL.

Parts list:
4 AA battery holder with leads (digikey)


Lights

For all of the different timing circuits we're about to build, they do little to no good if they don't make anything light up.  This being the 21st century and all, the popular thing to make light up in these type of projects are light emitting diodes (LEDs).  A normal diode is a device which only allows current to flow in one direction.  A light emitting diode behaves exactly the same, but when current is flowing, it creates light.  There is ways to tell which way is conducting in an LED2, but it's usually pretty easy to just try it one way, then reverse it if it doesn't work.

The problem is that diodes have the advantage of letting as much current flow as possible.  If you look closely at an LED, you can see the small gold wire connecting the two leads, and it's really freaking small.  If you aren't careful, you can easily push enough current through an LED to melt that wire, and burn the epoxy case, rendering the LED less light emitting, and more useless.  The question then becomes, "How to prevent too much current from flowing through an LED?"

Current Limiting Resistors: The cheapest solution is to use a current limiting resistor.  A current limiting resistor is exactly like any other kind of resistor, but happens to be used to limit current, as opposed to other possible applications for a resistor, such as a voltage divider, or heating element, or pull-up or pull-down resistor in digital logic.  To calculate how big your current limiting resistor needs to be requires a few pieces of information.  You need to know the voltage drop across the LED, the supply voltage, and the amount of current you want to have flowing through the LED when it's turned on.  Typical values for a red LED are a 1.8V drop across the diode, and a 20mA rating.

The equation you use is Ohm's law.  Ohm's law states that the voltage across a resistor is the product of the current through the resistor and it's resistance (V=IR).  Let us say that you want to calculate what size of resistor to use for a typical red LED from a 5V supply, which is a very standard voltage, and close to what we'll get from 4 NiMH batteries.  Solving for the needed resistance, we get R = V/I, but what is the voltage across the resistor?  The power supply voltage minus the LED voltage drop, so 5V - 1.8V = 3.2V.  We want to have 20mA flowing through the LED, so plugging those into Ohm's law gives us R = 3.2V / 0.020A = 160Ω.  Notice that this is nothing more than a guide as to what value to use.  Feel free to push your luck and use a smaller value (not recommended), or be conservative and use a larger value resistor (I typically use 330Ω resistors for my indicator lights).  You also will usually stand a very poor chance of getting the size resistor you calculate.  Engineers have very carefully selected 24 typical values for each order of magnitude, so although you may not be able to get your exact value, you can get something very close to it.

Since these lights are going to be outside, I would think you'd want them to be as bright as possible without risking damaging them.  I would also probably install several of them per box, using a separate resistor for each one.  If you do have several LEDs per circuit, make sure to check that the output current rating of whatever is driving them can handle it.  555s are usually rated for something like 150mA output, so you could easily drive 4-5 LEDs per 555.  7400 logic gates, which we'll get to later, only put out 25mA, so driving LEDs directly from those wouldn't be as great an idea.

Parts list:
High intensity red LEDs
A current limiting resistor to be put in series with each LED.


Version BX - LED lights for a fixed length of time

For this version, we are going to have the zombie repeller turn on for a fixed amount of time, and then turn off for a fixed amount of time.  When it's on, it will turn the lights on solid, then turn them off at the end of the cycle.  To do this, we're going to be using the classic 555 timer IC, which is a chip that times how long it takes to charge or discharge a capacitor through a set of resistors.  The 555 can be configured to do this in several different ways, but the form that we're interested in is the "astable mode."  In this mode, the 555 continually alternates between a HIGH and LOW output, from now until the cows come home.
Photo credit: Wikipedia

This is perfect for our repellers, since we want them to continually turn on and off throughout the game.  Now all we need to do is calculate the values needed for the capacitor and two resistors that set the frequency.  Since we're shooting for something like a 2 minute period, a good starting point would be a big electrolytic capacitor on the order of 1000μF.  You then plug this, and your target frequency (0.00833Hz = 1/120 seconds) into the frequency formula for the 555, and solve for the needed resistance:

The result you should get is that R1 + 2R2 = 172.8kΩ.  If you didn't get this, make sure you used the capacitors value in Farads (0.001F, not 1000μF, "micro-farads").  Now you just need to pick resistors to get a resistance kind of close to this calculated value.

When picking these values, realize that the HIGH time is a function of R1 + R2, while the LOW time is just a function of R2.  This means that if you pick equal values for both R1 and R2, the repeller will be on for 2/3 of the time (which may not be a bad thing, depending on what you want).  At the same time, both resistors must be at least 1kΩ, or else you can damage the 555.  To get as symmetrical of an output as possible (HIGH and LOW times both about the same length), I picked 1kΩ for R1, and then something a few orders of magnitude larger than that for R2.  Digging through my parts box, the closest I managed to come up with was a 56kΩ resistor for R2, which means that the HIGH time is less than 2% longer than the LOW time, which isn't noticeable (or even a concern, if you don't care).  This is smaller than the calculated value of 85kΩ, but that just means it'll turn on and off a little faster than 2 minutes.

Now that we have an output that turns on and off about once a minute, all we need to do is connect it to some LEDs.  I already explained how to calculate what size of resistor you need, so take that, and hook it up as 555 output - LED - resistor - ground or 555 - resistor - LED - ground.  Feel free to connect several LEDs to the output of the 555, since it can deliver quite a bit of current.

Parts list (per box):
1 555 timer IC (digikey)
1 large capacitor (100-5000μF) (digikey) (Pay attention to the cap's voltage rating, and note that there is a stripe down the negative side, and it's important to connect that side to ground)
1 1kΩ resistor*
1 large (50k-100k) resistor*
4 LEDs
4 current limiting resistors (~200Ω)

* These values are what I picked.  Like everything else here, these are just suggestions. Feel free to change them as you wish.


Version BY - A pair of LEDs flash back and forth for a fixed amount of time

This is going to build upon the BX version, so getting that working first wouldn't be a bad idea.  BX just turned on the LEDs directly.  Instead of connecting the output of the 555 to LEDs, consider connecting it to a second 555, tuned for a much higher frequency (2Hz), which is connected to LEDs.  For ~2Hz, I used a 10μF capacitor, and a 1k and 56k resistor.

To switch the second 555 with the first, connect the output of the first to the "RESET" pin on the second.  When the reset pin is grounded, the output will be LOW, and the oscillator will stop.  This means that where we used to have the 555 light an LED, we're now having it turn on a second 555, which turns on and off an LED much faster.

Finally, we want a second set of LEDs, possibly of a different color, to light up alternating with the first set.  To do this, we are going to take advantage of one of the properties of the 555 output, that it can both "source" and "sink" current.  Sourcing current is what we have been doing so far; the output goes HIGH, current flows out, through the LEDs, to ground.  Sinking current is a little harder to understand, but is just the opposite.  When the output on the 555 is LOW, current can flow back into it from another LED attached to a higher voltage than ground.  Connect both sets of LEDs, and one will light when the output is HIGH, and the other when it's LOW (See diagram on the right).

The problem you will soon notice is that, since the output is held LOW when the RESET pin is low, this second set of LEDs will stay lit when the device isn't active if you connect them directly to the battery.  Where could you connect the positive side of the "sink" LEDs so they only turn on when the device is active? (Answer: note 3)

Parts list (in addition to previous lists):
A second 555 timer (or a 556, which is two 555 cores in one chip) (556 on Digikey)
1 small electrolytic capacitor (10μF)
1 1k resistor
1 56k resistor
A second set of LEDs and current limiting resistors



Going further

Unfortunately, I've restarted class, so I have much less time to work on projects on this site.  I've built a random circuit, and taken some videos of it, but explaining it would take much longer.  It uses three digital logic gates, but time dictates I say little more about these more complicated versions of the repellers.

Logic gates used:
7404 Inverter (Digikey)
7486 Exclusive OR (Digikey)
74164 8 bit shift register (Digikey)
Note that I tested it with N family TTL gates, so using HC gates is untested, but should work.




I'm sorry I can't fully explain these as of now, but hopefully this all gives you some good ideas.


1. The classic solution to making a symmetric waveform is to use the 555s to drive a JK flip-flop, which means the frequency is half of what it was, but is *perfectly* symmetric.  I don't think this is particularly important for this application.
2. The side that needs to be connected to the positive voltage will usually have a longer lead when new, and the clear epoxy case will have a flat spot on the ground side.
3. Connect the positive side of the sink LEDs to the output of the first 555, since it can source as much current as the second one can sink.

References: TTL Cookbook by Don Lancaster.  Going into this book with a good fundamental understanding of electronics, this should be all you need to have come up with my solutions. Not only did I learn everything about the 555 from this book, but all of the digital logic blocks came from this.

Wednesday, January 6, 2010

Arduino Binary Clock



So far, this has been hands down my favorite clock.  Over the winter break, I meant to build a real case for it and turn it into something permanent, but like so many other things, I just couldn't keep it above the water mark that is utter exhaustion from last quarter and only three weeks of break.

Binary clocks are a classic nerd cred device.  Instead of displaying the time on a circle, or with numbers, this clock just lights up LEDs that stand for powers of 2, and you're on your own to figure out what they mean.  The bottom row is 1, then 2, then 4, then 8, 16, and finally 32 for minutes and seconds.  Most commercial binary clocks I've seen use binary coded decimal, where each digit is individually encoded in binary, but I liked the idea of only having three columns of LEDs: hours, minutes, seconds.  In the picture, the clock is reading 8:24:55 (8:8+16:1+2+4+16+32).  The trick is that the top two LEDs are worth 16 and 32, so they're close enough to a quarter and half hour that you can just look at them to get a good idea are far into the hour you are.

The LEDs are lit up by a 16 bit LED driver, which was required because lighting 16 LEDs is too much for the Arduino on its own, current-wise.  Just connect the positive side of the LEDs to some positive voltage, the negative sides to the driver, and then tell the driver which LEDs to light up, using the standard shift register interface.

Parts list:
  • DS1307 clock chip + 32.768kHz 12.5pF crystal (surprise?)
  • 16 bit LED driver (Digikey) Chaining two 8 bit drivers would be just as good, and trivial.
  • 16 LEDs + another one for AM/PM (I just used the pin 13 one on the Arduino board)
  • 2 push buttons for setting the time
Source code.

Monday, January 4, 2010

Arduino Four Digit Clock

After my last 7 segment clock, which only used one digit to progressively flash out the current time, the natural progression was to get a full 4 digit display and build a traditional digital clock.


Being back in the South Bay area meant Halted was within driving distance again, so I've taken several trips there to stock up for the next quarter stuck in Davis. I wasn't looking for anything specific, which is a DANGEROUS mindset to go in there with. You walk around, and you can pick up almost anything and convince yourself that you must be able to find something to do with this; whatever it is... Most of the trips are more Jeff bringing me along as his technical consultant, so it ends up being me wandering around drooling over the stuff I shouldn't buy for 10 minutes, answering his question about biasing photodiodes or something, and then back to drooling again.

Standing in the LED aisle, helping Jeff find 5mm green LEDs or some other specific part like that, when I saw it. An entire bin full of "2x2 digit 9 segment LED display," for the majestic price of NINETY-FIVE FREAKIN CENTS! That, good reader, is what we commonly refer to as less than a dollar. Needless to say, I bought that thing like fat kids buy candy.



Figuring Out the Pinout

Like most things at Halted, this screen came with little to no information. I didn't understand where they got "9 segment" from, since each digit looked to be 10 segments (7 numeral bars, decimal, and 2 for +/-), and with 15 pins on the board, the math didn't work out in any direction. This meant I just needed to start poking it with voltage, and see what happens.

Rule number one of LEDs: Limit the current. It'd be a real drag to get an awesome LED display, come home, and smoke one of the segments by pushing too much current through them. When you're working in a 5 volt environment, like the Arduino, a 330Ω resistor will give you 10mA through a red LED, and 15mA completely shorted out, neither of which stand a chance of doing damage. My test rig for the display was one wire attached to ground, and another attached to +5V through the 330Ω resistor.

Hold one of the wires on one of the pins, drag the other wire across the rest of the pins until something lights up. Mark down the pair of pins and which segment they light up on a piece of paper. Rinse, lather, and repeat.

Eventually, you end up with a drawing of the entire display, with a pair of numbers next to each segment, as to which two pins to use to light up that specific LED, which is useful later.

Results: In the end, it turned out to not be a 4 digit display, but a 3.5 digit display, which means that the top digit is only wired to be a 1. This is fine, because in 12 hour mode, a clock never gets above 12, so the top digit only ever needs to be 1 or blank. The display was also clearly meant to be installed in some piece of lab equipment. It has segments to make a plus or minus sign in front of the 1, and decimals after each digit. Might be useful if I ever want a 4 digit readout for +/-1999.

The pinout was pretty much everything I expected and hoped. All of each segment are connected to one pin (eg all of the middle bars share a pin, etc), and all of each digit share a pin. This means that to display a numeral on a digit, I enable the pin for the whole digit, and all of the pins for each individual segment. This has the problem that if you enable more than one digit at a time, you'll display the same digit on each. This is great if you're going for 666 or something, but less useful when you're displaying something like the time, and want it to work more than 10 times a day1.



So now we've got a three and a half digit display, but we can only display one digit at a time. What a drag, right? Luckily, microcontrollers are fast.

Like, really fast.

Like, so fast, you could light up one digit, then turn it off, light up three other digits in a row, then come back and light it up again, and some schmuck watching wouldn't be able to tell. This is called multiplexing, and is a fairly useful trick for when you have more LEDs to light up than you have pins to do it with. In this case, it means we can control 30 (6 + 8 + 8 + 8) LEDs with only 14 lines, instead of 31.

These four separate digit channels are controlled directly by four pins on the Arduino.  Unfortunately, each segment needs to be run at 20mA, and with 8 segments per digit, this could mean 160mA at a time.  This is much more than what each pin on an ATmega is rated for (40mA), so we need to use transistors to limit the load on the pins themselves.  A simple way to do this is with a current limiting resistor, and an NPN transistor.  The expected power dissipation in the transistor is pretty low, so I used a 2N2222, though you might be able to get away with even just a 2N3904.

Now that the positive end of the LEDs is taken care of, we need to be able to turn on or off each individual segment, to display actual numbers.  This could be done with a set of current limiting resistors and 8 more Arduino pins, but I prefer to use an LED driver.  This lets me use a simple 2-4 wire interface to tell the driver which LEDs to light up, and it handles all of the current limiting, while only using one resistor.  This is nice because it means to change the current only requires changing one LED.  I used an A6278 driver, which I found on Digikey.  I used the chart in the datasheet to select a 1k resistor, since I wanted the output current to be 20mA per channel.  Once I hooked the 8 cathodes for the 7 digit segments and the decimal to the driver, in software I wrote an eight bit map for each digit 0 - 9 based on the wiring.

 
The four transistor / resistor pairs can be seen in front of the display, with the control lines for each segment running off to the left to the 8 bit LED driver.

Now that we have 4 pins controlling which digit is lit up, and an LED driver running the individual segments based on manually calculated eight bit values for each digit, the software is simply a matter of extracting the current time from my old favorite DS1307 clock chip, pick one of the four digits, and display it for some period of time before moving on to display the next digit.  The only extra thing I did was OR the lowest bit of the seconds into the decimal point on the hours digit, so the decimal between the hours and minutes flash to show that the clock is running.

Source code.

1. Ever hear the joke about the broken clock? It isn't broken, it's just right two times a day! Ba dum, tisshhh.

Saturday, January 2, 2010

Switched 120V Outlet Junction Box

After spending 4 months worth of my free time playing with 5V electronics, I figured it'd be a nice change of pace if I started doing stuff with real 120VAC electricity.



For my first project, I figured I'd build something I've needed all year; one of my desk lamps doesn't have a switch (it came out of a dumpster, like so much in my apartment).  Plugging and unplugging it works, but isn't the ideal solution.  Last year, my dad built his mother a junction box with a wall switch in it so that she could turn on and off her lamps without requiring the dexterity needed to rotate the small knobs on the lamps themselves.  He was kind enough to let me pull supplies out of his junk boxes for this project, so I was able to completely do this from stock:

Parts list:
  • 1 spare 3 conductor computer power cable. I cut off the chassis connector on the end and connected the three wires (black - hot, white - neutral, green - ground) to the switch and plug.
  • 1 wall switch.  I found one that has an awesome pilot light with it, but a normal single toggle switch would work.
  • 1 wall socket.
  • 1 double face plate fitting your socket and switch.  Different styles of switches will need different face plates, and there should be a whole selection of them at your hardware store.
  • 1 double junction box.
  • 6 6-32 machine screws to close it all up.
  • 1 or 2 short pieces of heavy gauge wire (14 or 12 should do it).
The first thing to do is to cut the chassis connector off the computer power cable.  Cut back enough of the insulation (and possibly braided shielding) to expose the three conductors.  Assuming you have an American cable, the three wires should be colored black, white, and green.  Before connecting the power cable to the switch or outlet, be sure to feed it through one of the holes in the side of the junction box.  If there isn't any holes in the side of your junction box, that's fine, just take a screw driver and MAKE a hole to feed it through.  There should be about eight perforated places where you can punch out the plastic.
  • The black wire is the hot wire.  This is the one that generally hurts, and should not ever be touched by such conductive things as human fingers, toes, arms, heads, etc.  This wire gets connected to the switch, since we want to switch the hot line, and not the neutral, or else whatever we have plugged into it will still be dangerous, even when this box is switched off.  If you were just wiring up the outlet without a switch, you would connect this wire to the brass screws (yeah, even the screws are color coded, how awesome is that?).
  • The white wire is the neutral.  This one gets connected to the tin screws on the outlet, which are the ones on the left, facing the outlet (the larger slot in 15A circuits, or the T shaped one in 20A circuits (like in your bathroom)).
  • The green wire is ground.  This is different from neutral in that it isn't meant to carry any current.  If it does, something is going very wrong, and is refereed to as a ground fault.  Most modern outlets use GFCI, which is a device that can very quickly sense if there is a mismatch between the in and out current in a circuit and disconnect it.  As excellent example of when this would trip would be when some of the electrical current decides it would be much more awesome to flow through your body to ground, instead of back through such troublesome things as wires.  These are the outlets with the black and red buttons on them.
  • You might also find an unshielded wire in the cable.  This is a ripcord, designed to be used to remove the insulation.  I didn't even know this existed until I had finished cutting off the outer insulation with a razor blade.  Just cut this back to the insulation.
After those three wires are connected, the last thing to do electrically is to jumper between the switch and the hot (brass) side of the outlet.  I incorrectly used green wire to do this.  Luckily, this is "temporary wiring," (since it can be unplugged from the wall) so it doesn't have to be code compliant, just safe. Also, since my switch has a light in it, I needed to connect this to neutral as well, which I did with a second piece of green 14 gauge wire.

Now that we're done with the electrical part, all that's left is to screw it all down into the box, and test it (might want to test it before you screw everything it, depending on how lucky you're feeling tonight).  The main thing to test is that each of the contacts on the cable are connected to the matching hole in the socket, and that the switch turns off the right one.  It would also be prudent to make sure that the red indicator light actually turns off when you throw the switch; not that I would make such a foolish mistake as to assemble this entire project, then consider that 50% isn't very good odds on picking that direction.
 
 
And there it is! A switched outlet, useful for anything that you need to physically remove from power, but don't want to continually unplug.

Things I would possibly change:
  • I didn't think about it, and just grabbed the first computer power cable I found in my junk box, which turned out to be 18AWG, which is a little small.  The extra wire I used was 14 gauge, which is fine, but not perfect.  This entire project should be done in 12AWG wire, so as you're collecting parts for this, keep that in mind.
  • If you look on the side of outlet pairs, you'll notice that they have two screws on each terminal, with a small metal tab between them.  If you break out the tab on the hot side, you can electrically separate the two plugs.  You can then have one always be hot, or use a double switch to have two individually switchable outlets, if one channel isn't enough for what you want to do with it.