Raspberry Pi IO Devices Guide
Raspberry Pi IO Devices Guide
- S.P. Chong
         Connection
                                                  Applications
                                                  • Position control            The duty cycles for
                                                                              different arm positions
                                                  • Sorting / diverting         can be obtained by
                         It may                   • Remote control                  trial & error.
                        look like
                                                    car/boat/aeroplane
   6/1/2020               this.     RPi - Python - write-ups on IO devices
                                                                                            4
   DC motor                                                                  Applications
Intro                                                                        • Movements – turning a
                                                                               wheel / a conveyor belt
• A DC motor can rotate continuously when a                                  • Cooling fan
  current passes through it.                                                 • Pumping air or water
• However, an RPi output pin does not produce
  enough current to drive it directly.
• So, a BJT transistor is often used, to increase                                            Voltage depends on
  the current.                                                                                   motor used.
• A diode is often needed to protect the circuit.
                                                                               Connection
        Sample Code
                                      For a DC motor to turn
        • Please refer next page.      in both directions, a
                                      “H-bridge” is needed.
                                                                                                            5
   6/1/2020                         RPi - Python - write-ups on IO devices
DC motor (cont.)                                            Sample Code
                             Using PWM to
                          control motor speed.
                                                                          6
6/1/2020           RPi - Python - write-ups on IO devices
Solenoid                                                           Applications
Intro                                                              • Locking & unlocking
                                                                   • Dispensing mechanism
• When a current passes through a coil, it                         • Kicking action
  becomes an electromagnet and the                                 • Pinching to control liquid flow
  magnetic force can pull in (or push out) a
  plunger. This is how solenoid works.
• Again, an UNO output pin does not produce
  enough current to drive it directly.                                  Connection
• So, a BJT transistor is often used, to                                                     Plunger.
  increase the current.
              Sample Code
              • Please refer next page.
                                                                                                        7
6/1/2020                       RPi - Python - write-ups on IO devices
Solenoid (cont.)                               Sample Code
                                                             8
6/1/2020           RPi - Python - write-ups on IO devices
WiFi
           Connection
6/1/2020                        RPi - Python - write-ups on IO devices                                       14
Tilt Switch (cont.)
                                                   Sample Code
                                                                                                      It may
                                                                                                     look like
                                                                                                       this.
                   Voltage division
                  Voltage at X, VX =
    6/1/2020       3.3V * 10k / (10k + R1)   RPi - Python - write-ups on IO devices                        16
                                    A -> D Conversion
LDR (cont.)
SPI
Applications                                    Connection
• Obstacle detection
• Height / distance measurement
               Sample Code
               See next page…
  6/1/2020                          RPi - Python - write-ups on IO devices               19
                                                          Ultrasonic Ranger
                                                          (cont.)
           Sample Code
Refers to Lesson 4
    Connection                                                               Applications
                                                                             • Motion detection
                                                                             • Fall alert
                                                                             • Vandalism prevention
                                                  https://www.anstack.com/blog/2016/07/05/accelerometer-intro.html
Sample Code
We will use the adxl345 module from pimoroni.
At a terminal, type
  git clone https://github.com/pimoroni/adxl345-python
This will download the folder “adxl345-python”, with the file “adxl345.py”.
Modify the print statements at the end of this file as follows:
                                                                                       After modification, this file can be
                                                                                       run to print out the acceleration in
                                                                                             the X, Y & Z-axes once.
Applications
                                                                                                      See next slide on
• Various tracking                                                                                      sample code.
  applications
https://www.gpsinformation.org/dale/nmea.htm
                                                                            Sample Code
         Sample run:
Sample run:
Sample Code
                          Sample code
             for hard keypad, change to:
             ROW=[13,21,20,26]
             COL=[19,6,16]
                                                                                                           30
6/1/2020                                   RPi - Python - write-ups on IO devices
                                                                     It may
   LCD (TWI)                                                       also look
                                                                   like this.
Intro
• An LCD can be used to prompt for password, or display
  info & instruction etc.
• We will use the 4-pin version below, to reduce the
  number of RPi pins needed for connection.
• FYI, this is using TWI (or Two Wire Interface – not very                       Applications
  easy to understand).
                                                                                 • Info / instruction
• Some setup steps are required – see next few pages.
                                                                                   display
                                                                                 • Password prompt
  Sample Code                                                           Connection
                                                                                                        31
   6/1/2020                         RPi - Python - write-ups on IO devices
LCD (TWI) (cont.)
    Setup
                                                                                 32
6/1/2020                    RPi - Python - write-ups on IO devices
LCD (TWI) (cont.)
      Setup (cont.)
                        You can find out the I2C
                       address of the connected
                      LCD by using this command.
                                                                                The i2c-tools can be installed
                                                                                   this way, if necessary.
                                                                                                    33
6/1/2020                    RPi - Python - write-ups on IO devices
                        http://www.circuitbasics.com/raspberry-pi-i2c-lcd-set-up-and-programming/
    LCD (TWI) (cont.)
Setup (cont.)
                                                                             Access this webpage...
                                                                                                      34
    6/1/2020                RPi - Python - write-ups on IO devices
    LCD (TWI) (cont.)                                                            Sample code
Setup (cont.)
                                                                                                                      35
    6/1/2020                                RPi - Python - write-ups on IO devices
                                                                                         Connection
   Voice playback module
Intro
• To play back voice / music, place the mp3 or wav files
  in a thumb-drive and insert this into a USB port, or
  place them in the SD card (with the RPi OS).
Sample Code
The code below uses omxplayer to play back a                         Switch to the correct       Connect an earpiece or active
                                                                        audio output.            speaker to the audio output.
media file, including video, voice / music.
                                                                                             Applications
                                                                                             • Voice prompt / instruction
                                                                                               / alert
  Text can be converted to mp3 file at www.fromtexttospeech.com                              • Attract attention
   6/1/2020                                 RPi - Python - write-ups on IO devices           • Entertainment 36
               Intro
File I/O’s   • The RPi OS (Operating System) is on an SD card, which can be
                used to store other data files. This sample code shows how a file
                can be opened for writing, appending & reading.
                                                                        Applications
                                  Sample Code                            • Data logging
Indeed…
6/1/2020             RPi - Python - write-ups on IO devices   After this, it should contain data3.    38
File I/O’s (cont.)       Assuming a thumb drive is inserted
                          and the test1 file is moved into it.
How about thumb drive?
Connection
Applications
• Selection
• Control input
                                                                          Sample Code
 Connection
Refers to Lesson 3
Refers to Lesson 3
RFID tags.
CS
                       Sample Code
                       Identify Cards.py – to check card against database.
                                 http://www.diymalls.com/Optical-Fingerprint-Reader-Sensor
Intro
• Adafruit, an electronic parts manufacturer & seller, has a very good write-up on the reader:
  https://learn.adafruit.com/adafruit-optical-fingerprint-sensor
• You can also refer to the following:
  https://tutorials-raspberrypi.com/how-to-use-raspberry-pi-fingerprint-sensor-authentication/
• The library installation & connections are not simple – refer next pages.
             It looks                                                         Applications
            like this.
                                                                              • Authentication e.g.
                                                                                door access,
                                                                                attendance marking,
                                                                                cash-less payment
        6/1/2020                     RPi - Python - write-ups on IO devices                           49
   Fingerprint reader (cont.)
Connection
• You need to get a 6-pin FTDI (USB to UART) adaptor cable, as
  shown on the right.
• Connect this to the fingerprint reader as shown below.
• Then plug the FTDI cable to one of the Raspberry Pi’s USB ports.