Updated 24 September 2020

Kits and Gadgets Electronics Inc.
Farwaniya, Kuwait 80003
Kuwait

ph: +965 99571844

kits@kits-gadgets.com

  • Electronic Kits
  • Parts on Sale
  • Parts in StockClick to open the Parts in Stock menu
    • Resistor
    • Potentiometer
    • Capacitor
    • Trimmer Capacitor
    • Transistor
    • Crystal
    • Diode
    • LED
    • Diac
    • Thyristor
    • IC Socket
    • IC - Logic
    • IC - Linear
    • IC - Op-Amp
    • IC - VLSI
    • Relay
    • Motor
    • Lamp
    • Transducer
    • Gear
    • Switch
    • Heatsink
    • Parts Store Inventory
  • Brand New Items
  • Used Items
  • Power Supply
  • Raspberry Pi VaultClick to open the Raspberry Pi Vault menu
    • Demonstration Kit
    • Demonstration Kit - R3
    • The P1 Header Guide
    • BBC BASIC Tutorials
    • Activating the GPIO
    • LED Program
    • Motor Program
    • Solenoid Program
    • Stepper Motor
    • 8 Relay Program
    • GPIO as Input
    • Motors and Sensors Connection Diagram
    • Autonomous Program
    • Infrared Sensor
    • Ultrasonic Sensor
    • Camera Controller
    • PWM Program
    • Robotic Arm
    • STRYDER Robot
    • Demo Kit Program
  • Arduino VaultClick to open the Arduino Vault menu
    • Stepper Motor w/ LED
    • DCMotor
  • NodeMCU ProjectsClick to open the NodeMCU Projects menu
    • Running 8 LEDs
    • 8 LEDs and ULN2803
    • 8 LED Active Modes
    • ON-OFF Switch
    • Switch, Relay and LEDs
    • Motor Drive Interface
    • Connecting to Wi-Fi
    • IR Sensor
  • More GPIOsClick to open the More GPIOs menu
    • Accessing the P5 GPIOs
  • Prototypes
  • Computer Parts
  • Picture GalleryClick to open the Picture Gallery menu
    • Other Prototypes
    • Revived HP Laptop
    • Dismantled Helicopter
    • Dismantled Phone
    • ACER Laptop Repair
    • Revived Laptop
    • Inside of Electric Drill
    • Video Cam Automation
    • My Tours and Travels
  • My Original DesignClick to open the My Original Design menu
    • Handycam Automation
    • Autonomous Robot
  • What's New!Click to open the What's New! menu
    • Article-1
    • Article-2
    • Article-3
    • Article-4
    • Article-5
    • Article-6
    • Article-7
    • Article-8
    • Article-9
    • Article-10
    • Article-11
    • Article-12
    • Article-13
    • Article-14
    • Article-15
    • Article-16
    • Article-17
    • Article-18
    • Article-19
    • Article-20
    • Article-21
    • Article-22
    • Article-23
    • Article-24
    • Article-25
    • Article-26
    • Article-27
    • Article-28
    • Article-29
    • Article-30
    • Article-31
  • Forum
  • Video GalleryClick to open the Video Gallery menu
    • IR Proximity Sensor
    • Robot Vacuum Cleaner
    • Robotic Arm - RPi
    • Raspberry Pi Demo Kit
  • About Us
  • Contact Us

GPIO as Input

       Any of the GPIOs on Raspberry Pi can be programmed to work as either INPUT or OUTPUT pin depending on how you want to use it. In RPi Model A &  B, there are two GPIO terminals called P1 and P4. The P1 which is a 26 pin header usually called an Expansion  header or terminal has 17 available GPIOs while the 8 pin headerless P4 has 4 GPIOs to make a total of 21 GPIOs for both models.

The only difference between the P1 and P4 GPIOs is the command and declaration needed to access them due to the way the GPIO Module program for is written. 

However, this issue apply only to those using RISCOS BBC BASIC and if  your RPi is the first generation models. Also, there is no difference between P1 and P4 if you want to access the 21 GPIOs using Phyton or RTB Programming Languages.

