0% found this document useful (0 votes)
36 views8 pages

2019 MANUAL Raspberry Pi v.2

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Raspberry Pi Manual and Labs

Manual written by Lauren Cugliotta


Labs originally written by Daniel Esteves, Updated by Lauren Cugliotta

http://www.raspberrypi.org/
Setting up your Raspberry Pi
Equipment:

In order to use your Raspberry Pi, you will need the following equipment:

1. A DVI to HDMI cable


2. A MicroSD card with Raspbian image loaded on it
3. A mouse and keyboard
4. A microUSB power supply
5. A Raspberry Pi kit

Please let your TA know if you do not have any of the above equipment.

Hooking up your Raspberry Pi:

A Raspberry Pi is a computer complete with a desktop. In order to use it:

1. Insert the SD card loaded with Raspbian into the port on the bottom of the pi.
2. Connect the mouse and keyboard to the USB ports.
3. Connect the HDMI end of the cable to your pi, and the DVI end to a monitor at your station.
4. Plug in your power supply into the Raspberry Pi
a. Note: It is recommended that you plug into the wall to prevent a lack of power.

Model by Fritzing
Setting up internet on your Raspberry Pi:

Your Raspberry Pi may already be configured to use the internet. If not, here’s how:

1. Obtain your Raspberry Pi’s MAC Address


a. A MAC address is a code that is associated with your network interface.
b. You can find it by opening the terminal on your desktop, typing ifconfig, and pressing enter and looking for the HWAddr under the wlan0
interface.
c. Here’s an example screenshot from a terminal:

2. Connect to the EC_GUEST_WIFI from the wifi settings in the top right corner of the desktop.
a. Connect to the network, and then disconnect by right clicking on the network.
3. Register the device
a. Register the device at this link on a separate computer: https://eccampusman.etown.edu/registration/GameRegister.jsp
b. Use your regular login and register the device.
i. Note: By using this, you will be put on the EC_GUEST_WIFI on the pi.
ii. You should now be able to use the internet wirelessly. Register the eth0 MAC Address to use Ethernet.

Software:

The libraries on your Raspberry Pi terminal may need to be updated. If so:

1. Run the update commands in the terminal


 sudo apt update
 sudo apt upgrade

The pins:

When you are hooking up wires to the Raspberry Pi, you will need to utilize the pins. Go to https://pinout.xyz/ in order to see what pin is which. This website will be
helpful for both labs one and two. The numbers next to the pins are the names of the physical pin locations. Next to that tells you the purpose of that pin, whether it’s
a GPIO pin or power, for example. Hovering over the pins on the website will tell you the pin number for WiringPi.
Elizabethtown College
Raspberry Pi Lab #1 - MOTION SENSOR
by Daniel Esteves, 2017 – Modified by Lauren Cugliotta, 2019

This shouldn't take much time, but expect it to be a time consuming process in the future, since more libraries, more time to check all.
For the beginning, we are going to use scripts in C and/or Python. If you are not used to it, don't worry. The goal is to make you dig and find solutions, not learn a new language.
This example I'm going to demonstrate uses Python.

2.1- THE EXAMPLE


Python should already be installed on your Raspberry Pi, as it comes standard with Raspbian.
If it is not installed, it can be installed by the following commands:
sudo apt install python-dev
sudo apt install python-pip
Now lets install rpi.gpio, a control module for GPIO channels. This does not come standard with Raspbian.
pip install rpi.gpio
Test Python using:
sudo python
A prompt like this one should appear:

If sudo python doesn’t open a prompt like this one, return to the start of 2.1. If it didn’t work after doing so, let the TA know.
Enter exit() to quit Python.

To create our first script? Enter the commands below:


sudo touch led.py – The touch command creates a new empty file.
sudo nano led.py
If you’re not familiar with Linux, nano is a text editor for Terminal. Sometimes you need to use it for permission problems and it provides a raw data file, different from programs like Word.

With led.py file open on terminal using nano, copy the following (This turns on and off an LED depending on motion sensor):
import RPi.GPIO as GPIO #importing the RPi.GPIO module
import time #importing the time module
GPIO.cleanup() #to clean up at the end of your script
led_pin = 37 #select the pin for the LED
motion_pin = 35 #select the pin for the motion sensor
def init():
GPIO.setmode(GPIO.BOARD) #to specify which pin numbering system
GPIO.setwarnings(False)
GPIO.setup(led_pin,GPIO.OUT) #declare the led_pin as an output
GPIO.setup(motion_pin,GPIO.IN,pull_up_down=GPIO.PUD_UP) #declare the motion_pin as an input
print("-----------------------------------------------------------------------")

