- Enabling the GPIOs to work in the I2C and serial mode
- Setting up the Raspberry Pi to program using python
- Blinking an LED using a python code
For this project, you will need
- Raspberry Pi 3 [RPi3] (quantity 1 no.)
- 2-pin LED (quantity: 1 no.)
- 100Ω resistor (quantity: 1 no.)
- Micro SD card (quantity: 1 no.)
- Power adapter (quantity: 1 no.)
- Micro USB cable A to B (quantity: 1 no.)
- VNC Viewer software on your computer
Raspberry pi is a credit card sized single board computer. This makes it different from programmable boards like Arduino and NodeMCU. Programmable boards are microcontrollers with firmware which help us write codes on an external computer and upload them to the board. This is what we do on the Arduino Uno and NodeMCU. They can only run one code at a time and are relatively slow due to their limited processing and storage capabilities.
A Raspberry Pi (RPi) is a low power yet full scale computer in a small size board. It has a microprocessor and can run a large scale operating system(OS) like Linux. It also doesn’t have dedicated pins like the programmable boards. Instead it has General Purpose Input Output (GPIO) pins which can be used for almost any purpose. Also, since it has an OS running on it, it can run multiple programs simultaneously.
This piece of hardware revolutionized the opensource world. Hobbyists and engineers started building things like home automation to servers, bitcoin mining rigs etc.
Due to all these features, Raspberry Pi is a very powerful board and expands the possible IoT projects which can be done.
The board comes with a HDMI port and USB ports meant to be used for a HDMI monitor and USB mouse and keyboard. This is the normal way to use a Raspberry Pi. However, it becomes difficult and expensive to have a monitor, mouse and keyboard every time we need to use the Raspberry Pi.
For this reason, it is possible to use the Raspberry Pi without monitor and keyboard. This is conveniently called the “Headless” use of Raspberry Pi.
Linux Terminal and SSH
The most commonly used OS for the Raspberry Pi is Raspbian which is a conventional Linux based OS. Since it is Linux based, a Terminal (similar to Command Prompt on Windows) can be used to execute commands on it. During normal use, Terminal can be opened from the graphical desktop of the RPi visible on the HDMI monitor
However, if we choose to use the RPi as Headless, we can obtain a terminal with which we can send commands to the RPi remotely. The easiest way to do this will be by connecting the RPi to the Local Area Network (LAN) either via Ethernet or WiFi and then connecting to it from another computer.
The most common way to connect to it is by using a protocol called SSH. SSH stands for Secure Shell. By obtaining the IP address of the RPi on the local area network, we can start SSH communication with the RPi. This will give us a remote terminal on the other computer, which we can use as we would use the terminal on the RPi.
Using your terminal
If you have never used a Linux system before, navigating around the system may be difficult for you. Let’s explore the fundamentals.
- The command line.
When you connect to a remote terminal using SSH, the terminal gives you a command line. Your terminal line should be as follows
This means that you are logged in with the username pi on the raspberry pi. The “$” symbol indicates that it is a command line. You will be typing all Linux commands on such a line.
This command is used to change the current working directory. Usually, when you connect to a remote terminal using SSH, the current directory is home by default. This is denoted by the “~” symbol on the command line (see image above). If you need to navigate to the Codebase directory inside the Documents directory, you will type the following command.
The current working directory will then be changed to the specified path. This can be seen on the command line.
If you want to navigate to any folder inside Codebase, say the Image_processing folder, you need not type the entire path since Codebase is already your current working directory. The following command is enough
To be able to write codes on the Raspberry Pi, you need a text editor. Nano is a text editor that works on the Terminal. To type a new code, you can type the following command.
If the file test.py exists, it will open the file in the text editor. If the file “test.py” didn’t exist before, it will create a new blank python file called test.py and then open the editor so that you can type your code.
Once you type your code, you save the file by doing Ctrl+O (Write Out). It will ask to confirm the filename at the bottom of the screen. Hit Enter to confirm. Then use Ctrl+X (Exit) to close the nano text editor.
Note: Do not use Ctrl+Z to try to undo a piece of typed code. In Linux Systems, using Ctrl+Z will suspend the running action, which essentially means that your text editor will close and your changes will not be saved.
Your regular Copy and Paste using Ctrl+C and Ctrl+V will not work on the SSH terminal. If you need to paste text from Windows,
Step 1 : Copy the text from Windows by selecting it and using Ctrl+C.
Step 2 : Open your SSH terminal and make sure the green cursor is placed wherever you want the text to be pasted.
Step 3 : Right click on the terminal window to paste the copied text.
Let us dive into the hardware of Raspberry Pi by enabling the required hardware interfaces.
Enabling the required hardware interfaces for Raspberry Pi
We can do this by two methods – using ‘Raspi-config’ on the command line or by using options in the menu itself.
Method 1: Using ‘Raspi-config’ on the command Line
Step 1: From the command line or terminal window, start by running the following command.
Step 2: This will launch the raspi-config utility. Select Interfacing Options.
Step 3: Highlight the SPI option and activate <Select>.
Step 4: Select and activate <Yes>.
Step 5: Highlight and activate <Ok>.
The Raspberry Pi will reboot and the interface will be enabled.
Method 2: Using “Raspberry Pi Configuration”
If your Pi boots to the desktop, you can either open a Terminal window and use Method 2 or use the graphical tool “Raspberry Pi Configuration”.
Step 1: Navigate to Menu > Preferences > Raspberry Pi Configuration.
Step 2: Select the Interfaces tab and set SPI to ‘Enabled’.
Step 3: Click the OK button. If prompted to reboot select Yes, so that the changes will take effect.
The Raspberry Pi will reboot and the SPI interface will be enabled.
We can enable I2C, camera, VNC etc. similarly.
Note: Unlike the NodeMCU that has only 3.3V tolerant GPIO pins whereas the RPI’s GPIO pins are 3.3V and 5V tolerant.
Note: The GPIO.BOARD option specifies that you are referring to the pins by the number of the pin.
The GPIO.BCM option means that you are referring to the pins by the “Broadcom SOC channel” number, these are the labels on the rectangle box on the pins in the diagram shown above (like: “GPIO2”).
For example, if you are using pin 7 on the RPi, when using GPIO.BOARD option, you will be referring to the pin as 7 in the code. However, if you are using the GPIO.BCM option, you have to refer to the pin as 4 in the code since pin 7 is GPIO4 by the BCM standard. To know the GPIO number for the pin you want to control, you will have to refer the pinout given above.
Interfacing the LED with Raspberry Pi
The 2-pin LED is connected to the Raspberry Pi using a breadboard, 100Ω resistor and jumper wires. The circuit is as below.
Coding in Python on Raspberry Pi
In order to execute this project, we will use the following commands to code on the RPi.
Here BCM corresponds to Broadcom chip which is SoC.
The above command disables all warning messages.
The above command will set the particular GPIO as Output. Here GPIO18.
The above command will turn ON the LED
This command will create a delay of 1 second.
The above command will turn OFF the LED.
Open the terminal and type the following command
sudo nano blinky.py
Here blinky is the filename.
This will open a window which will allow you to write your code. For LED blinking, we need to import RPi’s GPIO, and to give some delay in ON and OFF, we need to import the time.
Our final code will look like this.
To copy the code, right click on view raw at the bottom of the code, click on open link in new tab and then copy the code.
We run the code by typing the following command in the terminal. This will blink the LED.
There You Go