How is a microcontroller programmed to read inputs and drive outputs, and how do we plan that program?
Programmable control with a microcontroller: inputs and outputs, programs described by flowcharts and pseudocode, and the use of loops, decisions and sub-routines.
An SQA Advanced Higher Engineering Science answer on programmable control, covering the microcontroller and its inputs and outputs, programs described by flowcharts and pseudocode, and the program structures of sequence, selection, iteration and sub-routines.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this key area is asking
The SQA wants you to describe programmable control using a microcontroller: how it reads inputs and drives outputs, how a control program is planned and communicated using flowcharts and pseudocode, and how the three program structures, sequence, selection (decisions) and iteration (loops), together with sub-routines, are combined to build a working controller.
The microcontroller and its inputs and outputs
Sensors and switches connect to the input pins, where the microcontroller reads each as a logic 1 or 0 (or, with an analogue-to-digital converter, as a number). Motors, solenoids, lamps and displays connect to the output pins, usually through an interface such as a transistor or driver because the pins cannot supply much current directly. The decisive advantage over wired logic is flexibility: behaviour is set by the program, so it can be changed without altering the circuit.
Flowcharts and pseudocode
Before writing a program, engineers plan it in a form that is independent of the exact language:
- A flowchart uses standard symbols: a rectangle for a process or action, a diamond for a decision (a yes/no test that branches the flow), a parallelogram for input/output, and arrows showing the order. It shows loops as arrows that return to an earlier point.
- Pseudocode is structured plain English using keywords such as IF, THEN, ELSE, REPEAT, UNTIL, WHILE and DO, laid out with indentation to show structure.
Both communicate the logic of the program clearly, so it can be checked and then translated into whatever language the microcontroller uses.
Sequence, selection and iteration
Every control program is built from three structures:
- Sequence - instructions carried out one after another in order.
- Selection - a decision that chooses between paths: IF a condition is true THEN do one thing, ELSE do another. This is the flowchart diamond.
- Iteration - a loop that repeats a block: WHILE a condition holds, or REPEAT UNTIL a condition becomes true, or a fixed-count FOR loop.
A sub-routine sits alongside these: a named block of code that performs a task and can be called from anywhere, after which control returns to the point of the call. It lets a repeated task be written once.
Examples in context
A central-heating controller reads a thermostat and a clock and switches the boiler using selection and timing. A traffic-light sequencer steps lamps through a fixed cycle using sequence and iteration, with a sub-routine for the pedestrian phase. A washing machine runs the wash programme as a long sequence with loops for the spin cycles and decisions that respond to the door switch and water level. A robot buggy reads line or distance sensors each pass of its main loop and decides how to drive its motors. All of these are software running on a microcontroller rather than fixed logic.
Try this
Q1. State what flowchart symbol represents a decision. [1 mark]
- Cue. A diamond.
Q2. Name the three basic program structures. [3 marks]
- Cue. Sequence, selection (decision) and iteration (loop).
Q3. State one advantage of programmable control over hard-wired logic. [1 mark]
- Cue. The function can be changed by editing the program, without rewiring the circuit.
Exam-style practice questions
Practice questions written in the style of SQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
SQA AH style5 marksA microcontroller must switch on a motor (output 0) when a start button (input 0) is pressed, and keep it on until a stop button (input 1) is pressed. Write pseudocode for this behaviour.Show worked answer →
Use a loop that waits for the start, runs the motor, then waits for the stop.
do forever
if input0 = 1 then ' start pressed
set output0 on ' motor on
repeat
wait ' keep running
until input1 = 1 ' stop pressed
set output0 off ' motor off
end if
loop
Markers reward reading input0, switching output0 on, an inner loop that continues until input1 is pressed, then switching output0 off, all within an outer loop. The key idea is that the motor latches on and only the stop input clears it.
SQA AH style4 marksExplain the purpose of a sub-routine in a microcontroller program and give one advantage of using one.Show worked answer →
A sub-routine is a named, self-contained block of code that performs a specific task and can be called from anywhere in the program.
When called, the program jumps to the sub-routine, runs it, then returns to the instruction after the call.
Advantage: a block of code that is needed several times is written once and reused, which shortens the program, reduces errors, and makes it easier to read and modify.
Markers reward describing a sub-routine as a reusable, callable block that returns control afterwards, plus a valid advantage such as code reuse, shorter programs or easier maintenance.
Related dot points
- Sequential logic with memory: flip-flops storing a bit, counters dividing and counting pulses, and astable and monostable timing circuits.
An SQA Advanced Higher Engineering Science answer on sequential logic, covering the flip-flop as a one-bit memory, counters that count and divide clock pulses, and astable and monostable timing circuits that generate pulses and delays.
- Combinational logic built from logic gates, described by truth tables and Boolean expressions, simplified using Boolean algebra and Karnaugh maps.
An SQA Advanced Higher Engineering Science answer on combinational logic, covering logic gates, truth tables and Boolean expressions, simplification with Boolean algebra and Karnaugh maps, and designing a logic circuit from a specification.
- Control systems described by the system model, the difference between open-loop and closed-loop control, and the role of feedback, the error signal and the components of a closed-loop system.
An SQA Advanced Higher Engineering Science answer on control systems, covering the system model, open-loop versus closed-loop control, negative feedback and the error signal, and the comparator, controller, actuator and sensor of a closed-loop system.
- The operational amplifier as a high-gain difference amplifier, and the inverting, non-inverting, summing and difference configurations with their closed-loop gain relationships and saturation.
An SQA Advanced Higher Engineering Science answer on operational amplifier circuits, covering the ideal op-amp, open-loop gain and saturation, negative feedback, and the inverting, non-inverting, summing and difference amplifier configurations with their closed-loop gain relationships.
- The capacitor charge and discharge through a resistor, the time constant, and passive RC filters selecting signals by frequency.
An SQA Advanced Higher Engineering Science answer on analogue signal processing, covering capacitor charge and discharge through a resistor, the RC time constant, and passive low-pass and high-pass RC filters that select signals by frequency.