Circuit Symbols

Circuit Schematic

figures/circuit-1.png

Circuit Schematic (Contd.)

figures/circuit-2.png

Sensors with Binary Output

PIR Sensor

figures/pir.jpg
  • PIR Sensor: Passive Infrared Sensor
  • Detects presence of humans by measuring radiated infrared light

Reed Switch

figures/reed-switch.jpg
  • Reed switch, detects the presence of a magnet
  • When a magnet comes closer to the switch the switch closes
  • Can be used as a door sensor

Gas Detector

figures/smoke-sensor.jpg
  • Gas sensors, detects the presence of
    • Smoke
    • Alcohol
    • Carbon Monoxide

Simplified Digital Sensor Model

  • Sensor measures a parameter and outputs 1 or 0
    • Indicates 1, by connecting output to VCC
    • Indicates 0, by connecting output to GND
figures/sensor-model.png

Binary Control

Relay

figures/relay.jpg
  • Electrically controlled switch
  • 230V AC appliance can be turned on / off based on input

Solenoid Valve

figures/solenoid-valve.jpg
  • Electrically controlled valve
  • Flow can switched on / off based on input

GPIO

GPIO Controller

  • GPIO controller is the hardware which provides register interface using which the pin status can be accessed.
  • GPIO controller resides in the memory bus of the SOC.
figures/gpio-arm.png

GPIO Direction

  • It can operate in both the directions, as an input to sample the logic level or as an output to drive any logic level
  • Their direction as input or output can be chosen.
  • The pin status can be read or modified from software.

GPIO Ports and Pins

  • The GPIO controllers are also called GPIO ports.
  • A GPIO ports, controls a set of GPIO pins
  • GPIOs pins are referred as, particular pin in a particular port
  • Example: P2_3 - pin 3 in port 2

Linux GPIO Numbering

  • Vybrid VF510 has 6 GPIO ports, Port 0 - Port 5
  • Each port controls 32 pins
  • Pins are numbered sequentially
    • Port 0, Pin Numbers: 0 - 31
    • Port 1, Pin Numbers: 32 - 63
    • Port 2, Pin Numbers: 64 - 95

GPIO Usage in ZKit-ARM-VF51

Device Port Pin Pin Sequence No

RLED

P2_6

GPIO70

BLED

P2_0

GPIO64

GLED

P2_1

GPIO65

KEY1

P2_5

GPIO69

KEY2

P2_3

GPIO67

SYSFS GPIO

GPIO Files in Sysfs

  • Inside sysfs in path /sys/class/gpio we have files which allows to monitor and control the GPIO.
  • For each GPIO a folder /sys/class/gpio/gpio<no> is created and it would have below files.
  • the direction file allows to control direction of pin and value file would allow to access the pin state.
