KEMBAR78
2computer Architecture Lab | PDF | Raspberry Pi | Arduino
0% found this document useful (0 votes)
6 views16 pages

2computer Architecture Lab

The document provides a comprehensive guide on setting up and programming the Raspberry Pi for embedded systems, covering hardware and software setup, GPIO programming, PWM, and interfacing with Arduino using the PyFirmata protocol. It includes detailed instructions for installing the Raspbian OS, configuring GPIO pins, and implementing various programming examples for controlling devices. Additionally, it discusses the use of interrupts and PWM for advanced control of peripherals.

Uploaded by

liaillust94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views16 pages

2computer Architecture Lab

The document provides a comprehensive guide on setting up and programming the Raspberry Pi for embedded systems, covering hardware and software setup, GPIO programming, PWM, and interfacing with Arduino using the PyFirmata protocol. It includes detailed instructions for installing the Raspbian OS, configuring GPIO pins, and implementing various programming examples for controlling devices. Additionally, it discusses the use of interrupts and PWM for advanced control of peripherals.

Uploaded by

liaillust94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Lab

To setup Raspberry pi board Hardware


and Software setup for Embedded system
design
Objective
To setup Raspberry pi board, its Operating system and VNC server setting.

Introduction
The Raspberry Pi is a series of small single-board computers developed Raspberry Pi
Foundation to promote the teaching of basic computer science in schools and in developing
countries. A Raspberry pi is a mini computer with IO pins so we can interface devices to
develop an embedded board.

Raspberry pi 3 Model B Image

A Raspberry pi is a mini computer that means that it is working like a computer. In


comparison with other microcontroller board like Arduino which required a computer
system to program. The Raspberry pi does not require a computer to program rather
working as a computer having all necessary devices attached with it. Include basic devices
like keyboard; mouse and monitor with more include Ethernet, WIFI, Bluetooth, speaker,
microphone, printer etc. The following image show Raspberry pi peripheral information.

Raspberry pi models
Parameter Model A Model B Computer Model Zero
Generation 1 1+ 1 1+ 2 3 1 3 1.2 1.3 W(wireless)
USB 2.0 1 2 4 1 2 1
300mA 200 mA 700 mA 600 mA 800 mA 200 mA 700 mA
Power 160mA (0.8W)
(1.5W) (1 W) (3.5 W) (3 W) (4 W) (1 W) (3.5 W)
Ethernet,
On board Wireless Wireless and
None Ethernet None
network and Bluetooth
Bluetooth
GPIO 8 17 8 17 46 17

The following are the pre install software available in the Raspbian Operating System.

VNC Viewer: For simple and secure remote access, direct or via
the cloud, free for non-commercial use.

Google Chromium lightweight version of Google Chrome


LibreOffice is a free and open source office suite, a project of
The Document Foundation.

Python 2 IDLE

Python 3 IDLE

Geany is a text editor IDE provide a small and fast IDE. It


supported programming languages and markup languages
includes C, C++, C#, Java, JavaScript, PHP, HTML, LaTeX,
CSS, Python, Perl, Ruby, Pascal, Haskell, Erlang, Vala etc
BlueJ is an integrated development environment (IDE) for the
Java programming language, developed mainly for educational
purposes, but also suitable for small-scale software development.
Scratch is a free visual programming language developed by the
MIT Media Lab. Scratch was created to help young people learn
to think creatively, reason systematically and work
collaboratively.
Mathematica is a computational programming tool used in
science, maths, computing and engineering. It is proprietary
software that you can use for free on the Raspberry Pi and comes
bundled for free with Raspbian. Mathematica is generally used
for coding projects at university level and above.

Flow-based programming for the Internet of Things

Sonic Pi is a live coding environment based on Ruby, originally


designed to support both computing and music lessons in
schools. Its Supercollider synthesis engine and accurate timing
model use for live coding and other forms of algorithmic music

LXTerminal is the standard terminal emulator of LXDE. The


terminal is a desktop-independent VTE-based terminal emulator
for LXDE without any unnecessary dependency.
PDF Viewer

MINICRAFT Raspberry pi edition game

