Teaching robots to see and understand colors in the world around them
Color recognition allows robots to identify, track, and respond to colored objects. This enables applications like traffic light recognition, object sorting, and color-based navigation.
#include "esp_camera.h"
// Color range structure
struct ColorRange {
uint8_t r_min, r_max;
uint8_t g_min, g_max;
uint8_t b_min, b_max;
};
// Red color range
ColorRange redRange = {150, 255, 0, 100, 0, 100};
bool detectColor(uint8_t r, uint8_t g, uint8_t b, ColorRange range) {
return (r >= range.r_min && r <= range.r_max &&
g >= range.g_min && g <= range.g_max &&
b >= range.b_min && b <= range.b_max);
} void followColorObject() {
camera_fb_t * fb = esp_camera_fb_get();
if (!fb) return;
uint16_t* pixels = (uint16_t*)fb->buf;
int sumX = 0, sumY = 0, count = 0;
// Find color centroid
for (int y = 0; y < fb->height; y++) {
for (int x = 0; x < fb->width; x++) {
int index = y * fb->width + x;
uint16_t pixel = pixels[index];
uint8_t r = (pixel >> 11) << 3;
uint8_t g = ((pixel >> 5) & 0x3F) << 2;
uint8_t b = (pixel & 0x1F) << 3;
if (detectColor(r, g, b, redRange)) {
sumX += x;
sumY += y;
count++;
}
}
}
if (count > 50) {
int centerX = sumX / count;
int error = centerX - 160; // Image center
// Control robot based on object position
if (abs(error) < 20) {
moveForward(100);
} else if (error > 0) {
turnRight(50);
} else {
turnLeft(50);
}
}
esp_camera_fb_return(fb);
} Build a robot that detects, tracks, and follows colored objects using the ESP32-S3 camera.
Safety: Keep speeds low, test in open areas, have emergency stop ready.