This articale was originally written for The Best Case Scenario. You can read the original here!
Welcome to the second part of this multi-part miniseries on adding custom electronics into your project. In part 1 we gathered some background info on planning your mod. This part will focus on the electronics hardware and software side of the mod. Read on to find out more...
Getting Started
The simplest way for someone who is new to electronics to get their hands dirty quickly, in my opinion, is with Arduino. Sure there are LOTS of other development platforms out there, but most require knowledge and learning of the systems before you actually start making stuff happen. I started with Arduino (like LOTS of people around the world) because of its ease of use, large libraries, shields (plug-on modules for various input and outputs) and it's HUGE support system. For the background and various info visit www.arduino.cc. There you'll find the free download for the IDE (development environment for your desktop), examples and the forums to ask questions or show off your work! You can also visit the MicroCentral section of our forums and post your questions and ideas there!
For this project we'll be using an Arduino and an Arduino sensor shield. The sensor shield is useful when connecting servos and the like as it gives you 3-pin breakouts with a 5V and ground line in addition to a signal line that's attached to a specific pin on the Arduino. This enables you to connect a servo (or other sensor) without needing lots of jumper wires.

The next batch of parts we will need for this project are a breadboard, some jumper wires, a 10k Ohm resistor, and a normally open momentary pushbutton. A breadboard and jumper wires allow you to connect parts together without the need for solder. This enables you to assemble and disassemble your circuits quickly and easily. For more info on how to use a breadboard check out this Sparkfun tutorial.

The next component is a servo. Servos come in many sizes, strengths and styles. The most common is your standard hobby servo. It has a ~180º rotation capability. The servo we'll be using for this mod is a 9g micro servo that I got from Adafruit for $6.00. I chose the micro servo as it will allow us to mount it between the drive cage and side panel on our case, but more on that in part 3.

Assembly
Here you can see I've got all the components assembled in the breadboard.

This close-up shows how I've got the button and resistor connected. One side of the button is connected to 5V, the other to digital pin 2 on the Arduino. The side going to digital 2 is also connected to ground via a 10k Ohm resistor. This is called a pull-down resistor. Basically what this does is makes sure the signal is always grounded when the button is not pressed. Without this resistor the value can float around between 5v and ground and cause the servo to not stay where we want it. For more on pull-up and pull-down resistors, read here.

Now pictures are all great and everything, but sometimes you want an easier way to show your circuit. Enter Fritzing. Fritzing is free and allows you to show your circuits in a much friendlier way. Fritzing also lets you draw schematics and even export .pdf files for etching your own circuit boards. We won't get into that here, we'll save that for another tutorial. To download Fritzing and learn how to use it, be sure to visit www.fritzing.org.
Here you can see I've drawn up our layout in Fritzing. You can clearly see how it's easier to follow this than to decipher pictures. You can also export this to a graphic to share.

Here is the schematic that was generated based on my parts layout. You can export this to an image to share just like with the graphic layout.

This is one of my favorite parts about Fritzing. You can design your own circuit board based on your circuit or schematic. You can then export this to a .pdf and print it and use it to etch your own PCBs. This is the method I used when I made the PCB for my ICHIWZ project. Here you can see I've gone and made a board that, when assembled, will plug into the Arduino as a shield and allow you to connect the servo and a push button directly to it. Fritzing has the ability to make both single and double-sided boards. It also features an autorouter that you can use to lay down the board traces for you. Keep in mind that doing this may require you to move components around to avoid getting jumper wires, but that's for another tutorial.

Testing
Here you can see I've got the whole assembled system hooked to my Power House mod for testing.

Here is the code I've come up with for this project. It's based off of the State Change Detection example that comes built into the Arduino IDE. I modified it to use a servo rather than the built-in LED on pin 13. I also used the "VarSpeedServo" library I had assembled for me for my Tempest SXR project. You can download the .zip for the VarSpeedServo library (.zip) here. Feel free to copy and paste this code into your Arduino IDE to use yourself on your project. You will need the VarSpeedServo library for this code to work, but if you don't want the variable servo speed, let me know and I'll modify it to work with the Arduino IDE as it comes.
The VarSpeedServo library gives you the ability to slow down the servo. The servo itself will run from 0º to 180º in ~1 second, so this is a little too fast for our door. Instead of "myservo.move(180);" (standard servo code) you use "myservo.slowmove(180, 15);" The first number, 180, is the position you want the servo to go to. The second, 15, is the speed you want it to move at. The lower the number, the slower it will move. This value can be adjusted from 1-255, however no noticeable difference can be seen after about 150. For more in-depth into the coding, feel free to email me or visit the Arduino forums and ask!
Basically what this code does is read the state of the button and count how many times it's been pressed. It starts the servo at position A (closed). Every time the button is pressed it will either move the servo to position A (closed) or position B (open), depending on the counter. As it starts off in the closed position, the first button press will move it open, the second will close it and so-on and so-on.
You'll notice a pair of slash marks a lot in the coding. Putting "//" in any line means that anything after that on that line is a comment, and the code won't read it. It's a good idea to add lots of comments when you're making your code, that way it's easier to troubleshoot. Read through the code, and as you do notice the comments. These help explain what each part of the code does so you can better understand it.
Click here to view a text version of the code. Simply select all, copy it then paste it into your Arduino IDE.
And here is a video of it in action. You can see how every time the button is pressed, the servo will move to either open or closed. This video was recorded before the VarSpeedServo was added, so this shows the servo at it's normal full speed. The code above is the finished code, with all the correct servo positions for the setup in part 3.
Stay tuned for part 3 where we get into the modding end of things! We'll be modding the CM Storm Enforcer case to accept the servo and linkage for the door.
Comments
two servos, different signal pins
hi, i was wondering if it is possible, to amend your code to support a second servo on a different signal pin. my reason for this is that i need one servo to go one direction while another servo goes in the opposite. any help is aprpreciated
Two servos
Hey Cody. I can whip up a different batch of code for you to do two different servos on, that's a piece of cake. I'll email it to you at the address you provided during signup.
Add new comment