Real-time clock
None of the current Raspberry Pi models have a built-in real-time clock, and so they don't
know the time of day. As a workaround, a program running on the Pi can get the time from
a network time server or user input at boot time, thus knowing the time while powered on.
To provide consistency of time for the filing system, the PI does automatically save the time
it has on shutdown, and re-installs that time at boot.
A real-time hardware clock with battery backup, such as the DS1307, which is fully binary
coded, may be added (often via the I²C interface).

Updating and Upgrading Raspbian operating system


First, update your system's package list by entering the following command in LXTerminal
or from the command line:
$ sudo apt-get update
Next, upgrade all your installed packages to their latest versions with the command:

$ sudo apt-get dist-upgrade


Doing this regularly will keep installation up to date, in that it will be equivalent to the latest
released image available from raspberrypi.org/downloads.
However, there are occasional changes made in the Foundation's Raspbian image that
require manual intervention, for example a newly introduced package. These are not
installed with an upgrade, as this command only updates the packages you already have
installed.

Updating the Kernel and Firmware


The kernel and firmware are installed as a Debian package, and so will also get updates
when using the procedure above. These packages are updated infrequently and after
extensive testing.
If you memory space become low on running ‘sudo apt-get dist-upgrade’, it will show how
much data will be downloaded and how much space it will take up on the SD card. It's
worth checking with df -h that you have enough disk space free, as unfortunately apt will
not do this for you. Also be aware that downloaded package files (.deb files) are kept in
/var/cache/apt/archives. You can remove these in order to free up space with

$ sudo apt-get clean


Lab
To study Raspberry pi General Purpose
Input Output (GPIO) programming
Objective
To interface devices with Raspberry pi using its GPIO pins

Introduction
The Raspberry Pi is a series of small single-board computers developed Raspberry Pi
Raspberry pi GPIO Programming. The Raspberry pi is a single board computer with a
powerful feature of GPIO (General Purpose Input Output) interfacing pins. This GPIO pins
are available in other microcontroller board like Arduino but the thing make Raspberry pi
superior is its performance to run operating system, Ethernet connection and peripherals like
keyboard, mouse etc directly attached with it. These pins are a physical interface between
the Pi and the outside world.
There are 26 pins header on Raspberry pi board out of which 17 are GPIO pins,
other power supply include two 5V pins , two 3.3V pins and five ground(0V) pins.

Reading GPIO pins of Raspberry pi


The some Raspberry Pi board is designed specifically with add-on boards which mount over
it. The terminology use for it is called ‘HAT’ (Hardware Attached on Top). A HAT is an
add-on board for Raspberry Pi that conforms to a specific set of rules that will make life
easier for users. A significant feature of HATs is the inclusion of a system that allows it to
identify a connected HAT and automatically configure the GPIOs and drivers for the board,
making life for the end user much easier.
There are two way to read the GPIO pin configuration of Raspberry Pi board
1) BCM pin layout
2) Board layout

BCM (Braodcom SoC) Pin layout


The following is the pin layout of BCM. These are the GPIO pins as the computer sees
them. The pin numbers are unarranged and difficult to read and remember it. You will need
a printed reference or a reference board that fits over the pins.

Physical (Board) pin layout


The other way to refer to the pins is by simply counting across and down from pin 1 at the
top left (nearest to the sd-card). This physical number is term as Board layout in
programming and it is recommended to use.

Output data from GPIO:


The GPIO pins are use output in bit addressable mode mean each pin can turn ON or OFF
(HIGH or LOW in computing terms). When the pin is HIGH it outputs 3.3 volts (3v3);
when the pin is LOW it is off.
Example: The following program blink the LED connect at pin 8 with delay of 1Sec
import time
from time import sleep #Import sleep for delay
import RPi.GPIO as GPIO #Import Raspberry pi Python module
GPIO.setwarnings(False) #Did`t display any GPIO warning
GPIO.setmode(GPIO.BOARD) #Follow the BOARD pinout for connection
GPIO.setup(8,GPIO.OUT) #Making GPIO pin 8 as output pin
print('This is a LED Blinking program')
print('"To stop and exit the code press Ctrl + c"')
try:
while True:
GPIO.output(8,GPIO.HIGH) #Set pin 8 to High
sleep(1)
GPIO.output(8,GPIO.LOW) #Set pin 8 to Low
sleep(1)
finally:
GPIO.cleanup() #Returning GPIO pin back to board

