← Back to N5 Engineering Science
All lesson slides
Beta This page is new and still in beta testing — the content is being checked and may change. Spotted a mistake or something that doesn't work? Please tell Mr Stewart.

Logic & Programmable Control

How digital systems decide — logic gates, truth tables and Boolean expressions — and how a microcontroller runs them as a program.

Topic 4a & 4b booklets — PDFs coming soon Data booklet (flowchart symbols) Go to self-check

Exam = tested in the written paper most years. Assignment = also used in assignment Tasks 2 (flowchart + circuit) and 4 (logic from a Boolean expression).

Sections:
Concept 1 · Booklet 4a

Digital signals and logic states Exam

An analogue signal can take any value within a range and changes smoothly. A digital signal can only take one of two values: logic 1 (high, on, true — near the supply voltage) or logic 0 (low, off, false — near 0 V).

AnalogueDigital
Number of valuesmany — changes smoothlytwo — logic 1 or logic 0
Examplevoltage from an LDR sensor (Topic 3)pushed / not-pushed switch
TypeExamples
Digital inputspush button, limit switch, guard switch, infrared sensor (detected / not detected)
Digital outputslamp/LED on or off, buzzer on or off, motor on or off, relay on or off

Logic 1 or logic 0?

Give the logic state of each situation, then check. (Task 1 in your 4a booklet.)

Concept 2 · Booklet 4a

AND, OR and NOT gates Exam

A logic gate is a digital component that takes one or more digital inputs and produces a single digital output. AND: output 1 only when all inputs are 1. OR: output 1 when any input is 1. NOT: the output is the opposite of the input (an inverter).

National 5 note: you only need AND, OR and NOT. NAND, NOR and XOR gates are not required at this level.
AND gate
AND
Output 1 only when all inputs are 1.
OR gate
OR
Output 1 when any input is 1.
NOT gate
NOT
Output is the opposite of the input (inverter).

The three truth tables

AND gate truth table
ABQ (AND)
000
010
100
111
OR gate truth table
ABQ (OR)
000
011
101
111
NOT gate truth table
AQ (NOT)
01
10

🚦 Gate playground — live

Tap the inputs (0/1) and watch each gate's output respond — this is the logic kit / Yenka practical, on screen.

NOT Q 0
AND Q 0
OR Q 0
Challenge: set the inputs so all three outputs read 1 at the same time.

Concept 3 · Booklet 4a

Truth tables for combinations ExamAssignment

A truth table needs a row for every combination of inputs: rows = 2ⁿ for n inputs (2 → 4 rows, 3 → 8 rows; N5 never needs more than three). Use an intermediate column for the inner gate — each row then becomes two simple steps.

N5 logic is combinational: the output depends only on the current combination of inputs — it does not remember previous inputs. List the input combinations in neat binary order so none is missed.
Worked example — Q = A AND (NOT B)
Intermediate column
Complete the truth table for Q = A AND (NOT B). Let X = NOT B.
ABX = NOT BQ = A AND X
0010
0100
1011
1100

Work out the NOT column first, then AND it with A row by row.

Worked example — three inputs: Q = A AND B AND C
Three inputs — 8 rows
ABCQ
0000
0010
0100
0110
1000
1010
1100
1111

Only the row where all three inputs are 1 gives an output of 1.

Complete the truth table — Q = (A AND B) OR C

Let X = A AND B. Fill in X and Q for each row, then check. (Task 5 in your 4a booklet.)

Concept 4 · Booklet 4a

Boolean expressions and logic design ExamAssignment

A Boolean expression writes logic in symbols: Q = A · B (AND), Q = A + B (OR), Q = NOT A. It is the link between a description, a logic diagram and a truth table — you must be able to convert between all three.

AND
Q = A · B
1 only when all inputs are 1
OR
Q = A + B
1 when any input is 1
NOT
Q = NOT A
the opposite of the input

The four-step design method

  1. Read the description carefully and underline each input and the output.
  2. Decide how the inputs combine: all needed (AND), any of them (OR), or inverted (NOT)?
  3. Write the Boolean expression.
  4. Draw the logic diagram and check it with a truth table. Work combined gates from the inside out: NOT and inner brackets first, then the final gate.
Worked example — description → expression
Description → Boolean
A motor turns on when the start button is pressed and the safety guard is in place. (A = start button, B = guard sensor, Q = motor.)

Both conditions must be true at the same time → an AND gate: Q = A · B.

Write the Boolean expression

Choose the expression for each engineering description, then check. (Task 6 in your 4a booklet.)

