Module Introduction
Welcome back, robot builders! In Module 1, we learned what a robot is. In Module 2, we learned how to power a robot with electricity and circuits. Now comes the most exciting part – programming! Programming is how we give our robot instructions. It is like teaching a robot a language it understands. Without programming, a robot is just a pile of parts that do nothing. With programming, it comes to life! In this module, we will learn how to write simple programs to make robots move, sense, and make decisions. We will start with visual programming (like Scratch) and then look at text-based programming (like Python). By the end, you will be able to write your first robot programs. Let's bring your robot to life!
Tunde is 11 years old. He built a robot car with his uncle. It had wheels, a battery, and a small computer board called a micro:bit. But when he turned it on, the car just sat there. "Why won't it move?" he asked. His uncle smiled and said, "Because you haven't told it what to do yet! A robot needs instructions – that's called programming." Tunde opened his laptop and saw a colourful block-based program. He dragged a block that said "move forward" and connected it to a block that said "forever." He clicked "upload," and the car started moving! Tunde was amazed. He added more blocks: "if the light sensor sees darkness, turn left." His robot could now follow a line. Tunde learned that programming is like giving a robot a recipe – step by step, it does exactly what you tell it.
Definition: Programming is the process of giving a robot (or computer) a set of instructions to follow. These instructions are called a program.
Why it's important: Programming makes robots smart and useful. It tells them what to do, when to do it, and how to respond to the world.
Simple explanation: Programming is like writing a recipe for a robot. The recipe tells the robot step‑by‑step what to do.
Real‑life example: The program in a washing machine tells it when to fill with water, when to spin, and when to stop.
School example: You might use Scratch to program a character to move on the screen.
Home example: A programmable thermostat uses a program to turn the heater on and off.
Nigerian example: A Nigerian engineer writes a program for a drone to spray crops in a farm.
+------------------------------------------+ | What is Programming? | |------------------------------------------+ | - Giving instructions to a robot | | - Like a recipe for a robot | | - Tells the robot what to do | | - Makes robots smart and useful | | - Robots only do what they are | | programmed to do | +------------------------------------------+
Mini summary: Programming is giving a robot instructions. It is how we make robots useful.
Definition: Visual programming is a way to program using blocks that snap together like puzzle pieces. You don't need to type any words. Scratch is a popular visual programming language.
Why it's important: Visual programming is great for beginners because it is easy to learn and understand. You can see what each block does.
Simple explanation: Visual programming is like building with LEGO blocks. Each block has a job, and you snap them together to make a program.
Real‑life example: Scratch is a visual programming language used by millions of kids around the world.
School example: Many schools teach visual programming with Scratch to introduce coding.
Home example: You can use Scratch at home to make stories, games, and even control robots.
Nigerian example: Nigerian schools are starting to use Scratch to teach children how to code.
+------------------------------------------+ | Visual Programming Example | |------------------------------------------+ | [when green flag clicked] | | ↓ | | [move 10 steps] | | ↓ | | [turn right 15 degrees] | | ↓ | | [if touching edge? then] | | ↓ | | [bounce] | | | | Blocks snap together like puzzle pieces | +------------------------------------------+
Mini summary: Visual programming uses blocks that snap together. It is great for beginners.
Definition: Text‑based programming is when you type words and symbols to write a program. Python, C++, and JavaScript are examples of text‑based programming languages.
Why it's important: Text‑based programming is more powerful and flexible than visual programming. It is what professional programmers use.
Simple explanation: Instead of snapping blocks, you type instructions in a language the robot understands.
Real‑life example: Python is a text‑based language used to program robots, websites, and AI.
School example: Older students learn to write Python code to control robots.
Home example: You can write Python code to make a Raspberry Pi robot move.
Nigerian example: Nigerian university students learn C++ and Python to build advanced robots.
+------------------------------------------+ | Text‑Based Programming Example | |------------------------------------------+ | import time | | from robot import * | | | | while True: | | if light_sensor() > 50: | | move_forward() | | else: | | turn_left() | | time.sleep(0.1) | | | | This is Python code for a line‑follower | +------------------------------------------+
Mini summary: Text‑based programming uses typed instructions. It is more powerful but harder to learn.
Definition: An instruction is a single command that tells the robot to do one thing, like "move forward" or "turn on the LED."
Why it's important: A program is made of many instructions. The robot follows them one by one in order.
Simple explanation: Instructions are like steps in a recipe. You do step 1, then step 2, then step 3.
Real‑life example: A program to bake a cake would have instructions like "mix flour," "add eggs," "bake for 30 minutes."
School example: You write instructions to make a character move across the screen.
Home example: A robot vacuum follows instructions like "go forward," "turn right," "clean for 5 seconds."
Nigerian example: A drone program has instructions like "take off," "fly to location," "land."
+------------------------------------------+ | Instructions in a Program | |------------------------------------------+ | Program: | | Instruction 1: Start | | Instruction 2: Move forward 2 seconds | | Instruction 3: Turn right 90 degrees | | Instruction 4: Stop | | Instruction 5: End | | | | Robot follows them in order | +------------------------------------------+
Mini summary: Instructions are single commands. A program is a list of instructions the robot follows in order.
Definition: Sequence is the order in which instructions are executed. The robot follows instructions from top to bottom, one after another.
Why it's important: The order of instructions matters! If you tell a robot to turn before moving, it will act differently than if it moves first.
Simple explanation: Sequence is like a dance routine. You do step 1, then step 2, then step 3 – in that exact order.
Real‑life example: A recipe says "crack eggs, then whisk, then pour into the pan." You can't whisk first!
School example: A program that makes a robot dance has a specific sequence of moves.
Home example: A robot vacuum cleans in a sequence: move forward, turn, move forward, turn.
Nigerian example: A robot in a factory follows a sequence: pick up part, move right, place part, return.
+------------------------------------------+ | Sequence Example | |------------------------------------------| | Instruction 1: Move forward 1m | | Instruction 2: Turn left 90° | | Instruction 3: Move forward 0.5m | | Instruction 4: Turn right 90° | | Instruction 5: Stop | | | | This sequence makes the robot go around | | a corner and stop. | +------------------------------------------+
Mini summary: Sequence is the order of instructions. The robot follows them in that exact order.
Definition: A loop is a way to repeat a set of instructions multiple times. You can loop forever or a specific number of times.
Why it's important: Loops save us from writing the same instructions over and over. They let robots do repetitive tasks.
Simple explanation: A loop is like a record player that keeps playing the same song on repeat.
Real‑life example: A robot vacuum uses a loop: "clean for 5 seconds, turn 90°, repeat forever."
School example: You use a loop to make a sprite bounce around the screen forever.
Home example: A washing machine has a loop: "spin, drain, rinse" that repeats.
Nigerian example: A water pump robot uses a loop: "check moisture, if dry, water for 10 seconds, wait 5 minutes, repeat."
+------------------------------------------+ | Loop Example | |------------------------------------------+ | forever: | | move_forward() | | if obstacle_detected(): | | turn_left() | | end forever | | | | This loop makes the robot keep moving | | and turning when it sees obstacles. | | It never stops! | +------------------------------------------+
Mini summary: Loops repeat instructions. They are useful for repetitive tasks.
Definition: A condition is a rule that checks if something is true or false. The robot uses conditions to make decisions: "IF something is true, THEN do this."
Why it's important: Conditions allow robots to react to the world around them. They make robots "smart."
Simple explanation: A condition is like a question: "Is it raining?" If yes, take an umbrella. If no, don't.
Real‑life example: A thermostat has a condition: "IF temperature is below 20°C, THEN turn on the heater."
School example: In Scratch, you use "if-then" to make a sprite change direction when it hits the edge.
Home example: A nightlight uses: "IF it is dark, THEN turn on the light."
Nigerian example: A solar tracking robot uses: "IF light is brighter on the left, THEN turn left."
+------------------------------------------+ | Condition Example | |------------------------------------------+ | IF light_sensor() > 50 THEN | | move_forward() | | ELSE | | turn_left() | | END IF | | | | If light is bright, move forward. | | If light is dark, turn left. | | This helps a robot follow a line! | +------------------------------------------+
Mini summary: Conditions help robots make decisions. They use "IF-THEN" logic.
Definition: A variable is like a box where a program can store information, like a number or a word. You can change what is inside the box at any time.
Why it's important: Variables let robots remember things. For example, a robot can count how many steps it has taken.
Simple explanation: A variable is like a labelled jar. You can put things in it, take things out, or change what's inside.
Real‑life example: A program uses a variable called "score" to count points in a game.
School example: In Scratch, you use variables to keep score in a game.
Home example: A robot vacuum uses a variable to remember how much time it has cleaned.
Nigerian example: A farm robot uses a variable to store how many seeds it has planted.
+------------------------------------------+ | Variable Example | |------------------------------------------+ | steps = 0 | | steps = steps + 1 | | steps = steps + 1 | | print(steps) # This will show 2 | | | | The variable "steps" stores how many | | steps the robot has taken. | | It starts at 0 and increases by 1 | | each time the robot moves. | +------------------------------------------+
Mini summary: Variables are boxes that store information. Robots use them to remember things.
Definition: To move a robot, you program the motors to spin. Writing code to control motors is called "motor control."
Why it's important: Motors make robots move. Programming them is how we tell robots where to go.
Simple explanation: Programming a motor is like telling a car to go forward, reverse, or stop.
Real‑life example: A self-driving car's program tells the motors when to speed up and when to turn.
School example: You write code to make a robot car drive in a square.
Home example: A robot mower uses motor programs to move in patterns.
Nigerian example: A robot used for delivery uses motor programs to navigate streets.
+------------------------------------------+ | Programming a Motor | |------------------------------------------+ | // Micro:bit with motor driver | | pins.analog_write(0, 255) // Left motor forward | | pins.analog_write(1, 0) // Left motor stop | | pins.analog_write(2, 255) // Right motor forward| | pins.analog_write(3, 0) // Right motor stop | | | | This code makes the robot move forward. | | Change the pins to make it turn. | +------------------------------------------+
Mini summary: Programming motors controls how a robot moves. You write code to go forward, back, or turn.
Definition: To use a sensor, you write code that reads the sensor's data. The data might be a number (like light level) or a yes/no answer (like touch).
Why it's important: Reading sensor data is how robots "see" the world. They use the data to make decisions.
Simple explanation: Reading a sensor is like asking a robot "what do you see?" and it answers with a number.
Real‑life example: A camera sensor sends data to a computer to recognize faces.
School example: You read a light sensor to make a robot follow a line.
Home example: A motion sensor sends data to turn on a light.
Nigerian example: A soil moisture sensor sends data to decide when to water crops.
+------------------------------------------+
| Reading a Sensor Example |
|------------------------------------------|
| // Reading a light sensor on micro:bit |
| light_level = pins.analog_read(0) |
| if light_level > 500: |
| display.show("Bright") |
| else: |
| display.show("Dark") |
| |
| The sensor reads a number. |
| The program decides what to do based |
| on that number. |
+------------------------------------------+
Mini summary: Programming sensors means reading their data. Robots use sensor data to understand the world.
Definition: A line‑following robot uses a light sensor to follow a dark line on a light surface. The program reads the sensor and tells the motors how to steer.
Why it's important: This is a classic robot project that teaches you how to combine sensors, motors, and programming logic.
Simple explanation: The robot looks at the floor, sees the line, and turns to stay on it.
Real‑life example: Robots in factories follow lines on the floor to carry goods.
School example: A robotics competition where robots race to follow a line track.
Home example: A toy robot that follows a drawn line.
Nigerian example: A robot used in a warehouse to follow a path and deliver items.
+------------------------------------------+ | Line‑Following Program (Simplified) | |------------------------------------------+ | while True: | | left_sensor = read_left_sensor() | | right_sensor = read_right_sensor() | | | | if left_sensor > 500 and right_sensor > 500: | | move_forward() | | elif left_sensor < 500 and right_sensor > 500: | | turn_left() | | elif left_sensor > 500 and right_sensor < 500: | | turn_right() | | else: | | stop() | | | | This program makes the robot follow a | | line by comparing two sensor readings. | +------------------------------------------+
Mini summary: A line‑following program uses sensors and motor control to keep a robot on a line.
Definition: Uploading means sending the program from your computer to the robot's controller (like Arduino or micro:bit).
Why it's important: The robot needs the program to work. Uploading is how you put the program into the robot's brain.
Simple explanation: Uploading is like copying a recipe from a book into the robot's memory.
Real‑life example: You connect a micro:bit to a computer with a USB cable and press "upload."
School example: Students upload programs to their robots during class.
Home example: You upload a program to a robot kit to test it.
Nigerian example: Nigerian robotics teams upload code to their robots before competitions.
+------------------------------------------+ | Uploading Code | |------------------------------------------+ | Computer | | | | | | USB Cable | | | | | v | | Robot Controller (Arduino/micro:bit) | | | | | v | | The program is copied into the robot's | | memory. The robot now follows the | | instructions in the program. | +------------------------------------------+
Mini summary: Uploading sends your program from the computer to the robot. The robot then follows your instructions.
Definition: Debugging is the process of finding and fixing errors (called "bugs") in a program.
Why it's important: Programs often have mistakes. Debugging is how you make them work correctly.
Simple explanation: Debugging is like being a detective. You look for clues to find the problem and fix it.
Real‑life example: If a robot moves left instead of right, you check the code and find the motor pins are swapped.
School example: A student finds a bug in their Scratch game and fixes it.
Home example: A robot doesn't stop at the line – you check the code and add a condition.
Nigerian example: A robotics team tests their robot and finds a sensor reading issue – they debug it.
+------------------------------------------+ | Debugging Steps | |------------------------------------------+ | 1. Observe the problem | | (Robot is not turning) | | 2. Check the code | | (Look at the turn instruction) | | 3. Identify the bug | | (The turn angle is too small) | | 4. Fix the bug | | (Change the angle) | | 5. Test again | | (Robot now turns correctly) | | 6. Repeat if needed | +------------------------------------------+
Mini summary: Debugging is finding and fixing mistakes in a program. It is part of every programmer's job.
Definition: A programming language is the set of words, symbols, and rules used to write programs. Different robots use different languages.
Why it's important: Knowing different languages helps you program different robots.
Simple explanation: Programming languages are like human languages – English, French, Yoruba. Robots understand different ones.
Real‑life example: Arduino uses C++. micro:bit uses Python or Blocks. LEGO robots use their own language.
School example: Students learn Scratch first, then Python.
Home example: A robot kit might use a simplified version of Python.
Nigerian example: Nigerian university students learn C, C++, and Python for robotics.
+------------------------------------------+ | Common Robot Programming Languages | |------------------------------------------+ | - Scratch: Visual blocks, beginners | | - Python: Text-based, easy to learn | | - C++: Text-based, fast and powerful | | - Arduino: Based on C/C++ | | - Blockly: Visual, like Scratch | | - MakeCode: Visual for micro:bit | | | | Each language has its own strengths. | +------------------------------------------+
Mini summary: Different robots use different programming languages. Scratch and Python are good for beginners.
In this module, we learned how to program robots. We discovered that programming is giving a robot instructions. We explored visual programming with blocks and text‑based programming with code. We learned about sequence (order of instructions), loops (repeating instructions), conditions (making decisions), and variables (storing information). We wrote programs to move motors, read sensors, and even follow a line. We also learned about uploading code and debugging. You now have the skills to bring your robot to life with code!
Concept 1: Programming is giving instructions. Without instructions, a robot does nothing.
Concept 2: Sequence matters. The order of instructions changes what the robot does.
Concept 3: Loops save time. Use loops to repeat instructions instead of writing them many times.
Concept 4: Conditions make robots smart. They let robots react to the world.
Concept 5: Debugging is normal. All programmers make mistakes. Fixing them is part of the process.
How to write a program to make a robot move in a square:
How to debug a program:
We've seen many real‑life examples in the lessons, such as washing machine programs, thermostats, self‑driving cars, factory robots, and delivery robots. These show how programming powers the robots around us.
In Nigeria, students and engineers are writing programs for drones, farm robots, and delivery robots. Nigerian robotics teams are competing internationally, writing code in Python and C++. Learning to program robots opens up many opportunities in Nigeria's growing technology sector.
Imagine you are the director of a robot show. Your robot is the dancer. You write a program that tells the robot to: "move left, spin, move right, wave, repeat." The robot performs the dance perfectly every time!
Another fun example: You write a program that makes a robot sing a song. The program tells the robot to beep at different times and for different lengths. You have made a robot musician!
Key points: Emphasise that programming is about giving clear, step‑by‑step instructions. Use analogies like recipes and dance routines. Start with visual programming (like Scratch) before moving to text‑based. Encourage students to experiment and make mistakes – that's how they learn.
Activity idea: Have students write a program (in Scratch or pseudocode) to make a robot navigate a simple maze.
Parents can help children learn programming by exploring Scratch or Blockly together. Support them in making mistakes and debugging. Encourage them to think step‑by‑step. Celebrate their successes. There are many online resources and robot kits that are perfect for beginners.
Did you know that the code for self‑driving cars can have millions of lines of code? That's a lot of instructions!
Did you know that the first robot to be programmed was called "Unimate" and was used in a car factory in 1961?
+------------------------------------------+ | Programming Flowchart | |------------------------------------------+ | +-------------------+ | | | Start Program | | | +-------------------+ | | ↓ | | +-------------------+ | | | Read Sensors | | | +-------------------+ | | ↓ | | +-------------------+ | | | Process Data | | | +-------------------+ | | ↓ | | +-------------------+ | | | Make Decision | | | +-------------------+ | | ↓ | | +-------------------+ | | | Send to Motors | | | +-------------------+ | | ↓ | | +-------------------+ | | | Repeat Loop |--------------------+ | +-------------------+ | | | | This is the basic cycle of a robot | | program: sense, think, act, repeat. | +------------------------------------------+ +------------------------------------------+ | Line-Following Robot Program | |------------------------------------------+ | Start | | ↓ | | Read left sensor | | ↓ | | Read right sensor | | ↓ | | Are both on line? Yes → Move forward | | Is left off line? Yes → Turn left | | Is right off line? Yes → Turn right | | ↓ | | Go back to "Read left sensor" | | | | This flowchart shows the program logic | | for a line‑following robot. | +------------------------------------------+
| Feature | Visual Programming | Text‑Based Programming |
|---|---|---|
| How it works | Blocks snap together | Typed words and symbols |
| Ease of learning | Very easy | Harder |
| Power | Limited | Very powerful |
| Example | Scratch, Blockly | Python, C++, Java |
| Best for | Beginners, kids | Professionals, advanced |
| Concept | What it does | Example |
|---|---|---|
| Sequence | Follows instructions in order | Step 1, Step 2, Step 3 |
| Loop | Repeats instructions | forever: move_forward() |
| Condition | Makes a decision | IF light > 50 THEN turn |
| Variable | Stores information | count = 5 |
In this module, we learned the essential skill of programming for robotics. We discovered that programming is giving a robot instructions to follow. We explored visual programming using blocks and text‑based programming using code. We learned about sequence (the order of instructions), loops (repeating instructions), conditions (making decisions), and variables (storing information). We wrote simple programs to make motors move and sensors read data. We created a line‑following robot program, learned how to upload code, and how to debug mistakes. You now have the power to bring any robot to life with code. Keep practicing and experimenting – the more you program, the better you become!
Match the term with its definition.
| Term | Definition |
|---|---|
| Loop | a) The order of instructions |
| Condition | b) Repeats instructions |
| Variable | c) Makes a decision |
| Sequence | d) Stores information |
| Debugging | e) Finding and fixing mistakes |
Answers: Loop – b, Condition – c, Variable – d, Sequence – a, Debugging – e
Scenario 1: You have a robot that should move forward for 3 seconds, then turn right, then move forward for another 3 seconds. Instead, it moves forward for 3 seconds, then turns left. What might be wrong with the program?
Scenario 2: You are programming a robot to follow a line. The robot keeps going straight even when the line curves. What part of the program might be incorrect?
"Program a Robot Dance" – In groups of 4, write a program (in Scratch or pseudocode) to make a robot perform a dance. It should include at least 5 different moves, a loop, and a condition. Present your dance to the class.
Write a simple program in pseudocode for a robot that avoids obstacles. The robot should move forward, and if it sees an obstacle (using a touch sensor), it should turn left and continue.
"Build a Simple Robot Program" – Design a program for a robot that can navigate a 2‑meter square track. The robot should start at the beginning, go around the track, and return to the start. Write the program in pseudocode or Scratch.
Use a robot kit (like micro:bit or Arduino) to write a program that makes the robot move forward, then stop when it encounters an obstacle (use a touch sensor or distance sensor). Upload the program and test it.
"Line‑Following Challenge" – Write a program that makes a robot follow a line. The line should have a 90‑degree turn. The robot must successfully follow the entire track. Use two light sensors for better accuracy.
In Module 4, we will dive into Sensors and Actuators in Depth. You will learn about different types of sensors (light, ultrasonic, touch, sound) and actuators (motors, servos, LEDs) in detail. We will explore how to choose the right sensor for the right task and how to connect them to your robot. You'll also learn advanced techniques like sensor calibration and using multiple sensors together. Get ready to make your robot even smarter!