Professional Documents
Culture Documents
L.D. College of Engineering, Gujarat Technological University
L.D. College of Engineering, Gujarat Technological University
SEM - VII
IOT - 3171108
1
180280116096 | SAHILKUMAR
IOT - Practical -:2
Practical-2
What is GPIO?
A GPIO is a sign pin on a coordinated circuit or board that can be utilized to perform
computerized information or yield capacities. By plan it has no predefined reason and can be
utilized by the equipment or programming engineer to play out the capacities they pick.
Ordinary applications incorporate controlling LEDs, understanding switches and controlling
different kinds of sensors.
Most models of Raspberry Pi, for instance, have a 40-pin GPIO connector that gives
admittance to around 25 GPIO lines.
GPIO might be carried out by devoted incorporated circuits, or all the more frequently are
straightforwardly upheld by framework on a chip (SoC) or framework on a module (SoM)
gadgets.
GPIO pins are advanced, which means they just help high/low or on/off levels. They for the
most part don't uphold simple info or yield with numerous discrete voltage levels. Some GPIO
pins may straightforwardly uphold normalized correspondence conventions like sequential
correspondence, SPI, I2C, PCM, and PWM. (I'll cover these in future posts).
2
180280116096 | SAHILKUMAR
IOT - Practical -:2
Initial a Drove and pushbutton are associated with the Pi. Next these are moved toward
utilizing the GPIO order line utility. Last they are interfaced from a python script.
#Pinout of Raspberry Pi
A helpful site showing the pinout of the Raspberry Pi can be found at https://pinout.xyz/. It
arranges all pins pleasantly as per their capacity. It even has an Arduino viable pinout for
WiringPi https://pinout.xyz/pinout/wiringpi
Do note that WiringPi uses different pin numbers than the actual processor pins (BCM). Both
numbering schemes are supported by wiringpi but it is recommended to the use the
wiringpi pinout numbering when using the library or gpio utility. Installing WiringPi
cd
git clone git://git.drogon.net/wiringPi cd
wiringPi
./build
Raspberry Pi Details:
3
180280116096 | SAHILKUMAR
IOT - Practical -:2
Interfacing a Drove
Beginning straightforward is vital. The most fundamental equipment arrangement one can
construct is appending a Prompted a GPIO and turning it on or off. The Drove would then be
able to fill in as a yield marker.
The Drove has an anode and a cathode side. The anode side ought to be associated with the
positive inventory, while the cathode ought to be associated with the ground.
If we somehow happened to interface the Drove straightforwardly to the force supply it would
attract approach to much current. To restrict the current we need to put a resistor in series
with the Drove (as displayed in the schematic in the following area). For this a 1k resistor can
be utilized.
A 1k resistor - Source:
https://www.pinterest.com/pin/794040978023017042/?autologin=true
While a 1k resistor attempts to restrict the current through the Drove, it won't be ideal for
the voltage and sort of LEDs utilized here. By and by you ought to consistently take the voltage
drop of the Drove, the force supply and the favored current (regularly 10mA or 20mA) into
account. There are different locales you can use for this like for instance:
http://www.ohmslawcalculator.com/drove resistor-mini-computer.
4
180280116096 | SAHILKUMAR
IOT - Practical -:2
It is significant not to cause the GPIO to give to much power as these are straightforwardly
associated with the processor pins and no assurance for overcurrent is given. A
microcontroller/chip can't give a lot of current.
By associating the anode side of the Prompted VCC (+3V3 for this situation) through a resistor,
we really don't allow the GPIO to source the current. The current is sourced by the force
supply and it is sinked by means of the GPIO. A GPIO is frequently ready to sink significantly
more current than it can source.
Choosing what GPIO pin to utilize isn't in every case simple. You need to ensure you are not
interfacing with an all around utilized pin or to a pin with an extraordinary capacity. A site like
https://pinout.xyz/can be a pleasant guide.
Here we utilize GPIO4 (BCM23) to interface the cathode of the Drove. Interfacing all that
accurately should show a comparable outcome to the picture displayed beneath.
5
180280116096 | SAHILKUMAR
IOT - Practical -:2
Interfacing a pushbutton
Next a pushbutton can be joined to the Raspberry Pi so its state can be perused. Try to
follow the schematic precisely. The pullup resistor (10k or higher) pulls the information high
if the pushbutton isn't squeezed. In case this isn't joined, the info may glide.
Interfacing with the GPIO's from Python is not a lot harder than using the gpio utility. Scripting
it from Python has the advantage that it is a lot more versatile and and can be integrated into
our applications.
#Driving an Output
The example code below shows how an output can be driven high and low from python
using wiringpi:
6
180280116096 | SAHILKUMAR
IOT - Practical -:2
import wiringpi
from time import sleep
PIN_NUMBER = 4
while True:
print("Setting LED on")
wiringpi.digitalWrite(PIN_NUMBER, 0) # Write 0 ( LOW ) to LED pin
sleep(1)
print("Setting LED off")
wiringpi.digitalWrite(PIN_NUMBER, 1) # Write 1 ( HIGH ) to LED pin
sleep(1)
Integrating this functionality into the previously created Led class leads to a much cleaner
application. import wiringpi
from time import sleep
def on(self):
self.set_state(True)
def off(self):
self.set_state(False)
def toggle(self):
self.set_state(not self.get_state())
def get_state(self):
return self.isOn
7
180280116096 | SAHILKUMAR
IOT - Practical -:2
while True:
print("Toggling the LED")
led.toggle()
sleep(1)
#Reading an Input
The example code below shows how an input can be read from python using wiringpi:
import wiringpi
from time import sleep
PIN_NUMBER = 25
class Button(object):
def __init__(self, pin):
self.pinNumber = pin
wiringpi.wiringPiSetup() # Use WiringPi numbering
wiringpi.pinMode(self.pinNumber, 0) # Set button pin to 0 ( INPUT )
def get_state(self):
return wiringpi.digitalRead(self.pinNumber)
8
180280116096 | SAHILKUMAR
IOT - Practical -:2
Last but not least, we can combine the code from the previous example into a single
application. The code below demonstrates how the state of the Button can drive the LED.
import wiringpi
from time import sleep
class Button(object):
def __init__(self, pin):
self.pinNumber = pin
wiringpi.wiringPiSetup() # Use WiringPi numbering
wiringpi.pinMode(self.pinNumber, 0) # Set button pin to 0 ( INPUT )
def get_state(self):
return wiringpi.digitalRead(self.pinNumber)
def on(self):
self.set_state(True)
def off(self):
self.set_state(False)
def toggle(self):
self.set_state(not self.get_state())
def get_state(self):
return self.isOn
while True:
led.set_state(button.get_state())
sleep(0.1)
Notice how the sleep is decreased to make it more responsive.
9
180280116096 | SAHILKUMAR
IOT - Practical -:2
10
180280116096 | SAHILKUMAR