Search This Blog

Sunday, January 1, 2017

My 1st Hack with Raspberry Pi!

Day 1 (or how I managed to break stuff)

Tree before hacking
A couple of weeks before Christmas, I discovered some cute little bug sculptures at a craft show, created from the recycled insides of various electronics. It was love at first site and so I bought a bug for each of my colleagues, and for myself, I purchased a circuitboard Christmas tree with blinking LEDs-- the perfect holiday decoration for a tech geek like myself.

A couple of days before my district's winter break, I tweeted out a picture of the blinking tree, thinking my techy friends would also find joy in such a geeky little gift to myself. One of those friends responded back with kudos, thinking I had made the tree myself. "Unfortunately I can't take credit for this cool gadget..." I replied, and then I started to wonder, could I maybe hack my new toy?

With my Raspberry Pi close at hand, and grandiose thoughts of programming my own lighting effects, I asked my office mates for their thoughts. (None of us know all that much about electronics, and although I'm getting better and better at programming my RPi, up to this point, I'd definitely never hacked anything from scratch.) That being said, the next thing I knew, one of my TOSA teammates was flipping through some books she had on electronics, the other was texting her engineering husband for advice, and I was digging around online looking for projects that might give us some insight into how we might hack the tree.


The hacking was officially underway!


Step one: Analyze, and get familiar with, all of the pieces of the tree
  • LEDs.... got it. Feeling pretty good about those.
  • Resistors... yup, we know what those are, too. Sweet.
  • 9V... right now this guy is powering the tree. 
  • Cylindrical blue thingies with writing on them that kind of look like larger resistors... might have to look into what those are. (Turns out those are electrolytic capictors.)
  • Other black thingies popping out at us... no idea on those. Will have to figure that out, too. (BTW, those are transistors.)
Step two: Snip the wires connected to the battery pack
Broken ground wire...boo.
We figured that if we were going to do anything at all, we first needed to remove the 9V battery pack. We no longer wanted the LEDs on the tree to be controlled simply by that battery. Our goal was to eventually get this guy connected to my Raspberry Pi for power and programming.

Once I had the wires cut, my next plan was to strip those wires and then extend them using some jumper cables I had in my toolkit. Unfortunately, in the process of stripping the wires (with scissors since I did not have wire strippers handy), I also managed to twist the wires right out of the soldering on the back of the tree (bummer).

Step three: Order soldering iron on Amazon (sigh.)

Interlude

Soldering kit arrived! Set up & plugged in then ran out of time to get to work before department holiday party. Did get a chance to watch Carrie Anne Philbin's intro to soldering video, though. Feeling ready!


Day 2 (learning that I had more to learn)

So after mentioning to my dad that I'd started this hack, his interest was piqued and he asked if I wanted to bring my new soldering kit over for a lesson. Before retiring, my dad worked at Applied Materials for 20-some years and used to do this kind of electronics work professionally, so yeah, if he was interested in giving me a lesson, I was in! 

First up-- removing the old soldering work from the +/- connections on the circuit board. We got the soldering kit set up on my parents' dining room table and, after my dad gave me a mini-lesson in using the "solder sucker", we successfully cleaned out the broken wires from the +/- connections. Then I took a couple of my extra jumper cables, snipped off the female headers, stripped the wires and soldered them into the circuit board-- a task that was a lot less painless than I originally expected it to be!



LED fail
Next up was figuring out whether I could now indeed control the LEDs by simply plugging the newly attached jumper cables into Ground & a GPIO pin on my Raspberry Pi. I hooked everything up, powered up, and connected the jumper cables on the tree to the 3.3V & GND pins and... nothing. I hooked up to a GPIO pin and tried a "blink" command and... nothing. I moved to a 5V pin and nothing. My dad, who definitely has more insight than I do into how these things work, pointed out that the tree originally was powered by a 9V battery. A 3 volt pin isn't going to provide enough power for all those LEDs. 

Ah, yes...

So I had to revise my original plan a bit. I did some more Googling and, lucky for me, plenty of others have blogged about their experiences with similar projects (hacking Christmas lights or 9V battery powered electronics). The missing link-- a relay board. My tree will still need to be powered by the 9V, but the relay board will act as a switch that will give me some control over the LEDs. And so, it was back to Amazon I went (those guys are making so much money off of me since I got my first Raspberry Pi!) to order my Waveshare Power Relay Board.