/sys/class/gpio/
| -- gpio<no>
     | -- value
     ` -- direction

LEDs

LED Circuit

figures/led-circuit.png

LED Switch

figures/led-switch.png

LED Control

figures/led-control.png

GPIO LED Control

figures/led-gpio.png

GPIO Request

  • GPIOs can be used internally by other kernel subsystems
  • GPIO has to be made available to user, by the kernel, before they can be used
  • If GPIO is used by kernel, this will fail
$ echo 65 > /sys/class/gpio/export
  • You would find /sys/class/gpio/gpio65 available, if no other peripheral subsystem has not reserved it.
  • A GPIO pin can be freed as shown below
$ echo 65 > /sys/class/gpio/unexport

GPIO Direction

  • The processor pin can be configured as output pin by writing out to the GPIO direction file
$ echo out > /sys/class/gpio/gpio65/direction
  • The processor pin can be configured as output pin by writing in to the GPIO direction file
$ echo in > /sys/class/gpio/gpio65/direction

GPIO State

  • When value 1 or 0 written in the value file, it can drive logic level high or low.
$ echo 1 > /sys/class/gpio/gpio65/value
  • The value file can be read to know the state of input pin.
$ cat /sys/class/gpio/gpio65/value

LED Glow Program

LED = "64"

export_led = open("/sys/class/gpio/export", "w")
export_led.write(LED)
export_led.close()

led_direction = open("/sys/class/gpio/gpio"+LED+"/direction", "w")
led_direction.write("out")
led_direction.close()

led_value = open("/sys/class/gpio/gpio"+LED+"/value", "r+")
led_value.write("0")
led_value.close()

GPIO Module

from os.path import exists

__gpio_path = "/sys/class/gpio/gpio{}"
__gpio_export_path = "/sys/class/gpio/export"
__gpio_unexport_path = "/sys/class/gpio/unexport"
__gpio_direction_path = "/sys/class/gpio/gpio{}/direction"
__gpio_value_path = "/sys/class/gpio/gpio{}/value"

def cat(file):
    file = open(file, "r")
    value = file.read()
    file.close()
    return value

def echo(value, file):
    file = open(file, "w")
    file.write(value)
    file.close()

def gpio_export_pin(pin):
    if not exists(__gpio_path.format(pin)):
        echo(str(pin), __gpio_export_path)

def gpio_unexport_pin(pin):
    if exists(__gpio_path.format(pin)):
        echo(str(pin), __gpio_unexport_path)

def gpio_get_direction(pin):
    return cat(__gpio_direction_path.format(pin))

def gpio_set_direction(pin, direction):
    echo(direction, __gpio_direction_path.format(pin))

def gpio_get_value(pin):
    value = cat(__gpio_value_path.format(pin))
    return int(value)

def gpio_set_value(pin, value):
    echo(str(value), __gpio_value_path.format(pin))

LED Blinky Program

The GPIO related actions are implemented as a GPIO class and it is used in blink code gpio.py

from time import sleep
from gpio import *

LED = 64

gpio_export_pin(LED)
gpio_set_direction(LED, "out")

for count in range(5):
    gpio_set_value(LED, 1)
    sleep(1)

    gpio_set_value(LED, 0)
    sleep(1)

gpio_unexport_pin(LED)

Try Out

  • Glow the tri color LED connected to GPIO64, 65 & 70 through command line.
  • Modify the LED Blink Code led-blink.py to change the color of led every one second.

Try Out Codes

Keys

Key Circuit

figures/keys.png

Input States

  • Digital inputs can be of three states high, low or floating.
  • If they are not connected to any signal it would be a floating input and said to be open.
  • The floating pins doesn’t have a fixed state and it would pick near by signal and would have unstable state.

Default Value

  • The inputs lines has to be fixed to a default state by tying that
    • to VCC for default high state or
    • to GND for default low state.
  • Making a pin to stay high, by connecting it to VCC through a resistor is called weak pull-up.
  • Making a pin to stay low, by connecting it to GND through a resistor is called weak pull-down.
figures/keys-pullup.png

Code

from time import sleep
from gpio import *

KEY = 67
gpio_export_pin(KEY)
gpio_set_direction(KEY, "in")

print("Press Key1")
while True:
    key_value = gpio_get_value(KEY)
    if key_value == 0:
        print("Key is pressed")
        break
    sleep(0.1)

gpio_unexport_pin(KEY)

Try Out

  • Modify key.py, to glow
    • Red LED, when Key 1 pressed
    • Green LED, when Key 2 pressed

References

Backup Slides

Seven Seg

figures/segment.png
figures/sseg-pinmap.png

Program for Seven Segment

from gpio import *

ON = 64

gpio_export_pin(ON)
gpio_set_direction(ON, "out")
gpio_set_value(ON, 1)

gpio_pins = ["70", "69", "67"]

for i in gpio_pin:
    gpio_export_pin(gpio_pins[i])
    gpio_set_direction(gpio_pins[i], "out")
    gpio_set_value(gpio_pins[i], 1)

GPIO Usage Models

Various GPIO Usage patterns

  • Individual GPIOs for single LED, Keys, Relays, Reed switches and solenoid valves.
  • Parallel GPIOs for 7 Segment, Stepper Motor etc.,
  • Multiplexed GPIOs for Multi Seven Segment Array, Matrix Keypad etc.,
  • GPIOs can be coupled with timer for periodic signalling or to create pulse trains of various width.

Bitbanging

  • GPIOs as its name suggests, has countless possibility that using which we can build simple busses like
    • I2C,
    • 1 wire,
    • SPI and
    • even complex Memory bus
  • This done using Bitbanging is a technique, which allows to simulate serial busses using software over GPIO.

Key Debounce

  • The switches or keys used are mechanical devices and when key is pressed or depressed they tend to vibrate creating noises until it settles down.
  • This condition can be handled in hardware by adding a debounce filter.
  • It can also be handled in software by waiting for specific time from the event occurence till it settles down.
  • This delay time is typically around few hundred millisecond and called debounce delay.

Programming for Keys

from gpio import *

KEY = 67
gpio_export_pin(KEY)
gpio_set_direction(KEY, "in")

print("Press Key1")
while True:
    key_state = gpio_get_value(KEY)

    if key_state == 0:
        print("Key is pressed\n")
        break

gpio_unexport_pin(KEY)

Key Polling

  • Since the key press is asynchronous, it has to be polled continuously or periodically.
  • Continuously polling for key may burn the CPU, instead periodical polling should be sufficient for keys.
  • Typical key stroke period by humans, couldn’t be less than few hundred millisecs.
  • it’s enough that we can poll for key press once in 100 msec.

Sensors and Actuators

  • Sensors - produces two values on or off
    • Door Sensor
    • Presence Sensor
    • Smoke Detector
  • Actuators
    • Relays: Turn ON / OFF Appliances
    • Solenoids

Introduction

  • General Purpose Input Output(GPIO) are single pin IO, represents voltage signal as binary logic level high and low.
  • Typically GPIO high maps to voltage closer to VCC and low maps to voltage closer to ground.
  • high state is considered as logical 1 and low as 0.
figures/gpio-bistate.png

Try Out Code