Logic & Programmable Control
How digital systems decide — logic gates, truth tables and Boolean expressions — and how a microcontroller runs them as a program.
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).
| Analogue | Digital | |
|---|---|---|
| Number of values | many — changes smoothly | two — logic 1 or logic 0 |
| Example | voltage from an LDR sensor (Topic 3) | pushed / not-pushed switch |
| Type | Examples |
|---|---|
| Digital inputs | push button, limit switch, guard switch, infrared sensor (detected / not detected) |
| Digital outputs | lamp/LED on or off, buzzer on or off, motor on or off, relay on or off |
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).
The three truth tables
| A | B | Q (AND) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
| A | B | Q (OR) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
| A | Q (NOT) |
|---|---|
| 0 | 1 |
| 1 | 0 |
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.
Worked example — Q = A AND (NOT B)
| A | B | X = NOT B | Q = A AND X |
|---|---|---|---|
| 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 |
Work out the NOT column first, then AND it with A row by row.
Worked example — three inputs: Q = A AND B AND C
| A | B | C | Q |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 1 |
Only the row where all three inputs are 1 gives an output of 1.
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.
The four-step design method
- Read the description carefully and underline each input and the output.
- Decide how the inputs combine: all needed (AND), any of them (OR), or inverted (NOT)?
- Write the Boolean expression.
- 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
Both conditions must be true at the same time → an AND gate: Q = A · B.
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.
| Sector | Examples of microcontrollers in use |
|---|---|
| Commercial | washing machines, microwave ovens, vending machines, alarm panels |
| Industrial | production-line conveyors, packaging machines, robot arms, lift control |
| Transport | engine management, anti-lock braking, automatic doors on buses and trains |
| Home | central 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 program | Behaviour is fixed by the wiring |
| Few components — one chip does many tasks | More components needed for complex behaviour |
| Easier to change or update | Difficult to change once built |
| Fewer wires — smaller PCB | Many connections — larger PCB |
| Time delays, counts and conditions are easy | Delays and counting need extra components |
| Needs a power supply and someone who can program | Simple to understand at component level |
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".
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 do | Pseudocode | Arduino C |
|---|---|---|
| Set a pin as an output / input | (done in setup) | pinMode(pin, OUTPUT); / pinMode(pin, INPUT); |
| Switch an output ON / OFF | high (output) / low (output) | digitalWrite(pin, HIGH); / digitalWrite(pin, LOW); |
| Wait | pause N seconds | delay(ms); |
| Read a switch / button | if input = on then | digitalRead(pin) |
| Read a sensor (analogue) | read sensor | analogRead(A0) |
| Yes/no decision (branch) | if/then … end if | if ( … ) { … } |
| Fixed loop (set number of times) | for…next | for (int i = 0; i < N; i++) { … } |
| Continuous loop (forever) | loop … end loop | put the code inside void loop() |
Worked example 1 — flashing warning lamp (continuous loop + delay)
Pseudocode
loop
high lamp
pause 1 second
low lamp
pause 1 second
end loopArduino 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)
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)
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.
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.
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
- An analogue signal can take any value in a range and changes smoothly; a digital signal has only two values (1 or 0).
- Any two: push button, limit switch, guard switch, infrared sensor.
- Any two: lamp/LED, buzzer, motor, relay.
- Near the supply voltage (about 5 V).
- Near 0 V.
- Logic 1.
- The guard is not in place (0 = not detected).
- e.g. a buzzer sounding an alarm when a fault is detected.
- 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.
- Analogue.
Practice — AND, OR and NOT gates 4a §2
- AND, OR and NOT.
- The output is 1 only when all inputs are 1.
- The output is 1 when any input is 1.
- The output is the opposite of the input.
- AND: Q column reads 0, 0, 0, 1.
- OR: Q column reads 0, 1, 1, 1.
- AND.
- OR.
- 0.
- 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
- rows = 2ⁿ for n inputs.
- 4 rows.
- 8 rows.
- Logic where the output depends only on the current combination of inputs (no memory).
- The intermediate column works out the inner gate first, so each row becomes two simple steps instead of one complicated one.
- Boolean: AND: Q = A · B · OR: Q = A + B · NOT: Q = NOT A.
- Fan: Q = A · B · (NOT C) — temperature on AND master on AND safety NOT triggered.
- Q = (A · B) + C for the diagram in question 7.
- "On when A, OR when both B and C": Q = A + (B · C).
- Draw the inner gate(s) first, then the final gate — check with a truth table.
Practice — Designing logic for engineering problems 4a §5
- AND — Q = A · B (both limit switches pressed).
- OR — Q = A + B + C (any window).
- Q = A · B · (NOT C) — start AND guard closed AND e-stop NOT pressed; diagram: NOT on C, three-way AND.
- OR — Q = A + B (too hot OR too damp).
- AND.
- AND.
- Q = A · B (hot AND lid open).
- Inputs: tank-full sensor (A), master switch (B); output: pump. Q = (NOT A) · B.
- Work inside-out: NOT first, then the inner gate, then the output gate.
- Evaluate the expression for every input combination, row by row.
Booklet 4b — Programmable Control
Practice — Programming structures 4b §3
- A program (or section) that repeats forever until the power is removed.
- A section that repeats a set number of times, then the program continues.
- A pause for a set time — the outputs stay in their current state while the program waits.
- A yes/no decision — the program follows a different path depending on the answer.
- Fixed loop (6 flashes, then stop).
- Continuous loop (watches the sensor forever).
Practice — Flowcharts Q1–Q5 4b §3
- START → Lamp (output, pin 13) ON → wait 4 s → Lamp OFF → STOP.
- START → Motor (output) ON → wait 5 s → Buzzer (output) ON → wait 2 s → Buzzer OFF → Motor OFF → STOP.
- START → LED ON → wait 1 s → LED OFF → wait 1 s → arrow back to LED ON (continuous loop — no STOP).
- 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.
- 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
- setup() runs once when the board switches on — used to set which pins are inputs or outputs.
- loop() runs forever after setup() finishes — the main control program goes here.
digitalWrite(13, HIGH);pinMode(4, INPUT);- 2500 ms = 2.5 seconds.
- Pseudocode: for count = 1 to 4 / high LED / pause 0.5 seconds / low LED / pause 0.5 seconds / next count / end.
- setup(): pinMode(2, INPUT); pinMode(8, OUTPUT); — loop(): if (digitalRead(2) == HIGH) { digitalWrite(8, HIGH); delay(3000); digitalWrite(8, LOW); }
- 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
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).