Assalamualaikum and Hi.
Today, we will continue with GPIO.
Before this many people ask how to control Raspberry Pi's input and output
So today we will do a basic one by light up an LED as an output
And we will get an input using the push button.
Let's watch it.
 
 
 
 
 
 
So I'll continue the lesson on GPIO
What we will need is Raspberry Pi
This is the same SD card that we use in our last tutorial
Next we need a breadboard
A few jumper wires
Male to Female jumper wires
The female jumper wires is connected to GPIO pin on Raspberry Pi
And male jumper wire is connected to the breadboard.
Next, resistor 330 ohm
We will use it with LED.
This is for digital output.
As for digital input, we will use push button.
The small push button
It only has two pins.
Next, we need a micro B USB cable
I will power up the Raspberry Pi using my laptop because
I just want to control the LED and push button
So, I don't need a high current for this
Everything is okay. I think we can start now
Firstly, I'll remote the Raspberry Pi
Same with the last tutorial
Power up it first
Maybe it'll took a few minutes
For it to boot.
Then I can do the remote connection.
Now I'll do remote connection to the Raspberry Pi.
Search remote desktop connection and key the IP address.
The IP address is different with the IP address in the last tutorial.
But, it's okay.
As long as the IP address is same with the Raspberry Pi
Connect
User name : pi
Password : raspberry
Okay, we manage to load it.
So now, before we write the program
We will make a simple circuit connection to light up the LED.
Before we start to connect it, we have to know
All the GPIO pins on the Raspberry Pi.
So just google it. All the information is there.
This is one of the example
This image has all the GPIO pins and its name on Raspberry Pi
For example..
Pin number 1 is 3.3V
Pin number 2 is 5V
And next we have GPIO 2, 3,Ground and many more pins.
The total pins on it is 40 pins.
Each pin has their own function
For the first circuit, we will try to light up an LED.
On the GPIO pin 23.
Search first where is the pin for GPIO 23 is located
Okay, here it is
So I'll use one jumper wire
Just use any colour for the wire.
But, it's better to use black for Ground.
And red for Power.
But if you don't have it
Any colour will do.
So, I'll connect the circuit first.
(Counting from 1 to 8)
So, this is the pin for GPIO 23.
I just connect it to the breadboard.
Next, I'll
Take an LED
The long leg is positive
and is connected to the pin GPIO 23.
 
Then, I need this 330 ohm resistor
 
Connect it in series to the LED.
Meaning, the cathode (short leg) of LED is connected to the resistor.
 
And another leg of the resistor is connected to the ground.
 
Check first which one is the Ground's pin.
(Counting 1 to 3) So this is the pin.
It's better before you connect the circuit
I advice you to turn off the power supply for Raspberry Pi.
But, since we are using the Raspberry Pi
We need to shut down it first to turn it off.
It takes time.
So, for beginner
It's better to shut down first and do the circuit after that.
Now we will program the Raspberry Pi.
We will use Python language to control the GPIO.
For Raspbian that comes from NOOBS
It comes with the Python software inside.
Go to Menu > Programming > Python 2 (IDLE)
This one
A new window of Python 2.7.13 Shell will come out
Okay, then click on File.
New File
You will get a new blank window
So, here is where we write all the program.
For now, just close the Python 2.7.13 Shell window.
Now we will focus on writing the programming on this blank window.
Save the file before we write any codes.
Click on File and choose Save.
This is the location to save the file.
So it's up to you if you want to change it to the other directory.
For me, I'll just save at home/pi
For the first one I'll name the file as Lesson1
Save
Now, we have save the file.
Now, we can start to write the program.
I'll start to write the python program.
First, we have to import the GPIO library.
 
When we write word "import" it will change to orange colour
Means you write it correctly
There is a certain words in Python language that will turn to other colour
 
 
If I change the "I" to capital letters, it will not change to other colour
Write "import RPi.GPIO as GPIO
This command is to import GPIO library to Raspberry Pi
Next, we will do a simple LED blinking.
When we said about LED blinking, we need a delay
For delay, we need to import time
Next, this one is optional.
Write "GPIO.setwarnings(False)"
This is to disable all the warning
If there is warning, it will not be displayed on the output.
 
