Monday, November 30, 2009

Seven Segment LED Arduino Clock

As an early Christmas present this weekend, my dad let me go through his old electronics parts boxes to pick out anything I might find useful. One of the first things to really catch my eye was a seven segment LED display.

These are the type displays you see in most digital clocks, so making a clock out of them isn't particularly ground-breaking. Unfortunately, I only have one digit, so just displaying the full time was out of the option. I've seen videos of single tube nixie clocks online, and appreciate the style of flashing each digit individually (Granted, they're doing it because nixie tubes are #%$! expensive, and not because they just don't feel like spending the $3 for a 4 digit display).

The thing on the left is the module I built for my DS1307 so that it'll always be backed by a battery and never lose the time. Super nice when you're moving it between projects.


I pretty much explain everything in the video, but as anyone who has recorded themselves know, you get 50% less articulate once you press record. I point out that AM/PM is easy, since you only need to divide by 12. This is correct, since I'm running the DS1307 in 24 hour mode, but kind of silly, since the DS1307 has a 12 hour mode. I simply run it in 24 hour as a matter of personal preference, since I'm using the same clock chip over and over in all my projects, some of which really lend themselves to 24 hour mode, and don't find converting between 24 and 12 hour mode that difficult when needed.

I also mention using a shift register LED driver. I haven't mentioned these yet, but I bought a couple LED drivers, which are nice because they let you drive 8 or 16 LEDs, using only one resistor to limit current, and only 2-4 pins on the controller. This means it's trivial to change resistor values to adjust brightness, and that I don't have to worry about my power budget on the controller. (Link to the 8 bit LED driver I bought)

Source code.

Note that I got lazy, and used delay()s everywhere in the display system, so there really isn't any way to set the time on the clock as is, other than plugging it into a computer and using the serial port. Feel free to convert it to being non-blocking, but I didn't feel like putting that much effort into a quickie weekend project like this.

Sunday, November 22, 2009

Arduino RGB LED Mood Light

I wasn't feeling creative today, so I decided to hack together the classic mood light, where you take a Red-Green-Blue LED and have it fade from color to color. I did this back when I originally got the RGB LED, but didn't do a very good job on the algorithm for the colors.

The algorithm I used this time was a little better, but still needs a little work. As it stands now, it picks a new color as follows:
  • Generate a random number between zero and the maximum intensity and assign it to the target value for a random one of the three colors.
  • Generate a random number between zero and the maximum minus the target for the first, and assign this number to the next color's target value.
  • Subtract both of the previous target values from the maximum, and assign this to the third.
This would normally generate red heavy colors, since it would average 50% red, 25% blue, 25% green, but since I have it start randomly between the three, it is still biased away from (33, 33, 33), but that's white, so it's not necessarily a bad thing.

Once the target color has been selected, all that's left is to drift to it from the current color, to create the soothing ebb of color.
  • Is the current red level higher or lower than the target? If so, add or subtract one to get closer.
  • Write out the current state to the PWM pins on the Arduino.
  • Pause for 10ms
  • Move on to blue, then green, pausing another 10ms after each.
  • After moving all three one value up or down, if we're still not at the target color, loop back and repeat.
  • Once the target color is reached, pause for 2 seconds to let the viewer enjoy the new color, then select a new target color and repeat the entire cycle.
Overall, I think it does a decent job, but the colors tends to be more washed out half-whites than I'd really like. The ideal solution would be to build a table of ~20 colors that you know look good on the RGB LED, then drift between those randomly. Unfortunately, that requires picking ~20 orthogonal pretty colors, and just calling random() a couple times is a lot easier when you aren't feeling creative.

My video camera isn't very good at recording the color of a light source, but if you watch the breadboard it's plugged into, you can see what's going on, color wise. (If you're reading this in Facebook, you're gonna need to click through to the original post. Yeah, right there, at the bottom, right above the comments, theeere you go...)


Link to the source code.
The simplest circuit diagram you've ever seen:

