Simple i/o for the Raspberry Pi.

Simple Raspberry Pi electronics example.

Screenshot from 2013-08-02 19:14:30.png
Screenshot from 2013-08-02 19:17:06.png
Screenshot from 2013-08-02 19:40:44.png
The Raspberry Pi per se does not have analog ports like the Arduino. though you can get converters that can be attached to the gpio or use an r2r setup. To light an led, you will need to use at least two pins. A gpio and a ground pin. You will also need an led and an appropriate resistor. Once you master these simple steps you can additional electronics and control quite a few things. Remember the gpio only uses very low voltage. Additional safety circuitry is needed for other projects.
# Turn light on
cd /sys/class/gpio
# Turn on pin but defaults to low.
echo 17 > export
# Set port direction in this case we aredoing output.
echo out > gpio17/direction
# Set pin high and turn on led.
echo 1 > gpio17/value

#Turn light off
echo 0 > gpio17/value

================================

Using a button

Controlling the LED with the button

Now let’s put input and output together, and use the state of the button to turn the LED on or off.
The RPi has only one ground pin which you need to connect both the LED circuit and the button circuit to. If you have an M/M wire, you can easily do this by connecting the -column to ground like we did with the LED. If not, it might seem like there isn’t space to have a resistor, a wire connected to ground, and the button all in one group of five columns. But to make extra space, you can place the button bridging the gap in the middle of the breadboard.
One possible configuration has:
  • Wires connecting pin 3 to 7d, 6 to 9j and 11 to 1f.
  • The LED connecting 1g (+) to 2g (-).
  • The resistor connecting 2h to 9h.
  • The button in terminals 7e7f9e and 9f.
With that set up, you can have the LED light up as long as the button is not pressed:
while true; do
    cat gpio0/value > gpio17/value
done
(Use control-C to interrupt this and return to a prompt.) Or as long as the button is pressed:
while true; do
    read val < gpio0/value
    echo $(( ! val )) > gpio17/value
done
Or to toggle the LED every time the button is pressed:
while true; do
    read val < gpio0/value
    if (( val == 0 && last == 1 )); then
        read state < gpio17/value
        echo $(( ! state )) > gpio17/value
    fi
    last=$val
done
(This last one makes use of the fact that if you try to read an output pin, it tells you its current value.)
If that all worked, congratulations! Now you should probably install an actual GPIO library, and turn your mind to more interesting projects.

Comments

Popular posts from this blog

Guiless?

MSOffice vs Libreoffice

Raspberry pi zero.