Air or electronics — same logic. The pneumatic AND (two valves in series) and OR (a shuttle valve) in Pneumatics do exactly the same job as these gates.
Concept 5 · Booklet 4b

Microcontrollers and programmable control Exam

A microcontroller is a small, programmable computer chip used to control engineering systems. It runs a program stored in its memory, reading inputs and operating outputs with digital signals.

SectorExamples of microcontrollers in use
Commercialwashing machines, microwave ovens, vending machines, alarm panels
Industrialproduction-line conveyors, packaging machines, robot arms, lift control
Transportengine management, anti-lock braking, automatic doors on buses and trains
Homecentral heating control, smart lighting, smart thermostats

Programmable vs hard-wired control

Programmable control (microcontroller)Hard-wired control (logic gates / relays)
Behaviour can be changed by editing the programBehaviour is fixed by the wiring
Few components — one chip does many tasksMore components needed for complex behaviour
Easier to change or updateDifficult to change once built
Fewer wires — smaller PCBMany connections — larger PCB
Time delays, counts and conditions are easyDelays and counting need extra components
Needs a power supply and someone who can programSimple to understand at component level
Concept 6 · Booklet 4b

Flowcharts and programming structures ExamAssignment

A flowchart shows the steps of a program in order, using the standard data-booklet symbols. The four programming structures at N5 are the continuous loop (repeats forever), the fixed loop (repeats a set number of times), the time delay (pauses; outputs hold their state), and the branch (a yes/no decision).

⚠ Exam conventions — the most-dropped marks

  • The exam describes programs with flowcharts or pseudocode only — never answer a programmable-control question in Arduino C.
  • Label every input/output with its pin number — "Lamp (output, pin 13) ON", not just "Lamp ON".
  • Give every delay a unit — "wait 4 s" or "4000 ms", never just "wait".
Terminator
Terminator
Start or stop the program.
Input / output
Parallelogram
An input or an output (with its pin number).
Process
Rectangle
A process or instruction.
Decision
Diamond
A yes/no decision (branch).
Flow arrow
Arrows
Show the order the steps are carried out.

Order the flowchart

A warning lamp turns on for 4 seconds, then off, then the program stops (Flowchart Q1 in your 4b booklet). Tap the blocks into the right order — one block is not needed.

Concept 7 · Booklet 4b

Programming in real life — pseudocode and Arduino C

Every Arduino C program has two parts: setup() runs once when the board switches on (set the pin modes here), and loop() runs forever afterwards (the main control program lives here — that is the continuous loop).

Command reference — pseudocode ↔ Arduino C

What you want to doPseudocodeArduino C
Set a pin as an output / input(done in setup)pinMode(pin, OUTPUT); / pinMode(pin, INPUT);
Switch an output ON / OFFhigh (output) / low (output)digitalWrite(pin, HIGH); / digitalWrite(pin, LOW);
Waitpause N secondsdelay(ms);
Read a switch / buttonif input = on thendigitalRead(pin)
Read a sensor (analogue)read sensoranalogRead(A0)
Yes/no decision (branch)if/then … end ifif ( … ) { … }
Fixed loop (set number of times)for…nextfor (int i = 0; i < N; i++) { … }
Continuous loop (forever)loop … end loopput the code inside void loop()
delay() works in milliseconds, not seconds: 1 s = 1000 ms · 0.5 s = 500 ms · 2 s = 2000 ms. The pseudocode set the exam uses: high · low · pause · if…then · for…next · end.
Worked example 1 — flashing warning lamp (continuous loop + delay)
Pseudocode ↔ Arduino C
A machine's warning lamp must flash on and off the whole time the machine is switched on. LED on pin 13.

Pseudocode

loop
  high lamp
  pause 1 second
  low lamp
  pause 1 second
end loop

Arduino C

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

Everything inside loop() repeats forever — that is the continuous loop. delay(1000) holds the state for 1000 ms (1 s); outputs stay as they are during a delay.

Worked example 2 — UK traffic-light sequence (several outputs)
Multiple outputs
Model traffic lights run the UK sequence forever: red → red + amber → green → amber → red. Red pin 9, amber pin 10, green pin 11.
void setup() {
  pinMode(9, OUTPUT);     // red
  pinMode(10, OUTPUT);    // amber
  pinMode(11, OUTPUT);    // green
}

void loop() {
  digitalWrite(9, HIGH);  // red on
  delay(3000);
  digitalWrite(10, HIGH); // amber on (red + amber)
  delay(1000);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH); // green on
  delay(3000);
  digitalWrite(11, LOW);
  digitalWrite(10, HIGH); // amber on
  delay(1000);
  digitalWrite(10, LOW);
}

