Basic Output: Blinking an LED test
Before going to write a program to blinking an LED, first we need to write a program to test whether IO (Input/Output) connections are working properly or not.
First setup the basic connections to start Raspberry Pi. Before we plug-in the power supply, need to construct a circuit that able to access input/output ports. To construct such a circuit we need,
Before going to write a program to blinking an LED, first we need to write a program to test whether IO (Input/Output) connections are working properly or not.
First setup the basic connections to start Raspberry Pi. Before we plug-in the power supply, need to construct a circuit that able to access input/output ports. To construct such a circuit we need,
- LED (Light Emitting Diode)
- Breadboard
- GPIO (General Purpose Input ans Output) Header Pin
- 270 Ohms resistor (In our case 330 Ohms resistor)
- Python 3
LED (Light Emitting Diode): Has 2 legs, longer one called the "anode'- it receives power, other called the "cathode"- it is the output leg.
Steps to check the IO connection:
- Connect 40-pin GPIO Ribbon Cable one end into Raspberry Pi-GPIO Header, and the other end into GPIO-Header placed on the BeadBoard.
- Place anode(longer) leg of LED on the same row as 3.3 V wire(pin 1), and cathode leg of LED is not the same row.
- Take one 330 Ohms resistor and place on end on the same row as cathode leg of LED, and other end on the same row as GROUND wire(pin 6).
- Connect the Pi to its power plug and now you will see the LED turn on.
Now you will test the circuit works, when you place the LED under the Pi's control.
- Shutdown the Pi and unplug the power supply.
- Disconnect the 3.3 V wire from header pin 1 and then connect it to pin 3 (GPIO2).
- Plug the power back into the Raspberry Pi, and allow the Raspbian to load.
Controlling GPIO2 from a Terminal Window:
- Open Application Launcher, and select LXTerminal.
- Enter super admin access command: 'sudo su' and press enter.
- To export the GPIO plugins enter following command: 'echo 2 > /sys/class/gpio/export'
- List the files: 'ls /sys/class/gpio/gpio2'.
- To set GPIO2 as an output and turn it on, excute following two commands: 'echo out > /sys/class/gpio/gpio2/direction' and 'echo 1 > /sys/class/gpio/gpio2/value'.
- To turn GPIO2 off: 'echo 0 > /sys/class/gpio/gpio2/value'.
It is always a best practice to unexport the pin so that it is no longer under the control of the file system entries: 'echo 2 > /sys/class/gpio/unexport'.
Controlling GPIO2 from Python 3:
- Open a new file in python 3 and save it as experiment1.py in any accessible directory (preferably /home/pi/).
- Write the following program to control the GPIO2:
import RPi.GPIO
import time
RPi.GPIO.setmode (RPi.GPIO.BCM)
RPi.GPIO.setup (2, RPi.GPIO.OUT)
while True:
RPi.GPIO.output (2, True)
time.sleep(0.15)
RPi.GPIO.output (2, False)
time.sleep(0.15)
- Execute the program in LXTerminal by using following command: 'python experiment1.py'.
- Now, you will observe that LED blinking on and off with 1 second as its time span in each state.
- If you want to stop the program, press Ctrl + C.
Basic Input:
We can find out whether the voltage that is coming into a pin from your external circuit is high or whether it is low, by using GPIO input pins. In this basic input experiment we construct a circuit that will see how to detect when a switch is closed. For this we need,
- Switch (either a two-terminal on/off switch or four-terminal tactile push button)
- 330 Ohms resistor
Raspberry Pi has pull-up and pull-down rsistors built-in to all of the GPIO pins. We can choose any one to use from software programs.
Steps to construct a circuit:
- Connect one end of switch to the same line of the GPIO input pin(pin 3) wire and other end to the one side of 330 Ohms resistor.
- Other side of the 330 Ohms resistor should connect to the GPIO output pin wire.
Reading a GPIO pin from Terminal Window:
- Open LXTerminal and enter as super admin by using command: 'sudo su'.
- Enter the following commands: 'echo 2 > /sys/class/gpio/export'
'echo in > /sys/class/gpio/gpio2/direction'
'echo 0 > /sys/class/gpio/gpio2/active_low'
'cat /sys/class/gpio/gpio2/value'
- Now, press the switch and then enter.
- Enter the following command to unexport: 'echo 2 > /sys/class/gpio/unexport'.
Reading a GPIO pin from Python 3:
- Write the following python program and save it as experiment2.py,
import RPi.GPIO
import time
import time
RPi.GPIO.setmode (RPi.GPIO.BCM)
RPi.GPIO.setup (2, RPi.GPIO.IN, pull_up_down = RPi.GPIO.PUD_UP)
RPi.GPIO.setup (2, RPi.GPIO.OUT)
RPi.GPIO.setup (2, RPi.GPIO.OUT)
while True:
if RPi.GPIO.input (2) == RPi.GPIO.LOW:
print("Switch pressed.")
RPi.GPIO.output (2, True)
time.sleep(0.15)
RPi.GPIO.output (2, False)
time.sleep(0.15)
else:
RPi.GPIO.output (3, False)
else:
RPi.GPIO.output (3, False)
RPi.GPIO.cleanup ()
- This program loops until the switch is pressed and GPIO2 goes low.
No comments:
Post a Comment