4.1 Color Recognition
4.1.1 Algorithm

It will specify one or more recognition areas, whose positions and sizes can be set as needed, and then it will return the color label of this area as well as the red component value R, green component value G and blue component value B.
4.1.2 Color Label
Sengo1 defines 7 colors of labels:
Label value |
Meaning |
Label value |
Meaning |
|---|---|---|---|
1 |
Black |
2 |
White |
3 |
Red |
4 |
Green |
5 |
Blue |
6 |
Yellow |
0 |
Unknown |
Note: Common colors not listed in the table, such as purple, cyan(blue-green), orange, and gray, have relatively low color discrimination and are easily misidentified as the colors in the table. Therefore, they are classified as unknown ones. If you need to recognize the above colors, you can make your own judgment based on the returned RGB values.
Sample 1:

The serial port outputs the label values: (the recognition box outputs 3, corresponding to the label values in the table.)

Sample 2:

The serial port outputs the label values: (The output label values correspond to that in the color label table.)

4.1.3 Configuration Parameters
Users can set the coordinates of the recognition area and the size of the recognition box. If no new parameters are specified, it will run with the default values that are defined as follows:
Parameter |
Definition |
Default |
|---|---|---|
param.x_value |
Central coordinate x of the recognition area |
50 |
param.y_value |
Central coordinate y of the recognition area |
50 |
param.width |
Width w of the recognition area |
20 |
param.height |
Height h of the recognition area |
20 |
Code:
sentry_object_t param; // Parametric structure
Serial.println("Sengo begin Success.");
//Set the x-coordinate of the detection area
param.x_value = 50;
//Set the y-coordinate of the detection area
param.y_value = 50;
//Set the width of the detection area
param.width = 20;
//Set the height of the detection area
param.height = 20;
//Write the parameters into the sensor
err = sengo.SetParam(VISION_TYPE, ¶m);
4.1.4 Returned Values
When the main controller acquires the detection results, the algorithm will return the followings:
Formal parameter |
Definition |
|---|---|
kRValue |
red component value R, with a range of 0-255 |
kGValue |
green component value G, with a range of 0-255 |
kBValue |
blue component value B, with a range of 0-255 |
kLabel |
color label value |
Code:
// Obtain the label (color ID) of the object
int label = sengo.GetValue(VISION_TYPE, kLabel, 1);
sengo.GetValue(VISION_TYPE, kLabel, 1); returns the color label value. If I replaced it with “kRValue”: sengo.GetValue(VISION_TYPE, kRValue, 1);, it will return the red component value R(0-255).
4.1.5 Tips of Color Recognition Algorithm
When the recognition area is relatively small, such as 2x2, although the recognition speed is fast, there will be an impact on results due to the small number of pixels, thus with low credibility. It is only suitable for scenarios with a single controllable background.
When the recognition area is large, such as 20x20 with the large number of pixels, the interference of variegated colors will be filtered out, resulting in a relatively high credibility, but the recognition speed is slow.
Within the recognition area, when the areas occupied by different colors are approximately the same, the results may repeatedly change.
4.1.7 Test Code
#include <Arduino.h>
#include <Sentry.h> // Import the Sentry vision sensor library
typedef Sengo1 Sengo; // Create an alias Sengo for the Sengo1 type to simplify subsequent usage
// Select the communication method (enable one by uncommenting)
#define SENGO_I2C // I2C communication is currently enabled
// #define SENGO_UART // Alternative option: UART serial communication
#ifdef SENGO_I2C
#include <Wire.h> // Libraries required for I2C communication
#endif
#ifdef SENGO_UART
#include <SoftwareSerial.h> // Soft serial port library (for non-hardware serial ports)
#define TX_PIN 11 // Customize the TX pin
#define RX_PIN 10 // Customize the RX pin
SoftwareSerial mySerial(RX_PIN, TX_PIN); // Create a soft serial port object
#endif
#define VISION_TYPE Sengo::kVisionColor // Define the type of visual algorithm (color recognition)
Sengo sengo; // Create a Sengo sensor object
void setup() {
sentry_err_t err = SENTRY_OK; // Error status variable
Serial.begin(9600); // Initialize the serial port for debugging the output
Serial.println("Waiting for sengo initialize...");
// Initialize the sensor according to the selected communication method
#ifdef SENGO_I2C
Wire.begin(); // Initialize I2C bus
// Keep trying to connect until done
while (SENTRY_OK != sengo.begin(&Wire)) {
yield(); // Allow other tasks to run while waiting
}
#endif
#ifdef SENGO_UART
mySerial.begin(9600); // Initialize the soft serial port
while (SENTRY_OK != sengo.begin(&mySerial)) {
yield();
}
#endif
sentry_object_t param; // Parametric structure
Serial.println("Sengo begin Success.");
//Set the x-coordinate of the detection area
param.x_value = 50;
//Set the y-coordinate of the detection area
param.y_value = 50;
//Set the width of the detection area
param.width = 20;
//Set the height of the detection area
param.height = 20;
//Write the parameters into the sensor
err = sengo.SetParam(VISION_TYPE, ¶m);
//Error handling
if (err) {
Serial.print("sengo.SetParam ");
if (err) {
Serial.print("Error: 0x");
} else {
Serial.print("Success: 0x");
}
Serial.println(err, HEX); // Print hexadecimal error code
for (;;)
; // Infinite loop blocking (manual restart required)
}
// Enable the visual recognition algorithm
err = sengo.VisionBegin(VISION_TYPE);
Serial.print("sengo.VisionBegin(kVisionColor) ");
if (err) {
Serial.print("Error: 0x");
} else {
Serial.print("Success: 0x");
}
Serial.println(err, HEX); // Output the initialization result
}
void loop() {
// Read the number of detected objects (kStatus is the acquisition status)
int obj_num = sengo.GetValue(VISION_TYPE, kStatus);
if (obj_num > 0) { // If an object is detected
Serial.print("Totally ");
Serial.print(obj_num);
Serial.println(" objects");
// Obtain the label (color ID) of the object
int label = sengo.GetValue(VISION_TYPE, kLabel, 1);
Serial.print('|');
Serial.print(label); // Output label (such as |1|3|5|)
Serial.println("|"); // End mark
}
delay(500); // Delay 500ms to reduce the refresh rate
}
4.1.8 Test Result
After uploading the code, align the recognition box on the module with the color to be recognized, and the it will change to the same color as the recognized one. Then, the label value corresponding to the color will be printed on the serial monitor.

