📘 CodingMarble Learn

Robot Controllers and Programming

A controller is the small computer that is the brain of a robot. It reads inputs (sensors), runs a stored program again and again (read, decide, act), and sets outputs (motors, lights). Pins are weak, so a driver is used to power a motor.

🎬 Step-by-step story

  1. A controller is the robot's brain. It is a small computer on one board, with a chip, a memory and pins.
  2. A sensor sends a signal in. It enters the controller through an input pin. Watch the blue dots travel in.
  3. The program is a list of lines. The controller reads one line at a time, then goes back to the start. This is the loop.
  4. Here is one rule: if the obstacle is closer than the limit, stop the motor. The red box comes closer and the motor stops.
  5. A pin cannot give enough power for a motor. A driver takes power from a battery and the pin only tells it ON or OFF. This link is the interface.
  6. Free play: move the distance and the limit. Predict what the program will do, then check the motor.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Is the controller the same as the sensor?

No. The sensor only measures. The controller reads the number and decides.

Why does the program repeat for ever?

The world keeps changing. The robot must keep reading the sensor, so the program loops. Watch the highlight go round the four lines.

Why does the motor stop only when the distance is smaller than the limit?

The rule compares two numbers. While the distance is bigger than the limit, the else line runs and the motor is ON.

Why can't the pin run the motor directly?

A pin gives a tiny current. The driver and battery supply the large current. The pin just switches the driver.

What happens if I set the limit very high?

The robot stops early, even when the obstacle is far away. Try limit 60 in the free-play step.

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:

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:

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

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

Practice quiz

1. The three jobs of a controller are:
2. A small computer chip with memory and pins is a:
3. Why do we use a motor driver?
4. A 10-bit ADC has how many levels?
5. Which idea repeats the program steps?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is a robot controller in simple words?

It is the small computer that reads sensors, follows a stored program and switches motors and lights. It is the brain of the robot.

Is a microcontroller the same as a CPU?

No. A CPU is only the processor. A microcontroller puts the processor, memory and input-output pins on one chip, so it can control a device on its own.

Which language do I use to program a robot controller?

Many school boards use block coding, MicroPython or Arduino C++. The ideas (sequence, decision, loop) are the same in all of them.

Where this is taught

Japan高校(専門学科)1〜3年Electromechanics (Mechatronics)
Japan高校(専門学科)1〜3年Hardware Technology
China高二Sel.2 Robot design and making (engineering)

Learn first

Learn next

Related lessons

All Computer Science lessons