HuskyLens is an easy-to-use AI machine vision sensor. It is equipped with multiple functions, such as face recognition, object tracking, object recognition, line tracking, color recognition, and tag(QR code) recognition.
By hajnyondra
I'm trying to move x y tilt to the center of the object. I'm using 180 degree servos and huskylens is on top of them.
Problem is that for instance, I get coordinates x=100 and it moves in the opposite direction of the object.
This program is on esp32 :

#include <HardwareSerial.h>
#include <HuskyLensProtocolCore.h>
#include <HUSKYLENS.h>
#include <HUSKYLENSMindPlus.h>
#include <DFRobot_HuskyLens.h>
#include <M5Stack.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>

HUSKYLENS huskylens;
#define RXD2 17 //Oops Switched my wires!!
#define TXD2 16 //Oops Switched my wires!!

int ID1 = 1;
int ax;
int ay;

const int freq = 50;
const int ledChannel = 4;
const int ledChannel2 = 5;
const int resolution = 10;

Servo myservo;
Servo myservo2;// create servo object to control a servo

void setup() {

pinMode(4, OUTPUT);
pinMode(5, OUTPUT);

myservo2.setPeriodHertz(50);// standard 50 hz servo
myservo.attach(4, 771, 2740);
myservo2.attach(5, 500, 2400);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); //Communication with Huskylens

while (!huskylens.begin(Serial2))

void loop() {

int32_t error;
if (!huskylens.request(ID1)) {
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!"));
HUSKYLENSResult result =;

// Calculate the error: where it needs to move for center
error = (int32_t)result.xTarget - (int32_t)160;


void printResult(HUSKYLENSResult result) {

if (result.xCenter != 160) {

int xpr = result.xCenter;
xpr = map(xpr, 0, 320, 180, 0);


// if (result.yCenter != 120) {
// delay(100);

// int ypr = result.yCenter;
// ypr = map(ypr, 0, 240, 180, 0);
// myservo2.write(ypr);
// Serial.println("y=");
// Serial.println(ypr);}

It's probably some error in my calculation but I'm simply not able to find it
By james.stanley
I'm not clear on how your servo connections work, but you might just try reversing one of the pairs of arguments in your map() call. Either use

xpr = map(xpr,320, 0, 180, 0);
xpr = map(xpr, 0, 320, 0, 180);

I haven't used map() for this. so I'm not sure about it. I have used a servo to position a Huskylens, but have used more direct calculations to change the servo position.

Hope this helps.
By jeffroig1
I think I can help you with this as I have been working with the HuskyLens and have had some success do just what you are trying to do.
If you use the Map function for the servos you are going to work the servos to death and it will never actually center on the object/color etc...
What I have found that works is to first define a integer "int ServoY" then based on the Y value that you receive from the lens I do the following.
if Yvalue>130 then ServoY=ServoY-1 (you can change the 1 to a higher value to increase the speed)
If Yvalue<110 then ServoY=ServoY+1
If Yvalue>110 && YValue<130 then ServoY=ServoY (This is your sweet spot the center)
To handle values above or below the servo limits I do the following.
If ServoY>250 then ServoY=250
If ServoY<0 then ServoY=0
I am using it with an arduino and have had great success to using it this way.
Hope this helps you.