Controller principles: the brain of the robot
A controller is a small computer that decides what the robot does. A very common type is the microcontroller: one chip that has a processor, memory and pins. Boards such as Arduino, micro:bit and Raspberry Pi Pico use one.
Every controller does three jobs: Input (read sensors and buttons), Program (decide using stored rules) and Output (switch motors, lights and buzzers). It has a clock that makes it run millions of steps a second, so it looks instant.
The program is kept in memory, so it stays even when the power is off. Change the program and the same robot behaves in a new way. No new parts are needed.
Programming: sequence, decision and loop
A program is a list of clear steps. Three ideas are enough for most robots:
- Sequence: steps run in order, one after another.
- Decision: if something is true do this, else do that.
- Loop: go back and repeat. A robot program usually loops for ever.
A variable is a named box that holds a number, such as distance or limit. The limit (also called a threshold) is the number we compare against.
Our robot program: 1) read distance, 2) if distance < limit, motor OFF, 3) else motor ON, 4) go to 1. You can draw it as a flowchart before you write code. If the robot misbehaves, find the wrong line. This is called debugging.
Interfaces: how the controller talks to other parts
An interface is the link between the controller and another part. Four kinds are common:
- Digital pin: only two states, HIGH (1) or LOW (0). A button or an LED uses this.
- Analogue input and ADC: an ADC turns a smooth voltage into a number. A 10-bit ADC gives 1024 levels (0 to 1023).
- PWM output: the pin switches ON and OFF very fast. The longer it stays ON (the duty cycle), the faster the motor runs.
- Serial links (UART, I2C, SPI): a few wires that carry many messages to other chips such as a display.
A pin can give only about 20 mA, but a small motor needs hundreds of mA. So we use a driver (a transistor or motor-driver chip) with its own battery. The pin just says ON or OFF to the driver. Connect the grounds of both together.
Try it: be the controller
Write the four program lines on paper. A friend moves a book towards you (the obstacle). You are the controller: read the distance in cm out loud, compare with a limit of 30, then say ON or OFF. Keep repeating. Then change the limit to 50 and see what changes. In the 3D, do the same with the sliders and guess the motor state before you look.
Key formulas and definitions
- Controller = Input + Program + Output (I-P-O)
- ADC levels = 2^bits (10-bit: 1024 levels, 0 to 1023)
- Voltage = (reading / 1023) × reference voltage (10-bit ADC)
- Duty cycle (%) = ON time / total time × 100
- Average PWM voltage = duty cycle × supply voltage
Worked examples
1. A 10-bit ADC with a 5 V reference gives the reading 512. What is the voltage?
V = 512 / 1023 × 5 = 2.50 V (about half of 5 V).
2. A PWM pin at 5 V runs with a 40% duty cycle. What is the average voltage?
Average = 0.40 × 5 = 2 V. The motor runs slower than at full 5 V.
3. Limit = 30 cm. The sensor reads 50, 35, 20 in three loops. What does the motor do each time?
Rule: distance < limit means OFF. 50: ON. 35: ON. 20: OFF.
4. A motor needs 400 mA. One pin gives at most 20 mA. How many times too weak is the pin? What do we add?
400 / 20 = 20 times too weak. Add a driver with its own battery.
5. An 8-bit ADC reads a temperature sensor. How many different readings are possible?
2^8 = 256 readings (0 to 255).
Common mistakes
- Connecting a motor straight to a pin. The pin cannot give enough current and may be damaged. Use a driver.
- Thinking the program runs once and stops. A robot program loops for ever.
- Mixing up < and >. Check the rule with one sample number before you run it.
- Forgetting to join the grounds of the controller and the motor battery. Then the signal has no reference and nothing works.