31

I've read that the Raspberry Pi has a number of input and output pins.

  • How can I switch them? (from Python?)
  • What voltage logic?
  • Do I need pull-up or pull-down resistors?
  • Do I need to switch the pins from inputs to outputs?
  • Which pins are available?
Peter Mortensen
  • 2,014
  • 2
  • 15
  • 17
Alex L
  • 7,585
  • 12
  • 42
  • 53

3 Answers3

18

The GPIO pins are 3.3 V, and the maximum current is 16 mA. That means you'll be unable to power almost anything directly. That's why you need to at least use a transistor switch, if not a more advanced protection circuit. You can read more about them in RPi Tutorial EGHS:GPIO Protection Circuits.

A general guide is RPi Low-level peripherals.

Peter Mortensen
  • 2,014
  • 2
  • 15
  • 17
Avogado
  • 609
  • 6
  • 9
  • 3
    Could you please expand your answer? I'd like to see all the questions answered, and the information in links provided summarised. At the moment the information about RTC is incorrect (RTC is irrelevant) – Alex L Jun 25 '12 at 08:48
12

These instructions aren't Python-specific, but they might help you get started with experimenting with GPIO. https://raspberrypi.stackexchange.com/a/350/668 has info about a library and usage specific for Python.

When you have booted your Raspberry Pi using the recommended Debian distro, GPIO is disabled. You have to enable each pin individually.

If you're doing it via /sys you will find "Paths in Sysfs" interesting (search within https://www.kernel.org/doc/Documentation/gpio/sysfs.txt). In particular, you would be enabling a pin by "exporting" it. Any commands below assume you are running as root privileges (sudo or otherwise) or you have changed the permissions/ownership of the virtual files being modified.

echo 4 > /sys/class/gpio/export

This enables the GPIO pin #4 which then causes /sys/class/gpio/gpio4 to exist, which contains several virtual files. Those files include "direction" which defines whether it's an input or an output pin, "value" which is either read-only for input or writable for output and contains the current value, and others.

echo out > /sys/class/gpio/gpio4/direction # set it as an output pin
echo 1 > /sys/class/gpio/gpio4/value # set the value to ON
echo 0 > /sys/class/gpio/gpio4/value # set the value to OFF
echo in > /sys/class/gpio/gpio4/direction # set it as input
cat /sys/class/gpio/gpio4/value # get the value
echo 4 > /sys/class/gpio/unexport # disables pin 4 and removes the gpio4 directory

Of course you'll probably prefer to use some preexisting library to do GPIO supplied with or compatible with your language of choice. But if you're wanting something simple, you can just interface directly with sysfs to do very basic GPIO.

Emmaly
  • 331
  • 1
  • 6
0

Import the python library and you can easily control the pins. There are a lot of tutorials online.You need and ide with python supports and basic knowlegde about python programming.

The GPIO pins are available for inputsand outputs.

Sohan Arafat
  • 1,808
  • 1
  • 9
  • 40