MYP Design Physical Computing and Microcontrollers Notes
Previous
Next
Physical Computing Is Code That Touches the World
Physical computing means writing a program that reads real inputs and changes something physical, rather than only drawing on a screen.
A microcontroller is a whole small computer on one board with pins along the edge, built to run a single program with no operating system underneath.
It starts in under a second and draws so little power that a pair of AAA cells will run a micro:bit project for weeks.
The shape of the program is always the same three parts: an input, some logic, an output.
Going this route for a design project means a real user can hold your prototype and press it, which is what Creating the solution rewards.
Analogy
A microcontroller is closer to a light switch with a memory than to a laptop.
It does one job from the moment it gets power until the moment it loses it.
Three Boards, Three Reasons to Pick One
The BBC micro:bit already carries a 5 by 5 LED display, two buttons, an accelerometer and a compass, so it does something useful with nothing plugged in.
You program it in MakeCode blocks or in MicroPython, which makes it the quickest route from an idea to a thing that works.
The Arduino Uno has no sensors of its own but gives you 14 digital pins, 6 analogue inputs and a huge range of add on boards built to fit it.
Arduino is written in C++ in the Arduino IDE, which is stricter about spelling and gives you finer control over timing.
The Raspberry Pi Pico runs MicroPython or C++, costs a few pounds, and has far more memory and speed, which suits a project logging thousands of readings.
Choose by what the project needs, so a wearable step counter fits a micro:bit while a greenhouse data logger fits a Pico.
Tip
Prototype the logic on a micro:bit even when the finished product will use an Arduino.
The thinking transfers directly, because read, decide, act is the same shape on every board.
Digital and Analogue Pins Do Different Jobs
A digital pin reads or writes only two states, which your code sees as 0 and 1.
A push button, a reed switch and a plain LED are all digital, since each one is either on or off.
An analogue pin reads a whole range, handing back 0 to 1023 on both a micro:bit and an Arduino Uno.
A light sensor, a potentiometer and most temperature sensors are analogue, because "fairly bright" is a genuine answer.
Analogue output is faked with PWM, which flicks a pin on and off hundreds of times a second so an LED looks dimmed to your eye.
A digital input left unconnected floats between states, which is why a button needs a pull up resistor to hold it at a known value, wired as set out in the electronic components and sensors articles.
Reading a Sensor and Acting on It
Reading a sensor is one line, and in MicroPython on a micro:bit it is light = pin1.read_analog().
Sensors hand back raw numbers rather than units, so a reading of 512 means nothing until you compare it with something you trust.
Calibrate by writing down the raw reading in two known conditions, such as inside a dark cupboard and on a lit desk.
Set your threshold between those two numbers, which is how you arrive at a line like if light < 300.
Average five readings when the value jumps about, since one stray reading should never switch a motor on.
Example
MicroPython: light = pin1.read_analog()
if light < 300: pin0.write_digital(1)
else: pin0.write_digital(0)
Setup and Loop: the Shape of Every Physical Program
Every physical computing program splits into a part that runs once and a part that runs forever.
Arduino names them outright, since setup() runs once at power on and loop() repeats for as long as the board has power.
setup() is where you declare which pins are inputs and which are outputs, and open the serial connection you will debug through.
loop() reads the inputs, decides, then drives the outputs, in that order, hundreds of times a second.
MicroPython uses the same shape without the names, with your settings at the top of the file and a while True: block underneath.
Anything slow inside that loop, such as a 2 second sleep, stops every other check for those 2 seconds.
Common Mistake
A long sleep in the main loop means a button pressed during it is missed entirely.
Count up how much time your loop spends waiting before you decide the button is faulty.
Outputs, Power and Where the Circuit Lives
Driving an output is one line too, such as pin0.write_digital(1) for an LED or pin0.write_analog(512) for roughly half brightness through PWM.
A servo takes an angle instead, so the MakeCode block "servo write pin P0 to 90" swings it to the centre.
Code cannot supply current, so a motor that stutters is a power and wiring problem rather than a fault in your program.
Test each output with a three line program on its own before you join it to the rest of the project.
The components, resistor values and circuit diagrams belong to the electronic components and sensors articles, which is where to go when the code is right and the hardware still is not.
Keep one photo of the working circuit beside the screenshot of your code in your design folder, so the two can be checked against each other later.
Active recall
What does a micro:bit give you that an Arduino Uno does not, straight out of the box?
Which pin type would you use for a potentiometer, and what range of values does it give?
How do you turn a raw sensor reading into a threshold you can trust?
What belongs in setup() rather than in loop()?
Why does a 2 second sleep inside the main loop cause missed button presses?