Page 1 of 1

Raspberry PI Programming Guide

Posted: 07 Oct 2023, 04:21
by Oleg
To learn programming, it's best to start by trying to modify some simple programs and gradually understand how they work. All the programs you'll need for this book are available for download, so you can work on them even without programming experience. However, it's useful to have an idea of how things are structured.

Hello, World

Typically, the first program you write in a new language simply displays the words "Hello, world" on the screen. To get acquainted with it, open the nano text editor and enter the following command:


$ nano hello.py


The ".py" extension indicates that the file contains a Python program. Next, enter the following text into the nano editor and save the file:


print('Hello, World')


Then run the program:


$ python hello.py
Hello, World



Indentation and Whitespace

Programmers are accustomed to formatting code in a specific way to make it more readable. In the C language for Arduino, code blocks inside functions or statements are written with indentation to show which code belongs to which function or command. In Python, this formatting style isn't just a matter of preference; it's a requirement.

In Python, there are no curly braces { or } to indicate the beginning and end of a code block. Instead, indentation is used to show which lines of code are grouped together.

Consider this code example:


while True:
GPIO.output(control_pin, False)
time.sleep(5)
GPIO.output(control_pin, True)
time.sleep(2)
print("Complete")



This is a `while` loop (similar in structure to a loop in the C language for Arduino, as described in the "Arduino Programming Guide" forum). In this case, the condition is always `True`, so the loop runs indefinitely. At the end of the first line, there is a colon (:), indicating that a code block follows. In Python, the colon is analogous to { in C for Arduino, except that there's no closing symbol.

Lines within a code block should be indented relative to the previous line. As long as you stick to the formatting, the specific number of spaces between lines of code doesn't matter. Most programmers use four spaces for each level of indentation.

At the end of a code block, no special symbol is used; you simply stop indenting. Notice that in the provided code example, the last `print` command is outside the loop. Since the `while` loop is infinite, the last line will never be executed.

Variables

Variables in Python are similar to variables in C for Arduino but differ in that you don't need to specify whether a variable is of type `int`, `float`, or any other type the first time you use it. You simply assign a value to the variable, and nothing (except common sense) stops you from assigning an integer value to a variable one moment and a string value the next. For example, the following code is valid but not recommended:


a = 123.45

a = "message"



When working with strings in Python, you can use either double quotes (as in the example) or single quotes.

Re: Raspberry PI Programming Guide

Posted: 07 Oct 2023, 04:26
by Oleg
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 2497 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.