Monday, September 1, 2014

Wiring for Raspberry Pi Test Application (Part 3 of 5)

Before I got started I knew I wanted to create an application that would run on my Raspberry Pi that would enable me to open and close my garage door from my iPhone. I wanted to create a test application that would allow me to detect if a button was pressed. The press of the button would mean the garage door was in the up state. I also wanted the program to push the garage door button. At this point I did not know how to do that but I figured if I could turn on an LED that would give me the power I needed to make the garage door open or close. I will discuss all of the details of the garage door application in my last blog post of this series.

The test application I planned on writing was very simple it was going to have a label and two buttons. The label would display a message if the hardware button were pressed. The application would also have some software buttons that could be clicked on and the LED would turn on.

Before getting too deep in the applications functionality let’s start by learning more about how to wire up the hardware components.

For complete disclosure I am not an electrician and electronics is definitely not my strong area. I'm going to take you through the process I went through to get the LED and button working on my Raspberry Pi. Initially when I started this phase of the project I approach the wiring like a hacker. I grabbed an LED and touched the legs to pin 1(3.3v) and pin 6(ground) to see if it would power on.

I noticed other projects had resistors connected up to the LEDs and switches. I was not sure what size resister I needed nor did I know how to read the resistor’s color code. I knew this was an area that I could dig a lot deeper into. To start, I decided to see if I could find some online calculators to help me learn what size resistors to use.

I started by wearing up the LED first. I found an LED Calculator website that worked perfectly.

My first problem is it was asking for values I did not know. I ordered my LEDs through Digi-Key so I went to their website to see if I could find a data sheet. Everything I needed was on the product description webpage.

Here are some screenshots showing the values:

At this point I knew I needed 150 ohm resistors. A quick stop at RadioShack and I was able to pick up the correct resistors for this part of the project.

Now that I felt comfortable with the resistor I was using for the LED portion of this project I decided I wanted to figure out how the switch worked. The first thing I learned is the GPIO on the Raspberry Pi either needs to be hot or grounded. If it's just left without ground or power it can toggle between states and it's very unreliable.

The first thing I ran into was people talking about pull up and pull down resistors. I found this blog post that did a good job explaining the differences between the two and how the circuit below actually works.

Check out keith's blog post here:

Pull Up Example
A basic summary of this diagram shows that the 3.3 V is going through a 10K resistor. The 10K resistor will definitely give us enough power to keep the GPIO pin hot. As long as the switch is not closed none of the current will be directed to ground. As soon as the switch is pressed all the current will pass through the 1K resistor and go to ground. This will leave the GPIO pin void of any current, which will make it in the grounded or off state.

Technically I don't think I needed to add the 1K resistor but a lot of people were talking about its importance. This will restrict the current flowing from the GPIO pin when the switch is closed, in case the pin has been configured as an output. The 1K resistor will allow a maximum of 3.3 V to go to ground.

Lets start soldering! Remember to disconnect power and video before you do any soldering. For my application I will use pin #1 (3.3V), pin #6 (Ground), pin #7 (GPIO-4), and pin #11(GPIO-17)

I am going to start with my LED. I'm going to connect the power side of the LED to pin #11 (GPIO–17). Through code we will make the pin hot to turn on the LED or off to turn the LED off. The longer leg is the side the gets connected to the power or in our case pin #11. The shorter of the two legs will be connected to ground, which is pin #6.

I left the ground wire a little longer because I'm going to need to tap into ground when I'm working on the switch circuit.

Now I'm going to work on my switch.

Here's how I wired up the switch. One side of the switch is hooked up to the ground leg on the LED. The other side is connected to a 1K resistor. The 1K resistor is soldered to a 10K resistor. Where these two resistors are soldered together it is connected to pin #7(GPOI-4). The other side of the 10K resistor is soldered to pin #1 (3.3 Volts)

Okay so at this point we are done soldering everything we need to the Raspberry Pi to begin working on our test application. For the next phase I'm going to connect up a keyboard and mouse to the Raspberry Pi. As soon as the Raspberry Pi starts I will boot into X Windows where I can run the demo application that I will cover in the next post.