GPIO V2.04 using GPIO Zero by Barend on 2017-01-12

GPIO IO Board using “GPIO Zero” in Python

 

 

Introduction

This Guide covers the necessary basics of getting your PiShop supplied kit up and running so you can start enjoying it as soon as possible.

This installation Guide assumes the following to be true:

  • You have purchased a kit from PiShop, either directly or via one of our resellers and therefor have all of the required items in 1 easy to use bundle.
  • You have a working knowledge or understanding of how to solder small electronic components onto a PCB.

 

Disclaimer and Warning

We sell goods for domestic purposes only and for the purpose for which they were manufactured. If you want to make use of the GPIO capabilities and this results in damage due to improper usage, no return or warranty/guarantee will be honoured.

WARNING: If you follow the instructions, then messing about with the GPIO is safe and fun. Randomly plugging wires and power sources into your Pi, however, may kill it. Bad things can also happen if you try to connect things to your Pi that use a lot of power; LEDs with resistors are fine, motors are not. If you are worried about this, then you might want to consider using a breakout board such as the Pibrella until you are confident enough to use the GPIO directly. As a safety measure always shut down and unplug the RPi before plugging or unplugging jumper cables and components

WARNING: Soldering irons get very hot – be careful when soldering to avoid burns.

 

Abbreviations

PCB Printed Circuit Board
RPi

LDR

LED

Raspberry Pi

Light Dependant Resistor

Light-emitting diode

 

 

Whats in the Box

  • 1 x Printed Circuit Board
  • 3 x 5mm LEDs
  • 3 x 330 Ohm Resisters
  • 1 x 8 pin male to male PCB connector (Header)
  • 1 x Light Dependant Resister
  • 1 x 1uF Capacitor
  • 1 x buzzer
  • 1 x tactile switch
  • 8 x Jumper cables (if option was selected)

 

Hardware Prerequisites

You will need the following items not included directly with this kit in order to continue with the tutorial.

  • Raspberry Pi Model 2/3 Model B (this kit also works with other RPi models)
  • A Micro SD Card running the latest version of Raspbian
  • USB Keyboard and Mouse (this kit can also be enjoyed with a Headless RPi)
  • Screen with respective connection
  • A Working internet connection (for required software downloads)
  • Soldering Iron
  • Soldering Wire

 

Software Prerequisites

You will need the following software components in order to follow the steps detailed in this manual:

 

 

Raspbian

Raspbian is a free operating system based on Debian Linux optimized for the Raspberry Pi hardware. An operating system is the set of basic programs and utilities that make your Raspberry Pi run. Raspbian is also the most popular of the Operating Systems available for the Raspberry Pi and therefore is the most widely used for tutorials.

 

If you have a blank Micro SD Card

Download NOOBS

  • Using a computer with an SD card reader, or a USB card Reader, visit the Raspberry Pi Foundation Downloads page at http://www.raspberrypi.org/downloads/.
  • Click on the Download ZIP button under ‘NOOBS (offline and network install)’, and select a folder to save it to.
  • Once the download completes; extract the files from the zip.

 

