💡 Your First Program
Your First Program
Welcome to programming! Today you'll write your first program and make an LED blink on a Raspberry Pi.
What is Programming?
Programming is giving instructions to computers. Just like giving directions to a friend, you tell the computer exactly what to do, step by step!
When you use apps like Grab, Shopee, or WhatsApp, you're using programs that someone created with code!
What is Raspberry Pi?
Raspberry Pi is a small, affordable computer that you can connect to hardware like LEDs, sensors, and motors. It's perfect for learning programming and building cool projects!
Small and Affordable
Fits in your hand and costs much less than a regular computer!
Hardware Ready
Connect to real hardware - LEDs, buttons, sensors, motors
Python Friendly
Learn Python programming easily - it's built-in!
Build Projects
Create amazing projects - from simple to advanced
Setting Up Your Raspberry Pi
Before you start programming, you need to set up your Raspberry Pi:
- Connect Hardware: Connect your Raspberry Pi to a monitor, keyboard, and mouse
- Insert SD Card: Insert the SD card with Raspberry Pi OS (operating system)
- Power On: Power it on and wait for it to start (you'll see the desktop)
- Open Editor: Open the terminal or Thonny (Python editor) - you can find Thonny in the Programming menu
Introduction to Python
Python is a programming language that's easy to read and write. It's perfect for beginners because it looks almost like English!
Hello, World!
Every programmer starts with this simple program. It prints a message to the screen!
Output: Hello, World!
Try it: Type this in Thonny and click the Run button (green play icon). You should see "Hello, World!" appear!
Variables: Storing Information
Variables are like boxes that store information. You give them a name and put a value inside! Think of it like labeling a box with "toys" and putting toys inside.
Example: Creating Variables
What happens:
- We create three variables:
name,age, andlikes_programming - We store different types of information in each
- We use
print()to display the values
Output:
Basic Data Types
Python uses different types of data. Here are the most common ones:
String (Text)
Words and sentences, always in quotes
Number
Integers (5) and decimals (3.14)
Boolean
True or False (yes or no)
Project: Blink an LED
Now let's build your first hardware project! We'll make an LED blink on and off. This is a classic first project that teaches you how to control hardware with code.
Materials Needed:
- Raspberry Pi (any model - Pi 3, Pi 4, or Pi Zero)
- LED (any color - red, green, blue, or white)
- Resistor (220Ω recommended - this protects the LED)
- Breadboard (makes connections easier)
- Jumper wires (male-to-female recommended)
Wiring Instructions:
Important: LEDs have two legs - a long one (positive/anode) and a short one (negative/cathode).
- Connect the LED: Place the LED in the breadboard. The long leg (anode) goes to one row, the short leg (cathode) goes to another row.
- Add Resistor: Connect one end of the resistor to the same row as the LED's long leg. Connect the other end to a different row.
- Connect to GPIO 18: Use a jumper wire to connect the resistor (the end not connected to LED) to GPIO pin 18 on the Raspberry Pi.
- Connect to Ground: Use another jumper wire to connect the LED's short leg (cathode) to GND (ground) on the Raspberry Pi. Look for any pin labeled "GND".
- Double Check: Make sure all connections are secure and nothing is loose!
Simple Wiring Diagram:
Raspberry Pi GPIO 18 → Resistor → LED (long leg)
Raspberry Pi GND → LED (short leg)
The Code:
Now let's write the Python code to make the LED blink:
Understanding the Code:
- import RPi.GPIO as GPIO: This brings in the library to control GPIO pins
- import time: This lets us add delays (waiting)
- GPIO.setmode(GPIO.BCM): Sets the pin numbering system
- GPIO.setup(18, GPIO.OUT): Sets pin 18 as an output (we're sending signals out)
- GPIO.HIGH: Turns the pin on (sends power to LED)
- GPIO.LOW: Turns the pin off (stops power to LED)
- time.sleep(1): Waits for 1 second
- while True: Creates a loop that runs forever
- KeyboardInterrupt: When you press Ctrl+C, it stops and cleans up
- Open Thonny (Python editor) on your Raspberry Pi
- Type or paste the code above
- Click the Run button (green play icon)
- Watch your LED blink!
- Press Ctrl+C to stop the program
Common Mistakes to Avoid
⚠️ Watch Out For:
- Wrong Indentation: Python uses spaces/tabs to show code structure. Make sure your code inside loops and functions is indented correctly!
- Missing Colon: Don't forget colons (:) after
if,for,while, anddefstatements - Typo in Name: Variable names are case-sensitive.
Nameandnameare different! - Not Imported: Remember to import libraries like
RPi.GPIOandtimeat the top of your code - Wrong Pin Number: Make sure you're using the correct GPIO pin number (we used 18)
- Loose Connections: Check that all wires are connected securely
Summary
You've learned:
- ✅ Programming means giving instructions to computers
- ✅ Raspberry Pi is a small computer perfect for hardware projects
- ✅ Python is an easy-to-learn programming language
- ✅ Variables store information with names (like labeled boxes)
- ✅ Python has different data types: strings, numbers, and booleans
- ✅ You can control hardware like LEDs with Python code
- ✅ GPIO pins let you connect and control external hardware
🎮 Try It: Write Your First Code!
Practice writing Python code. Try these challenges:
📝 Challenge 1: Print Your Name
Write code to print your name on the screen:
Hint: Use print("Your Name") - don't forget the quotes!
📝 Challenge 2: Create Variables
Create variables to store:
- Your age (as a number, no quotes)
- Your favorite food (as text, with quotes)
- Whether you like programming (True or False, no quotes)
Then print all three variables:
📝 Challenge 3: Print Multiple Lines
Write code that prints three different messages, one on each line:
print() to display information, and use = to assign values to variables! Remember: text needs quotes, numbers don't.
🎯 Activity: LED Blinking Project
What You'll Build:
Build a working LED that blinks on and off continuously! This is your first hands-on hardware project.
Step-by-Step Instructions:
- Gather Materials: Get your Raspberry Pi, LED, resistor (220Ω), breadboard, and jumper wires
- Set Up Breadboard: Place the LED in the breadboard. Remember: long leg (anode) in one row, short leg (cathode) in another row
- Add Resistor: Connect the resistor between the LED's long leg and a new row
- Connect to GPIO: Use a jumper wire to connect the resistor to GPIO pin 18 on your Raspberry Pi
- Connect to Ground: Use another jumper wire to connect the LED's short leg to GND on your Raspberry Pi
- Double Check: Make sure all connections are secure - no loose wires!
- Write the Code: Open Thonny and type (or copy) the LED blinking code from the Learn tab
- Run the Code: Click the Run button and watch your LED blink!
- Experiment: Try changing the
time.sleep(1)values to make it blink faster or slower
Testing Checklist:
- ✅ LED turns on when code runs
- ✅ LED turns off after 1 second
- ✅ LED continues blinking in a loop
- ✅ Program stops when you press Ctrl+C
for loop instead of while True)
Bonus Challenge Solution:
Here's how you can make it blink 5 times:
💪 Practice Challenges
Challenge 1: Print Multiple Messages
Write code that prints three different messages, one on each line:
Your code should: Use three separate print() statements
Challenge 2: Variable Practice
Create variables for:
- Your school name (as a string)
- Number of students in your class (as a number)
- Your favorite subject (as a string)
Then print all three using one print() statement! (Hint: you can put multiple things in one print by separating them with commas)
Challenge 3: LED Patterns
Modify the LED code to create different patterns:
- Pattern A: Blink 3 times fast (0.2 seconds), then 3 times slow (1 second)
- Pattern B: Blink in a pattern: on for 1 second, off for 0.5 seconds, repeat forever
- Pattern C: Blink 10 times total, then stop
Challenge 4: Code Detective
What's wrong with this code? Find and fix the mistakes!
Hints:
- Variable names can't have spaces
- Variable names are case-sensitive
Click to see the answer
Challenge 5: Create Your Own
Create a program that:
- Stores your name, age, and favorite hobby in variables
- Prints a sentence about yourself using those variables
- Example output: "Hi! I'm Ali, I'm 12 years old, and I love programming!"