Basic Peripherals for Raspberry Pi
Some of the most commonly interfaced peripherals are:
1. Input Devices
o Switch / Push button
o Sensors (Temperature, Motion, Light, etc.)
o Keyboard, Mouse
2. Output Devices
o LED (Light Emitting Diode)
o Buzzer
o Display (LCD, OLED, Monitor)
o Motors
3. Communication Devices
o Wi-Fi, Bluetooth
o USB Devices
o GPIO-controlled Modules
Interfacing Using GPIO Pins
Raspberry Pi has GPIO (General Purpose Input/Output) pins to
connect external devices.
GPIO pins can be configured as Input (for reading sensors,
switches) or Output (for controlling LEDs, buzzers, etc.).
Example – LED Interfacing
Connection:
o LED → GPIO Pin 18
o Ground → GND Pin
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
while True:
GPIO.output(18, True) # LED ON
time.sleep(1)
GPIO.output(18, False) # LED OFF
time.sleep(1)
Example – Switch Interfacing
Connection:
o Switch → GPIO Pin 25
o LED → GPIO Pin 18
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN) # Switch
GPIO.setup(18, GPIO.OUT) # LED
while True:
if GPIO.input(25): # If switch is pressed
GPIO.output(18, True) # LED ON
else:
GPIO.output(18, False)# LED OFF
Example – Buzzer Interfacing0
Connection:
Buzzer → GPIO Pin 23
GND → Ground
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
for i in range(5):
GPIO.output(23, True) # Buzzer ON
time.sleep(0.5)
GPIO.output(23, False) # Buzzer OFF
time.sleep(0.5)
GPIO.cleanup()
Example: Temperature Sensor (DHT11)
Connection:
DHT11 sensor data pin → GPIO Pin 4
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 4
humidity, temperature = Adafruit_DHT.read(sensor, pin)
if humidity is not None and temperature is not None:
print("Temp={0:0.1f}°C Humidity={1:0.1f}
%".format(temperature, humidity))
else:
print("Failed to read sensor data")
Adafruit Industries is an open-source hardware and software company
based in New York, USA.
It was founded in 2005 by Limor Fried (Ladyada).
The company designs and manufactures electronics, sensors, development
boards, and DIY kits for learning and building projects in:
Electronics
IoT (Internet of Things)
Robotics
Wearables
Automation
Uses of Adafruit
1. Hardware Products
o Sensors (Temperature, Humidity, Pressure, Motion, etc.)
o Displays (OLED, LCD, LED matrices)
o Development Boards (Adafruit Feather, Circuit Playground,
etc.)
o Robotics and IoT kits
2. Software Libraries
o Provides ready-to-use libraries for Python, Arduino (C++),
and CircuitPython.
o Makes it easier to read data from sensors and control devices
without writing complex code.
o Example libraries:
Adafruit_DHT → for DHT11/DHT22 temperature
& humidity sensors
Adafruit_SSD1306 → for OLED displays
Adafruit_BME280 → for environmental sensor
(temp, humidity, pressure)
Example:Servo Motor (Output Actuator)
Used for robotics, IoT projects.
servo = 17
GPIO.setup(servo, GPIO.OUT)
p = GPIO.PWM(servo, 50) # 50Hz(pulse width Modulation)
p.start(2.5)
p.ChangeDutyCycle(7.5) # Rotate to 90°
time.sleep(1)
p.ChangeDutyCycle(12.5) # Rotate to 180°
time.sleep(1)
p.stop()