Format your SD Card

  • Visit the SD Association’s website and download SD Formatter 4.0 for either Windows or Mac. (https://www.sdcard.org/downloads/formatter_4/)
  • Follow the instructions to install the software.
  • Insert your SD card into the computer or laptop’s SD card reader and make a note of the drive letter allocated to it, e.g. G:/
  • In SD Formatter, select the drive letter for your SD card and format it.

 

Copy NOOBS Files

  • Once your SD card has been formatted, drag all the files in the extracted NOOBS folder and drop them onto the SD card drive.
  • The necessary files will then be transferred to your SD card.
  • When this process has finished, safely remove the SD card and insert it into your Raspberry Pi.

 

If you have an SD Card Pre-Loaded with NOOBS:

  • Plug in your keyboard, mouse, monitor cables and SD Card.
  • Now plug in the USB power cable to your Pi.
  • Your Raspberry Pi will boot, and a window will appear with a list of different operating systems that you can install. For this kit we require that you use Raspbian – tick the box next to Raspbian and click on Install.
  • Raspbian will then run through its installation process. Note this can take a while.
  • When the install process has completed, the Raspberry Pi configuration menu (raspi-config) will load. Here you are able to set the time and date for your region. You can exit this menu by using Tab on your keyboard to move to Finish.

 

If you already have Raspbian installed:

  • You will see a screen with a login prompt
  • The default login for Raspbian is username pi with the password raspberry. Note you will not see any writing (text) appear when you type the password. This is a security feature in Linux.
  • Login and execute the following commands in a terminal, waiting for them to finish before moving onto the next.
    • sudo apt-get update
    • sudo apt-get upgrade
    • sudo apt-get dist-upgrade

 

Soldering Guide

In order to make use of your new kit you will need to get your soldering equipment out (there are many tutorials on YouTube should you get stuck or be new to soldering)

Figure 3: Circuit Diagram

Beginners Guide

Should you not be familiar with electrical diagrams, follow the set of guidelines below to get your PCB connected:

  • The PCB’s top side (upward facing) is the side that has white text on it, the underside has a copper inlay as can be seen in figure 3 above.
  • The Button has no specific direction, if the legs fit the holes, it’s mounted correctly.
  • The Buzzer has a positive (long leg) symbol on the top, this should face toward the connector pins. Don’t forget to remove the sticker from the buzzer that’s blocking the sound.
  • The Capacitor is marked with (-) symbol on one side, this should face away from the connector pins.
  • The LEDs positive side (long leg) should face toward the connector pins.
  • The 330 Ohm Resisters and the LDR can be soldered in facing any direction as there is no positive or negative sides.
  • The connector pins should be soldered in with the short legs mounted in the board.

Connecting the Kit

Before we run the code we have to connect the kit to the GPIO on the Pi. The pins are numbered from 1 to 40. Pin 1 will be the pin on the side to the inside of the board on the left, have a look at the specifications of your specific model that will tell you which cable should be connected to which pin.

Figure 4: GPIO Layout View 1

 

 

 

 

Jumper cable connections

 

 

 

 

Figure 5: GPIO Layout View 2

The below connections are based on our Python app used to demonstrate the workings of the PCB kit.

  • The pins on the PCB are marked from 1 to 8
  • Pin 1 is a 3.3V live feed to the LDR connected to pin 1 on the GPIO
  • Pin 2 is used to read light intensity and connected to pin 13 on the GPIO
  • Pin 3 is used to power the red LED and connected to pin 37 on the GPIO
  • Pin 4 is used to power the yellow LED and connected to pin 35 on the GPIO
  • Pin 5 is used to power the green LED and connected to pin 33 on the GPIO
  • Pin 6 is used to sound the buzzer and connected to pin 31 on the GPIO
  • Pin 7 is used to read the button and connected to pin 29 on the GPIO
  • Pin 8 is used as ground and connected to pin 39 on the GPIO

 

 

 

The CODE:

Download the code here

or copy and paste the following http://www.pishop.co.za/kits/GPIO/code/GPIO_IO_Kit_2_04.py

——————————————————————————————————————————————————-

 

# GPIO IO kit 2.04

# www.pishop.co.za

# Written on Python version 3.4.2

 

import os, sys

from gpiozero import Button, Buzzer, LED, LightSensor

import time

 

 

 

print (“Your version of python”)

print (sys.version)       #report the python version to command line

 

 

red = LED(26)             #GPIO Board PIN 37 /  Variable red to board 37/gpio26

yellow = LED(19)          #GPIO Board PIN 35 /  Variable yellow to board 35/gpio19

green = LED(13)           #GPIO Board Pin 33 /  Variable green to board 33/gpio13

buzz = Buzzer(6)          #GPIO Board Pin 31 /  Variable buzz to board 31/gpio6

button = Button(5)        #GPIO Board Pin 29 /  Variable button to board 29/gpio5

ldr = LightSensor(27)     #GPIO Board Pin 13 /  Variable ldr to board 13/gpio27

 

 

 

def testbutton():                                                         #the button function

done = False                                                        #flag used to stop the test

print(“Waiting for button…”)                          #report to the command line

while not done:                                                  #check if test ended

if button.is_pressed:                                #button will turn buzz on when pressed

print(“Button detected”)                     #report to command line

buzz.beep(0.5, 0.25, 2)                       #Make the buzzer beep twice

done = True                                           #stop the test

 

 

def displayldr():                                                                                                               #defy the displayldr function

counter = 0                                                                                                        #reset the count to 0 before start

               

while counter < 21:                                                                                           #will run less than 21 times befor stop

print(“test ” + str(counter) + ” ” + str(ldr.value*100))             #print the word “test” (that will increase by 1 each time. + value of ldr

time.sleep(0.5)                                                                                  #sleep 0.5sec

 

counter = counter +1                                                                       #definition to display on screen

 

 

#Main body

answ = “0”

print(“Please select one of the below keys and [ENTER] to activate the GPIO port”)

answ = input(“r red y yellow g green b buzz s button l LDR x exit: “)                                    #get the requested option

while answ != “x”:                                                                                                                              #stop the loop when you get to the “x” option

if answ == “r”:                                                                                                                               #if the option was “r” then:

red.on()                                                                                                                                      #turn on the red LED

time.sleep(1)                                                                                                                              #wait 1 second

red.off()                                                                                                                                      #turn off the red LED

if answ == “y”:

yellow.on()

time.sleep(1)

yellow.off()

if answ == “g”:

green.on()

time.sleep(1)

green.off()

if answ == “b”:

buzz.beep(0.5, 0.25, 2)

time.sleep(1)

if answ == “s”:

testbutton()

if answ == “l”:

displayldr()

answ = input(“r red y yellow g green b buzz s button l ldr x exit: “)

 

———————————————————————————————————————————————-

 

Executing the code

Geany can be opened either via the GUI as seen below, or via the terminal.

  1. Open a new terminal window
  2. Execute: sudo geany

 

 

 

Once Geany is running, from the File menu, select Open and navigate to /home/pi, then select the GPIO_IO_Kit_2_04.py file that we downloaded earlier.

Hit the F5 key on your keyboard to execute the code. (You should see an image very similar to the one below)

 

 

 

 

 

PiShop Code introduction

The base PiShop code provides the following features:

Command Description
x Exits the program
r Switches on the red LED
y Switches on the yellow LED
g Switches on the green LED
b Sounds the Buzzer
s Listens for Button Push
l Activates the LDR for light measurement

LDR Basics – some info on how the LDR works

  • Set the GPIO pin as an output and set it Low. This will discharge any charge in the capacitor.
  • Set the GPIO pin as an input. This will start a flow of current through the LDR and through the capacitor to ground. The voltage across the capacitor will start rising. The higher the resistance of the LDR, the longer it will take to charge the capacitor.
  • When you check the value of the GPIO pin. Make use of a counter that increments until the value of the pin changes.
  • When the voltage of the capacitor has increased enough to be considered as a High by the GPIO pin, the value of your counter will be proportional to the light level seen by the LDR.
  • Repeat the above steps while covering the LDR or shine a light on it to get different readings.
  • Note: LDR’s are not precision instruments. Readings from one LDR might differ from another.

 

 

 

 

Further reading and special thanks

This instruction manual, while written and developed by PiShop would not have been possible without the information provided by the following sources:

Take some time now to try out some of the other code versions available for this kit:

They might use other pin connections

 

Conclusion

At this time you should feel very comfortable with your new Kit and the Raspberry Pi. If you have any questions or comments we would be very happy if you got in touch with us on [email protected]


 

 

GPIO IO v2.04 Quick Start Guide

www.Pishop.co.za