Sunday, February 6, 2011

Diving into Microcontrollers - My Tiny Charlieplexed Display

Welcome! Thank you HackADay for publishing my project, and thanks for coming and reading :-)

For those of you who are used to reading about my usual exploits in cooking, fixing things, and family, be forewarned this post gets a bit long, technical, and geeky! :-)

I've been interested in electronics since I was a kid, and have always enjoyed experimenting, building, and learning something new along the way. With the growing popularity of microcontrollers in the hobbyist community, it's become easier to get started in what used to seem too complicated to approach in my spare time.

Since microcontrollers are essentially tiny computers in a chip, you can do much more than you would be able to do with a handful of passive electrical components, and making changes to your device usually only requires making changes to the software that runs on the chip. While I can write software on the computer to work with data, there's something more satisfying about being able to write software to control objects in the physical world like sensors, motors, lights, etc.

I've actually been picking away at this project for several months, in the limited time I have available to me as a parent of a 1-year-old. I'm intrigued by how much can be accomplished by some of the tiny chips available. I decided to start learning with something small, the ATTiny85, with a mere 8 pins to work with. I was also intrigued by what I had read about Charlieplexing LEDs. Basically controlling many LEDs with only a few pins. For simplicity, I wanted to avoid any extra hardware/chips for my first project. To see how much I could do with only a few pins, I decided to take the 5 output pins available to me and make a charlieplexed LED display. Technically I could have used 6 pins, but that would mean losing my ability to reprogram the chip easily. By wiring up my LEDs just right, I could control up to 20 LEDs with only 5 pins.

So I have a tiny chip driving 20 LED lights, but I have no source of input since I used up all my pins. I might as well try doing something interesting... random blinking isn't very intellectually stimulating. I decided I would make a small 4 by 5 grid which could run Conways Game of Life, a simple simulation of cellular life invented by a mathematician in 1970, and a simple programming exercise for me to learn to program my microcontroller in C. By turning on a set of random LEDs, you can let the "colony" evolve based on a set of rules, and see how long it lasts. Sometimes the colonies die off quickly, sometimes the right configuration will get into a loop and survive forever. It's a bit abstract, but interesting to watch.

Setting up the wiring was a bit tricky. I wanted 20 LEDs in a 4x5 grid, all aligned the same direction on the board, and had to run 5 wires to the 40 different connection points. I ended up running bus-lines for each wire on the top and bottom of the board. One side of the board would run to the positive side of each LED, and the other side to the negative leads. After some sketching I ended up with my basic concept for wiring the array and it ended up conceptually simpler than I had expected.

Next step was prototyping. I did some simple experiments with 2 or 3 pins and a breadboard to prove out the charlieplexing concept. Only one LED can be lit at a time. To do this, you have to set one side of the LED high on an output, set the other side to ground on an output, and set all other pins on the array to an input mode. Setting the others to inputs prevents current from flowing and thus keeps all the other LEDs dark. This was perhaps one of the more complicated prototypes I've built, since it involved so many jumper wires on the topside of the board, and such complex wiring in a tight space. I could handle it for 20 LEDs, but probably wouldn't have the patience for much more.

Programming was a new challenge in itself. I've written software in a variety of languages and knew enough to start playing around with some low-level C language with the help of other people's sample code. Getting the programming on the chip required some extra hardware. Awhile back I picked up the USBTinyISP AVR Programmer from Adafruit. The kit was easy to build and in the end I had a top-notch programmer for getting software into my chips. I downloaded the WinAVR development environment and got familiar with it, and after much reviewing of datasheets and other people's software posted online, I started to get simple programs running on my ATTiny85. One big drawback was in-system programming. The AVR chips allow you to program the chip while it's plugged into the rest of your circuit. However, due to the wiring of my board, where basically every output pin is connected to every other with little in between... this interfered with the programming data. I could only get programming to work if I pulled the chip and put it in my dedicated programmer. Not a major issue, but a bit of a nuisance. This also meant I wouldn't be able to include a programming header on my final board design.

I actually started developing my Game of Life software while on vacation in northern Minnesota. I had the WinAVR software working on my laptop, had my prototype hardware built and working, and with this vacation I had plenty of pockets of free time where I wasn't responsible for doing anything in particular. I developed some routines for lighting specific LEDs, storing the current grid, calculating the next generation, and did a little "software PWM" to make it look like the LEDs were fading in and out. I'm sure there are dozens of ways my code could be improved/optimized, but as a first project I'm pretty proud of what came out of it.

As I am always learning, and willing to learn from others, I've posted my code & schematic as an open-source project on GitHub:
https://github.com/benbrandt22/TinyLife5
I just installed Git and just signed up for GitHub, so hopefully I did it right! But my files are there, so that's a good sign.

