Revisiting Basics

Using a Raspberry Pi 4

A blinking LED is the Hello World for physical computing.  Raspberry OS allows many ways to do this.  Here are 3 ways to Blink an LED from the RPi 4.

Figure 1: Blinking LED wiring on RPi 4 (from Tutorial 3 below)

(gpiozero) Blinking LED Raspberry Pi Tutorial 1

This tutorial is great not just for blinking LED but also intro to "Physical Computing":
  • Getting started with Breadboard.
  • Resistor labeling code diagram included.
  • Using a multimeter to test power provided by the RPi
  • Understanding basic circuit first not controlled, then controlled by the RPi.
  • Using Python from a command window, testing basics 1 line at a time.
  • Intro to import gpiozero, a pre-installed on RPi library with import for basic needs like LEDs and buttons.
from gpiozero import LED
import time

led = LED(17)  # GPIO17
led.on

while True:
    led.toggle()
    time.sleep(1)

    
(GPIO) Blinking LED Raspberry Pi Tutorial 2
Alternative to gpiozero, whose interface is more basic and similar to Arduino is GPIO.
Here is the Python code using GPIO to blink an LED:

# blinking LED code example is from 
#    Module 4 of Coursera IOT course: The RPi Platform and Python Pgmg for the RPi.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)  # uses GPIO numbering
GPIO.setup(17,GPIO.OUT)  # GPIO17 = pin 11
while True:
GPIO.output(17,True)
time.sleep(1)
GPIO.output(17, False)
time.sleep(1)

OR use physical pin #s 

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)  # uses physical pin numbering
GPIO.setup(11,GPIO.OUT)  # pin 11 = GPIO17
while True:
GPIO.output(17,True)
time.sleep(1)
GPIO.output(17, False)
time.sleep(1)


Scratch 3 is the MIT block puzzle language used as a first programming language for children. This tutorial covers adding the extension to RPi's scratch 3 and coding the blinking LED, plus several challenges. 

Comments

Popular posts from this blog