Example Code for Arduino-Read Position Data(I2C)

Last revision 2026/01/20

This article guides you through the process of connecting a HuskyLens AI machine vision sensor to an Arduino board using the I2C protocol. It provides detailed instructions on hardware setup, software installation, and example code to help you read real-time position data of objects via the Arduino serial monitor.

In this project, HuskyLens will be connected to Arduino mainboard. And Arduino Uno will read position data of the object from HuskyLens. Then the serial port monitor will print the data. So that, you can read the position of the object in real time.

Hardware Preparation

Wiring Diagram

Huskylens_Wiring_Diagarm_I2C

Software Preparation

Install the HUSKYLENS Library

  1. Unzip the file, then copy the folder to the "libraries" folder of the Arduino IDE. Then check whether the folder name is "HUSKYLENS". If not, please change it as "HUSKYLENS". Please note that the library file name must be HUSKYLENS.

    HUSKYLENS_Library_Position

  2. All .h files and .cpp files must in the root directory of the "HUSKYELSN" folder.

    HUSKYLENS_Library_Directory

HuskyLens Protocol Setting

You need to set the protocol type of HuskyLens. The protocol should be 'I2C'. Of course, your can adopt the Auto Detect protocol, which is easy-to-use and convenient.
HuskyLens_Protocol_Setting_I2C

Sample Code

#include "HUSKYLENS.h"
#include "SoftwareSerial.h"

HUSKYLENS huskylens;
//HUSKYLENS green line >> SDA; blue line >> SCL
void printResult(HUSKYLENSResult result);

void setup() {
    Serial.begin(115200);
    Wire.begin();
    while (!huskylens.begin(Wire))
    {
        Serial.println(F("Begin failed!"));
        Serial.println(F("1.Please recheck the \"Protocol Type\" in HUSKYLENS (General Settings>>Protocol Type>>I2C)"));
        Serial.println(F("2.Please recheck the connection."));
        delay(100);
    }
}

void loop() {
    if (!huskylens.request()) Serial.println(F("Fail to request data from HUSKYLENS, recheck the connection!"));
    else if(!huskylens.isLearned()) Serial.println(F("Nothing learned, press learn button on HUSKYLENS to learn one!"));
    else if(!huskylens.available()) Serial.println(F("No block or arrow appears on the screen!"));
    else
    {
        Serial.println(F("###########"));
        while (huskylens.available())
        {
            HUSKYLENSResult result = huskylens.read();
            printResult(result);
        }    
    }
}

void printResult(HUSKYLENSResult result){
    if (result.command == COMMAND_RETURN_BLOCK){
        Serial.println(String()+F("Block:xCenter=")+result.xCenter+F(",yCenter=")+result.yCenter+F(",width=")+result.width+F(",height=")+result.height+F(",ID=")+result.ID);
    }
    else if (result.command == COMMAND_RETURN_ARROW){
        Serial.println(String()+F("Arrow:xOrigin=")+result.xOrigin+F(",yOrigin=")+result.yOrigin+F(",xTarget=")+result.xTarget+F(",yTarget=")+result.yTarget+F(",ID=")+result.ID);
    }
    else{
        Serial.println("Object unknown!");
    }
}

Result

  1. Upload the above codes to your Arduino board.

  2. Let your HuskyLens learn a new thing first. You can refer to the previous chapters of this tutorial.

  3. Open the serial monitor of Arduino IDE, then you will get the position data of the object.

    If HuskyLens is in the face recognition, object tracking, object recognition, color recognition, tag recognition mode, you will get the results like follows:

    Arduino_Read_Position_Data_Result_Frame

    If HuskyLens is in the line tracking mode, you will get the results like follows:

    Arduino_Read_Position_Data_Result_Arrow

Was this article helpful?

TOP