Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

IOT - Practical -:2

L.D. College of Engineering,


Gujarat Technological University.
Information Technology Department,
Ahmedabad, Gujarat

Sahilkumar Prajapati - 180280116096

SEM - VII
IOT - 3171108

1
180280116096 | SAHILKUMAR
IOT - Practical -:2

Practical-2

Aim: GPIO Interfacing and programming.

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.

The most well-known elements of GPO pins include:

1. Being configurable in programming to be info or yield


2. Being empowered or debilitated
3. Setting the worth of an advanced yield
4. Perusing the worth of a computerized yield
5. Creating a hinder when the info changes esteem

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

gpio version: 2.46


Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:

3
180280116096 | SAHILKUMAR
IOT - Practical -:2

Type: Pi 3+, Revision: 03, Memory: 1024MB, Maker: Sony


* Device tree is enabled.
*--> Raspberry Pi 3 Model B Plus Rev 1.3
* This Raspberry Pi supports user-level GPIO access.
Last we can install wiringpi using the command sudo
pip3 install wiringpi

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.

A graph to distinguish the two sides is displayed beneath:


Cathode and Anode of a Drove - Source:
http://www.blocksignalling.co.uk/index.php/trafficsignals module-normal anode-tlc2a

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.

Hardware Schematic and BreadBoard

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.

Driving the LED


gpio mode 4 output
gpio write 4 1 gpio
write 4 0 gpio blink
4 gpio mode 25
input
gpio read 25

Interfacing GPIOs from Python

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

wiringpi.wiringPiSetup() # Use WiringPi numbering

PIN_NUMBER = 4

wiringpi.pinMode(PIN_NUMBER, 1) # Set LED pin to 1 ( OUTPUT )

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

class Led(object): def __init__(self, pin):


self.pinNumber = pin wiringpi.wiringPiSetup() #
Use WiringPi numbering
wiringpi.pinMode(self.pinNumber, 1) # As output
self.off()

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 set_state(self, state):


self.isOn = state
wiringpi.digitalWrite(self.pinNumber, state) # Write state to LED pin

def get_state(self):
return self.isOn

7
180280116096 | SAHILKUMAR
IOT - Practical -:2

# The main program


led = Led(4)

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

wiringpi.wiringPiSetup() # Use WiringPi numbering

PIN_NUMBER = 25

wiringpi.pinMode(PIN_NUMBER, 0) # Set Pushbutton pin to 0 ( INPUT )

while True: state =


wiringpi.digitalRead(PIN_NUMBER) print("The
push button state = {}".format(state)) sleep(1)
Now refactoring this to a nice Button class: 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)

# The main program


button = Button(25)

while True: print("The push button state =


{}".format(button.get_state())) sleep(1)

#Making the Button drive the LED

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)

class Led(object): def __init__(self, pin):


self.pinNumber = pin wiringpi.wiringPiSetup() #
Use WiringPi numbering
wiringpi.pinMode(self.pinNumber, 1) # As output
self.off()

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 set_state(self, state):


self.isOn = state
wiringpi.digitalWrite(self.pinNumber, state) # Write state to LED pin

def get_state(self):
return self.isOn

# The main program


led = Led(4) button
= Button(25)

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

You might also like