Can't turn on a virtual LED

I’m sorry if this is a very beginner question, but I’ve looked through all of the docs and forums and can’t find a solution. All I’d like to do is turn on an LED on a virtual pin in the Blynk app.

I’ve tried all of the example sketches and nothing has worked. I am able to turn on a real LED on my breadboard by using a Blynk button connected to a digital pin. I am also able to read from a slider on a virtual pin, but I can’t seem to virtual write anything.

Any help would be greatly appreciated, thank you!

1 Like

Send a value of 255 instead of 1… Blynk app LEDs are variable. Sending a 128 lights 50% of it.

Thanks for the reply, I’ve tried that as well. I tried a couple different ways to turn it on.

Here’s an example of a sketch that won’t work for me:

#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial

// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleSerialBLE.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "396bef6a49a54b3ab4a48f07bac5aeba";

WidgetLED led1(V1);

BlynkTimer timer;

// V1 LED Widget is blinking
void blinkLedWidget()
{
  if (led1.getValue()) {
    led1.off();
    Serial.println("LED on V1: off");
  } else {
    led1.on();
    Serial.println("LED on V1: on");
  }
}

void setup()
{

  DebugSerial.begin(9600);

  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  timer.setInterval(1000L, blinkLedWidget);
}

void loop()
{
  Blynk.run();
  timer.run();
}

After trying for a few hours, now the virtual LED is permanently ON… but I’m pretty sure this sketch is supposed to make it blink.

Change that line to:

