LED on solderless breadboard
This is a beginner-level "first project" for people who want to learn how to do hardware hacking. Although this project is targeted to the C.H.I.P. single-board computer, the basic skills it teaches are widely-applicable.
Read the short Hardware Hacking Introduction first.
What To Get
To avoid unnecessary frustration, I suggest purchasing the following:
- Miniature solderless breadboard:
- Jumper wires:
I got a nice assortment through Amazon for $12 (US). It would also be possible to save money by getting 22-gage solid hookup wire, which you will need for future projects, but that adds the time and effort of cutting and stripping. For solderless breadboards, the pre-made jumpers are a pleasure to use.
- LED and 1K Resistor:
The LED is the red component with two wires emerging side-by-side out the bottom. The resistor is the component with the wires coming out either side, and bent to the side. The US penny is for scale (not good for much else).
You can get individual components from DigiKey, but their selection is so huge as to be exhausting. I went with a good assortment of parts through Amazon for $15. It even came with a breadboard! (You can't have too many of those.) And a power regulator, which I might talk about in some future tutorial.
Regarding resistors, they come in different values, measured in units called "ohms". A 1K resistor means its value is 1000 ohms. BUT THERE'S A PROBLEM! They come with color coded stripes on them which indicate their values. But on these tiny resistors, the colored stripes are so thin that even with a magnifier I cannot make out the colors. If you are a much more careful person than I am, you could just keep them all separated by memory, but I suspect most people will eventually get confused which is which. I just went with a digital meter for $13 with which I can directly measure the values of resistors. That particular meter requires a bit of practice to use effectively, partly because it is not "auto-ranging"; you have to find the right setting to get an accurate reading. A more-expensive meter would be easier to use. But you can practice with all the resistors in your assortment until you get the hang of it. Just remember that resistors are not manufactured to exact values. A 1K resistor might measure at 0.98K or maybe 1.03K.
Here's me trying to measure a resistor without my hands being in the way:
But the measurement is not working. With the resistor wires just laying on the test probes, they don't make a good enough connection. To get a good reading, I have to squeeze the wires against the test probes with my fingers. Don't worry, there are no dangerous voltages here.
You don't need a meter for this project, though. If you get an assortment of parts, just peel off a 1K resistor and don't get it confused with the others. :-)
You should already know how to get to a "shell prompt" on your CHIP. If you don't know how to do this, you will have to learn. Ask on the message board.
Breadboard projects tend to be temporary, experimental circuits. The whole point of breadboards is to make it easy to connect components together without the need for soldering. Let's say you want to connect one of the LED wires to one of the resistor wires. You might try holding them together with glue or tape. BUT DON'T!!! Glue or tape will almost certainly not provide the kind of electrical connection that is needed. Instead, you will be plugging them into the breadboard such that the proper wires are electrically connected, but are isolated from other wires.
Take a look at this short tutorial to see how a standard full-size breadboard is connected.
Basically, a row of 5 holes are connected to each other. But each row is isolated from the other rows. This means that it is important to insert components in the correct orientation. Here's the WRONG way:
The LED wires are inserted in the same row, which means they are electrically connected to each other. I can't think of any project where you would want this.
Here's the right way:
Each of the LEDs wires is in its own row.
When inserting a component's wires into a breadboard, don't apply too much force. Gently rock the wire until it slides in with minimal resistance. If you are having trouble, it is better to select a different hole rather than force it. Also, make sure the wires are straight up-and-down. It is OK to bend the wires to get them in right. For example, here's a transistor whose wires are too close together for the breadboard's holes:
Use thin pliers (tweezers might work) to bend the wires to be further apart (spaced the same as the breadboard holes) but parallel:
Note that DigiKey sells transistors that are pre-bent in this way. :-)
A Diode is an electronic component that lets electricity flow in one direction but not the other.
An LED ("Light Emitting Diode") has the same "one-way only" property as a normal diode, but in addition will produce light.
LEDs come in many different physical forms. The most common form for hobbyist use is shown here:
Look very carefully at the base and you will see that one side (closest to the shorter wire) is flattened. You may need to angle it to get a light reflection off of it. That wire (shorter) is called the "cathode" (abbreviated "C"), and should normally be connected to negative power ("-" or "GND"). The longer wire (rounded edge) is called the "anode" (abbreviated "A") and should normally be connected to positive power ("+" or "VCC 3V3"). BUT THE LED SHOULD NOT BE CONNECTED DIRECTLY TO POWER! You will probably burn it out. You need a resistor to cut down the current. More on that later.
Assembling the Circuit
Let's connect a black wire to the LED's cathode (short wire), then connect the LED's anode (long wire) to the 1K resistor, and a red wire to the other side of the resistor (click the pic to zoom):
It is a bit hard to see that, so here is a circuit diagram using standard symbols, and a crude (VERY crude) drawing showing which wires are going into which holes of the breadboard:
Lighting It Up
Let's first try it out on CHIP without the GPIO. Connect the black wire to GND (there are several; I picked pin 39 on U14. Then connect the red wire to VCC-3V3, pin 5 on U13. Go here and scroll down to see the positions of the different pins on CHIP's headers. Here's what it looks like:
Now let's control the LED with software. Move the black wire to XIO-P7, pin 20 on U14. The LED turns off.
Now look at GPIO_Info#GPIO_Line_Numbers to see what GPIONUM is associated with XIO-P7. Did you look? Be careful because the XIO numbers are different between CHIP OS 4.3 and 4.4. I have 4.3 so XIO-P7's GPIONUM is 415. If you are on CHIP OS 4.4, then XIO-P7's GPIONUM is 1023.
In the commands that follow, I assume you are running CHIP OS 4.3. Enter them exactly as shown. Your best bet is to cut-and-paste the text into your terminal window. If you are running CHIP OS 4.4, change every 415 to 1023.
Log into CHIP's shell and enter the following command:
When I did it, I saw this output:
export gpiochip0 gpiochip408 unexport
It's possible you might see a minor difference. Now enter the commands:
sudo sh -c 'echo 415 >/sys/class/gpio/export' ls /sys/class/gpio
If you haven't entered the "sudo" command recently, it will prompt you for your password. Go ahead and enter it. Here's my output:
export gpio415 gpiochip0 gpiochip408 unexport
There's a new directory there named "gpio415". It is associated with the XIO-P7 line that we plugged our black wire into. Enter this:
Here's my output:
active_low device direction edge power subsystem uevent value
Now enter the following commands:
sudo sh -c 'echo out >/sys/class/gpio/gpio415/direction' sudo sh -c 'echo 0 >/sys/class/gpio/gpio415/value'
The LED should turn on! If not, carefully check the header positions that the wires are plugged into. Red should still be in VCC-3V3, and black should be in XIO-P7.
sudo sh -c 'echo 1 >/sys/class/gpio/gpio415/value'
The LED should turn off.
At this point, you can turn it on by echoing a 0 to /sys/class/gpio/gpio415/value, and turn it off by echoing a 1 to it.
while sleep 1; do sudo sh -c 'echo 1 >/sys/class/gpio/gpio415/value'; sleep 1; sudo sh -c 'echo 0 >/sys/class/gpio/gpio415/value'; done
This is an "infinite loop" which blinks the LED on and off forever. Enter "control-C" to stop it.
How It Works
The XIO pins can be programmed individually to be either inputs or outputs. The shell command:
sudo sh -c 'echo out >/sys/class/gpio/gpio415/direction'
programs it as an output.
The output can be either 1 or 0. When it is 1, the XIO line outputs a weak positive voltage. You might think you would want to connect the LED up such that the 1 output voltage turns on the LED, but as it happens, the XIO pin doesn't have enough power. So instead, I use this circuit:
The LED's anode is always connected to positive voltage (through the resistor to limit current). The cathode is on the XIO line. So when the XIO output is 1, there is positive voltage on both sides of the LED, and no current flows. The LED is off. When the XIO output is 0, the cathode is grounded, and current flows from VCC-3V3, through the resistor, through the LED, and into the XIO's ground. This turns on the LED.
I hope you liked this tutorial! If you did, go to the BBS and "like" it. :-) Constructive criticism is welcomed.