Upgrade to Pro — share decks privately, control downloads, hide ads and more …

IoT Experiments: LED Switching

IoT Experiments: LED Switching

stwn

May 28, 2021
Tweet

More Decks by stwn

Other Decks in How-to & DIY

Transcript

  1. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 1 Iwan

    Setiawan <stwn at unsoed.ac.id> IoT Experiments: LED Switchingon/off
  2. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 2 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  3. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 3 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  4. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 4 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  5. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 5 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  6. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 6 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  7. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 7 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  8. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 8 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  9. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 9 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  10. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 10 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  11. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 11 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  12. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 12 Iwan

    Setiawan <stwn at unsoed.ac.id> Electronic Tools + 3V3 GND
  13. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 13 Iwan

    Setiawan <stwn at unsoed.ac.id> Electronic Tools + 3V3 GND
  14. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 14 Iwan

    Setiawan <stwn at unsoed.ac.id> Mu and GPIO Zero • Mu, a simple Python editor • GPIO Zero, an easy-to-use GPIO library
  15. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 15 Iwan

    Setiawan <stwn at unsoed.ac.id> General-Purpose Input/Input (GPIO) • Interfacing between Raspberry Pi and electronic components • Raspberry Pi 40-pin GPIO header – Command: pinout – Website: pinout.xyz
  16. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 16 Iwan

    Setiawan <stwn at unsoed.ac.id> Raspberry Pi Foundation, CC By-SA 4.0
  17. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 17 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  18. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 18 Iwan

    Setiawan <stwn at unsoed.ac.id> Preparation • Raspberry Pi 4 set +Ethernet cable • Breadboard • Jumper wires • Resistors • Capacitor • LEDs • LDR • Button
  19. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 19 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  20. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 20 Iwan

    Setiawan <stwn at unsoed.ac.id> 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 <Enter> led = LED(4) <Enter> led.on() <Enter> led.off() <Enter> Importing LED module from gpiozero package See gpiozero.LED class* GPIO 4 * https://gpiozero.readthedocs.io/en/stable/api_output.html#led
  21. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 21 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  22. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 22 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  23. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 23 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  24. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 24 Iwan

    Setiawan <stwn at unsoed.ac.id> 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()
  25. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 25 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  26. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 26 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  27. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 27 Iwan

    Setiawan <stwn at unsoed.ac.id> 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
  28. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 28 Iwan

    Setiawan <stwn at unsoed.ac.id> Resources • Raspberry Pi Foundation, “Physical computing with Python,” 2020. [Online]. Available: https://projects.raspberrypi.org/en/projects/phys ical-computing