Electronics & control · Higher

Microcontrollers, flowcharts & programming

11 cards · 9 quiz · 0 exam
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?".
StartPin 1 on?NYPin 7 onWait 0.5 sPin 7 offWait 0.5 sDone 6 times?NYDecision (diamond):tests an input or a countOutput (parallelogram)Delay (rectangle) with unitFixed loop: 6 timesthen a continuous loopback to the start
Every decision has Y and N exits, every delay has a unit, and every loop arrow returns to the right place. Checking an input is a decision, not an output.

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.

  1. Draw a flowchart for one complete cycle.
  2. Use a loop so the system keeps running.
  3. State suitable time delays for each stage.
  4. 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-procedureCode called with gosub and ended with return.
ADC readingVin ÷ Vref × 255
Decision symbolDiamond with Y/N exits, used to test an input or a count.
Output symbolParallelogram, e.g. "pin 7 on".
Delay symbolRectangle with a time and unit, e.g. "wait 2 s".
Fixed loopRepeats a set number of times.
Continuous loopRepeats forever.
pause 500 (PBASIC)A 0.5 s delay: pause is in milliseconds.
Advantage of a microcontrollerReprogrammed without changing the circuit; fewer components.
PWMRapid on/off switching; the mark-to-space ratio sets average power.
Duty cycleThe fraction of each cycle the output is on.

Quick check

1. Which is an advantage of a microcontroller over a hard-wired circuit?

  1. It needs no software to be written
  2. Fewer components, so more reliable
  3. It is always cheaper than any chip
  4. It never needs a driver for outputs
Show answer
B: Fewer components, so more reliable. Fewer parts to fail.

2. In PBASIC, which command returns from a sub-procedure?

  1. goto
  2. end
  3. return
  4. next
Show answer
C: return. gosub … return.

3. An 8-bit ADC with a 5 V reference reads 2 V as about…

  1. 40
  2. 255
  3. 128
  4. 102
Show answer
D: 102. 2 ÷ 5 × 255.

4. Why can't a relay be used to drive a PWM motor?

  1. It is too slow and its contacts would wear out
  2. It uses too little current to turn the motor
  3. It inverts the signal from the microcontroller
  4. It needs an analogue input to work properly
Show answer
A: It is too slow and its contacts would wear out. Use a MOSFET.

5. Which symbol tests whether a switch is pressed?

  1. Parallelogram
  2. Diamond
  3. Rectangle
  4. Oval
Show answer
B: Diamond. A decision.

6. "for b0 = 1 to 15 … next b0" is a…

  1. Continuous loop
  2. Decision
  3. Fixed loop
  4. Delay
Show answer
C: Fixed loop. Repeats 15 times.

7. In PBASIC, pause 2000 waits for…

  1. 2 ms
  2. 0.2 s
  3. 20 s
  4. 2 s
Show answer
D: 2 s. 2000 ms.

8. An advantage of a microcontroller over a hard-wired circuit is…

  1. It can be reprogrammed without changing the circuit
  2. It never needs a power supply to run
  3. It works without any input devices
  4. It is always faster than a hard-wired circuit
Show answer
A: It can be reprogrammed without changing the circuit. Flexibility.

Printable worksheet

An I do, we do, you do worksheet for this topic, with a separate answer sheet.

Worksheet (PDF)Answers (PDF)