1 of 17

EG Raspberry Pi Workshop

EG1003 | Presented by Prototyping

2 of 17

Overview

  • Raspberry Pi
  • Python
  • Ultrasonic Sensor
  • Sonic Pi Library

3 of 17

Raspberry Pi Overview

  • Credit card sized computer
  • Used for electronic projects
  • Runs on Raspberry Pi OS (a Debian-based operating system)
  • Has a CPU, GPU, and RAM
  • Used in Lab 4B-RAD Workshop

4 of 17

Raspberry Pi Hardware

  • SD Card Slot
  • USB Ports
  • Camera Module Port
  • Micro HDMI
  • USB-C Power
  • Audio Jack
  • Ethernet Port

5 of 17

Raspberry Pi GPIO Pins

  • 28 General Purpose Input/Output Pins
  • 4 Power Pins
  • 8 Ground Pins

6 of 17

GPIO Pin Code Setup

GPIO Libraries

GPIO.setmode(pin identification type)

Sets the GPIO mode; defines how pins are called (GPIO.BCM or GPIO.BOARD)

GPIO.setup(port/pin, GPIO.OUT or GPIO.IN)

Sets the specific GPIO pin for output or input

GPIO.output(port/pin, 1/HIGH/True or 0/LOW/False)

Sets an output pin to a specific state

GPIO.input(port/pin)

Reads the data from an input pin

GPIO Commands

7 of 17

Python

  • Text-based programming language
  • Specific Syntax
    • No closing mark needed after each line
    • Indentations for loops must be made
  • Comments
    • Line comments – begin with #
    • Block comments – begin and end with ’’’

8 of 17

Loops

  • While loops
    • Executed so long as the condition is true
  • For loops
    • Iterates over a set sequence
  • Break statements
    • Used to break out of a loop at any time, regardless of the condition

9 of 17

Ultrasonic Sensor

  • Used to detect distance between itself and the environment
  • Power, Trigger, Echo, Ground Pins
    • Trigger: transmits the ultrasonic wave
    • Echo: produces the ultrasonic wave

10 of 17

Ultrasonic Wiring & Code

Coding:

  • Import the distance sensor
  • Define/setup the distance sensor echo & trigger pins

from gpiozero import DistanceSensor

sensor = DistanceSensor(echo=pin, trigger=pin)

  • sensor.distance returns distance value obtained from the sensor

11 of 17

Sonic Pi

  • Code-based music creation & performance tool
  • Used to create new sounds through code

12 of 17

Sonic Pi Commands

play note[i]

Plays a note

sleep

Delay function/break

live_loop: listen do

Loops the note pattern following it, must be named

use_real_time

Uses real time data as the instrument type… plz don’t ask more about it

sync “python file path

Connects a variable to a python program

13 of 17

EG Raspberry Pi Workshop

EG1003 | Presented by Prototyping

14 of 17

Activity

Make your own Theremin using a Raspberry Pi!

15 of 17

Activity - Coding Instructions

In Python,

  • Import the sleep and distance sensor modules from time and gpiozero respectively
  • Setup the distance sensor based on which pins were wired for echo and trigger
  • Make the following a loop:
    • Print the distance read
    • Have a 1 second delay using sleep

In Sonic Pi,

  • Create a live loop function called listen do
  • Base the loop with respect to real time readings
  • Make a note equal to the message leaving the Python program
  • Play the first note from the set

16 of 17

Activity - Circuit

  • Vcc - 5v
  • Trig - GPIO 4
  • Echo - GPIO 17
  • Gnd - Ground

17 of 17

Solution