Projects / Colour and object sorter with HuskyLens

Colour and object sorter with HuskyLens

A HuskyLens AI camera learns objects with one button press, and an Arduino sorts them left or right with a servo and counts each type.

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. UNOHUSKYLEN

    Install the HUSKYLENS library

    From the Library Manager or GitHub.

  2. UNOHUSKYLEN

    Set the protocol to I2C

    In the HuskyLens menu.

  3. UNO59°

    Wire the camera and servo

    As in the table.

  4. UNO

    Teach object 1 and object 2

    With the learn button.

  5. UNO

    Upload the sketch

    Check the Serial Monitor.

  6. UNO59°

    Show each object

    Watch the servo swing.

  7. UNO

    Make a ramp and two boxes

    Cardboard works.

  8. UNO

    Try Colour Recognition

    Change the algorithm.

In short

How do you build a colour and object sorter with HuskyLens?

A HuskyLens AI camera learns objects with one button press, and an Arduino sorts them left or right with a servo and counts each type. You need Arduino Uno R3, Huskylens AI vision sensor, SG90 Micro Servo and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
HuskyLens red (VCC) and black (GND)Uno 5V and GNDPower (the camera draws several hundred mA, use a good USB port)
HuskyLens green (SDA)Uno A4I2C data
HuskyLens blue (SCL)Uno A5I2C clock
Servo orange or yellow signalUno D9Sorting arm
Servo red and brownUno 5V and GND (use a separate 5 V supply with a shared GND if it twitches)Servo power

Code

The HuskyLens is a camera with its own small AI chip, so it does the hard vision work and the Arduino only reads a number. Switch the HuskyLens to I2C in its menu (General Settings, Protocol Type), wire the four pins as in the table, and install the HUSKYLENS library from DFRobot's GitHub or the Arduino Library Manager. Teach it with the board's button: point at an object in the box (a red ball, for example), long-press the learn button until the frame is learned, and release to give it ID 1; do the same for a second object as ID 2. The sketch asks the camera what it sees, and if it sees ID 1 it swings the servo to the left, ID 2 to the right, and it counts each time a new object appears. When nothing is seen the arm returns to centre. In real use you would put a small ramp or a conveyor for the objects to slide down, and push them into two boxes. Good light matters: use a plain background and a lamp, and learn each object from a few angles. After this works try Colour Recognition, Face Recognition or Tag Recognition by changing the algorithm line. Setup: Arduino IDE, board "Arduino Uno". Install the HUSKYLENS library.

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <HUSKYLENS.h>
#include <Servo.h>

HUSKYLENS huskylens;
Servo arm;
const uint8_t SERVO_PIN = 9;
const int POS_LEFT = 40, POS_CENTRE = 90, POS_RIGHT = 140;
int countA = 0, countB = 0;
int lastId = 0;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  arm.attach(SERVO_PIN);
  arm.write(POS_CENTRE);
  while (!huskylens.begin(Wire)) {            // HuskyLens menu: General Settings, Protocol, I2C
    Serial.println("HuskyLens not found, check I2C wiring and protocol setting");
    delay(500);
  }
  huskylens.writeAlgorithm(ALGORITHM_OBJECT_CLASSIFICATION);
  Serial.println("Ready: teach ID 1 and ID 2 with the camera button");
}

void loop() {
  if (!huskylens.request() || !huskylens.available()) {
    arm.write(POS_CENTRE);
    lastId = 0;
    delay(100);
    return;
  }
  HUSKYLENSResult r = huskylens.read();
  if (r.ID == 1) {
    arm.write(POS_LEFT);
    if (lastId != 1) countA++;
  } else if (r.ID == 2) {
    arm.write(POS_RIGHT);
    if (lastId != 2) countB++;
  }
  if (r.ID != lastId) {
    lastId = r.ID;
    Serial.print("A: "); Serial.print(countA);
    Serial.print("  B: "); Serial.println(countB);
  }
  delay(100);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Uno with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

HuskyLens not found, check I2C wiring and protocol setting

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Show object 1Servo swings left and the count rises
Show object 2Servo swings right
Remove the objectServo returns to centre
Show a different objectNothing moves, or it is guessed as one of the two
Change the lightSee how recognition changes

If it does not work

HuskyLens not found

Wrong protocol setting, or SDA and SCL swapped.

Arm twitches or Uno resets

Servo power; use a separate 5 V supply with shared GND.

Wrong object chosen

Teach it again from several angles in the same light.

Servo does not move

Check D9 and the object IDs learned.

Compile error

Install the HUSKYLENS library.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the colour and object sorter with HuskyLens?

Arduino Uno R3, Huskylens AI vision sensor, SG90 Micro Servo and Jumper wires male to male (40 pcs, 20 cm).

How long does the colour and object sorter with HuskyLens take to build?

About 90 minutes for a beginner maker, in 8 steps.

How do I know the colour and object sorter with HuskyLens works?

Show object 1: Servo swings left and the count rises. Show object 2: Servo swings right. Remove the object: Servo returns to centre.

HuskyLens not found: what should I check?

Wrong protocol setting, or SDA and SCL swapped.

Arm twitches or Uno resets: what should I check?

Servo power; use a separate 5 V supply with shared GND.

Wrong object chosen: what should I check?

Teach it again from several angles in the same light.

Servo does not move: what should I check?

Check D9 and the object IDs learned.