Write "GPIO.setmode(GPIO.BCM)"
It has two types. One is GPIO.BCM
Another one is GPIO.BOARD
This is refer to the pin arrangement.
When we write the program, we need to point to the pin
If we choose GPIO.BCM
It will refer to the name of the pin for BCM.
For example, it goes with GPIO 2, GPIO 3 and GPIO 4
For example, if we want to control GPIO 4, then we write 4.
It will point to that GPIO pin.
Meanwhile, for GPIO.BOARD
It will refer to the pin arrangement on the board.
(Counting from 1 to 7)
If you don't want to write GPIO 4, you have to write number 7 for GPIO.BOARD
 
I recommend use GPIO.BCM
 
GPIO.setup() comand is to set the pin as Output or Input
For LED we have to set it as OUTPUT.
 
So, write "GPIO.setup(23,GPIO.OUT)"
So, it will make GPIO pin 23 as OUTPUT.
So, now we finished initialize for the pin.
Now we want to control it.
To make the LED blinking
So we will make one continuous loop.
Write "while True :"
GPIO.output(23,GPIO.HIGH)
We write it as HIGH, since we want the LED to HIGH.
 
 
 
For delay. Write "time.sleep(1)"
The number inside the bracket is for second.
If write 1, then it will be 1 second.
HIGH will give 3.3V to the GPIO pin 23.
Then it'll wait for 1 second.
And next I want it to be LOW.
Copy this.
Now, make it as LOW. Low is 0V
So the LED will turn off.
Write "time.sleep(1)" for 1 second delay
It's just a simple program like this.
We try to see the output first.
Go to Run and click Run Module.
Or push F5.
Save
Now the LED is blinking.
It turns ON for 1 second, and turn off 1 second.
To stop the program to run
Just close the Python 2.7.13 Shell window.
Close it.
It will no more run the file. But it maintain at the last state.
Now I want to add a push button.
That I'll connect to the pin GPIO 24.
Connect the push button to the breadboard.
I took one jumper wire.
Connect 1 pin from the push button (it has two pins, just choose 1)
To GPIO pin 24.
and
another pin of push button is connected to the ground.
In this case, I will do an active-low
Meaning, when the push button is released, the GPIO 24 is HIGH.
And when it's pressed, the GPIO 24 will be LOW.
Write "GPIO.setup(24,GPIO.IN)"
Next
Since it's a push button. I need to put a pull up resistor.
To make it by default as HIGH.
When it's not pressed, it will get 3.3V
Since I don't have a resistor now
I will use a software to make it pull-high.
Write "GPIO.PUD_UP"
 
So, this command will make the pin as an Input and has an internal pull-up resistor.
Just now, we just do a simple LED blinking.
Now, when I pressed the button, LED is ON
When I released the button, LED is OFF
 
 
Here I add if
Write "if GPIO.input(24) == GPIO.LOW)
 
When LOW, meaning we pressed the button
When released, it is HIGH
So, when LOW, led is ON/HIGH
 
 
 
Since it's digital it only has two state. HIGH and LOW.
Else the button is HIGH
Write "else"
To turn it off. GPIO.LOW
Okay, now let's run the code
And see the result
Now, I didn't press the button.
The LED is OFF
When I pressed it, the LED is ON
 
When I released, the LED is OFF.
 
I hope you guys understand
about the input and output for raspberry pi.
This just covered the basic part.
Because our tutorial focus on beginner
and to those that know nothing but want to try something
on raspberry pi
I hope this tutorial do help you
 
It will be best if you could share with me your result
after learning from this tutorial
Thank you for watching.
If you have any feedback
you can post in our forum
at forum.cytron.io
Or just drop a comment below.
 
