Internet of Things (IoT) –
Architecture, Communication Technology, and Applications
(EED-379)
Dr. Rohit Singh 1
GPIO Pins
If you've used Arduino, you're probably familiar with the fact that you have to
declare a "pin mode" before you can use it as either an input or output.
To set a pin mode, use the setup([pin], [GPIO.IN, GPIO.OUT]) function.
Ex: GPIO.setup(21, GPIO.OUT)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Digital Output
To write a pin high or low, use the GPIO.output([pin], [GPIO.LOW, GPIO.HIGH]) function.
For example, if you want to set pin 18 high, write:
Ex: GPIO.output(18, GPIO.HIGH)
Writing a pin to GPIO.HIGH will drive it to 3.3V, and GPIO.LOW will set it to 0V. For the
lazy, alternative to GPIO.HIGH and GPIO.LOW, you can use either 1, True, 0 or False to set a
pin value.
Dr. Rohit Singh 2
Pins of Raspberry Pi 4
Syllabus
Dr. Rohit Singh 3
Protocol Pins: I2C
Syllabus
To use I2C communication protocol use the following
library:
Import smbus
Dr. Rohit Singh 4
Protocol Pins: SPI
Syllabus
To use SPI communication protocol use the following library:
Import spidev
Dr. Rohit Singh 5
Protocol Pins: UART
To use UART communication protocol use the following
library:
Import serial
Syllabus
Dr. Rohit Singh 6
Installation of Raspberry Pi OS
Step 1: Visit https://www.raspberrypi.org/software/operating-systems/#raspberry-pi-os-
32-bit
Step 2.
Syllabus
Step 3: Visit https://www.raspberrypi.org/software/
Step 4:
Dr. Rohit Singh 7
Installation of Raspberry Pi OS
Syllabus
Dr. Rohit Singh 8
GPIO Pins
Digital Input
If a pin is configured as an input, you can use the GPIO.input([pin]) function to read its value.
The input() function will return either a True or False indicating whether the pin is HIGH or
LOW.
Ex: GPIO.input(18)
Dr. Rohit Singh 9
GPIO Pins: PWM mode
Generate PWM using Python
The PWM generated by Python on Raspberry Pi is software PWM. This PWM has timing
resolution of 1 µs.
Functions Related to PWM Output
PWM=> (Fmin = 10 Hz and Fmax = 8KHz)
For PWM output pin mode must be GPIO.OUT
pi_pwm = GPIO.PWM(pin_number, frequency)
pi_pwm.start(duty_cycle)
pi_ChangeDutyCycle(new_duty_cycle)
pi_pwm.stop()
Dr. Rohit Singh 10
Some other GPIO functions
GPIO.VERSION()
GPIO.cleanup()
GPIO.setwarnings()
GPIO.wait_for_edge()
GPIO.add_event_detect()
GPIO.event_detected()
Dr. Rohit Singh 11
Our first program in Raspberry Pi
Dr. Rohit Singh 12
PWM on Raspberry Pi Pins
Dr. Rohit Singh 13
Syllabus
Dr. Rohit Singh 14