0
0
Raspberry Piprogramming~10 mins

Python on Raspberry Pi

Choose your learning style9 modes available
Introduction

Python on Raspberry Pi lets you control hardware and create fun projects easily. It helps you learn programming and electronics together.

You want to blink an LED light using code.
You want to read sensor data like temperature or motion.
You want to build a small robot controlled by Python.
You want to create a simple home automation system.
You want to learn programming with real hardware feedback.
Syntax
Raspberry Pi
print('Hello, Raspberry Pi!')
Python code runs on Raspberry Pi just like on any computer.
You can use libraries to control hardware pins and sensors.
Examples
This prints a message on the screen.
Raspberry Pi
print('Hello, Raspberry Pi!')
This turns on an LED connected to pin 18.
Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
This counts from 0 to 4 with a 1-second pause each time.
Raspberry Pi
import time
for i in range(5):
    print(f'Count: {i}')
    time.sleep(1)
Sample Program

This program blinks an LED connected to pin 18 five times. It turns the LED on and off with half-second pauses. Finally, it cleans up the pin settings.

Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
LED_PIN = 18
GPIO.setup(LED_PIN, GPIO.OUT)

print('Blinking LED 5 times')
for _ in range(5):
    GPIO.output(LED_PIN, GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output(LED_PIN, GPIO.LOW)
    time.sleep(0.5)

GPIO.cleanup()
print('Done')
OutputSuccess
Important Notes

Always call GPIO.cleanup() to reset pins after your program ends.

Use GPIO.setmode(GPIO.BCM) to refer to pins by their Broadcom number.

Make sure your LED is connected with a resistor to avoid damage.

Summary

Python on Raspberry Pi lets you write code to control hardware easily.

You can use the RPi.GPIO library to work with pins and devices.

Simple programs can blink LEDs, read sensors, and make projects come alive.