The Raspberry Pi is an excellent platform for learning and developing projects using Python. With built-in support for GPIO (General Purpose Input/Output) pins, networking, and various sensors, the Raspberry Pi + Python combo is perfect for automation, IoT, robotics, and AI projects (See Raspberry Pi projects).
In this tutorial, we’ll guide you through setting up Python on Raspberry Pi, writing your first script, and interacting with the GPIO pins to control LEDs and read sensor data.
1. Setting Up Python on Raspberry Pi
Check Python Version
The Raspberry Pi OS comes with Python pre-installed. To check the version, open the terminal and type:
python3 –version
You should see output similar to this:
Python 3.9.2
If Python 3 is not installed, install it using:
sudo apt update
sudo apt install python3 python3-pip
2. Writing your first Python script
Let’s create a simple Python script that prints “Hello, Raspberry Pi!”
1. Open the terminal and type:
nano hello.py
2. Enter the following code:
print(“Hello, Raspberry Pi!”)
3. Save the file by pressing CTRL + X, then Y, and hit Enter.
4. Run the script:
python3 hello.py
5. You should see:
Hello, Raspberry Pi!
3. Controlling Raspberry Pi GPIO Pins with Python
One of the most exciting things about the Raspberry Pi is its ability to control external hardware using the GPIO pins. Let’s blink an LED using Python and the RPi.GPIO library.
What you need:
- Raspberry Pi (any model with GPIO pins)
- An LED
- A 330Ω resistor
- Jumper wires
- A breadboard
Wiring diagram (GPIO Pin 17)
Raspberry Pi GPIO → Resistor (330Ω) → LED → Ground
Step 1: Install RPi.GPIO Library
Before writing the script, install the RPi.GPIO library (if not already installed):
sudo apt install python3-rpi.gpio
Step 2: Write the Python script
1. Open a new script:
nano blink.py
2. Enter the following code:
import RPi.GPIO as GPIO
import time
# Set the GPIO mode
GPIO.setmode(GPIO.BCM)
# Define the GPIO pin for the LED
LED_PIN = 17
# Set up the LED pin as an output
GPIO.setup(LED_PIN, GPIO.OUT)
print(“Blinking LED… Press Ctrl+C to stop.”)
try:
while True:
GPIO.output(LED_PIN, GPIO.HIGH) # Turn LED on
time.sleep(1) # Wait 1 second
GPIO.output(LED_PIN, GPIO.LOW) # Turn LED off
time.sleep(1) # Wait 1 second
except KeyboardInterrupt:
print(“\nExiting… Cleaning up GPIO.”)
GPIO.cleanup() # Reset GPIO settings
3. Save and run the script:
python3 blink.py
The LED should blink on and off every second. (Note: This code may not work properly on Raspberry Pi 5, you would need to make some adjustments to control the LED).
4. To stop the program, press Ctrl + C.
4. Reading sensor data from a button
Let’s modify our script to detect button presses using a push button connected to GPIO pin 18.
Wiring the button
- Connect one leg of the button to GPIO 18.
- Connect the other leg to Ground (GND).
Python Script for button press detection
1. Open a new file:
nano button.py
2. Enter the following code:
import RPi.GPIO as GPIO
import time
# Set GPIO mode
GPIO.setmode(GPIO.BCM)
# Define button pin
BUTTON_PIN = 18
# Set up the button pin as an input with a pull-down resistor
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print(“Press the button…”)
try:
while True:
if GPIO.input(BUTTON_PIN) == GPIO.HIGH:
print(“Button Pressed!”)
time.sleep(0.5) # Debounce delay
except KeyboardInterrupt:
print(“\nExiting… Cleaning up GPIO.”)
GPIO.cleanup()
3. Save and run:
python3 button.py
When you press the button, the terminal should display “Button Pressed!”
5. Advanced Raspberry Pi Python projects
Now that you’re familiar with Python on Raspberry Pi, here are some advanced projects to try:
IoT sensor dashboard – Use the Raspberry Pi to collect temperature, humidity, or motion data and display it on a web dashboard.
Smart home automation – Control lights, fans, or security systems using Python scripts and IoT services like Home Assistant or MQTT.
AI & computer vision – Use OpenCV with the Raspberry Pi Camera to create face recognition or object detection projects.
Web-controlled Raspberry Pi – Set up a Flask web server to control GPIO pins from a browser or mobile app.
Robotics & automation – Program motors and sensors to build autonomous robots using Python.
Are you ready to start using Python on your Raspberry Pi?
The Raspberry Pi + Python is an excellent combination for learning programming, electronics, and IoT development. Whether you’re blinking LEDs, controlling sensors, or creating complex automation projects, Python makes Raspberry Pi easy to use and powerful.
Recommendations:
- Experiment with different sensors (temperature, motion, ultrasonic, etc.).
- Explore Python libraries like OpenCV (for computer vision) or Flask (for web control).
- Try building IoT projects by connecting your Raspberry Pi to the cloud.
Finally, here are some Raspberry Pi articles you can also check:
Do you want to build an IoT project based on Raspberry Pi? Click on the button below and we can talk about your project.