Each lamp is a separate output, so setup() needs three pinMode lines. Two outputs HIGH at once gives the "red + amber" stage.

Worked example 3 — darkness alarm with a counter (sensor + branch + fixed loop)
Sensor + branch + fixed loop
A security unit watches an LDR. When it gets dark, a warning LED flashes 5 times, then it carries on watching. LDR on A0, LED on pin 13.
void setup() {
  pinMode(13, OUTPUT);
  // A0 is an analogue input - no pinMode needed
}

void loop() {
  int light = analogRead(A0);   // 0 = dark, 1023 = bright

  if (light < 300) {            // branch: only when dark
    for (int i = 0; i < 5; i++) {   // fixed loop: 5 flashes
      digitalWrite(13, HIGH);
      delay(500);
      digitalWrite(13, LOW);
      delay(500);
    }
  }
}

analogRead(A0) gives 0–1023. The if test is the branch; for (int i = 0; i < 5; i++) is the fixed loop — it counts 0,1,2,3,4: five flashes.

Find and fix — debugging like an engineer

Engineers spend a lot of time finding faults. Each program below has two faults — find them before opening the answer.

Debug build 1 — flashing LED (should flash 1 s on / 1 s off, forever)
void setup() {

}

void loop() {
  digitalWrite(13, HIGH);
  delay(1);
  digitalWrite(13, LOW);
  delay(1000);
}

Fault 1: setup() is empty — it needs pinMode(13, OUTPUT);. Fault 2: delay(1) is 1 millisecond, not 1 second — it should be delay(1000).

Debug build 2 — button and buzzer (buzzer on pin 8 for 2 s when button on pin 2 pressed)
void setup() {
  pinMode(2, INPUT);
  pinMode(8, OUTPUT);
}

void loop() {
  if (digitalRead(2) = HIGH) {
    digitalWrite(8, HIGH);
    delay(2000);
    digitalWrite(8, LOW);
  }

Fault 1: the test uses = (assignment) instead of == (comparison) — it should be if (digitalRead(2) == HIGH). Fault 2: the closing brace } for loop() is missing.

What does this line do?

Match each Arduino C line to its job, then check.

Assignment link. When you simulate and test your program for the assignment (Task 2: flowchart + circuit, test-and-fix), see Booklet 8 — Assignment Skills: match the flowchart pin numbers to the circuit, check battery orientation, describe all aspects of each test result, and end the sequence — no infinite loop.

Common mistakes — watch out for these in the exam

  • Answering a programmable-control question in Arduino C — the exam uses flowcharts or pseudocode only.
  • Leaving pin numbers off inputs/outputs in a flowchart.
  • Giving a delay with no unit — write "wait 1 s" or "1000 ms".
  • Confusing a fixed loop (set number of times) with a continuous loop (forever).
  • Mixing up AND and OR — AND needs all inputs high; OR needs only one. Work combined gates inside-out.
Booklet check

Check your booklet work

Try each task in your 4a and 4b booklets first, then open the matching answer.

Booklet 4a — Logic and Digital Control

Practice — Digital signals and logic states 4a §1
  1. An analogue signal can take any value in a range and changes smoothly; a digital signal has only two values (1 or 0).
  2. Any two: push button, limit switch, guard switch, infrared sensor.
  3. Any two: lamp/LED, buzzer, motor, relay.
  4. Near the supply voltage (about 5 V).
  5. Near 0 V.
  6. Logic 1.
  7. The guard is not in place (0 = not detected).
  8. e.g. a buzzer sounding an alarm when a fault is detected.
  9. A digital signal has only two widely-separated levels, so small amounts of noise cannot change a 1 into a 0 — the signal is still read correctly.
  10. Analogue.
Practice — AND, OR and NOT gates 4a §2
  1. AND, OR and NOT.
  2. The output is 1 only when all inputs are 1.
  3. The output is 1 when any input is 1.
  4. The output is the opposite of the input.
  5. AND: Q column reads 0, 0, 0, 1.
  6. OR: Q column reads 0, 1, 1, 1.
  7. AND.
  8. OR.
  9. 0.
  10. Because its output is always the inverse (opposite) of its input.
Tasks 4–5 — Combination truth tables 4a §3
  • (4a) Q = A OR (NOT B) — X = NOT B: rows (A,B → X,Q): 0,0 → 1,1 · 0,1 → 0,0 · 1,0 → 1,1 · 1,1 → 0,1
  • (4b) Q = (NOT A) AND B — X = NOT A: 0,0 → 1,0 · 0,1 → 1,1 · 1,0 → 0,0 · 1,1 → 0,0
  • (Task 5) Q = (A AND B) OR C — X = A AND B: Q column in binary order (000→111): 0, 1, 0, 1, 0, 1, 1, 1 (X is 1 only for 110 and 111).