Newer RPi versions that has 40 pins header doesn't have the above issue because all GPIOs are already integrated into P1 expansion header. The latest GPIO Module from Tankstage is also compatible with the latest models if you want to use RISCOS BBC BASIC.

The picture below shows how to connect the IR Sensor being sold here to Raspberry Pi where two GPIOs are programmed as INPUT pins.

For a HIGH level INPUT simulation purposes, you can just ignore the IR sensor relay switch and connect the 3 Kilohms resistors directly to Pin 1 of Raspberry Pi  giving both GPIO14 and GPIO15 a 3.3 Volts supply at a current of 1.1 mA each.  Since the P1 3.3 Volts pin 1 can give out a maximum current limit of 50 milliAmps, using a 3 Kilohms resistor for 21 GPIOs to be used as INPUT will be safe enough at a total of 23 milliAmps. Note that you can use higher resistance to 10 Kilohms but this depends on the power supply you use. If you are powering your RPi through a power supply and can deliver a stable maximum current of upto 2.5 to 3 Amperes, then higher than 3Kilohms is advisable. If it is through a rechargeable or ordinary batteries, then 3 Kilohms is in the middle and safe value that will prevent malfunction of GPIOs due to low current input when the battery started to drain-out. Why is it so? The reason is because the main supply input to RPi only has a voltage regulator and not a current regulator. The moment your batteries started to drain-out the maximum 50mA capability of 3.3 Volts pin will also drop, giving rise to GPIO HIGH and LOW level instability. It should be the same case for A+, B+ and Pi 2 models that uses switch mode regulators. So there will be balancing act in deciding how much limiting resistor you want to use depending on your application and how long you are going to use your RPi.   

 

 

  

To program the P1 GPIO as INPUT pin, you need to declare explicitly that it is to be used  as INPUT pin or terminal and write a code that would be able to read the voltage status on that GPIO pin as either HIGH or LOW state. In plane language, HIGH means 3.3 Volts and LOW means GROUND level or equal or below 1.7 Volts in this case. Using BBC BASIC programming, the code below shows how to do it:

 

SYS "GPIO_ExpAsGPIO",1        

The number 1 on the above command means all GPIOs on P1 are to be used as INPUT otherwise use 2 for OUTPUT.

To READ the data or the status on specific GPIO and store that data to a memory to be used later on, use the following command:

 

SYS "GPIO_ReadData",14  TO A%

 

The number 14 is the Broadcom ID GPIO number not the pin number on RPi while the A% is the allocated memory to store the data.

By default, most of the GPIO pins are at LOW level state upon powering-up your RPi. In order to pull the GPIO pin to a HIGH state, you need to apply a POSITIVE 3.3 Volts taken from Pin 1 of the RPi.

Applying the 3.3 Volts to the GPIO pin must be done through a limiting resistor (as shown above) to prevent damaging your RPi due to overcurrent or overheating. The user of RPi must be aware on the total maximum current limit that the GPIO can handle when configured as either INPUT or OUTPUT and when all GPIOs are fully utilized.

Note that some GPIO pins have PULL-UP and PULL-DOWN internal resistors depending on its function. So, it is advisable to identify which pins are those that require external resistors for proper current biasing.

 

NOTE: 

You cannot access the P4 GPIOs using the above command examples. You need to declare the four extra GPIOs individually to use as INPUT or use the FOR-NEXT statement.  Below is an example how to do it:

 

SYS "GPIO_ReadMode",28,1

SYS "GPIO_ReadData",28 TO A%



For those who want to embark on old but powerful BBC BASIC Programming Language coding for Raspberry Pi, the example below will provide productive understanding on how you can program your RPi for Robotics and Automation projects.

 

THE SCREENSHOT

 

 

 

 

