Week-8 Ece
Week-8 Ece
1.1.1. apt
1.2. PC/Mac
In order to use GPIO Zero’s remote GPIO feature from a PC or Mac, you’ll need to
install GPIO Zero on that computer using pip. See the Configuring Remote
GPIO page for more information.
1.3. Documentation
The article guide you through blinking a LED using the GPIO pins of Raspberry Pi.
Hope that you should have gone through the initial raspberry pi setup as given in our
previous tutorials : - ) Raspberry Pi 3 B+ have a total of 40 GPIO
pins
Above image properly mentions pin configuration of the Raspberry Pi 3 B+,
now explaining it briefly -
1. Voltage Pins - Two 5V ( pin 2 and 4 ) pins and two 3V3 ( pin 1 and 17 ) pins
are present on the board, as well as a number of ground pins i.e 0V ( pin
6,9,14,20,25,30,34 and 39 ) which are unconfigurable. The remaining pins are
all general purpose 3V3 pins, meaning outputs are set to 3V3 and inputs are
3V3-tolerant.
2. Outputs - A GPIO pin designated as an output pin can be set to high (3V3) or
low (0V).
3. Inputs - A GPIO pin designated as an input pin can be read as high (3V3) or
low (0V). This is made easier with the use of internal pull-up or pull-down
resistors. Pins GPIO2 and GPIO3 have fixed pull-up resistors, but for other
pins this can be configured in software.
4. More - As well as simple input and output devices, the GPIO pins can be used
with a variety of alternative functions, some are available on all pins, others
on specific pins.
I. PWM (pulse-width modulation)
o Software PWM available on all pins
o Hardware PWM available on GPIO12, GPIO13, GPIO18,
GPIO19
II. SPI
o SPI0: MOSI (GPIO10); MISO (GPIO9); SCLK (GPIO11); CE0
(GPIO8), CE1 (GPIO7)
o SPI1: MOSI (GPIO20); MISO (GPIO19); SCLK (GPIO21);
CE0 (GPIO18); CE1 (GPIO17); CE2 (GPIO16)
III. I2C
o Data: (GPIO2); Clock (GPIO3)
o EEPROM Data: (GPIO0); EEPROM Clock (GPIO1)
IV. Serial (UART)
o TX (GPIO14); RX (GPIO15)
Step 1 - Connections
Here we will be using GPIO 21 or pin no. 40 to make the LED blink. Ground ( 0V)
at pin 39 will be used to provide ground with a resistance ( 270,330,1k ohm ) in
series to LED. Make connection by referring the Diagram below
import time
LedPin = 40 # Defining pin40 as LedPin
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output
GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to turn on led
def blink():
while True:
GPIO.output(LedPin, GPIO.HIGH) # led on
time.sleep(1) # keeping it in previous state for 1 sec
GPIO.output(LedPin, GPIO.LOW) # led off
time.sleep(1) # keeping it in previous state for 1 sec
def destroy():
GPIO.output(LedPin, GPIO.LOW) # led off
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
blink()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child
program destroy() will be executed.
destroy()<code></code>
Required Components:
Raspberry Pi 4 controller.
Monitor/Laptop.
And other connecting wires such as (Ethernet Cable, HDMI Cable, & Powering
cable for both Raspberry Pi and Monitor, etc.)
LED of any color.
Jumper Wire.
Breadboard.
Resistor 100 Ohm.
Now we will proceed further with our first activity i.e. Turning On & Off LED.
Circuit Connection for controlling the brightness of LED using Raspberry Pi:
The circuit diagram remains the same for all 3 activities there is no change in the
circuit diagram only we have to change the code for all activities to blink it and to
control its brightness from 0 to 100 by applying PWM signal via Board pin
11(in raspberry pi there are two different types of pin numbering available, you
can use anyone out of these two there is a slight change in the code by which you
can switch these pin numbering configuration). The 2 pin configurations are GPIO
pin configuration, and the second one is Board Pin configuration. further, I’ll tell
you what is the change required in the code to switch between available pin
configurations.
Circuit
Diagram to turn On/Off LED using Raspberry Pi.
As you can see that in the above figure the positive terminal of LED is connected
to the board pin 11 and on which we will apply 5V to turn ON&OFF LED. Now
we have done with the circuit diagram, now think! how to power the LED
with raspberry pi? to answer this let us move to the coding part.
GPIO.setmode (GPIO.Board) //If you are using board pin numbering ||or||
GPIO.setmode(GPIO.BCM) //If you are using GPIO pin numbering you can use
either one
GPIO.output(11, True) //To turn on LED pin 11 supplied with logic 1 and by
switching this you can turn on and off LED.
GPIO.setup(11, GPIO.OUT)
while:
GPIO.output(11, True)
time.sleep(1)
GPIO.output(11, False)
time.sleep(1)
import time
GPIO.setmode(GPIO.Board)
GPIO.setup(11, GPIO.OUT)
p = GPIO.PWM(11, 100) //11 is pin number and 100 is max range of PWM.
p.start(0) //Starting point of PWM signal you can select any value between 0 to
100.
while True:
for x in range (0, 100, 1): //Increasing brightness of LED from 0 to 100
p.ChangeDutyCycle(x)
time.sleep(0.1)
p.ChangeDutyCycle(x)
time.sleep(0.1)
Week-9:
DHT11 Sensor
DHT11 sensor measures and provides humidity and temperature values
serially over a single wire.
It can measure relative humidity in percentage (20 to 90% RH) and
temperature in degree Celsius in the range of 0 to 50°C.
It has 4 pins; one of which is used for data communication in serial form.
Pulses of different TON and TOFF are decoded as logic 1 or logic 0 or start
pulse or end of a frame.
For more information about the DHT11 sensor and how to use it, refer the
topic DHT11 sensor in the sensors and modules topic.
Here, we are going to interface the DHT11 sensor with Raspberry Pi 3 and
display Humidity and Temperature on the terminal.
We will be using the DHT Sensor Python library by Adafruit from GitHub.
The Adafruit Python DHT Sensor library is created to read the Humidity and
Temperature on Raspberry Pi or Beaglebone Black. It is developed for DHT
series sensors like DHT11, DHT22, or AM2302.
Once the library and its dependencies have been installed, open the example
sketch named simpletest from the library kept in the examples folder.
In this code, raspberry Pi reads Humidity and Temperature from the DHT11
sensor and prints them on the terminal. But, it read and displays the value
only once. So, here we made a change in the program to print value
continuously.
Note:
Assign proper sensor type to the sensor variable in this library. Here,
we are using DHT11 sensor.
sensor = Adafruit_DHT.DHT11
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# Try to grab a sensor reading. Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
'''
else: