Slide 1

Slide 1 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 1 Iwan Setiawan IoT Experiments: LED Switchingon/off

Slide 2

Slide 2 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 2 Iwan Setiawan Previous Week’s Material ● Setting up Raspberry Pi, plus remote access ● Practices on Raspberry Pi OS, e.g. CLI ● Starting to learn electronics, GPIO, observation * https://raspberrypi.org/documentation/ again √ a simple

Slide 3

Slide 3 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 3 Iwan Setiawan IoT Assignment 1 ● Raspberry Pi 4 Model B specifications – System-on-chip: Broadcom BCM2711 – Instruction set: ARM v8 – CPU, GPU: Quad-core Cortex-A72 64-bit @1.5GHz, VideoCore VI ● Useful websites – pinout.xyz: An interactive reference to the Raspberry Pi GPIO pins – resistorcolorcodecalc.com: Identifying resistor value and tolerance – gpiozero.readthedocs.io: Documentation of an easy-to-use GPIO library 500 MHz included in RasPi OS * https://raspberrypi.org/products/raspberry-pi-4-model-b/specifications/ RPi.GPIO pigpio

Slide 4

Slide 4 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 4 Iwan Setiawan Objectives ● Learn basics of electronic components+tools ● Learn how to connect electronic components to the Raspberry Pi ● Learn how to interact with the electronic compo- nents, specifically LEDs, through Python programs

Slide 5

Slide 5 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 5 Iwan Setiawan Contents ● Electronic circuits prototyping ● Python programming on Raspberry Pi with Mu editor and GPIO Zero library ● Setting up and running experiment set 1 – LED switching and controlling – Traffic lights simulation

Slide 6

Slide 6 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 6 Iwan Setiawan Sensor and Actuator ● Sensor: sensing or measuring the environment ● Actuator: acting or interacting with the environment “read” “write” Analog signal Compute Storage Digital signal Stallings, 2016 Communications

Slide 7

Slide 7 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 7 Iwan Setiawan Sensor and Actuator ● Sensor: sensing or measuring the environment ● Actuator: acting or interacting with the environment “read” “write” Analog signal Compute Storage Digital signal Stallings, 2016 Communications

Slide 8

Slide 8 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 8 Iwan Setiawan Sensor and Actuator ● Sensor: sensing or measuring the environment ● Actuator: acting or interacting with the environment “read” “write” Analog signal Compute Storage Digital signal Stallings, 2016 Communications

Slide 9

Slide 9 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 9 Iwan Setiawan Sensor and Actuator ● Sensor: sensing or measuring the environment ● Actuator: acting or interacting with the environment “read” “write” Analog signal Compute Storage Digital signal Stallings, 2016 Communications

Slide 10

Slide 10 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 10 Iwan Setiawan Electronic Components ● Resistor, electrical resistance ● Capacitor, store electrical energy or “charge” ● Photoresistor, or “light sensor” ● Light-emitting diode (LED), or “indicator” ● Buzzer, or “beeper” current limiting, etc. Piezoelectric Decreasing resistance when exposed to light

Slide 11

Slide 11 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 11 Iwan Setiawan Electronic Tools ● Breadboard for electronic prototyping ● Jumper wires/cables for interconnecting ● Fritzing for designing electronic circuits – Breadboard – Schematic – PCB * https://fritzing.org/ * http://dl.kuliax.net/acnt/iot/fritzing/ +Code

Slide 12

Slide 12 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 12 Iwan Setiawan Electronic Tools + 3V3 GND

Slide 13

Slide 13 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 13 Iwan Setiawan Electronic Tools + 3V3 GND

Slide 14

Slide 14 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 14 Iwan Setiawan Mu and GPIO Zero ● Mu, a simple Python editor ● GPIO Zero, an easy-to-use GPIO library

Slide 15

Slide 15 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 15 Iwan Setiawan General-Purpose Input/Input (GPIO) ● Interfacing between Raspberry Pi and electronic components ● Raspberry Pi 40-pin GPIO header – Command: pinout – Website: pinout.xyz

Slide 16

Slide 16 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 16 Iwan Setiawan Raspberry Pi Foundation, CC By-SA 4.0

Slide 17

Slide 17 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 17 Iwan Setiawan Organizing Your Mess ● Sort your jumper wires/cables – F/F, M/M, M/F – Light or dark colors ● Check if your cables/connections are good. Buzzer? ● Positive and negative sides, e.g. top and down ● Open your references, e.g. pinout, GPIO Zero doc ● Be careful

Slide 18

Slide 18 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 18 Iwan Setiawan Preparation ● Raspberry Pi 4 set +Ethernet cable ● Breadboard ● Jumper wires ● Resistors ● Capacitor ● LEDs ● LDR ● Button