THE PROGRAM CODE

 

  10 MODE 20: GCOL 3

  20 COLOUR 18: PRINT TAB(1,1); "Raspberry Pi - GPIO as INPUT"

  30 COLOUR 12: PRINT TAB(46,1); "PROGRAMMED BY: Rolly Estomaguio"

  40 COLOUR 2: PRINT TAB(54,5); "12 Aug 2015"

  50 COLOUR 31: PRINT TAB(46,3); "http://www.kits-gadgets.com"

  60 COLOR 38: PRINT TAB(2,10); "GPIO   -"  : COLOUR 45: PRINT TAB(14,10); "STATUS": COLOR 78: PRINT TAB(24,10); "-     USED FOR"

  70 COLOR 10: PRINT TAB(3,14); "15    -": PRINT TAB(24,14); "-    RIGHT IR SENSOR"

  80 PRINT TAB(3,16); "14    -": PRINT TAB(24,16); "-    LEFT IR SENSOR"

  90 PRINT TAB(3,18); "8     -": PRINT TAB(24,18); "-    FRONT U-S SENSOR"

  100 PRINT TAB(3,20); "25    -": PRINT TAB(24,20); "-    WHEEL REED SENSOR"

  110 PRINT TAB(3,22); "24    -": PRINT TAB(24,22); "-    REAR U-S SENSOR"

  120 PRINT TAB(3,24); "23    -": PRINT TAB(24,24); "-    FAR RANGE IR SENSOR"

  130 PRINT TAB(3,26); "18    -": PRINT TAB(24,26); "-    PWM FOR L298"

  140 PRINT TAB(3,28); "2     -": PRINT TAB(24,28); "-    HUMP SENSOR"

  150 PRINT TAB(3,30); "3     -": PRINT TAB(24,30); "-    DISTANCE COUNTER"

  160 ON ERROR PRINT REPORT$;" at line ";ERL:END

  170

  180 PROCinit

  190

  200 COLOUR 27: PRINT TAB(1,50); "Press G to READ the GPIOs or Escape to finish.": OFF

  210

  220  PROC_switch

  230

  240 DEFPROCinit

  250 OSCLI"RMEnsure GPIO 0.00 RMLoad GPIO"

  270 OSCLI"RMensure GPIO 0.40 ERROR Please install the GPIO module"

  290 SYS"GPIO_ExpAsGPIO",1

  310 SYS"GPIO_GetBoard" TO PiType%

  330 DIM A%(21)

  340 IF PiType%=12 A%()= 2,3,4,17,27,22,10,9,11,7,8,25,18,23,15,14,24,28,29,30,31

  350 ENDPROC

  360

  370 DEFPROC_switch

  380 L$= GET$: OFF

  390 IF L$= "G" OR L$= "g" THEN PROC_gpio_in: REM Press G key to read the GPIO data.

  400 IF L$= "Q" OR L$= "q" THEN QUIT

  410 PROC_switch

  480

  490 DEFPROC_gpio_in

  500 SYS "GPIO_ReadData",15 TO A%: PRINT TAB(16,14); A%

  510 SYS "GPIO_ReadData",14 TO B%: PRINT TAB(16,16); B%

  520 SYS "GPIO_ReadData",8 TO C%: PRINT TAB(16,18); C%

  530 SYS "GPIO_ReadData",25 TO D%: PRINT TAB(16,20); D%

  540 SYS "GPIO_ReadData",24 TO E%: PRINT TAB(16,22); E%

  550 SYS "GPIO_ReadData",23 TO F%: PRINT TAB(16,24); F%

  560 SYS "GPIO_ReadData",18 TO G%: PRINT TAB(16,26); G%

  570 SYS "GPIO_ReadData",2 TO H%: PRINT TAB(16,28); H%

  580 SYS "GPIO_ReadData",3 TO I%: PRINT TAB(16,30); I%

  590 K%= INKEY (1)

  600 IF INKEY (-99) THEN PROC_gpio_out : REM This is not included in the program.

  610 PROC_gpio_in

 

NOTE:  

There are some relics of modifications on the above program so feel free to edit as it fits. Nevertheless, it works fine for its purpose.

 

 

 

Copyright 2013 Kits and Gadgets Electronics Inc. All rights reserved.

Web Hosting by Yahoo

Kits and Gadgets Electronics Inc.
Farwaniya, Kuwait 80003
Kuwait

ph: +965 99571844

kits@kits-gadgets.com