Raspberry PI Programming Guide

  • if, while, and More

    Python has an if...else structure, similar to C on Arduino, but code blocks inside the if statement are indicated using colons and indentation:

    Code: Select all

    if x > 10:
        print("x is greater!")
    else:
        print("x is smaller")

    The basic principles of these instructions are similar to those in C for Arduino.

    RPi.GPIO Library

    Just like in Arduino C, Python supports libraries that you need to import and can then use in your program. In most cases, you'll need to work with the GPIO (General Purpose Input/Output) pins on the Raspberry Pi. The most popular Python library for controlling GPIO pins, which is used in this book, is called RPi.GPIO. This library comes pre-installed on Raspbian, so you don't need to install it; you can simply import it into your program and start using it.

    There is a slight confusion when it comes to naming the pins on the GPIO header of the Raspberry Pi. Sometimes, the pins are referred to by their physical position on the header (e.g., 1, 2, 3, 4, etc.), and sometimes by their function. In the early days of the Raspberry Pi, both naming conventions were fairly common. Today, most users prefer to use the functional names. The RPi.GPIO library supports both pin naming conventions, but you need to specify which one you want to use. You will typically see the following code at the beginning of most Python programs in this book:


    import RPi.GPIO as GPIO
    GPIO.setmode(GPIO.BCM)



    The first line imports the RPi.GPIO library, and the second line sets the pin numbering mode to BCM (Broadcom SOC Channel). Unlike Arduino, the Raspberry Pi doesn't have analog inputs. However, the RPi.GPIO library supports PWM (Pulse Width Modulation) pins, which can simulate analog outputs.

    GPIO Pinout

    The diagram shows various ways to connect to the GPIO header. If you have an older model of the Raspberry Pi with only 26 pins, you won't have the additional pins shown below the dashed line. Most projects can be carried out on any model of Raspberry Pi, so we'll work with the first 26 pins.
    GPIO pad
    image.png (74.07 KiB)
    GPIO pad Viewed 2495 times
    Unlike Arduino, the Raspberry Pi's PCB (Printed Circuit Board) does not have pin labels. Therefore, it can be challenging to determine which pin is which. To make things easier, you can buy or create a pinout diagram that can be placed over the GPIO pins, such as the Raspberry Leaf (https://www.adafruit.com/products/2196). Note that some pins have both a numerical designation (e.g., 2) and a functional designation (e.g., SDA). When referencing GPIO pins in your code, you can use either the number or the function name.

    Digital Outputs

    The following example demonstrates how to configure pin 18 on the GPIO header as a digital output and set it to a high logic level:

    Code: Select all

    import RPi.GPIO as GPIO
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(18, GPIO.OUT)
    GPIO.output(18, True)

    When we set an output using the `GPIO.output` command, we can set it to a high logic level by using `True` or `1`, or to a low logic level using `False` or `0`.

    Digital Inputs

    Digital inputs on the Raspberry Pi are functionally similar to digital inputs on Arduino:

    Code: Select all

    GPIO.setup(18, GPIO.IN)
    value = GPIO.input(18)
    print(value)
    You can specify that a pull-up resistor is enabled on the input pin like this:


    GPIO.setup(switch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)


    Analog Outputs

    Working with analog outputs on the Raspberry Pi involves two steps. First, you need to set the pin as an output, and then you need to define the PWM channel that will use this output.