First acquaintance

  • GPIO Connectors on Raspberry Pi - unlike Arduino, these are pins, not connectors. Consequently, you cannot use "male-to-male" jumpers with them, as you do with Arduino. When working with Raspberry Pi, you will need "female-to-male" jumper wires instead. One end of such a wire has a connector that fits onto the GPIO pin header of the Raspberry Pi, and the other end has a male pin that can be inserted into a breadboard. The diagram below illustrates how to connect Raspberry Pi GPIO pins to a specific row on a breadboard using "female-to-male" jumpers.
    Connecting Raspberry Pi to the breadboard
    image.png (220.37 KiB)
    Connecting Raspberry Pi to the breadboard Viewed 2893 times
    Identifying GPIO Pins on Raspberry Pi
    The GPIO pins on the Raspberry Pi board are not labeled. Therefore, to avoid having to count pins meticulously, it's a good idea to get a GPIO pinout diagram or template that can be placed over the pins. The template shown in the picture is called a Raspberry Leaf.
  • Components

    For this experiment, you will need the following components:

    Red LED
    470 Ohm 0.25W Resistor
    400-Point Breadboard
    "Male-to-Male" Jumper Wires (only for Arduino)
    "Female-to-Male" Jumper Wires (only for Raspberry Pi)

    Breadboard Layout

    The breadboard layout for our project is shown in the diagram. It is the same whether you are working with Arduino or Raspberry Pi, but the connection of the breadboard to Arduino and Raspberry Pi is different.
    The diagram shows that the current from the output pin of Arduino or Raspberry Pi should first pass through the resistor and then to the LED.

    Regardless of how the resistor is connected, the positive lead of the LED (it's slightly longer than the negative lead) should be connected to the upper of the two active rows on the breadboard. You can identify the negative lead of the LED by the flat edge on the LED's rim.
    Layout of the breadboard
    image.png (63.18 KiB)
    Layout of the breadboard Viewed 2893 times
    Connecting Raspberry Pi

    Unlike Arduino, the GPIO pins on Raspberry Pi are not labeled, making it challenging to identify which pin is which. There are two options: either refer to a GPIO pinout diagram to count the pins you need, or work with a pinout template, such as the Raspberry Leaf shown in the picture, which overlays the GPIO pins.
    Connecting breadboard for LED control with Raspberry Pi
    image.png (196.66 KiB)
    Connecting breadboard for LED control with Raspberry Pi Viewed 2893 times
    Layout of the breadboard and its connection to Raspberry Pi:
    Layout of breadboard for LED control with Raspberry Pi
    image.png (169.43 KiB)
    Layout of breadboard for LED control with Raspberry Pi Viewed 2893 times
    Raspberry Pi Program

    To program Raspberry Pi, you don't need a separate computer; you can write and run the program directly on Raspberry Pi itself. So, let's load the program onto Raspberry Pi from the on_off_control.py file in the python/experiments directory:

    Code: Select all

    import RPi.GPIO as GPIO //1
    import time //2
    
    GPIO.setmode(GPIO.BCM) // 3
    
    control_pin = 18 // 4
    GPIO.setup(control_pin, GPIO.OUT)
    
    try: // 5
        while True: // 6
            GPIO.output(control_pin, False) //7
            time.sleep(5)
            GPIO.output(control_pin, True)
            time.sleep(2)
    
    finally:
        print("Reset")
        GPIO.cleanup()
    This program is essentially a counterpart to the Arduino program. Let's go through it step by step, using the comments to break it down:

    To access the GPIO contacts of a Raspberry Pi, you will need the RPi.GPIO library in Python, which was created by Raspberry Pi enthusiast Ben Croston. So, in the first line of code, we import this library. The RPi.GPIO library comes pre-installed in all the latest versions of the standard Raspbian distribution, so you don't need to install it unless you are working with an older version of Raspbian. In the case of an older version, it's most convenient to install the library by simply updating the system, which you will probably do. To do this, run the following command in the terminal:

    $ sudo apt-get upgrade

    2. You will also need to import the time library because it provides delays for turning the LED on and off.

    3. The line GPIO.setmode(GPIO.BCM) should be included in any Python program you write to control GPIO contacts before setting the mode of contacts or using them in any way. This command informs the GPIO library that the contacts will be identified by their Broadcom names (BCM) rather than their physical positions. The RPi.GPIO library supports both naming schemes, but the Broadcom naming is more popular, and that's what we will use in this book.

    NOTE

    Please note that when programming for Raspberry Pi, we don't use separate setup() and loop() functions as we did when programming for Arduino. The definitions that would be placed in the setup() function in Arduino are simply placed closer to the beginning of the program here, and the infinite while loop plays the role that is typically assigned to the loop() function in Arduino.

    4. The variable control_pin identifies GPIO contact 18 as the one to which you will connect the LED. It is defined as an output using the GPIO.setup command.

    5. We move on to the function that is equivalent to the Arduino loop. It is enclosed in a try...finally construction. Its purpose is that if there is any error in the program, you can either simply stop the program by pressing the <Ctrl>+<C> key combination in the terminal window where it was running, or the cleanup code contained in the finally block will run.

    You can do without this code and simply use the while loop, but the cleanup code automatically resets all GPIO contacts to a safe state (input), reducing the chances of accidental short-circuits or mounting errors damaging the Raspberry Pi.

    6. The while loop has a True condition. This may seem strange, but in Python, this is how we ensure the infinite execution of some code. In reality, the program will simply continue to execute the commands in the while loop as long as you don't stop it by pressing the <Ctrl>+<C> combination or disconnect the Raspberry Pi.

    7. The code inside the loop is very similar to its Arduino counterpart. The GPIO contact has a True value (high), then there is a 5-second delay, after which the GPIO contact gets a False value (low), followed by another 2-second delay, and the loop repeats.

    Loading and Running the Program

    To access GPIO contacts in Linux, superuser privileges are required. Therefore, to run the program, navigate to the directory with the "on_off_control.py" file and execute it with the following command:

    $ sudo python on_off_control.py

    Remember that by starting the command with "sudo," you run the program as a superuser.

    When the LED blinks enough times, press the <Ctrl>+<C> key combination to exit the program.