Slide 19

Slide 19 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 19 Iwan Setiawan LED Lighting ● Build the circuit 1. Resistor and LED 2. GND pin to “-” rail 3. 3V3 pin to “+” rail ● Make sure everything is OK before connecting the “+” rail to the component rail 3V3 GND Be careful and pray first + Comes late Indicates that you need to take a picture of your work

Slide 20

Slide 20 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 20 Iwan Setiawan LED Switching on/off ● Use a jumper wire to connect GPIO 4 pin to the positive terminal of the circuit ● Open REPL shell ● Type this line one by one from gpiozero import LED led = LED(4) led.on() led.off() Importing LED module from gpiozero package See gpiozero.LED class* GPIO 4 * https://gpiozero.readthedocs.io/en/stable/api_output.html#led

Slide 21

Slide 21 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 21 Iwan Setiawan LED Flashing ● Create a new file, led_flash.py ● Type this code, save, and run from gpiozero import LED from time import sleep led = LED(4) while True: led.on() sleep(1) led.off() sleep(1) GPIO 4 * https://gpiozero.readthedocs.io/en/stable/api_output.html#led

Slide 22

Slide 22 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 22 Iwan Setiawan LED Switching on/off with Button ● Create a new file, led_sw.py ● Type this code, save, and run from gpiozero import LED, Button from signal import pause led = LED(4) button = Button(21) button.when_pressed = led.on button.when_released = led.off pause() GPIO 4 GPIO 21 * https://gpiozero.readthedocs.io/en/stable/api_input.html#button

Slide 23

Slide 23 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 23 Iwan Setiawan LED Controlling with LDR ● Create a new file, led_ldr.py ● Type this code, save, and run from gpiozero import LED, LightSensor from time import sleep led = LED(4) ldr = LightSensor(12) threshold = 0.3 while True: if ldr.value < threshold: print(“It’s dark, switch on the light!”) print(ldr.value) led.on() sleep(5) led.off() else: led.off() GPIO 4 GPIO 12 - * https://gpiozero.readthedocs.io/en/stable/api_input.html#lightsensor-ldr

Slide 24

Slide 24 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 24 Iwan Setiawan Simple Traffic Light Simulation 1 ● Create a new file, traffic_light1.py ● Type this code, save, and run from gpiozero import LED from time import sleep red = LED(4) amber = LED(12) green = LED(21) while True: red.on() sleep(10) amber.on() sleep(1) green.on() amber.off() red.off() sleep(10) green.off() amber.on() sleep(1) amber.off()

Slide 25

Slide 25 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 25 Iwan Setiawan Simple Traffic Light Simulation 2 ● Create a new file, traffic_light2.py ● Type this code, save, and run from gpiozero import TrafficLights from time import sleep from signal import pause lights = TrafficLights(4, 12, 21) def light_sequence(): while True: yield(1, 0, 0) #red sleep(10) yield(0, 1, 0) #amber sleep(1) yield(0, 0, 1) #green sleep(10) yield(0, 1, 0) #amber again sleep(1) lights.source = light_sequence() pause() * https://gpiozero.readthedocs.io/en/stable/recipes.html#traffic-lights

Slide 26

Slide 26 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 26 Iwan Setiawan IoT Assignment 2 ● Design and build a pair of traffic lights simulation for vehicles and pedestrians – Use the information, e.g. cycle time, etc., that you observed from Assignment 1 ● Cut the cycle time to ½ if necessary ● Descriptions and explanations in the report – Add a button to crossing light by giving delay first before “green”, e.g. 8 seconds – Add a buzzer to signal pedestrians when crossing the road ● Long beeping for the first half of the crossing time ● Short beeping for the last half ● Python code – Use functions for each light switch, button, and buzzer – Use conditional or control flow for button and buzzer – Use exceptions handling for keyboard interupt, i.e. Ctrl+C coordinated * https://docs.python.org/3.9/tutorial/errors.html#handling-exceptions √ Be creative

Slide 27

Slide 27 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 27 Iwan Setiawan Results to be Uploaded ● Experiment results: photos and explanations ● Assignment 2 results – Fritzing export image using Breadboard view – Photos and a demo video showing the results – Source code, screenshots/figures, and explanations Upload your experiment and assignment 2 results to Moodle ● Using one PDF file ● One group, one document ● Upload your demo video somewhere, only attach the URL to the doc. 06/03 9pm code experiment

Slide 28

Slide 28 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 28 Iwan Setiawan Resources ● Raspberry Pi Foundation, “Physical computing with Python,” 2020. [Online]. Available: https://projects.raspberrypi.org/en/projects/phys ical-computing