GPIO (General Purpose Input/Output) pins on the Raspberry Pi

https://www.raspberrypi.org/documentation/hardware/raspberrypi/gpio/README.md

GPIO pins - also called General Purpose Input Output - are the central interface between the Raspberry Pi to external devices and digital circuits. In addition to simple control, certain pins also perform certain functions such as communication via I2C, UART or SPI.

Overview

This page expands on the technical features of the GPIO pins available on BCM2835 in general. For usage examples, see GPIO usage . When reading this page, reference should be made to the BCM2835 ARM peripherals data sheet, section 6. GPIO pins can be configured as either general-purpose input, general-purpose output, or as one of up to six special alternate settings, the functions of which are pin-dependent. There are three GPIO banks on BCM2835. Each of the three banks has its own VDD input pin. On Raspberry Pi, all GPIO banks are supplied from 3.3V. Connection of a GPIO to a voltage higher than 3.3V will likely destroy the GPIO block within the SoC. A selection of pins from Bank 0 is available on the P1 header on Raspberry Pi.

GPIO pads

The GPIO connections on the BCM2835 package are sometimes referred to in the peripherals data sheet as "pads" — a semiconductor design term meaning 'chip connection to outside world'. The pads are configurable CMOS push-pull output drivers/input buffers. Register-based control settings are available for:

  • Internal pull-up / pull-down enable/disable
  • Output drive strength
  • Input Schmitt-trigger filtering

Power-on states

All GPIO pins revert to general-purpose inputs on power-on reset. The default pull states are also applied, which are detailed in the alternate function table in the ARM peripherals datasheet. Most GPIOs have a default pull applied.

Interrupts

Each GPIO pin, when configured as a general-purpose input, can be configured as an interrupt source to the ARM. Several interrupt generation sources are configurable:

  • Level-sensitive (high/low)
  • Rising/falling edge
  • Asynchronous rising/falling edge

Level interrupts maintain the interrupt status until the level has been cleared by system software (e.g. by servicing the attached peripheral generating the interrupt). The normal rising/falling edge detection has a small amount of synchronisation built into the detection. At the system clock frequency, the pin is sampled with the criteria for generation of an interrupt being a stable transition within a three-cycle window, i.e. a record of '1 0 0' or '0 1 1'. Asynchronous detection bypasses this synchronisation to enable the detection of very narrow events.

Alternative functions

Almost all of the GPIO pins have alternative functions. Peripheral blocks internal to BCM2835 can be selected to appear on one or more of a set of GPIO pins, for example the I2C busses can be configured to at least 3 separate locations. Pad control, such as drive strength or Schmitt filtering, still applies when the pin is configured as an alternate function.

Voltage specifications

The following table gives the various voltage specifications for the GPIO pins, it was extracted from the Compute Module datasheet here.

SymbolParameterConditions  MinTypicalMaxUnit
VILInput Low VoltageVDD IO = 1.8V--0.6V


VDD IO = 2.7V--0.8V


VDD IO = 3.3V--0.9V
VIHInput high voltageaVDD IO = 1.8V1.0--V


VDD IO = 2.7V1.3--V


VDD IO = 3.3V1.6--V
IILInput leakage currentTA = +85◦C--5µA
CINInput capacitance--5-pF
VOLOutput low voltagebVDD IO = 1.8V, IOL = -2mA--0.2V


VDD IO = 2.7V, IOL = -2mA--0.15V


VDD IO = 3.3V, IOL = -2mA--0.14V
VOHOutput high voltagebVDD IO = 1.8V, IOH = 2mA1.6--V


VDD IO = 2.7V, IOH = 2mA2.5--V


VDD IO = 3.3V, IOH = 2mA3.0--V
IOLOutput low currentcVDD IO = 1.8V, VO = 0.4V12--mA


VDD IO = 2.7V, VO = 0.4V17--mA


VDD IO = 3.3V, VO = 0.4V18--mA
IOHOutput high currentcVDD IO = 1.8V, VO = 1.4V10--mA


VDD IO = 2.7V, VO = 2.3V16--mA


VDD IO = 3.3V, VO = 2.3V17--mA
RPUPullup resistor-50-65kΩ
RPDPulldown resistor-50-65kΩ

a Hysteresis enabled
b Default drive strength (8mA)
c Maximum drive strength (16mA)

wiringPi and GPIO connection overview

max. allowed power consumption for GPIO in total is 50 mA and a maximum of 16 mA per pin


DeviceConsumptionNoteswiringPi / GPIO commands for connected devices
1Gyroscope and acceleration sensor GY-521 (MPU6050) @ 3.3V
  • 3.9 mA Gyroscope + Accelerometer + DMP
  • 3.8 mA Gyroscope + Accelerometer
  • 3.7 mA Gyroscope only
  • 0.5 mA Accelerometer only
  • Values from official data sheet
  • Hardware address 0x69 changed by soldered AD0 pin
i2detect -y 1
#gpio i2detect #or using wiringPi
2Inertial Measurement Unit MPU 9250 / GY-250 InvenSense @ 3.3V(question)
  • Values from official data sheet
  • Hardware address default 0x68
i2detect -y 1
#gpio i2detect #or using wiringPi
3Relay JQC-3FF-S-Z @ 5V
  • 70 mA - Power supply per 5V from Freifunk Router, because permissible values of Raspberry Pi exceeded

pip3.7 install RPi.GPIO