Practice — Truth tables & Boolean expressions 4a §3–4
  1. rows = 2ⁿ for n inputs.
  2. 4 rows.
  3. 8 rows.
  4. Logic where the output depends only on the current combination of inputs (no memory).
  5. The intermediate column works out the inner gate first, so each row becomes two simple steps instead of one complicated one.
  6. Boolean: AND: Q = A · B · OR: Q = A + B · NOT: Q = NOT A.
  7. Fan: Q = A · B · (NOT C) — temperature on AND master on AND safety NOT triggered.
  8. Q = (A · B) + C for the diagram in question 7.
  9. "On when A, OR when both B and C": Q = A + (B · C).
  10. Draw the inner gate(s) first, then the final gate — check with a truth table.
Practice — Designing logic for engineering problems 4a §5
  1. AND — Q = A · B (both limit switches pressed).
  2. OR — Q = A + B + C (any window).
  3. Q = A · B · (NOT C) — start AND guard closed AND e-stop NOT pressed; diagram: NOT on C, three-way AND.
  4. OR — Q = A + B (too hot OR too damp).
  5. AND.
  6. AND.
  7. Q = A · B (hot AND lid open).
  8. Inputs: tank-full sensor (A), master switch (B); output: pump. Q = (NOT A) · B.
  9. Work inside-out: NOT first, then the inner gate, then the output gate.
  10. Evaluate the expression for every input combination, row by row.

Booklet 4b — Programmable Control

Practice — Programming structures 4b §3
  1. A program (or section) that repeats forever until the power is removed.
  2. A section that repeats a set number of times, then the program continues.
  3. A pause for a set time — the outputs stay in their current state while the program waits.
  4. A yes/no decision — the program follows a different path depending on the answer.
  5. Fixed loop (6 flashes, then stop).
  6. Continuous loop (watches the sensor forever).
Practice — Flowcharts Q1–Q5 4b §3
  1. START → Lamp (output, pin 13) ON → wait 4 s → Lamp OFF → STOP.
  2. START → Motor (output) ON → wait 5 s → Buzzer (output) ON → wait 2 s → Buzzer OFF → Motor OFF → STOP.
  3. START → LED ON → wait 1 s → LED OFF → wait 1 s → arrow back to LED ON (continuous loop — no STOP).
  4. START → read button (input) → decision "button pressed?" — No: loop back to read button; Yes: Buzzer ON → wait 2 s → Buzzer OFF → loop back to read button.
  5. START → set counter = 0 → LED ON → wait 0.5 s → LED OFF → wait 0.5 s → add 1 to counter → decision "counter = 5?" — No: loop back to LED ON; Yes: STOP.
Practice — Programming in real life 4b §4
  1. setup() runs once when the board switches on — used to set which pins are inputs or outputs.
  2. loop() runs forever after setup() finishes — the main control program goes here.
  3. digitalWrite(13, HIGH);
  4. pinMode(4, INPUT);
  5. 2500 ms = 2.5 seconds.
  6. Pseudocode: for count = 1 to 4 / high LED / pause 0.5 seconds / low LED / pause 0.5 seconds / next count / end.
  7. setup(): pinMode(2, INPUT); pinMode(8, OUTPUT); — loop(): if (digitalRead(2) == HIGH) { digitalWrite(8, HIGH); delay(3000); digitalWrite(8, LOW); }
  8. Put inside void loop(): digitalWrite(pin, HIGH); delay(1000); digitalWrite(pin, LOW); delay(1000); — with pinMode in setup().
Find and fix — the two debug builds 4b §4
  • Debug 1: setup() is missing pinMode(13, OUTPUT); and delay(1) should be delay(1000) (1 ms vs 1 s).
  • Debug 2: the if test needs == not =, and the closing brace } of loop() is missing.
Check yourself

Check yourself

Mixed multiple choice

One question from every part of the topic. Choose an answer for each, then mark them.

Rate your confidence — the booklets' success criteria

Success criteria from both the 4a and 4b booklets. Red = not yet, Amber = getting there, Green = confident. Saved on this device.

Ready for exam questions? Open the Past Paper Finder and look for the logic and programmable-control questions.

Sources & credits: The Topic 4a and 4b booklets © R Stewart, 2026. The Past Paper Finder is compiled by Mr McDonald, 2024; past-paper questions © Qualifications Scotland (SQA). The N4/N5 data booklet is reproduced for educational use, © Qualifications Scotland (SQA).