Getting INPUT from GPIO


All GPIO pins are set to general-purpose inputs on power-on reset. Inputs are a bit trickier
because of the way that digital devices work. Although it might seem reasonable just to
connect a button across an input pin and a ground pin, the Pi can get confused as to whether
the button is on or off. It might work properly, it might not. It's a bit like floating about in
deep space; without a reference it would be hard to tell if you were going up or down, or
even what up or down meant!
This is why you will see phrases like "pull up" and "pull down" in Raspberry Pi GPIO
tutorials. It's a way of giving the input pin a reference so it knows for certain when an input
is received.

Low side switch High Side switch Remarks


The connection is not
recommended. It may damage
IO pin because no resistor is
present to limit the current.
The resistor limit the current and
save the IO pin when button is
press but when button is not
press the IO pin detect a floating
state.

Recommended circuit
When button is not press the
input is pull to default state
(Pull-up or pull-down) to
overcome the problem of
floating state. When button is
press the current pass through
1K ohm resistor save the IO pin
from short circuit.
Example: In the following program a low side switch is connect to Raspberry pi pin 10 with
recommended setting and a LED is connect to pin number 5. When switch is pressed the
LED will ON and display message display on screen that "Switch is Press" and if switch is
not press the LED will OFF and display message on screen that "Switch is not Press"

import RPi.GPIO as GPIO


GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8,GPIO.OUT)
GPIO.setup(10,GPIO.IN)
print(‘Press Ctrl + c to exit’)
try:
while True:
if GPIO.input(10) == 0:
GPIO.output(8,GPIO.HIGH)
print("Switch is Press")
if GPIO.input(10) == 1:
GPIO.output(8,GPIO.LOW)
print("Switch is not Press")
finally:
GPIO.cleanup()
print('Program end')
Lab
To implement PWM (Pulse Width
Modulation) and Hardware Interrupt
programming in Raspberry pi
Objective
To work on PWM (Analog out) programming and Hardware Interrupt programming in
Raspberry pi

Introduction
The Raspberry pi has soft PWM generated by the Python programming. The PWM also
refer as analog out because through PWM an analog device can control like brightness of
LED, Sound of buzzer etc.

Different PWM Duty Cycle with voltage level of Raspberry pi

Example: The following program asks the user to enter Frequency and PWM values. The
program glow the LED according to the user input values.
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(5,GPIO.OUT)
LED=GPIO.PWM(6,1) #Set PWM at pin 6 at 1Hz frequency
LED.start(50) #Start PWM at 50% duty cycle
try:
while True:
freq=input("enter frequency=")
duty=input("enter duty cycle=")
LED.ChangeFrequency(int(freq))
LED.ChangeDutyCycle(int(duty))
except KeyboardInterrupt:
GPIO.cleanup()
print('Program close')
LAB
PyFirmata Protocol
Objective
To interface and control Arduino board through PC using Python programming Language.
The mode is Master Slave Mode in which PC act as Master and Arduino as Slave board

Introduction
To make Arduino board as a slave board which means to control Arduino board from
Raspberry pi or Computer the ‘Firmata’ module is use. When Firmata is uploaded on
Arduino board it took control over the pins and function of Arduino. In this lab the
‘Standard Firmata’ is use, which makes Arduino to work as slave and Raspberry pi work as
master and also Arduino pins are control by python programming.

Writing Digital Outputs on an Arduino from a PC

The following python from blink the LED with the frequency of 1 Hz