Way 1 (preferred)

vim opt/gpio/psu-off.py

#This program is used to dirtily turn off the relay module because  basic statements do NOT work (seems to fail due to some logic level issue with 3.3V from Raspberry Pi and 5V from the module). The modules themselves vary slightly (JQC-3FF-S-Z vs JQC3F-05VDC-C).

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

#TURN ON
GPIO.setup(27, GPIO.OUT)
with open('/var/lib/Repetier-Server/GPIO_27.pinstate', 'w') as the_file:
    the_file.write('1')

vim opt/gpio/psu-on.py

#This program is used to dirtily turn off the relay module because  basic statements do NOT work (seems to fail due to some logic level issue with 3.3V from Raspberry Pi and 5V from the module). The modules themselves vary slightly (JQC-3FF-S-Z vs JQC3F-05VDC-C).

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

#TURN OFF - both commands are required!
GPIO.setup(27, GPIO.OUT)
GPIO.cleanup()
with open('/var/lib/Repetier-Server/GPIO_27.pinstate', 'w') as the_file:
    the_file.write('0')

Use the scripts

python3.7 /opt/gpio/psu-on.py
python3.7 /opt/gpio/psu-off.py

Way 2

gpio export 27 out #switch off PSU
gpio export 27 in #switch on PSU

Way 3

gpio -g mode 27 out #switch off PSU
gpio -g mode 27 in #switch on PSU

Failed

gpio -g mode 27 out
gpio -g mode 27 in
gpio -g write 27 1
gpio -g write 27 0

Status

gpio -g read 27
watch -n0,001 gpio -g read 27
4Filament sensing with KY-040 Rotary Encoder(question)Encoder for filament monitor on extruder and for switchting into manual torque mode

Have a look at

5Relay JQC3F-05VDC-C
  • 70 mA - Power supply per 5V from Freifunk Router, because permissible values of Raspberry Pi exceeded
Relay to switch on/off center LED spot

There's a sepcial abusing python script for this because the relay has a logic level issue with Raspberry Pi (voltage differences) → See https://raspberrypi.stackexchange.com

pip3.7 install RPi.GPIO

vim /opt/gpio/ledspot-on.py


#This program is used to dirtily turn off the relay module because  basic statements do NOT work (seems to fail due to some logic level issue with 3.3V from Raspberry Pi and 5V from the module). The modules themselves vary slightly (JQC-3FF-S-Z vs JQC3F-05VDC-C).

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

#TURN ON
GPIO.setup(22, GPIO.OUT)
with open('/var/lib/Repetier-Server/GPIO_22.pinstate', 'w') as the_file:
    the_file.write('1')

vim /opt/gpio/ledspot-off.py

#This program is used to dirtily turn off the relay module because  basic statements do NOT work (seems to fail due to some logic level issue with 3.3V from Raspberry Pi and 5V from the module). The modules themselves vary slightly (JQC-3FF-S-Z vs JQC3F-05VDC-C).

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

#TURN OFF - both commands are required!
GPIO.setup(22, GPIO.OUT)
GPIO.cleanup()
with open('/var/lib/Repetier-Server/GPIO_22.pinstate', 'w') as the_file:
    the_file.write('0')

Use the scripts

python3.7 /opt/gpio/ledspot-on.py
python3.7 /opt/gpio/ledspot-off.py

Failed

gpio -g mode 22 out
gpio -g mode 22 in
gpio -g write 22 1
gpio -g write 22 0

Status

gpio -g read 22
watch -n0,001 gpio -g read 22

wiringPi GPIO and pintest utility

(info) pintest: this is to check the pins at once

Install wiringPi

apt install wiringpi

Get pintest

Install this tool manually because the the apt repo for wiringPi does not contain the utility "pintest".

#install wiringPi manually because the apt repo does not contain the utility "pintest" ...
cd
#git clone git://git.drogon.net/wiringPi #failed due to offline state
git clone https://github.com/WiringPi/WiringPi.git
cd ~/WiringPi
./build
cd ~/WiringPi/gpio
chmod +x pintest
./pintest

Review the result

PinTest
=======

This is a simple utility to test the GPIO pins on your Raspberry Pi.

NOTE: All GPIO peripherals must be removed to perform this test. This
  includes serial, I2C and SPI connections. You may get incorrect results
  if something is connected and it interferes with the test.

This test can only test the input side of things. It uses the internal
pull-up and pull-down resistors to simulate inputs. It does not test
the output drivers.

You will need to reboot your Pi after this test if you wish to use the
serial port as it will be left in GPIO mode rather than serial mode.

This test only tests the original pins present on the Rev A and B. It
does not test the extra pins on the Revision A2, B2 nor the A+ or B+

Please make sure everything is removed and press the ENTER key to continue,
or Control-C to abort...

          The main 8 GPIO pins  0: 7:  OK
                The 5 SPI pins 10:14:  OK
               The serial pins 15:16:  OK
                  The I2C pins  8: 9:  OK
reboot

Helpful commands

gpio readall
gpio exports #list all exported pin (also lists pins which were not exported with the GPIO tool itself)

Bash commands

(warning) This was not tested yet!

#unexport GPIO pins without external tools or libraries
sudo bash -c "for ((i=0; i<32; i++)); do echo \$i; echo in >/sys/class/gpio/gpio\$i/direction; echo \$i >/sys/class
/gpio/unexport; done"
  • No labels
Write a comment…