Help with Raspberry Pi 3 servo control with slider widget

I have connected the servo to GPIO 18 and setup the slider widget on Blynk app but the servo does not move.
The servo works fine without Blynk. Is it not that simple or am I missing something?
I can control basic GPIO functions like relay control with Blynk just can’t figure out servo control.
Any help will be greatly appreciated.

Servos don’t really use standard PWM… more like a variation of it… so best to use a proper Servo control script within the RPi (as you stated works) but supply positioning info via a slider using virtual pins, and not depend on direct GPIO control from the App.

And please do not connect the Servo directy to the Pi! Chances are you will blow up your Pi in the end :slight_smile:

Thank you Gunner, that makes sense why it did not work.
I’ve found this nodejs servo code that I would like to try to use with virtual pins. I’ve done a lot of reading on virtual pins but I can’t comprehend how to integrate with this code. Maybe someone can give me hint on how the script should look like.

With this code I can control servo with keyboard but I would like to substitute with virtual pins.

// require modules
    const Gpio = require('pigpio').Gpio;
    const keypress = require('keypress');
    // define servo GPIO ports
    const PAN_PORT = 22;
    const TILT_PORT = 23;
    // define minimum and maximum PWM values for servos
    const PAN_MIN = 520;
    const PAN_MAX = 2490;
    const TILT_MIN = 1000;
    const TILT_MAX = 1700;
    // define movement step
    const STEP = 20;
    // configure GPIOs
    const pan = new Gpio(PAN_PORT, {mode: Gpio.OUTPUT});
    const tilt = new Gpio(TILT_PORT, {mode: Gpio.OUTPUT});
    // helper function limiting value to [min, max] range
    function constraint(value, min, max) {
      return Math.min(Math.max(value, min), max);
    }
    // center servos
    let panValue = PAN_MIN + (PAN_MAX - PAN_MIN) / 2;
    let tiltValue = TILT_MIN + (TILT_MAX - TILT_MIN) / 2;
    pan.servoWrite(panValue);
    tilt.servoWrite(tiltValue);
    // make process.stdin start emitting keypress events 
    keypress(process.stdin);
    // handle keypress
    process.stdin.on('keypress', (ch, key) => {
      switch (key.name) {
        case 'left':
          panValue += STEP;
          panValue = constraint(panValue, PAN_MIN, PAN_MAX);
          pan.servoWrite(panValue);
          break;
        case 'right':
          panValue -= STEP;
          panValue = constraint(panValue, PAN_MIN, PAN_MAX);
          pan.servoWrite(panValue);
          break;
        case 'up':
          tiltValue -= STEP;
          tiltValue = constraint(tiltValue, TILT_MIN, TILT_MAX);
          tilt.servoWrite(tiltValue);
          break;
        case 'down':
          tiltValue += STEP;
          tiltValue = constraint(tiltValue, TILT_MIN, TILT_MAX);
          tilt.servoWrite(tiltValue);
          break;
        case 'c':
          // ctrl+c will still stop the script, needs to be added manually because of raw mode
          if (key.ctrl) {
            process.stdin.pause();
          }
      }
      console.log(`pan: ${panValue} tilt: ${tiltValue}`)
    });
    // use raw mode to enable character-by-character input
    process.stdin.setRawMode(true);
    // begin reading from stdin
    process.stdin.resume();
    console.log('Use keyboard arrows to control servos. Press ctrl+c to stop.');

I would suggest using a Joystick Widget, and setting the X & Y MIN MAX according to the existing suggested ranges (adjust as required)… for example:


In Joystick settings using split mode: X axis using V22 - (and yes I am matching the vPin and the physical pin numbers for ease of reference… but it is not required :wink: ) and set ranges MIN = 520 & MAX = 2490 (adjust as required) and add this into your code.

var JoyX = new blynk.VirtualPin(22);  // Setup Joystick X on V22
JoyX.on('write', function(panValue) {  // Watches for Joystick X and applys data to panValue
  pan.servoWrite(panValue);  // Set Servo to value
});

Repeat and set with proper variables and vPin accordingly for the Y axis.

This works for my RPi3 setup, but you may have to read up about NodeJS in case you need to tweak the syntax or placement of this example code to work for your sketch.

1 Like

Gunner you are the man! I’ve been trying to do this for weeks but with your code I got the servo running within minutes, also now I better understand how to integrate virtual pins with my sketches.
Thank you for taking the time to help us noobs.