Detecting when to reset the simulation was an interesting programming exercise as well. The simple ways didn't take much effort. If the next generation has all lights out (dead), then reset. If the next generation matches the current one (steady state) then reset. Detecting a loop was a bit more complex. I didn't want my device to get into a loop and stay there until the power was reset. I didn't have the memory to store an endless record of previous grids to compare to. So, at a certain interval, I saved the grid, and subsequent generations would be compared to that past one. Once the software detected that it's next grid matched one we had seen before, it would know a loop had occurred, and would reset. But how often should I record the grid? How many generations back should I look? I wanted to know how long of a loop I could expect. Since I was only dealing with 20 LEDs, my grid could only display 1,048,575 (2^20) possible combinations of lights, relatively small for a computer to deal with. I wrote some software in VB.NET to run through every possible combination and see how each would play out in the Game of Life simulation. What I found was that about 8.13% of the possible combinations would loop in one way or another, and the longest loop I could expect was 18 generations. Based on this, I knew that if I looked at every 20th generation, I should be guaranteed to catch every possible loop eventually. If my grid were larger I'm sure it would get much harder to analyze every possible configuration, but this worked well for my small setup.

Here's how it looks running on the breadboard:



Now I could have stopped here, but there was more to learn, and after all, I like to make things. I wanted to turn this into a little trinket I could have running on my desk that looked nice & clean. It was time to tighten up the design and put it on a circuit board. Moving my project off the breadboard into a finished product is the perfect way to wrap it up. I haven't done any circuit board designing in about 7 or 8 years since I created my VU-Meter Kit back in 2003. I designed that in ExpressPCB, a proprietary software package, so this was the perfect excuse to teach myself Eagle CAD, with some help from the excellent tutorials at Sparkfun. Once I got rolling, Eagle turned out to be a nice piece of software, and was easy enough to use. Bringing my schematic to a board design was a smooth intuitive process. I arranged my LEDs into a tighter grid, and added some extra circuitry to regulate a 9v power supply to an even 5 volts, and added a reset button. The whole thing fit into a board just 1.95 inches square. Since my board was a charlieplexed LED display running off 5 pins of an ATTiny, I named the board design "TinyChuck5". In the future I can modify my software to do other things besides the Game of Life simulation.

Before committing my design to a board, I ran through some checks to make sure it would work, besides double-checking my traces in Eagle. I printed out my design on paper and taped to a piece of styrofoam, and proceeded to insert the components. This allowed me to make sure everything would physically fit, and that everything lined up properly. I then generated my gerber files for having the board manufactured. As a last check, I viewed the gerber files with ViewPlot, and discovered that my voltage regulator included a drill-hole that I wasn't aware of, which put a hole right through one of my traces. I didn't see this in Eagle, so was thankful I could catch it by reviewing the gerber files. I moved my trace to avoid this hole and the design was finished!

At the time of this writing I have a few boards on order. Once they arrive I can solder some together and make it look nice. I'd like to put my LEDs behind some semi-transparent white plastic. In the end I may have a useless blinking gadget, but at least I can say I created it from scratch, and I have the ability to make it do whatever I want. With the knowledge I've gained in this experience, I can move on to another more elaborate, more interesting project.

Monday, January 17, 2011

Inside Dad's Toy Workshop - Toy Repair and Creation

While my basement workbench has served many purposes from wine making to laptop repair, I periodically find myself at work in the basement on one of our son's toys.

Tonight I set out to repair a music-playing toy shaped like a star. Each point of the star is supposed to play sound or music, but one hasn't worked since we got it. Even though he's only one, he knows that button should work. So, as The Dad, I need to fix it! Not sure if it came from a garage sale or the thrift store. The offending item was a squeezable cloth item, so it could be tricky getting at the switch inside the plush part.

Fortunately this one wasn't too difficult to diagnose. After removing a few screws, I was able to test the working switches with my multimeter. This gave me a basis for comparison to the broken switch. While prodding around the problem showed itself... a broken wire. Oddly enough this seems to be a trend. I fixed a musical toy book some months ago which also had a broken wire. This one I was able to strip and solder back together. In a pinch the strands of CAT5 ethernet cable works well as a small gage wire. Thankfully my wife knows me well, and has brought home other non-working toys from garage sales, knowing that I'll enjoy working on them, and hopefully getting them to work. Lucky for me the last one was only dead due to some battery acid corrosion on one of the battery contacts. A little sanding & scraping and we have a nice electronic drum and Nathan loves it.

