Microcontrollers, flowcharts & programming
Engineering Science · Higher · mrkellydt.co.uk
What you're learning in this topic
Learning intention WALT
We are learning about microcontrollers, flowcharts & programming, specifically to be able to:
- draw flowcharts with start/stop, input, output, decision and loop symbols
- use fixed loops, continuous loops and conditional loops
- write programs using high, low, pause, if…then, for…next
- use subroutines, variables and analogue inputs in a program
- find faults in a program and correct them
Success criteria WILF
- I can draw flowcharts with start/stop, input, output, decision and loop symbols
- I can use fixed loops, continuous loops and conditional loops
- I can write programs using high, low, pause, if…then, for…next
- I can use subroutines, variables and analogue inputs in a program
- I can find faults in a program and correct them
Microcontrollers
A microcontroller is a small programmable computer on a chip, with inputs and outputs, used in washing machines, microwaves, toys, traffic lights, car systems and industrial machines.
- Advantages over hard-wired circuits: reprogrammed without changing the circuit, so products can be changed or improved quickly; fewer components, so smaller, cheaper, quicker to assemble and more reliable (fewer parts to fail); one chip replaces many parts, so less stock and fewer resources used; can be reused, so less waste; can carry out complex sequences, counting and timing.
- Disadvantages: needs a computer, software and programming skills; software can be expensive; the chip can cost more than simple ICs; a fault in the program can stop the whole system; it may need driver circuits for high-current outputs.
Flowchart symbols
- Start / stop: rounded box (terminator).
- Output: parallelogram, e.g. "pin 7 on", "pin 7 off".
- Process / delay: rectangle, e.g. "wait 0.5 s". Always give a unit.
- Decision: diamond with Y and N exits, e.g. "pin 1 on?" or "repeated 6 times?".
Checking an input is a decision, not an output: the 2025 report saw input checks drawn as parallelograms. Give pin numbers and delay units, and make every loop arrow return to the right place.
Loops
- Continuous (infinite) loop: the program returns to the start forever.
- Fixed (finite) loop: repeats a set number of times, e.g. "for b0 = 1 to 15 … next b0", then carries on. The 2025 N5 report: most didn't name a fixed loop.
- A decision that loops back to itself waits for an input.
Program commands (PBASIC style)
main:
if pin1 = 1 then flash ' wait for the switch
goto main
flash:
for b0 = 1 to 6 ' fixed loop: 6 times
high 7 ' LED on
pause 500 ' 0.5 s (pause is in ms)
low 7 ' LED off
pause 500
next b0
goto main ' continuous loop
Other languages are fine (e.g. Arduino digitalWrite, delay). You won't be asked to write code from scratch, but you may read, complete or correct it, and flowchart answers must be detailed.
Design challenge: pedestrian crossing
The brief. Design the control for a crossing outside an Inverness primary school: traffic lights stay green until a button is pressed, then change to let people cross.
- Draw a flowchart for one complete cycle.
- Use a loop so the system keeps running.
- State suitable time delays for each stage.
- Write the program in PBASIC-style commands.
What a good answer includes
- Start, loop: check button (decision), then amber → red with the green man → amber → green.
- A continuous loop back to the button check.
- e.g. amber 3 s, red with green man 10 s; pause is in milliseconds (pause 3000).
- high/low for each output pin, if…then for the input, pause for delays, goto for the loop.
Higher programming HIGHER
- PWM (pulse-width modulation): switching a motor on and off rapidly; the average power depends on the mark-to-space ratio (duty cycle). A longer "mark" (on time) gives a faster motor. Draw PWM signals with all marks at the same voltage and at least three marks.
- Analogue inputs: read a sensor value (e.g. 0–255) and compare it with a threshold to make a decision.
- Up to four inputs and four outputs; logic (AND/OR) and arithmetic (counting, adding) in decisions.
- Microcontrollers controlling solenoid valves give electro-pneumatic systems.
Sub-procedures, counting and analogue inputs HIGHER
main:
let b1 = 5
gosub flash ' call the sub-procedure
pause 2000
end ' stops it running into the sub-procedure
flash:
for b2 = 1 to b1 ' b1 sets the number of flashes
high 7
pause 100
low 7
pause 100
next b2
return ' back to the line after gosub- A sub-procedure is a section of code that can be called from different places with gosub and ends with return. It saves writing the same code twice.
- Arithmetic: count events with a variable, e.g. let b0 = b0 + 1 each time a sensor is triggered, then if b0 = 10 then … to act on the total (stock counters, car parks, lifts).
- Analogue inputs: an ADC converts a voltage into a number from 0 to 255, e.g. readadc 0, b1. Reading = Vin ÷ Vref × 255, so 2 V with a 5 V reference gives 102. Compare the reading with a threshold to decide what to do.
- Signal conditioning: if a sensor's voltage doesn't fit the ADC range, an op-amp scales it: gain = required output ÷ input signal.
- PWM program: high motor, pause mark, low motor, pause space, loop. A longer mark gives a faster motor. Relays can't be used for PWM because the rapid switching would wear out their contacts: use a MOSFET.
Key terms
| Sub-procedure | Code called with gosub and ended with return. |
|---|---|
| ADC reading | Vin ÷ Vref × 255 |
| Decision symbol | Diamond with Y/N exits, used to test an input or a count. |
| Output symbol | Parallelogram, e.g. "pin 7 on". |
| Delay symbol | Rectangle with a time and unit, e.g. "wait 2 s". |
| Fixed loop | Repeats a set number of times. |
| Continuous loop | Repeats forever. |
| pause 500 (PBASIC) | A 0.5 s delay: pause is in milliseconds. |
| Advantage of a microcontroller | Reprogrammed without changing the circuit; fewer components. |
| PWM | Rapid on/off switching; the mark-to-space ratio sets average power. |
| Duty cycle | The fraction of each cycle the output is on. |
Quick check
1. Which is an advantage of a microcontroller over a hard-wired circuit?
- It needs no software to be written
- Fewer components, so more reliable
- It is always cheaper than any chip
- It never needs a driver for outputs
Show answer
2. In PBASIC, which command returns from a sub-procedure?
- goto
- end
- return
- next
Show answer
3. An 8-bit ADC with a 5 V reference reads 2 V as about…
- 40
- 255
- 128
- 102
Show answer
4. Why can't a relay be used to drive a PWM motor?
- It is too slow and its contacts would wear out
- It uses too little current to turn the motor
- It inverts the signal from the microcontroller
- It needs an analogue input to work properly
Show answer
5. Which symbol tests whether a switch is pressed?
- Parallelogram
- Diamond
- Rectangle
- Oval
Show answer
6. "for b0 = 1 to 15 … next b0" is a…
- Continuous loop
- Decision
- Fixed loop
- Delay
Show answer
7. In PBASIC, pause 2000 waits for…
- 2 ms
- 0.2 s
- 20 s
- 2 s
Show answer
8. An advantage of a microcontroller over a hard-wired circuit is…
- It can be reprogrammed without changing the circuit
- It never needs a power supply to run
- It works without any input devices
- It is always faster than a hard-wired circuit
Show answer
Printable worksheet
An I do, we do, you do worksheet for this topic, with a separate answer sheet.