Note: Apparently the common-cathode RGB LED (where the LED's all share one cathode) I have is uncommon. Reconfiguring this circuit to operate with a common-anode LED is relatively trivial, since AVR I/O pins can both source and sink current. Just connect the anode to 5V, and subtract the desired PWM value from 255 in the source code.

Sunday, November 15, 2009

How to Replace 62 Railroad Ties in a Day

In case you've missed every one of my blog posts since this quarter started, the theme this year has been fun with electronics. However great this theme is (the correct answer is "very"), I felt like this weekend solicited a change.

Thursday I muscled my way through my last engineering midterm for the quarter, and decided that to celebrate, this weekend I would give my actual major (Mechanical engineering. Who knew, right?) the tender love and care that it so deserves. So now the question becomes, "what to do?"

Luck would have it that this weekend happened to be a track work weekend at the Western Railway Museum, and what's more fun than riding on a piece of maintenance of way rail equipment out into the middle of the Central Valley on the old Sacramento Northern line to work your ass off for 8 hours?

Replacing Railroad Ties

Railroad ties are typically heavy pieces of lumber, which the rails are spiked to, to hold the track at the right width and help distribute the weight of trains onto the ground. Unfortunately, wood has this wonderful habit of decomposing, so ties eventually have to be replaced with new ones, which is what we happened to be doing this weekend.

Replacing ties is normally a lot of work. You're literally trying to remove a large piece of old wood from underneath an obscenely heavy ribbon of steel, shove a new large piece of wood under, and then drive four half inch spikes into each one with a sledgehammer. Lucky for us, the WRM track crew has a nice collection of equipment to make our lives easier.

First pull out all the spikes driven into the old tie. This is usually pretty easy, since the ties we're replacing aren't far from being something you'd call tanbark, and you use a claw bar, which is a 5 foot long crowbar. Our track is so bad, several of the spikes have rusted almost completely through as well, making this job even easier.

Next comes pulling out the old tie and inserting the new. We have a machine creatively called a tie inserter, which does pretty much what the name implies. It yanks out the old tie, we shovel out whats left of it, manually line up the new tie, and then the tie inserter comes back and shoves the new one in. In this video, you can see the inserter inserting new ties, while the rest of the guys line up the next few in front of it.

Once the new tie is in, it's time to reassemble the track on top of it. Tie plates, which are flat pieces of steel about 8 inches on a side that the rail sits on so as to not crush the wood underneath, need to be placed underneath the rail. If you're lucky, they'll slide in. If not, you need another guy to pry the rail up with a claw bar, and hope like hell he doesn't slip with your fingers in an unfortunate position.

Now you're almost ready to start driving spikes, but before you do, you want to be sure the new ties are stable in the ballast. Ballast consists of large, loose rock, so you need to compress it underneath the new ties. Quite a big job, usually, but not as bad when you can just use a ballast tamper instead. These things are a sight to be seen. Ours has four giant vibrating claws that plunge into the rock and then squeeze it underneath the tie to form a stable support for the rail. Ahead of the tamper, we shovel extra gravel onto the ties, and in the video you can see him dropping spikes out of the cab to be driven later.

Once the ballast is compacted, all that's left is driving the spikes to hold the track in place. This is the hardest part of the entire project, even with our power tools.

The spikes need to be started into the ties manually, which for 62 ties x 4 spikes per tie gets pretty tiring. I'll be the first to admit I couldn't keep up with the other guys on this, so I opted for assisting driving them the rest of the way with the spike driver once I got tired. The spike driver is a cantilevered jackhammer so that it's easy to move from spike to spike to drive them the rest of the way in. The hammer and compressor are on a track which lets the entire assembly move from side to side, which requires a second operator to guide it from the back.

Note that one of the plate bolts broke at the end of this video, but luckily we were able to limp through the rest of the ties. We did discover that the compressor is metric when we needed to open one of the panels to get it started after it backfired later. I'll let you guess if we happened to have a metric set of sockets out in the middle of nowhere. We had the museum send out a metric tool box on the next trolley, which dropped it off at our red flag, so we only had to haul it 100 yards, instead of 3 miles.

Once the spikes are driven, you're done. We worked a solid seven hours on Saturday, and managed to install all 62 ties we had left. This brought the total for the year to 425, which passes our goal of 400. You'll notice in all my videos that we only replaced every other tie in the bad places, so we've still got plenty of work for next season.

Tuesday, November 10, 2009

DS1307 Module

I've been doing a few projects lately using the DS1307 IC. If you don't already know, the DS1307 is a real time clock chip (as opposed to an unreal time clock chip, which is often used by musicians and people attending events on Facebook). This means that you just have to attach a quartz watch crystal to it, and it'll keep time for you. RTC chips are divided into two categories; those that keep time by counting seconds (usually as a 32 bit number) since some specific start point, and those that count seconds, minutes, hours, days, months, and years.

The DS1307 is of the latter type. This means that it stores the time as seconds : minutes : hours (12 or 24 hour) : day of the week : day of the month : month : year. It also has some smarts so it knows how many days are in each month, and even knows about leap years out to 2100.

This is great, and super useful, but every time I need to pull the chip out of the breadboard to move it, or even just accidentally remove power, it loses the current date and time. Since most of my projects aren't serious enough to warrant wiring up buttons and coding a way to reset the clock, I have to instead plug my Arduino into my laptop with a USB cable, reflash a DS1307 clock setting program I threw together, send it a 13 digit number over the serial port, reflash the firmware for whatever project I'm currently working on, and move forward. Can you see how this would get just a little annoying after the twentieth time? I also have been using the SRAM on the clock to store data, and losing that isn't catastrophic, but annoying as well.

Lucky me, the DS1307 has a neat little feature, where it has an extra pin that you can connect to a Lithium 3V battery, or tie to ground if you're not using it. Sparkfun has a board that has a surface mount DS1307 on top, and a battery clip underneath, which would be useful, but at $20, is a little rich for an inconvenience. I then saw an idea where you do the same thing, just with the DIP package, and all on one side of a piece of perf board.

Parts list
  • Perf board - 5 x ~17 (I used a leftover piece from Radio Shack, but any would do)
  • 5x right angle male header. (sourced locally at Halted, 14 cents) This is so I can turn the board on end and plug the board into my breadboards when I'm done, which is the only place I plan on using this.
  • 1x 8P3 DIP socket (sourced locally, ~30 cents), because soldering ICs hurt my soul.
  • 1x DS1307 RTC (Digikey, $3.74)
  • 1x 12.5pF 32.768kHz crystal (Digikey, 32 cents)
  • 1x CR2032 battery clip (Digikey, 96 cents)
  • 1x CR2032 cell (Digikey, 40 cents, USPS won't ship | Locally, $4.50, I hate you RiteAid)
  • Some wire, solder, soldering iron, etc.
Wiring it up
The DS1307 is nice enough to have 4 of the 5 IO pins on one side (5 - SDA, 6 - SCL, 7 - SQW, 8 - VCC). The fifth pin is for the ground connection, which you need to jump from the other side of the IC with some wire. The battery clip needs to be connected between VBAT and GND, so that'll take at least one piece of wire from the far end. I soldered the crystal straight to the perf board and DIP socket. You should end up with something that looks like this:

Plug in the DS1307 and a CR2032, and you're ready to set the time once, and use until DST.

Results
It works quite well when it's plugged into a project, and I power down everything else. It's super nice not having to try and keep this powered while I'm mucking around with all the other wiring on a board.

I have had a problem when it's just sitting loose. It seems that when VCC isn't pulled to ground, the chip only keeps time half as fast as reality, which makes it less real time, which personally, I find quite offensive. For the second revision, I would probably add a resistor, or even just a bypass cap (0.1μF) between the VCC and GND lines to improve this. While plugged into a project, the rest of the project keeps the VCC line very much at ground, so it's not a very big issue as far as I see it.

Considering that I managed to put this together for easily half of what the Sparkfun board costs, and that I got to break out the soldering iron, which is always welcome, I'd call this project a success.