Speaking of things Nathan loves... he loves turning light switches on and off, and loudly exclaiming "ON!" when the lights come on. I headed to the hardware store a couple weeks ago with the goal of giving him a way to play with light switches where we don't always have to lift him up. What I ended up finding was a WireMold light switch and enclosure made for mounting to brick walls. It's a small enclosure that holds the light switch and has just enough room inside for a little more. I had considered a normal electrical junction box, but the switch plate sticks out far and this presents a number of sharp edges. Not so kid-friendly. I drilled out a hole in the switch plate for a light. The light was purchased from Radio Shack as a 12 volt automotive indicator light, but I only bought it for the nice plastic lens which snaps easily into a 1/2" hole. I tore out the bulb and wired in a blue LED, a resistor for limiting current, and hooked it up to 2 AAA batteries in a battery enclosure I had in my bin of electronics supplies. Normally I wouldn't call so much attention to something as simple as wiring up an LED to a switch, but the application as a kids toy made it more fun.

With the enclosure snapped together, the electronics soldered up, and the switchplate screwed on tight, it was done! Nathan loves his personal light switch. I have some ideas for a better design if I could custom-manufacture the toy, but for now it works well and he enjoys it, so that's what matters.


Sunday, November 28, 2010

Happy Thanksgiving

Happy Thanksgiving! I'm thankful that I'm still here to celebrate it with my wife & son and everyone else in my life. It's been a good long weekend and we managed to fit in quite a bit. We enjoyed our Thanksgiving meal with Beth's family this year. The food was excellent. Nathan wasn't so sure about some of the food but had a good time, especially playing with his cousins. I ventured out Friday morning for some Black Friday deals like I do most years. It's crazy but I get a kick out of the experience. Friday afternoon I put up our Christmas lights after finally finding the right hardware to hang them up. Most normal hooks won't work with our new leaf-guarded gutters. Saturday morning we made Lefse with Beth's parents like we do each year. However, we decided to do it at our house this time which worked out well for Nathan and his nap schedule. The Lefse turned out great and will be enjoyed through the holidays and the rest of year whenever we decide to pull some out of the freezer. Saturday night we got together with my parents and my sister and her family for some Thanksgiving leftovers and good family time. We wrapped up the weekend with my niece's 5th birthday party on Sunday night. Overall a busy but good weekend. Happy Thanksgiving!

Monday, November 22, 2010

Wine Label Honorable Mention

I'm happy to report my Apple Wine label got an honorable mention in Winemaker Magazine's label design contest. The latest issue of the magazine arrived at the house with the results of this years label contest. The competition was good, and the winners had some fun label designs. They awarded some big prize packages to first, second, and third place. Mine was one of 16 other honorable mention labels. According to the magazine I won a Carboy Cleaner, so I may be receiving a package in the near future. Thank you Winemaker Magazine!

Friday, November 19, 2010

Wheel Cake!

Growing up in a family of artists has instilled a creative drive in me that cannot be ignored. I enjoy opportunities to be creative and make things, and baking is no exception. Growing up it was always fun to have my mom's creative cakes at our birthday parties, and now that I'm a parent I want to do the same for our children. Besides, it gives me a chance to be creative and have more fun than I would have going to buy a cake at the store.

For our son's first birthday party we had to feed 12 adults and 9 kids, so we would need a big cake, or maybe two, and one small cake for the birthday boy would be fun. At one year old, there's not necessarily much that a baby is "in to," but I can say for sure he likes spinning things. Mostly the wheels on his toys. So why not cake(s) shaped like wheels? Seems simple enough. Not quite as complex as his Baptism Cake, but that's OK by me. I'll save more complex designs for the future.

I made two boxes of cake mix, but divided up the batter evenly by weight so I could make three round cakes. Our only round pan had angled sides, so I used a springform pan instead, which worked very well. Bake-Even strips made a big difference too, keeping the cakes fairly level, and not too rounded on top. The cakes are much easier to work with, cut & sculpt, and frost when they've been frozen. So I made my three cakes ahead of time, wrapped them in plastic wrap, and froze them overnight.

Why three cakes? Two would be the base of each wheel. The third would have the center removed, which would become the birthday boy's personal cake. The remaining "donut" shape would be cut in half, and placed on the first two cakes as the raised shape of the tire. The picture at the right shows this in better detail.

Decorating was fairly straightforward. I used white frosting from the store and used black coloring to make different shades of gray. The base cake got a coating of light gray which would separate the layers and give color to the "hubcap" area. The outside of the cake got a dark gray frosting, and at my wife's suggestion I added some blue to the hubcap for a splash of color. A Monochromatic gray cake wouldn't be as fun, right?



With the help of a ziploc bag and cake decorating tip I put lettering on the cakes and we were done! I would have enjoyed adding more details, but my time and energy were limited, as being a parent can sometimes do to you. Still, it was fun and I think it turned out well.

Everyone thought the cake was wheelie good.

Friday, November 12, 2010

Happy Birthday Nathan

