Beginner Level • Lesson 1

💡 Your First Program

⏱️ 45 minutes 📚 Beginner 🎯 Programming Basics
💡

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!

💡 Fun Fact: Every app on your phone, every game you play, and every website you visit was created by programmers!

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:

  1. Connect Hardware: Connect your Raspberry Pi to a monitor, keyboard, and mouse
  2. Insert SD Card: Insert the SD card with Raspberry Pi OS (operating system)
  3. Power On: Power it on and wait for it to start (you'll see the desktop)
  4. Open Editor: Open the terminal or Thonny (Python editor) - you can find Thonny in the Programming menu
💡 Tip: If you're new to Raspberry Pi, ask your teacher for help with the initial setup!

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!

print("Hello, World!")

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

name = "Ali" age = 12 likes_programming = True print(name) print(age) print(likes_programming)

What happens:

  • We create three variables: name, age, and likes_programming
  • We store different types of information in each
  • We use print() to display the values

Output:

Ali 12 True

Basic Data Types

Python uses different types of data. Here are the most common ones:

📝

String (Text)

Words and sentences, always in quotes

"Hello" "Python" "Malaysia"
🔢

Number

Integers (5) and decimals (3.14)

42 3.14 100

Boolean

True or False (yes or no)

True False

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).

  1. 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.
  2. 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.
  3. 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.
  4. 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".
  5. 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)

⚠️ Safety Tip: Always use a resistor with LEDs! Without it, too much current flows and the LED can be damaged. The resistor limits the current to a safe level.

The Code:

Now let's write the Python code to make the LED blink:

import RPi.GPIO as GPIO import time # Set up GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) # Blink the LED try: while True: GPIO.output(18, GPIO.HIGH) # Turn LED on time.sleep(1) # Wait 1 second GPIO.output(18, GPIO.LOW) # Turn LED off time.sleep(1) # Wait 1 second except KeyboardInterrupt: GPIO.cleanup() # Clean up on exit

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
💡 How to Run:
  1. Open Thonny (Python editor) on your Raspberry Pi
  2. Type or paste the code above
  3. Click the Run button (green play icon)
  4. Watch your LED blink!
  5. 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, and def statements
  • Typo in Name: Variable names are case-sensitive. Name and name are different!
  • Not Imported: Remember to import libraries like RPi.GPIO and time at 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
🎉 Congratulations! You've written your first program and controlled real hardware! In the next lesson, you'll learn how to make your program make decisions.

🎮 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:

💡 Tip: Use 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:

  1. Gather Materials: Get your Raspberry Pi, LED, resistor (220Ω), breadboard, and jumper wires
  2. Set Up Breadboard: Place the LED in the breadboard. Remember: long leg (anode) in one row, short leg (cathode) in another row
  3. Add Resistor: Connect the resistor between the LED's long leg and a new row
  4. Connect to GPIO: Use a jumper wire to connect the resistor to GPIO pin 18 on your Raspberry Pi
  5. Connect to Ground: Use another jumper wire to connect the LED's short leg to GND on your Raspberry Pi
  6. Double Check: Make sure all connections are secure - no loose wires!
  7. Write the Code: Open Thonny and type (or copy) the LED blinking code from the Learn tab
  8. Run the Code: Click the Run button and watch your LED blink!
  9. 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
🏆 Bonus Challenge: Can you make the LED blink exactly 5 times, then stop? (Hint: use a for loop instead of while True)

Bonus Challenge Solution:

Here's how you can make it blink 5 times:

import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) # Blink 5 times for i in range(5): GPIO.output(18, GPIO.HIGH) time.sleep(0.5) GPIO.output(18, GPIO.LOW) time.sleep(0.5) GPIO.cleanup()

💪 Practice Challenges

Challenge 1: Print Multiple Messages

Write code that prints three different messages, one on each line:

Example output: Hello! Welcome to Python! Let's learn programming!

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)

# Example: school = "SK Taman Jaya" students = 30 subject = "Science" print(school, students, subject)

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!

print('Hello World') my name = 'Ali' print(myName)

Hints:

  • Variable names can't have spaces
  • Variable names are case-sensitive
Click to see the answer
print('Hello World') my_name = 'Ali' # Fixed: no spaces, use underscore print(my_name) # Fixed: must match the variable name exactly

Challenge 5: Create Your Own

Create a program that:

  1. Stores your name, age, and favorite hobby in variables
  2. Prints a sentence about yourself using those variables
  3. Example output: "Hi! I'm Ali, I'm 12 years old, and I love programming!"