Day 3 (setting up the relay board)


I received my Waveshare relay board just before Christmas. There are a lot of relay board options available for the Raspberry Pi, but what I like about this model is the easy hookup; the Waveshare 3-Channel is a hat easily connected directly onto the GPIO pins of my RPi3. The hat also comes with extender pins already soldered onto the board, so I can still access the GPIO pins while the hat is on the RPi. LEDs on the relay board are another nice feature and provide visual confirmation that a channel has been turned on.

Although plugging in the relay hat itself was easy as, well, pie, installing the needed libraries for the relay board was not so intuitive for a beginner like me. The Waveshare wiki prompted a lot of downloading, copying and upzipping of files via the terminal, which I'm not super comfortable working in just yet. I had to do a little extra Googling to find some help and finally, almost 3 hours later (ugh), libraries were installed properly and my first relay board test was a success!

(Click here to view instructions on setting up the Waveshare relay board, rewritten by me for fellow noobs.)

Day 4 (officially a hacker)

Back at my parents' house for a second evening of hacking with dad, I unpacked all of my materials in my dad's shop for another round of play time.
Completing the circuit
  • The bad news-- I discovered that one of the jumper cables had come unsoldered from my tree. 
  • The good news-- I realized that I needed to remove that cable anyway to get the 9V back in the mix in order to power the tree.
So, next steps were to get the soldering kit out again, clear out the positive connection on the tree with the solder sucker, connect one end of a 9V battery snap to a jumper cable (we just stripped the wires, wrapped them together and sealed with painters tape), and solder the other end of the battery snap into the positive connection on the tree. The lengthened cable on the battery snap I put into the channel 1 "normally closed" slot on the relay and the ground cable we soldered onto the tree went into the channel 1 "common" slot on the relay.

Once all of the wiring was complete, it was time for a test. I powered up the Raspberry Pi and plugged the 9V battery into the battery snap and... blinking lights! So far, so good!


But now... 
I wanted to write a program that would control the LEDs!

(Side note-- while I was programming, my dad decided that my tree needed a tree stand, so he grabbed a piece of scrap wood in his shop and very quickly created a makeshift stand on his bandsaw. You'll see it in pictures below. The perfectionist woodworker that he is, he told me not to tell anyone because he said the work was sloppy, but I thought it was a cool addition to our hacking project anyway, and I wanted to give him credit. Sorry Dad!)

  • First, I moved the jumper cable from "normally closed" on the relay board into the "normally open" slot (I wanted to interrupt the circuit so that the lights would remain off until the board was programmed to close the circuit letting electricity flow back to the tree)
  • Then, I opened the sample Python program that I downloaded from the Waveshare wiki and cut and pasted the few lines of code that I needed to be able to control the channel that my jumper cables were hooked up to
  • I edited the sleep timing between "high" and "low" commands to control how long the blinky lights stayed on and then went off
 

The results? 

Success! It took a while, but my first hacking project is complete! Nothing fancy, but I'm pretty proud of my work. With this project I managed to learn more about circuits, what transistors and capacitors are, how to solder (and desolder) electronics, what a relay board is and how it works, and I was able to manipulate something electronic using just a little bit of Python. It is a pretty empowering feeling being able to tinker with something and use code to control, and change, the way that it operates. Not bad for a couple weeks work!


The code

Demo code on right; I copied/pasted what I needed on left



















My 1st working program on relay board! (I started editing & playing with this afterward)

2 comments:

  1. A really great write up. As a teacher who has used Pi for 3 years and done a bit of tinkering and much googling about electronics, I was right with you on your ups and downs of this project. It was really heartening that you shared the set backs and solutions too. Have fun with your new creation.

    I have done something similar with cheap party lights and a motorised kids toy. Relays make it so much easier. In the UK. we have the Pimoroni Automation HAT or pHAT or the Pi Face digital, which both work well with relays.

    Look forward to hearing more about your Pi hacking in 2017.

    ReplyDelete
    Replies
    1. Thanks, Steve! Glad I could capture those feelings of struggling through while learning by doing. Definitely feeling more confident to do more, now, after this first experience!

      Delete