My wonderful, happy little son Nathan turned 1 yesterday! The time has flown by. We had all the immediate family over last Sunday for his party (grandparents, aunts/uncles, and all his cousins). It was a packed house with 12 adults and 9 children under the age of 5. Poor Nathan started the day in the Emergency Room at 5am, battling sickness and teething issues for the last couple weeks. At the time the doctors thought he might have pneumonia, but it seems more like a teething issue, but he was prescribed am antibiotic just to be sure. Thankfully he was back to his happy self in time for the birthday party.

Nathan enjoyed playing with all his cousins and relatives, and experiencing all the activity in the house that night for his party. Since Nathan loves anything that spins, especially toy wheels, I made two wheel cakes for everyone, and one little cake just for him. He got his hands good and dirty with his own little birthday cake, but we had to feed him a few bites since he doesn't usually feed himself. Once he got good and messy with his cake (as you should at your first birthday), it was straight to the bath tub! Nathan got some fun gifts from everyone and it was great party.



Yesterday we had our own little celebration. Nathan got some new foam blocks and a couple other small items from us, and a slice of cake with dinner. He definitely enjoyed his cake, as he giggled with each bite we fed him.



Happy Birthday Nathan!

Wednesday, October 20, 2010

Parking, Football, and Sightseeing in New York

Last week I had the opportunity to take a business trip out east to the New Meadowlands football stadium in East Rutherford, New Jersey; home of the New York Jets and New York Giants. This is the stadium's first year of operation, and has recently installed a parking management system from my company. Having worked on supporting this site's parking system, I was sent out with one of our developers and one of our testers to install some updates to the system and to see how well everything operated as nearly 80,000 fans showed up for a football game looking for a place to park.

We arrived on Sunday October 9th and got to work on the handheld computers in the attendant booths throughout the complex, and made some updates to the server as well. We were able to take care of most of the work Sunday, and complete the updates on Monday morning.

With the major work done, we had some preparation for the game and ensuing parking traffic. Double checking things, getting security credentials for the stadium, etc. After a busy morning we broke for lunch, and spent a little time touring the stadium in the daylight, checking out the view of the field from the front row at the 50-yard line.


Happy to say our parking system worked very well overall, which made our evening fairly easy. As thousands of tailgaters flowed in and fired up their grills, we drove around to the various parking entrances and checked on everyone as the evening progressed. I carried a two-way radio to keep in touch with the parking staff if something came up. At one point shortly before game time we ended up stranded out in a downpour, and got pretty soaked until we could make our way to shelter underneath a pedestrian footbridge for awhile. Aside from being wet I was having a great time!

With things running well, our remaining job for the evening was to monitor the system and send out reports every hour or two on how many transactions had gone through. Since this could be done with a laptop and internet access, we brought one with us and headed into the stadium hoping to see some of the game. As luck would have it, the game we went out there for was a MN Vikings game.

Once in the stadium, the rain really came down hard. They wouldn't let anyone into the seats until the rain and lightning let up, and the start of the game was delayed. Our security badges allowed us into the back areas of the stadium, so we could get out of the crowds of fans trying to stay dry. While roaming the halls we saw the entrances to the team locker rooms, and happened to come upon a tunnel down to the field. After chatting with security for awhile, he told us we were welcome to come down and watch the game once the rain delay was over. Until then we had expected we'd have to find some open seats in the upper decks. Sure enough, we came back later and walked right down by the field. It's surprising what wearing a badge and carrying a two-way radio can accomplish. We walked up the ramp to the handicapped seating area, pulled up some folding chairs, and took a seat front row in the end zone. If I were a real football fan I may have appreciated this more, but it was still an amazing experience to be watching a football game in the front row with 80,000 fans cheering around you.




The game was blast. We got rained on a few more times, but could at least duck into the tunnel if it got bad enough. We kept checking on the parking system and things continued to run well as the game started. It was a long day and long night, but overall a great success and a great time. We headed out just before the end of the game to beat the crowd, and turned in at the hotel around midnight, picking up some dinner in the hotel lobby.

Tuesday morning we slept in after a long day, and met with some coworkers to go over the details of the day before. With business & breakfast out of the way, we had a few hours until we had to get to the airport, so we took the rental car and made the most of our time... Manhattan or bust!

I've never been to New York before, so to spend two days just over the border and NOT go into the city would be a shame. We entered Manhattan through the Lincoln tunnel, and took a driving tour to see whatever we could see, including Times Square and the Empire State building.



We headed for Ground Zero and had about 20 minutes we could get out and walk before we'd need to head back. Of course, we had to stop for a hot dog from a New York street vendor, and it was good.



Ground Zero itself was under a lot of construction and was difficult to see from where we were, but there was a small opening in the wall by the subway entrance where I could get some good photos of the area. Pretty amazing how much of the space is still empty a full 9 years later.



After our brief tour of the city, we headed back to Newark airport and back to Minneapolis. All in all it was a great trip.