Raspberry Pi
Learn Raspberry Pi Programming and Linux in just 10 minutes!
Would you like to learn programming and creating fun and crazy projects, in this article you will learn how to program the Raspberry Pi and using the Linux OS step by step and in no time, after reading this article you will be able to create your own IoT projects like a home surveillance system, a weather station, or to turn your Pi into a home media center.
What is Raspberry Pi?
● Raspberry Pi was developed to encourage children that want to learn to compute. Raspberry Pi is one of the most popular platforms in the System on chip (SoC) devices market, thanks to the rapid development and the low cost of the Raspberry Pi which is starting from just $5 for the raspberry pi zero model. In 2015 more than five million Raspberry Pi board were sold. The Raspberry Pi boards are very complex to be used by the audience, but the ability of the Raspberry Pi to run embedded Linux that makes the platform powerful and accessible. Using Linux on embedded systems make the development very easy especially if we develop applications for smart things, the Internet of Things (IoT), robotics, smart cities and cyber-physical systems. Thanks to the integration between Linux software and electronics represents a paradigm shift in the development of embedded systems. You can use the Raspberry Pi not only in embedded systems development but also as a general purpose computer.
● As we said before that the Raspberry PI can be used as a general purpose computing device, because of that reason it may use to introduce computer programming for its users, but most of the developers use it as an embedded Linux platform.
●The most of Raspberry Pi models have the following features:
- Low-cost, start from $5 to $35
- Contain a powerful 1.2 GHz ARM Cortex – A53 processor which can process more than 700 million instructions per second
- The Raspberry Pi has many models for different that are suitable for different applications
- They save a lot of power, run at 0.5W and 5.5W
- If you need any support to build a project, you can easily find a solution thanks to the huge community of innovators
● The Raspberry Pi can run Linux operating system so you can install open source libraries and many applications directly with it
● Another feature which is called Hardware Attached on Top (HATs)
This actually an impressive feature because you can extend the Raspberry Pi functionality using HAT, that connects to the GPIO header you can design your own HATs and attach them to your Raspberry Pi header.
● If you want to learn electronics, programming, and Linux operating system, you should use the Raspberry Pi platform especially in IoT applications and robotics
● The Raspberry PI is better than other embedded Linux devices and more traditional embedded systems, like the Arduino, AVR and PIC microcontrollers, is when you use Linux operating system for your project, for example, if we develop a smart home system using the Raspberry Pi and you want to make information on the internet, you can use and install the Nginx web server. After that, you can use a server-side language like PHP, Python, and Perl or you can use any programming language you may prefer. Also, you may want to the remote shell access; you could install a Secure Shell without any effort by using the command sudo apt install sshd this save your time
● On Linux operating system you will find device driver support for many USB peripherals that make the installation of any USB device so easy like camera, Wi-Fi adapters and many more, instead of complex software driver development
● The Raspberry Pi is a device for playing HD videos because it has Broadcom BCM2835/6/7 processor for multimedia applications, and also it a has a hardware implementation of H.264 MIPG-4 and MPG-2/VC-1 decoders and encoders
● If you are going to develop applications for a real-time system then the Raspberry Pi will not be a good choice, for example you want to a sensor to sense some values every on millions of a second, it will be not easy to interrupt the system, but you can interconnect with real-time microcontrollers to the Raspberry Pi through the buses like UART,12C, and Ethernet
Raspberry Pi Hardware
The heart of every Raspberry Pi board is the Broadcom BCM2835, BCM2836, and BCM2837 system on chip (SoC). Raspberry Pi models are available for example (the Raspberry Pi A+, B+, 2, 3 and Zero), but I recommend to purchase the Raspberry Pi 3 because it has a multicore processor
Raspberry Pi Versions
● If you want to use the Raspberry Pi as a general purpose computer, you should consider the Raspberry Pi 3. The 1 GB of memory and 1.2 GHz processor provide the best performance out of all the boards
● for applications that interface electronics to the Internet on a network, use the Raspberry Pi 3 2 or Raspberry Pi B+
● if need you small board with wireless capability, in that case, the best choice will be the Raspberry Pi Zero
Now let’s take a closer look at the Raspberry hardware
1. Processor: The Raspberry Pi uses the Broadcom BCM2835/BCM2836/BCM2837 Processor.
2. Memory: The amount of system memory affects performance and the use of the Raspberry Pi as a general purpose computer. Memory is shared between the CPU and GPU (256 MB to 1GB DDR)
3. Storage: The Raspberry PI boards all boot from a micro SD or SD card, with the exception of the compute module. It has an on-board eMMC, which effectively an SD card on a chip. The Raspberry PI 3 uses a friction-fit slot, rather than a click in/clicks out slot
4. Power: a 5v supply is required that should ideally deliver a current of at least 1.1A and ideally 2.5A for the Raspberry Pi 3. Be careful not to continue the USB hub and USB power inputs on the Raspberry Pi zero
5. Video Out: used to connect the Raspberry Pi boards to a monitor or television. The Raspberry Pi models support 14 output resolutions, including full-HD (1920 x 1080) and 1920 x 1200
6. GPIOs: 40 Pins that are multiplexed to provide access to the following features (2x I2C, SPI bus, UART, PWM, GPCLIK)
7. USB: There is an internal USB hub on Raspberry Pi models with varying numbers of inputs
8. Reset: can be used as a reset button for the Raspberry Pi
9. Audio and video: This provides composite video and stereo audio on the Raspberry PI
10. Power LED: indicates that the board is powered
11. Activity LED: indicates that there is activity on the board
12. USB to Ethernet: This IC provides a USB 2.0 hub and a 10/100 Ethernet controller
13. Network: 10/100 Mbps Ethernet via a RJ45 connector
14. Camera: The Raspberry Pi has a mobile industry processor interface camera serial interface, a 15-pin connector that can be connected to a special purpose camera
15. Display: The Display Serial Interface is an interface that typically used by mobile phone vendors to interface a screen display
Setup the Raspberry Pi
Linux operating system has many distributions or versions of Linux that has its tools and software programs. There are many different Linux versions, such as Debian, Red Hat or OpenSUSE, they mainly used on servers, but versions like Ubuntu, Fedora or Linux Mint used for desktop users, but you should keep in mind that all have the same Linux kernel that created by Linus Torvalds in 1991
For embedded system we will choose a distribution based on the following:
● The stability of the distribution
● The package manger
● The good community support for the used device
● The device drivers support
Linux for the Raspberry Pi
As we said before that every Linux version has its own tools and configurations that result in quite a different user experience, but the main open-source Linux version used on the Raspberry Pi board include Raspbian, Arch Linux and Ubuntu
Raspbian is a version of Debian; there are three versions of Raspbian on the Raspberry Pi website
● Raspbian Jessie: An image based on Debian version 8.x
● Raspbian Jessie Lite: A minimal image based on Debian Jessie, but with limited desktop support
● Raspbian Wheezy: an older image based on Debian version 7.x
● Ubuntu distro is very close to Debian as described on the Ubuntu website “Debian is the rock upon which Ubuntu is built””
● Ubuntu is one of the most popular distributions because it has excellent desktop driver support, easy to install ad more accessible to new users
● Arch Linux is a lightweight Linux version that targeting competent Linux users. Prebuilt versions of the Arch Linux distribution are available for the raspberry Pi, but it has less support for new Linux users that use the Raspberry Pi platform
● The Raspberry Pi foundation developed a Linux installer called NOOBS. Which contains Raspbian but provides ease of download and installation of other Linux distributions
Now let’s create a Linux SD card image for the Raspberry PI
● If you want to setup an SD card to boot the Raspberry Pi, just download a Linux distribution image file form www.raspberrypi.org/downloads and write it to an SD card using any image writer
Connect to a Network
There are two ways to connect the Raspberry Pi to the network using regular Ethernet or using an Ethernet crossover cable.
Advantages
|
Disadvantages
|
You will have control over ip address setting
|
You will need administrative control
|
You can connect many boards
|
You will need a source power for the Raspberry Pi over Ethernet
|
The Raspberry Pi can connect to the internet without a desktop computer
|
The setup is more complex for beginners
|
● The first thing you should do is to find your Raspberry Pi on the network. By default, the Raspberry Pi request a Dynamic Host Configuration Protocol (DHCP) IP address. This service provided by DHCP server that run on the integrated modem – router –LAN
You can use any of the following methods to get the Raspberry Pi dynamic IP address:
● using a web browser: write 192.168.1.1, 192.168.0.1 or 10.0.0.1. log in and look under a menu such as Status for the DHCP Table. You should see an entry with details the IP address, the MAC address, and the lease time remaining for a device with hostname Raspberry Pi
● using a port scanning tool: Use a tool such as nmap under Linux or tool like Zenmap GUI version, available for Windows. You will search for an entry has an open port 22 for SSH. It identifies the range of MAC addresses to the foundation. You can ping to test the network connection
Let’s use the other type which is Ethernet crossover cable
Advantages
|
Disadvantages
|
In case you don’t have access to network , you can still connect the Raspberry Pi
|
When you desktop machine has only one network adapter, you will lose the access to the Internet
|
Raspberry Pi can have internet access if you have two network adapters and sharing is enabled
|
Raspberry Pi will need a source of power
|
You will have a stable network setup
|
You may need a specialized crossover cable
|
Here are the steps when you use windows operating system
- Plug one end of the cable into the Raspberry Pi and other end into the laptop socket
- Turn on the Raspberry Pi by attaching the micro-USB power supply
- Open control panel choose Network Connections then select two network adapters (wired and wireless) at the same time right click and choose bridge connection
- Restart the Raspberry Pi , You can use a USB to TTL serial cable to do this, or use the reset button directly, then your Raspberry Pi will get an IP address from the DHCP server
Basic Linux Commands
Command
|
Description
|
More/etc/issue
|
Returns the Linux Version
|
pp –p $$
|
Returns the shell you are suing (like bash)
|
Whoami
|
Returns who you are logged in as
|
Uptime
|
Returns how long the system has been running
|
Top
|
List all of the processes and programs executing
|
File system Commands
Name
|
Command
|
Information
|
Example
|
List files
|
Ls
|
Show all files
|
ls –alh
|
Current directory
|
Pwd
|
Show the working directory
|
pwd –p
|
Change directory
|
Cd
|
Change directory
|
cd /
|
Make a directory
|
Mkdir
|
Cerate a directory
|
mkdir new
|
Delete directory
|
Rm
|
Delete directory
|
rm new
|
Copy a directory
|
Cp
|
Recursive copy
|
cp new new2
|
Create an empty file
|
Touch
|
Create an empty file
|
touch f.txt
|
Get the calendar
|
Cal
|
Display the calendar
|
cal 7 2017
|
Now let’s program the RPi
● Any programming language available on Linux operating system will be also available on the Raspberry Pi, then you can choose the suitable language depend on the kind of the application
Languages on Raspberry Pi
● Interpreted: the source code won’t be translated directly to machine code, but the interpreter will read your code and then executes it line by line.
● Compiled: the compiler will translate the language directly to the machine code (0s and 1s)
Example: driving an LED with Raspberry Pi pins using transistor (wiring)
There are many types of languages
● Python: it’s a great and very easy language to learn and to use it for scripting, and support object-oriented features
● Bash: Good choice for short tasks and you don’t need advanced programming structures
● Perl: You can use this language for text or process data. It allows you to write code in object-oriented paradigm
● Lua: this scripting language used a lot with embedded application it is lightweight language and supports object-oriented programing style
Example: drive the LED using Lua
local LED4_PIN = “sys/class/IO/IO4”
local SYSFS_DIR = “sys/class/IO/”
local LED_Num = “4”
function writeIO(dir, filen, val)
file = IO.open(dir..filen,”w”)
file:write(val)
file:close()
end
print(“Driving the LED”)
ifarg[1] == nil then
print(“you should enter a command”)
print(“ usage is: command”)
print(“1 -> on or 0-> off”)
do return en
end
if arg[1] == “off” then
print(“The LED is on”)
wirteIO(“LED4_PIN”, “val”, “1”)
elseif arg[1] == “configure “then
print(“the LED is off”)
WirteIO(LED4_PIN, “val”, “0”)
Elesif arg[1] == “configure”
Print(“configure the IO”)
WriteIO(SYSFS_DIR, “xport”, LED_NUM)
Os.execute()
WriteIO(LED4_PIN,”DIR”,”out”)
Elseif arg[1]==”sta”then
Print(“turn IO off”
Print(“find the LED sta”)
File=io.open(LED4_PIN..”val”,”r”)
File:close()
Else
Print(“please insert a valid command”)
End
Print(“the end”)
Example: drive the LED using Python
Import sys
From time import sleep
LED4_PIN = “/sys/class/IO/IO4”
SYS_DIR = “/sys/class/IO”
LED_NUM = “4”
def wLED(fname, val, PIN = LED4_PIN)
“This function to set the value on the file”
Fileo = open(PIN + fname,”w”)
Fileo.write(val)
Fileo.close()
Return
Print(“start the script”)
If len(sys.argv) !=4
Print(“incorrect argument”)
Sys.exit(4)
If.argv[1]==”on”
Print(“the LED is on”)
wLED(fname=”val”, val=”1”)
elif sys.argv[1] ==”turn off”
print(“The LED is off”)
wLED(fname=”val”, val=”0”)
elif sys.argv[1]==”configure”:
print(“configure the IO”)
wLED(fname=”xport”, val=”LED_NUM”, PIN=SYS_DIR)
sleep(0.1)
wLED(fname=”DIR”, val=”out”)
eleif sys.argv[1] == “close”
print(“The IO I off”)
wLED(fname=”unexport”, val=LED_NUM, PIN=SYS_DIR)
eleif sys.argv[1]==”state”
print(“the LED state”)
fileo = open(LED4_PIN + “val”, “r”)
print(fileo.read())
fileo.close()
else
print(“please enter a valid command”)
print(“end of the script”)
Now you have a good understanding of how to use and program the raspberry pi, so you can just search for ideas and you will find thousands of them thanks to the huge community on the internet.
Comments
Post a Comment