Read or Not to Read....that is the question!
3 weeks of exams and here we go again.
This week focuses on raspberry pi GPIO.
This is the first time I deal with raspberry pi and python, but powering GPIO was quite easy!
with raspberry pi pin map and huge support community, you cannot face any problem without finding a solution for it as with Arduino
the raspberry pi uses its operating system so dealing with a new operating system was quite confusing but Linux turned out to be easy as pie
Let's see what we can do and start the GAME.
The raspberry pi is used by flashing the system to the sd card using the imager, insert and run the raspberry pi, connect a mouse and keyboard and you have a running RP
Note* use class 10 or better SD card
Note* use adaptor 5v 3amp
RP supports the VNC and SSH , you only have to enable them
from the terminal type ((sudo raspi-config)) and press enter, follow the following screen.
Now, you have to coonect with your computer
After you get the RP IP address, you can connect to the VNC viewer or putty SSH.
and with the recommended version OS installed you do not have to do further installation.
With Thonny you can write, check and run your code
We will open Thonny and write our first code and run it
and the function of this code is nothing!
As in Arduino, we have to define the input and output pins. but in RP we have to import the GPIOs library which is installed in default.
We can simply write {{import RPi.GPIO as GPIO}} , that is the basic way or we can use gpiozero to write a more efficient code
Also, you had a delay function with Arduino, now we have sleep and it must be imported in order to be used, the time library contains the sleep function, you can write {{import time}} or you can write {{from time import sleep}} it will be more efficient
you can find more HERE
You can set the PIN map to BCM or BOARD
the BCM is to deal with pins as its GPIO number
use {{GPIO.setmode(GPIO.BCM) }} to enable BCM mode
and to deal with pins as the board pin numbering
use {{GPIO.setmode(GPIO.BOARD) }} to enable board mode
Remember when we used pinMode with Arduino?
It is the same but we will use
GPIO.setup(23, GPIO.IN) to use as input
GPIO.setup(23, GPIO.OUT) to use as output
now writing the useless code
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.OUT)
Next step is to jump to a functional code,,,,,, or we can jump to an advanced code in ONE step.. you can do it!
The first step, we have to know the limitations of our GPIOs
The operating voltage of the GPIO pins is 3.3v with a maximum current draw of 16mA unlike the 5v 40mA of Arduino
Second, we need the PCM(PWM) pins in order to run the servo motor
The four highlighted pins are the PWM pins but jump to the pin map to see the limitation of each pin
we will use GPIO 18 as a PCM pin, for now, to control led brightness
the main annoying part of RP is that NOT having an integrated ADC converter like Arduino, so lame!
Note* The PWM pins used to output variable voltages to the DAC devices or motors, the Servo has another way of software PWM so we can use any pin we need not just the PWM pins
Note* The previous note needs to be checked! as in the video, it says otherwise
The servo needs some extra effort of software PWM control.
with servo you have three wires, red is power 5v, brown is ground and yellow is the PWM pin connected to a GPIO pin
starting with enabling the yellow pin as an output
with python it is not like hell after all, we can use some integrated functions
GPIO.PWM(11, 50) is very handy to us as we set the 11 as servo gpio and 50 as hz rate but we have to assign the function the servo name like
servoo = GPIO.PWM(11, 50)
and we also can use the start function to set the PWM to zero
servoo.start(0)
and use the following function in order to move the servo
servoo.ChangeDutyCycle(Mr.X)
Mr.x in the previous line did not introduce himself before... who the hell is he ??
Well, if you looked at the servo data sheet you will find the pulse range.
That means we have to set the duty (ON period) cycle to 1-2ms/20 which means 5 to 10 duty cycle but in every tutorial, they set it to 2 to 12 duty cycle. **for later
searching for the calculation used here you can find how to use duty>>
As mentioned in the video above that when we set the duty cycle to the servo, the servo tries to move to the exact position over and over again resulting in vibrating in place, in order to overcome that we have to set the duty cycle to zero ( or any value outside the range) that makes servo stops non-sense vibrating.
the stop and cleanup are used to make the code more efficient
Now the code of the servo will be as in the right>>
it moves to the angle and stops, that's it!
Time to add a button control and led pwm control plus a buzzer to sense the pushed button
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
servoo = GPIO.PWM(11,50)
servoo.start(0)
time.sleep(2)
duty = 2
angle = 100 #change the angle
servoo.ChangeDutyCycle(2+(angle/18))
time.sleep(0.5)
servoo.ChangeDutyCycle(0)
servoo.stop()
GPIO.cleanup()
import RPi.GPIO as GPIO
import time
button=13
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.IN)
servoo = GPIO.PWM(11,50)
servoo.start(0)
time.sleep(2)
duty = 2
angle = 100 #change the angle
try:
while True:
if GPIO.input(button):
angle = 180
servoo.ChangeDutyCycle(2+(angle/18))
time.sleep(0.2)
servoo.ChangeDutyCycle(0)
time.sleep(0.3)
else:
angle = 0
servoo.ChangeDutyCycle(2+(angle/18))
time.sleep(0.2)
servoo.ChangeDutyCycle(0)
time.sleep(0.3)
except KeyboardInterrupt:
servoo.stop()
GPIO.cleanup()
Final step is to add the led and buzzer but leave it for now, It is testing time
What I learned this week is...
Raspberry pi was not that hard, Loved it.
can't wait for the AI and multi-processing
just follow up