BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Blynk + PCA9685 Servo Motor

I’ve got some code that controls 5 servos with a potentiometer through a PCA9685. Im trying to incorporate Blynk into it as well but not having much luck. Im not too experienced with the code side of things. If anyone wouldn’t mind taking a look and letting me know if im close or way off that would be great! Also checked previous posts regarding the PCA9685 but still struggling.

On the Blynk side of things i have 3 sliders all 0 - 180 and each point to V1, V2, V3

Thanks
Gav

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// PCA9685 outputs = 12-bit = 4096 steps
// 2.5% of 20ms = 0.5ms ; 12.5% of 20ms = 2.5ms
// 2.5% of 4096 = 102 steps; 12.5% of 4096 = 512 steps
uint8_t servonum = 4;
int  InValue;

// Second Servo
#define SERVOMIN  125 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  575 //

;char auth[] = "****";
char ssid[] = "****";
char pass[] = "****";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
    Wire.begin();                 // Wire must be started first
  pwm.begin();
  pwm.setPWMFreq(60);
  
}

void loop()
{
  Blynk.run();
  InValue=analogRead(D2);

  map(InValue,0,4095,SERVOMIN,SERVOMAX);
}

BLYNK_WRITE(V1)
{
    pwm.setPWM(0, 0, InValue);
}

BLYNK_WRITE(V2)
{
    pwm.setPWM(1, 0, InValue);
}

BLYNK_WRITE(V3)
{
    pwm.setPWM(2, 0, InValue);
}

If you’re wanting to do something with the position of the slider widgets then you have to capture that value using the param.asInt function within the BLYNK_WRITE callback function, like this:

BLYNK_WRITE(V1)
{
    int slider_value = param.asInt();
}

This captures the setting of the slider (0-180 according to what you’ve said) ad stores it in the local variable slider_value

You can then use this to actuate your servo in whatever way you want.

Also, doing an analogRead every time the void loop executes may not work too well with Blynk.
Read this, so that you understand the implications and how to fix the issue i you have disconnection issues…

Pete.