Example Code for Arduino-Read Position Data(UART)
Last revision 2026/01/20
This article demonstrates how to connect a HuskyLens AI vision sensor to an Arduino Uno using UART to read position data of objects in real time, complete with hardware preparation, software setup, and sample code, making it perfect for object tracking projects.
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
- SEN0305 HuskyLens K210 AI Machine Vision Sensor x1
- DFR0216-2 DFRduino UNO R3 with IO Expansion Shield and USB Cable A-B x1
- FIT0773 Gravity: 4Pin I2C/UART Sensor Cable for Arduino(Included with SEN0305) x1
Wiring Diagram

Software Preparation
- Download Arduino IDE: Click to Download Arduino IDE
- Download and install the HUSKYLENS Library.
Install the HUSKYLENS Library
-
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.

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

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

Sample Code
#include "HUSKYLENS.h"
#include "SoftwareSerial.h"
HUSKYLENS huskylens;
SoftwareSerial mySerial(10, 11); // RX, TX
//HUSKYLENS green line >> Pin 10; blue line >> Pin 11
void printResult(HUSKYLENSResult result);
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
while (!huskylens.begin(mySerial))
{
Serial.println(F("Begin failed!"));
Serial.println(F("1.Please recheck the \"Protocol Type\" in HUSKYLENS (General Settings>>Protocol Type>>Serial 9600)"));
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
-
Upload the above codes to your Arduino board.
-
Let your HuskyLens learn a new thing first. You can refer to the previous chapters of this tutorial.
-
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:

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

Was this article helpful?
