GPIO: Blinklicht

Write a Python program that uses one of the Raspberry GPIO pins to blink an LED at a given frequency. The program will be invoked like so:

$ path/to/repo/root/exercises/lastname.firstname/blink 25 0.5

Commandline

The command takes two positional parameters,

  1. GPIO number

  2. Frequency, in seconds. The number can contain decimal points, and so can represent fractions of a second.

Use the argparse module to do the commandline stuff

sysfs GPIO

The program uses sysfs GPIO, and not RPi.GPIO.

Note

  • RPi.GPIO is Raspberry specific. This means it won’t work on any other Linux than the Raspberry’s.

  • RPi.GPIO is Python specific. One cannot easily port prototype code to, say, C++.

  • RPi.GPIO is horrible; it accesses hardware registers directly.

  • Kernel documentation

  • Youtube tutorial (one of many). This shows how to uses sysfs GPIO in the shell. Note that we are writing a Python program, though.

  • Distilled, what we want to do (in Python) is this …

    $ cd /sys/class/gpio
    $ echo 25 > export
    $ cat gpio25/direction
    in
    $ echo out > gpio25/direction
    $ cat gpio25/value
    0
    $ echo 1 > gpio25/value    # see if it works
    $  # now for real
    $ i=0
    $ while true; do
    >   echo $((i%2)) > gpio25/value
    >   i=$((i+1))
    >   sleep 0.5
    > done
    
  • The new kid, libgpiod solves many of the problems that Sysfs GPIO has. Will switch to that when time is right (after we learned what encapsulation is).

Electronic Aspect

External Circuitry

Well, this is embarassing …

../../../../../../_images/gpio25led.png

Raspberry Header Pinout

../../../../../../_images/GPIO-Pinout-Diagram-21.png

Implementation Notes

  • After exporting the GPIO (echo 25 > export), the directory gpio25 and the files it contains are not immediately visible. Rather, the operation completes asynchronously. This means that you have to wait between the export and the access. 1/10 second should be sufficient.

    You do this using time.sleep(). Read the documentation of it.

  • A starting point, regarding the “Use argparse for commandline parsing” can be found here.

  • Write the program locally, on your PC, using VS Code (or whatever is your favorite editor).

    • WinSCP. You can use WinSCP to transfer the program to the Raspberry. Be aware though that the execute permission might not be transferred correctly - Windows and Unix are different.

    • VS Code, There is also a VS Code extension, Remote SSH, that you might want to try.

Dependencies

cluster_python Python Programming: From Absolute Beginner to Advanced Productivity cluster_python_basics Python: The Language Fundamentals cluster_python_misc Python: Miscellaneous Topics cluster_ece19 ECE19: Exercises and Custom Topics python_basics_python_0140_variables Variables python_basics_python_0130_syntax_etc Syntax etc. python_basics_python_0140_variables->python_basics_python_0130_syntax_etc python_basics_python_0250_refs_flat_deep_copy References, (Im)mutability python_basics_python_0250_refs_flat_deep_copy->python_basics_python_0140_variables python_basics_python_0150_datatypes_overview Datatypes python_basics_python_0250_refs_flat_deep_copy->python_basics_python_0150_datatypes_overview python_basics_python_0150_datatypes_overview_compound Compound Datatypes python_basics_python_0250_refs_flat_deep_copy->python_basics_python_0150_datatypes_overview_compound python_basics_python_0150_datatypes_overview->python_basics_python_0140_variables python_basics_python_0160_boolean Boolean python_basics_python_0160_boolean->python_basics_python_0150_datatypes_overview python_basics_python_0110_blahblah Blahblah python_basics_python_0170_if The if Statement python_basics_python_0170_if->python_basics_python_0160_boolean python_basics_python_0500_files File I/O python_basics_python_0220_for for Loops python_basics_python_0500_files->python_basics_python_0220_for python_misc_encoding Encoding python_basics_python_0500_files->python_misc_encoding python_basics_python_0320_strings_methods Miscellaneous String Methods python_basics_python_0300_strings More About Strings python_basics_python_0320_strings_methods->python_basics_python_0300_strings python_basics_python_0193_while while Loops python_basics_python_0193_while->python_basics_python_0160_boolean python_basics_python_0193_while->python_basics_python_0170_if python_basics_python_0300_strings->python_basics_python_0250_refs_flat_deep_copy python_basics_python_0300_strings->python_basics_python_0150_datatypes_overview python_basics_python_0200_sequential_types Sequential Datatypes python_basics_python_0300_strings->python_basics_python_0200_sequential_types python_basics_python_0120_helloworld Hello World python_basics_python_0120_helloworld->python_basics_python_0110_blahblah python_basics_python_0220_for->python_basics_python_0193_while python_basics_python_0220_for->python_basics_python_0200_sequential_types python_basics_python_0200_sequential_types->python_basics_python_0150_datatypes_overview_compound python_basics_python_0130_syntax_etc->python_basics_python_0120_helloworld python_basics_python_0150_datatypes_overview_compound->python_basics_python_0150_datatypes_overview python_misc_encoding->python_basics_python_0150_datatypes_overview python_misc_encoding->python_basics_python_0320_strings_methods ece19_blinklicht GPIO: Blinklicht ece19_file_read Reading a File ece19_blinklicht->ece19_file_read ece19_file_read->python_basics_python_0500_files