0% found this document useful (0 votes)
30 views6 pages

Ex 5

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)
30 views6 pages

Ex 5

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/ 6

Ex. No.

5 INTRODUCTION TO RASPBERRY PI PLATFORM AND PYTHON


PROGRAMMING

OBJECTIVE:
To Setting up the Raspberry Pi Equipment using Python Program.

REQUIRED COMPONENTS:
● A DVI to HDMI cable
● A MicroSD card with Raspbian image loaded on it
● A mouse and keyboard
● A microUSB power supply
● A Raspberry Pi kit

BACKGROUND THEORY:
A Raspberry Pi is a computer complete with a desktop. In order to use it:
● Insert the SD card loaded with Raspbian into the port on the bottom of the pi.
● Connect the mouse and keyboard to the USB ports.
● Connect the HDMI end of the cable to the pi, and the DVI end to a monitor at the
station.
● Plug in the power supply into the Raspberry Pi
Note: It is recommended that the plug into the wall to prevent a lack of power.
Raspberry Pi.
Basic Operation
Setting up internet on the Raspberry Pi:
The Raspberry Pi may already be configured to use the internet. If not, here’s how:
1. Obtain the Raspberry Pi’s MAC Address
● A MAC address is a code that is associated with the network interface.
● It is possible to find it by opening the terminal on the desktop, typing ifconfig, and
pressing enter and looking for the HWAddr under the wlan0 interface.
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.
● Connect to the network, and then disconnect by right clicking on the network.
3. Register the device
● Register the device at this link on a separate computer:
https://eccampusman.etown.edu/registration/GameRegister.jsp
● Use the regular login and register the device.
Note: By using this, it is possible to put on the EC_GUEST_WIFI on the pi. It is then
possible to use the internet wirelessly. Register the eth0 MAC Address to use Ethernet.
CIRCUIT BASICS
Software:
The libraries on the Raspberry Pi terminal may need to be updated. If so run the update
commands in the terminal
● sudo apt update
● sudo apt upgrade
Python should already be installed on the 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 let’s 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 that will 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 the first
script? Enter the commands below:
● sudo touch led.py – The touch command creates a new empty file.
● sudo nano led.py
If not familiar with Linux, nano is a text editor for Terminal. Sometimes it is needed 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
the 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()
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. It is possible to connect an LED
with the power coming from pin 37, or GPIO26 (don't forget the resistor) A 220 Ω or 330
Ω resistor should work.
Hardware.

The Stuff Working


Finally, it is ready to go back to the terminal window. It is possible to 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.
Output Screen.

Steps
(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. It is not needed 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 possible to integrate it to the Phoenix Contact NanoPLC, it
is a plus. 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.”
Sample Output.

To Exit nano, press Ctrl+X, it will ask if sure, Press “Y” and enter for the led.py name. Run
the code by entering sudo python ./led.py

CONCLUSION:
Thus the study of setting up the Raspberry Pi Equipment using Python Program is
completed.

You might also like