import pyfirmata
import time
board = pyfirmata.Arduino('COM4’)
led_pin = board.get_pin('d:2:o')
try:
while True:
led_pin.write(1)
time.sleep(0.5)
led_pin.write(0)
time.sleep(0.5)
except KeyboardInterrupt:
board.exit()
print(‘Program exit’)

Reading Arduino Digital Inputs Using PyFirmata

To read data from Arduino, the program is probably handy to start with an iterator thread.
Otherwise the board will keep sending data to your serial, until it overflows:

import pyfirmata
import time
board = pyfirmata.Arduino(''COM4’)
switch_pin = board.get_pin('d:4:i')
led_pin = board.get_pin('d:2:o')
it = pyfirmata.util.Iterator(board)
it.start()
switch_pin.enable_reporting()
try:
while True:
input_state = switch_pin.read()
if input_state == False:
print('Button is Pressed')
led_pin.write(1)
time.sleep(0.5)
if input_state == True:
print('Button is Not Pressed')
led_pin.write(0)
time.sleep(0.5)
except KeyboardInterrupt:
board.exit()
print(‘Program exit’)

Reading Arduino Analog Inputs Using PyFirmata

import pyfirmata
import time
board = pyfirmata.Arduino(''COM4’)
analog_pin = board.get_pin('a:0:i')
it = pyfirmata.util.Iterator(board)
it.start()
analog_pin.enable_reporting()
try:
while True:
reading = analog_pin.read()
if reading != None:
voltage = reading * 5.0
print("Reading=%f\tVoltage=%f" % (reading, voltage))
time.sleep(1)
except KeyboardInterrupt:
board.exit()
print(‘Program exit’)

Analog Outputs (PWM) with PyFirmata


import pyfirmata
board = pyfirmata.Arduino(''COM4’)
led_pin = board.get_pin('d:10:p')
try:
while True:
duty_s = raw_input("Enter Brightness (0 to 100):")
duty = int(duty_s)
led_pin.write(duty / 100.0)
except KeyboardInterrupt:
board.exit()
print(‘Program exit’)

Control Servo Motor with PyFirmata

import pyfirmata
board = pyfirmata.Arduino(''COM4’)
servo_pin = board.get_pin('d:11:s')
while True:
angle_s = raw_input("Enter Angle (0 to 180):")
angle = int(angle_s)
servo_pin.write(angle)
LAB
Arduino LED Blinking

void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever


void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

LAB
Arduino LED Blinking Interrupt coding
#define LED_BUILTIN 13
#define LED_External 2
void setup()
{
pinMode(LED_External, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;

TCNT1 = 3036; // preload timer 65536-16MHz/256/1Hz


TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enable all interrupts

Serial.begin(9600);
}
ISR(TIMER1_OVF_vect)
{
digitalWrite(LED_BUILTIN, digitalRead(LED_BUILTIN)^1);
}

void loop() {
digitalWrite(LED_External, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(LED_External, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
}

Lab
ESP32 RTOS Coding
In the following code two LED is Blinking at different frequency using RTOS coding. In
this code no Interrupt code is use and also the void loop is remain empty as coding is done
in RTOS.

// Use only core 1 for this example if CONFIG_FREERTOS_UNICORE is defined


#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else
static const BaseType_t app_cpu = 1;
#endif

// Pin definitions
static const int led_pin_1 = 2; // Example LED pin
static const int led_pin_2 = 4; // Another example LED pin

// Task function for LED 1


void toggleLED1(void *parameter) {
while(1) {
digitalWrite(led_pin_1, !digitalRead(led_pin_1)); // Toggle LED state
vTaskDelay(500 / portTICK_PERIOD_MS); // Delay for 500ms

}
}
// Task function for LED 2
void toggleLED2(void *parameter) {
while(1) {
digitalWrite(led_pin_2, !digitalRead(led_pin_2)); // Toggle LED state
vTaskDelay(300 / portTICK_PERIOD_MS); // Delay for 300ms
}
}

void setup() {
// Configure pins as outputs
pinMode(led_pin_1, OUTPUT);
pinMode(led_pin_2, OUTPUT);

// Create tasks and pin them to a core (app_cpu)


xTaskCreatePinnedToCore(

toggleLED1, // Task function


"Toggle LED1",// Name of task
1024, // Stack size (bytes)
NULL, // Parameter to pass to function
1, // Task priority (0 to configMAX_PRIORITIES - 1)
NULL, // Task handle
app_cpu // Core to run on
);

xTaskCreatePinnedToCore(
toggleLED2, // Task function
"Toggle LED2",// Name of task
1024, // Stack size (bytes)
NULL, // Parameter to pass to function
1, // Task priority (0 to configMAX_PRIORITIES - 1)
NULL, // Task handle
app_cpu // Core to run on

);
}

void loop()
{
// loop() can be left empty as tasks are managed by FreeRTOS
}

You might also like