Cameras as sensors
What you’ll learn
- The range of “seeing” sensors, from simple to advanced
- What each can do and which boards they need
Colour sensors
Modules like the TCS34725 measure the red, green and blue light reflected from a surface close by. Great for sorting coloured blocks or spotting a coloured zone on a competition mat.
Smart camera modules
Pixy2 and HuskyLens contain a camera and their own processor. They do the vision work themselves (colour tracking, line tracking, face or tag recognition) and send simple results over I²C, UART or SPI: “red object at x = 140, y = 90”. An Arduino can use them easily.
Camera boards
- ESP32-CAM: a cheap ESP32 with a camera, good for streaming video over Wi-Fi or simple processing.
- Raspberry Pi + camera: a full Linux computer that can run OpenCV and AI models for real computer vision (see Computer vision basics).
What a camera gives a robot
| Task | Simplest suitable sensor |
|---|---|
| Which colour is this block? | Colour sensor |
| Follow a coloured ball | Pixy2 / HuskyLens |
| Read a QR code or tag | HuskyLens, or Pi + OpenCV |
| Recognise many object types | Pi with an AI model |
Example: sorting blocks by colour
A colour sensor under a chute reads each block. If red is highest, a servo flips a gate left; if blue is highest, right.
if (r > g && r > b) gate.write(30); // red: leftelse if (b > r && b > g) gate.write(150); // blue: rightExample: following a ball with a smart camera
The camera reports the ball’s x position (0–319). The robot turns to keep it centred:
int error = x - 160; // how far off centreint turn = error / 4; // proportional turning// left motor = base + turn, right motor = base - turnPick the sensorEasy
Which is the simplest sensor for: telling a black floor from a white one; finding a red ball across a room; telling a red block from a blue one held 1 cm away? An IR line sensor; a smart camera (Pixy2/HuskyLens); a colour sensor.
Centre the targetMedium
A smart camera’s image is 320 pixels wide. The target is at x = 250. Should the robot turn left or right, and by how much (using the example’s formula)? Error = 250 − 160 = 90, so turn right; turn = 90 ÷ 4 ≈ 22.
Plan a sorterHard
Sketch a robot that sorts red, green and blue blocks into three bins. List sensors, actuators and the decision rules.
Quick check
Finished this lesson?