Skip to main content
ScotlandEngineering ScienceSyllabus dot point

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.

Generated by Claude Opus 4.814 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this key area is asking
  2. The microcontroller and its inputs and outputs
  3. Flowcharts and pseudocode
  4. Sequence, selection and iteration
  5. Examples in context
  6. Try this

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

Sources & how we know this