def main():
while True:
value=GPIO.input(motion_pin)
if value!=0: #to read the value of a GPIO pin
GPIO.output(led_pin,GPIO.HIGH) #turn on led
time.sleep(2) #delay 2ms
print "LED on" #print information
else:
GPIO.output(led_pin,GPIO.LOW) #turn off led
time.sleep(2) #delay 2ms
print "LED off" #print information

init()
main()
GPIO.cleanup()
To Exit nano, press Ctrl+X, it will ask if you are sure, Press “Y” and enter for the led.py name.
Run the code by entering
sudo python ./led.py
This should load a screen, but wait... the pins are not connected... Lets fix this!

2.2 – THE HARDWARE


First of all, this exercise will be using the 5V port of the Raspberry Pi. Power the breadboard using pin 2, or the 5 volt pin. Ground the breadboard using pin 6. Connect the Vcc port of the
sensor to 5V and ground, of course, to ground. The OUT port in the sensor needs to be connected to pin 35, or GPIO19. We connect an LED with the power coming from pin 37, or
GPIO26 (don't forget the resistor) A 220 Ω or 330 Ω resistor should work.
A diagram of the hardware circuit

2.3 – THE STUFF WORKING


Finally, we are ready to go back to the terminal window. You should rerun the command
sudo python ./led.py
This will load a screen that will display LED ON/OFF depending if the sensor is triggering it.

3- NOW YOU!
(1) Check the LED.PY file. Try to understand exactly what each line does. For example, Why are led_pin= 37 and motion_pin= 35 if they are side-by-side?(https://pinout.xyz/).
(2) Using the same format, try to create a new project. You don't need to make anything too fancy, just use something from the sensor kit or something else from the lab. An
example would be to make the photoresistor to trigger the LED, like a sustainable energy saving device. Of course, if you can integrate it to the Phoenix Contact NanoPLC, it
is a plus. You may look online for tutorials for cool stuff to do, but, please, DO NOT just copy and paste the information. Try to understand exactly what you are doing.

Here’s an example project:


Instead of having a singular LED that turns on and off based on motion detected, this circuit uses a green LED to indicate motion, and a red LED to indicate there’s no motion. And rather
than the screen saying “LED on, LED off” it says “Motion detected, no motion detected.”

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Elizabethtown College
Raspberry Pi Lab #2 – Anolog toDigital, and PHOTORESISTOR
by Daniel Esteves 2017, Modified by Lauren Cugliotta, 2019
1- INTRODUCTION
Also known as light-dependent resistor (LDR), the photoresistor adjusts its resistance according to the light
received from the environment. It works not only with sunlight, but also with artificial light. Now lets see how
we can integrate it to the real world.

2- THE RASPBERRY PI (RPI)


With your RPi turned on, make sure it is connected to the internet by going to the Internet Browser and trying
to access google.com (don’t try to access etown.edu since the college authorizes this communication even
though you are not actually connected to the internet).
If you got the connection, open Terminal

3- THE TERMINAL
sudo apt-get update
sudo apt-get upgrade

ALWAYS do this before messing with Terminal. It updates the libraries and commands.
This shouldn't take much time, but expect it to be a time consuming process in the future, since more libraries, more time to check all.
For this Lab, we will be using WiringPi to run a C script. It works pretty much like rpi.gpio, from the last lab, but uses C.

4.1- THE HARDWARE SETUP


You should connect the project as in the picture below:

This IC is an A/D Analog to Digital Convertor(MCP3204)


and the resistor is 10K Ohm.

Use the website https://pinout.xyz/ to find the pin names so


you can connect to the right place.

4.2- THE SOFTWARE SETUP


Like python, git should have already come with your Pi.
If you need to install it:
sudo apt install git-core

If you are wondering what is GIT, it is a version control system to track changes on your
files and computer. Here is the link to the creators website: https://git-scm.com

After downloading GIT, you will be able to get WiringPi using this command:
git clone git://git.drogon.net/wiringPi

To build WiringPi
cd wiringPi
./build

Now lets create the Script by entering:


sudo touch res.c
sudo nano res.c MCP3204 schematics

After you enter this command, nano will prompt so you can edit res.c. For this example, the code used can be seen below:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <unistd.h>

#define CS_MCP3208 8 // BCM_GPIO8

#define SPI_CHANNEL 0
#define SPI_SPEED 100000 //

int read_mcp3208_adc(unsigned char adcChannel)


{
unsigned char buff[3];
int adcValue = 0;
buff[0] = 0x06 | ((adcChannel & 0x07) >> 7);
buff[1] = ((adcChannel & 0x07) << 6);
buff[2] = 0x00;

digitalWrite(CS_MCP3208, 0); // Low : CS Active

wiringPiSPIDataRW(SPI_CHANNEL, buff, 3);

buff[1] = 0x0F & buff[1];


adcValue = ( buff[1] << 8) | buff[2];

digitalWrite(CS_MCP3208, 1); // High : CS Inactive

return adcValue;
}

int main (void)


{
int adc1Channel = 0;
int adc1Value = 0;

if(wiringPiSetup() == -1)
{
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror(errno));
return 1 ;
}

if(wiringPiSPISetup(SPI_CHANNEL, SPI_SPEED) == -1)


{
fprintf (stdout, "wiringPiSPISetup Failed: %s\n", strerror(errno));
return 1 ;
}

pinMode(CS_MCP3208, OUTPUT);

while(1)
{
system("clear");
printf("\n\nMCP3208 channel output.\n\n");
adc1Value = read_mcp3208_adc(adc1Channel);
printf("adc0 Value = %04u", adc1Value);
printf("\tVoltage = %.3f\n", ((3.3/4096) * adc1Value));
usleep(1000000);
}
return 0;
}

To Exit nano, press Ctrl+X, it will ask if you are sure, Press “Y” and Enter for the res.c name.
Compile the code by entering:
gcc -Wall -o app res.c -lwiringPi
Run the code with this command:
sudo ./app
It should display this screen:

5- NOW YOU!
Check the res.c file. Make the prompt, instead of displaying “MCP3208 channel output.”, display “Lab 2 – Student name” on the prompt screen. Also, before each line of “adc0 Value = …”
display the counting number. The output should be something like:

Using the same res.c file, instead of showing values, trigger a LED to turn on if the voltage is lower or equal to 2V. This will require a little more work and you may want to check out this
page( https://projects.drogon.net/raspberry-pi/wiringpi/functions/ ). You can also make something else, other than trigger a LED, but check out with the TA beforehand to see if it is ok.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Elizabethtown College
Raspberry Pi Lab #3 – ARM ASSEMBLY CODE
by Daniel Esteves 2017, Modified by Lauren Cugliotta, 2019
1- INTRODUCTION
This lab is intended to introduce you to the ARM Architecture/Assembly Code. ARM has become the main processor for gadgets like Smart Phones, Tablets and the best one: Raspberry
Pi. Lets go over some examples

2- EXAMPLES
This is the code for the “Hello World” file

.data
string: .asciz "\nHello World!\n"
.text
.global main
.extern printf
main:
push {ip, lr}
ldr r0, =string
bl printf
pop {ip, pc}

To assemble, link and run files on ARM assembly code we need Terminal.

3- TERMINAL
Lets start typing the traditional:
sudo apt-get update
sudo apt-get upgrade

You can use your favorite text editor in Terminal, but for this tutorial I will use nano.
Create the assembly1.s file by typing
sudo nano assembly1.s

With nano opened you can type the Hello World example from above.
Close the file by Pressing Control+X and when prompted if you would like to save the changes, press Y and save the file with the same name (assembly1.s).
Now that we have the file done, lets assemble it.
as –g –o assembly1.o assembly1.s

Now, link it.


gcc –o assembly1 assembly1.o

The file is now assembled and linked. You can now run it by entering
./assembly1

Your screen should look like this.

5- NOW YOU!
By doing the example you now know how to assemble an ARM assembly code program. Write a program that does the following:
(1) Move the integer 13 to the Register R1
(2) Move the integer 25 to the Register R2
(3) Put the sum of Register R1 and R2 to Register R3
(4) Move the value of R3 to R0
You will need to add the lines in the end of the file. This is called a system call and is needed to return the right value.
MOV R7, #1
SVC 0
If you did everything right, in terminal you can enter
echo $?
A call that outputs stored arguments
Giving the answer to the sum:

You might also like