if (led1.getValue()==255) {

and/or try using

led1.setValue(255)  ; On

and

led1.setValue(0)  ; Off

I haven’t used Virtual pins with Arduino code yet.

That didn’t change anything but I noticed something else.

The Serial should be printing “LED on V1: off” / “on” but it’s not printing anything.

I also realized I sometimes get an error that my “SimpleTimer” library is invalid. I looked into that and realized the two text files that I installed were titled SimpleTimer.cpp.rtf and SimpleTimer.h.rtf and were both text files. I eliminated the .rtf extension and they transformed into .cpp and .h files and the error went away, but the content changed slightly:

the .cpp file changed from:

/*
 * SimpleTimer.cpp
 *
 * SimpleTimer - A timer library for Arduino.
 * Author: mromani@ottotecnica.com
 * Copyright (c) 2010 OTTOTECNICA Italy
 *
 * This library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser
 * General Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser
 * General Public License along with this library; if not,
 * write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */


#include "SimpleTimer.h"


// Select time function:
//static inline unsigned long elapsed() { return micros(); }
static inline unsigned long elapsed() { return millis(); }


SimpleTimer::SimpleTimer() {
    unsigned long current_millis = elapsed();

    for (int i = 0; i < MAX_TIMERS; i++) {
        enabled[i] = false;
        callbacks[i] = 0;                   // if the callback pointer is zero, the slot is free, i.e. doesn't "contain" any timer
        prev_millis[i] = current_millis;
        numRuns[i] = 0;
    }

    numTimers = 0;
}

etc.

and became:

{\rtf1\ansi\ansicpg1252\cocoartf1504\cocoasubrtf600
{\fonttbl\f0\fmodern\fcharset0 Courier;}
{\colortbl;\red255\green255\blue255;\red0\green0\blue0;}
{\*\expandedcolortbl;\csgray\c100000;\cssrgb\c0\c0\c0;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\sl280\partightenfactor0

\f0\fs24 \cf2 \expnd0\expndtw0\kerning0
\outl0\strokewidth0 \strokec2  /*\
 * SimpleTimer.cpp\
 *\
 * SimpleTimer - A timer library for Arduino.\
 * Author: mromani@ottotecnica.com\
 * Copyright (c) 2010 OTTOTECNICA Italy\
 *\
 * This library is free software; you can redistribute it\
 * and/or modify it under the terms of the GNU Lesser\
 * General Public License as published by the Free Software\
 * Foundation; either version 2.1 of the License, or (at\
 * your option) any later version.\
 *\
 * This library is distributed in the hope that it will\
 * be useful, but WITHOUT ANY WARRANTY; without even the\
 * implied warranty of MERCHANTABILITY or FITNESS FOR A\
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public\
 * License for more details.\
 *\
 * You should have received a copy of the GNU Lesser\
 * General Public License along with this library; if not,\
 * write to the Free Software Foundation, Inc.,\
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\
 */\
\
\
#include "SimpleTimer.h"\
\
\
// Select time function:\
//static inline unsigned long elapsed() \{ return micros(); \}\
static inline unsigned long elapsed() \{ return millis(); \}\
\
\
SimpleTimer::SimpleTimer() \{\
    unsigned long current_millis = elapsed();\
\
    for (int i = 0; i < MAX_TIMERS; i++) \{\
        enabled[i] = false;\
        callbacks[i] = 0;                   // if the callback pointer is zero, the slot is free, i.e. doesn't "contain" any timer\
        prev_millis[i] = current_millis;\
        numRuns[i] = 0;\
    \}\
\
    numTimers = 0;\
\}\

Maybe the SimpleTimer is the issue? I’ll look back at how to install that.

I fixed the SimpleTimer and I’m now using this code:

//#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial

// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleSerialBLE.h>


#include <SimpleTimer.h>

char auth[] = "396bef6a49a54b3ab4a48f07bac5aeba";

WidgetLED led1(V1);

// the timer object
SimpleTimer timer;

// a function to be executed periodically
void repeatMe() {
    Serial.print("Uptime (s): ");
    Serial.println(millis() / 1000);

  if (led1.getValue()) {
    led1.off();
    Serial.println("LED on V1: off");
  } else {
    led1.on();
    Serial.println("LED on V1: on");
  }
}

void setup() {
    Serial.begin(9600);
    Blynk.begin(Serial, auth);
    timer.setInterval(1000, repeatMe);
}

void loop() {
    Blynk.run();
    timer.run();
}

The serial monitor is giving me this:

LED on V1: on
Uptime (s): 76
LED on V1: off
Uptime (s): 77
LED on V1: on
Uptime (s): 78
LED on V1: off
Uptime (s): 79
LED on V1: on
 396bef6a49a54b3ab4a48f07bac5aebaUptime (s): 80
LED on V1: off
Uptime (s): 81
LED on V1: on
Uptime (s): 82
LED on V1: off
Uptime (s): 83
LED on V1: on
Uptime (s): 84
LED on V1: off
 396bef6a49a54b3ab4a48f07bac5aebaUptime (s): 85
LED on V1: on
Uptime (s): 86
LED on V1: off
Uptime (s): 87
LED on V1: on
Uptime (s): 88
LED on V1: off

It seems that commenting out “#define BLYNK_USE_DIRECT_CONNECT” allows me to see this on the serial monitor.

However the virtual LED is still not blinking for some reason, despite it’s value clearly changing.

Does Blynk connect at the start of debug output?

The Blynk logo doesn’t even come up in the monitor using the previous code I posted.

The only way I get that to show up is by commenting out “#define BLYNK_PRINT DebugSerial”, and then Login always times out:

[0] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.7 on Arduino Uno

[5001] Connecting...
 396bef6a49a54b3ab4a48f07bac5aeba[10104] Connecting...
 396bef6a49a54b3ab4a48f07bac5aeba[15209] Connecting...
 396bef6a49a54b3ab4a48f07bac5aeba[17313] Login timeout
[20313] Connecting...
 396bef6a49a54b3ab4a48f07bac5aeba[22417] Login timeout
[25417] Connecting...
 396bef6a49a54b3ab4a48f07bac5aeba[27522] Login timeout

What hardware, and how is it connected?

Arduino Uno, HC 05 bluetooth, Samsung tablet running Android.

What’s strange is I can still turn on my physical LED with the Blynk app (digital pin output), so it must be working somehow.

Also I appreciate your help so far! This has gotten pretty frustrating just trying to turn on an LED

I’m not sure how the physical LED is getting turned on since you aren’t getting a successful Blynk connect. AFAIK The Uno needs Wifi access in order to connect to the Blynk cloud server, not Bluetooth. Get the login timeouts resolved and I think your issue is resolved. Only way I know to do that is to use Wifi on the Uno. I could be wrong… I just started Blynk-ing 4 days ago.

Oh damn, that makes sense…

Did I just completely ignore the fact that the Arduino needs to be connected to the internet / Blynk server? I wonder how this button is still working, maybe it works with digital output.

I’ll look into a Wifi shield, or it looks like theres a way to just do it via USB. Thanks for your help!!!

I’ve used this Sparkfun board on a non-Blynk project. Did have trouble when I got the same board from a non-direct source. Sourced from Sparkfun it worked well.

1 Like

Get yourself a WeMos/NodeMCU style ESP Dev board and ditch the Arduino.

@ChrisPomerleau

Bluetooth will still work, it is just a more limited and serverless way of using Blynk, but the app still needs internet connection for the account login.

And SimpleTimer is now replaced with BlynkTimer… so no need to include any additional library.

Try a more current bluetooth example from the Sketch Builder (link in the upper right of this page) for a simple virtual LED blink

https://examples.blynk.cc/?board=Arduino%20Uno&shield=HC05%20or%20HC06&example=Widgets%2FLED%2FLED_Blink