Time To Blink The Red Light.


So I have hinted to you that the pin numbering of the GPIO confused me, and to be honest, until I learned a few tricks I remained confused.  However, I get ahead of myself.

First I hooked up the GPIO breakout board, it was easy to do, just need to do it gentle, don't want to break any of the pins.  While it is not important as how you connect it, it is important that both sides are hooked up identically.  Simply put, you should have no twist in the cable.

Once it is connected to the breadboard, you can now power your pi up.  I performed this one test to make sure I did hook the cable up correctly.  I hooked up 2 wires, a red to positive, a green to negative on the breadboard.  I then used my multi-meter (You do have one don't you) to test the power coming off of the 3 volt rail as well as the power coming off the 5V rail.

Once I was sure i had proper power it was time to build my first project.  After reading online, it seemed that I hooked power to the long side of the LED, and hooked the other side signal, (GPIO pin).  I was instructed to place a resister in-between.  As I only had a few types, and they were all within specification, I grabbed one of them, a red LED and a few wires.

From the breakout, the first pin I noticed was pin 17, I hooked up a wire between that pin and the short pin of the led.  I then hooked up a red wire to the 3V power rail and hooked it up with a resister between it and the long lead on the LED.

Time for the fun, going to turn that LED on and off!

From the info I had, I had to issue a few simple commands to accomplish this.

  • gpio mode <pin> out, to set the pin to output mode.
  • gpio write <pin> <state 1=on, 0 = off>
So from the cli, I type the first command, it takes it, no error.  From the cli i type the 2nd one, gpio write 17 1, fully expecting the light to go on.  No light goes one.  This is the first time, and not the last time that the board numbering will mess me up.

So while reading up, and examining the docs for gpio, i discover a command called, gpio readall.

Apparently this command will print a list of all the GPI pins, and the current state of the pins.  It also provided a cross reference between the different numbering schemes.  It was here that I first learned that the Breakout Board, pin 17 is in reality WiringPi pin number 0.

So I type the first command again, gpio mode 0 out, once I did this, the red light came on!!!!!!  so figuring the the pin was set to high, and thats why the light was on, i decided to shut it off, so I can turn it on myself.  I type gpio write 0 0, the light stayed on.

I retyped the command, and this time told it to turn it on, well as I assure you guessed, it turned it off.  Seems like my understanding of how this worked was incorrect.

Quickly, i switched the power for ground, i then placed the resister between the LED and the GPIO pin, I reran my tests and found that I was now able to correctly turn on and off the red LED.

Now that I could turn a light on and off from the CLI, it was time to do it programaticly, after all this is where this is all leading to.

No comments:

Post a Comment