Blynk button to light up LED on digital pin not working

Hi, I am still an intermediate Arduino learner and very new to Blynk. Sorry if I am asking stupid questions but thanks in advance for your kind help!

Objective: Build an Arduino UV & Temperature (TMP36) Station. Control a UV LED via the Blynk Button. View Sensor Readings on Blynk App.

Setup: Arduino Uno connected to Blynk Server through USB Serial as illustrated in the official example code. Sparkfun ML8511 UV Sensor and a TMP36 Sensor connected. Also added a UV LED to a digital pin for UV reading reference.

What Works: I can see sensor data and can even graph them using Blynk.virtualWrite Command.

What Doesn’t work: Before the sensor station, I tried the official USB Serial example code from the library where I can simply make a button and assign to one of the digital pin and light up a LED that is connect to it. According to my understanding that a digital HIGH signal is sent to the particular pin when I push the button assinged to it. That WORKS.** But when I try it on my Sensor Station code. The LED does not work at all.**

Below is my code:

//Hardware pin definitions
int UVOUT = A0; //Output from the sensor
int REF_3V3 = A1; //3.3V power on the Arduino board
int TMP36 = A2; //TMP36 Temperature Sensor Pinout

// Blynk Setup
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>
char auth[] = “5d376a0bb6b5480494d4bd131246b5e3”; //blynk Auth Token (From App.)

void setup()
{
Serial.begin(9600);

SwSerial.begin(9600); //Software Serial for Blynk
Blynk.begin(auth); //Blynk

pinMode(UVOUT, INPUT);
pinMode(REF_3V3, INPUT);
pinMode(9, OUTPUT);

}

void loop()
{
Blynk.run(); //Runs Blynk

//////////Temperature Reading//////////
int tmp36Val = analogRead(TMP36);
float tmp36voltage = (tmp36Val / 1024.0) * 5.0;
float tmp36tmp = (tmp36voltage - 0.5) * 100;

/////////UV Reading/////////
int uvLevel = averageAnalogRead(UVOUT);
int refLevel = averageAnalogRead(REF_3V3);

//Use the 3.3V power pin as a reference to get a very accurate output value from sensor
float outputVoltage = 3.3 / refLevel * uvLevel;

float uvIntensity = mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0); //Convert the voltage to a UV intensity level

Blynk.virtualWrite(0, uvIntensity); //Write Values to Blynk Virtual Inputs
Blynk.virtualWrite(1, uvIntensity);
Blynk.virtualWrite(2, tmp36tmp);
Blynk.virtualWrite(3, tmp36tmp);

delay(1000);
}

//Takes an average of readings on a given pin
//Returns the average
int averageAnalogRead(int pinToRead)
{
byte numberOfReadings = 8;
unsigned int runningValue = 0;

for (int x = 0 ; x < numberOfReadings ; x++)
runningValue += analogRead(pinToRead);
runningValue /= numberOfReadings;

return (runningValue);
}

//The Arduino Map function but for floats
//From: http://forum.arduino.cc/index.php?topic=3922.0
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Hello. Please go though Blynk examples they briefly explains what is wrong with your code.

To be specific - you can’t run Blynk.virtualWrite in a loop, delays also should be avoided in main loop.

Hi KingArt,
Just one comment… Don’t use Delays… Blynk likes movement! Use timer function instead.

Regards!!

It works now!

Thanks Dimitry and psoro for the kind help. This solved my problem after digging in the example. I did not know that I cannot put the Blynk.virtualWrite in loop and forgot that the delay totally stopped everything from running!

Now everything is working and the data flow rate is much smoother and more responsive!

I have another question. What are these?

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
#define BLYNK_PRINT SwSerial

I realized I can comment them and it doesn’t seem affect the functionality of the code. Also, if I put a Serial.print(“example”); at say the void setup(), then the Blynk App would always report that the “Arduino Uno is Offline”.

I have to admit that I don’t really understand much about the Serial Communication. :persevere:

Below is the working code:

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

SimpleTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth = “5d376a0bb6b5480494d4bd131246b5e3”;

int tmp36 = A2; //TMP36 Temperature Sensor Pinout
int tmp36tmp = 0;
int uvIntensity = 0;
int UVOUT = A0; //Output from the sensor
int REF_3V3 = A1; //3.3V power on the Arduino board

void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
Blynk.begin(auth);

pinMode(UVOUT, INPUT);
pinMode(REF_3V3, INPUT);

// Setup a function to be called every second
timer.setInterval(1000L, SendUpTime);
timer.setInterval(1000L, ReadSensors);

// Serial.print(“UV & Temperature Sensor Starting”);
}

// This function sends Arduino’s up time every second to Virtual Pin (5).
// In the app, Widget’s reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void SendUpTime()
{
// You can send any value at any time.
// Please don’t send more that 10 values per second.
Blynk.virtualWrite(V0, millis() / 1000);
}

void ReadSensors()
{
int tmp36Val = analogRead(tmp36);
float tmp36voltage = (tmp36Val / 1024.0) * 5.0;
float tmp36tmp = (tmp36voltage - 0.5) * 100;

int uvLevel = averageAnalogRead(UVOUT);
int refLevel = averageAnalogRead(REF_3V3);

//Use the 3.3V power pin as a reference to get a very accurate output value from sensor
float outputVoltage = 3.3 / refLevel * uvLevel;

float uvIntensity = mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0); //Convert the voltage to a UV intensity level

Blynk.virtualWrite(V1, tmp36tmp);
Blynk.virtualWrite(V2, tmp36tmp);
Blynk.virtualWrite(V3, uvIntensity);
Blynk.virtualWrite(V4, uvIntensity);
}

void loop()
{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer
}

//Takes an average of readings on a given pin
//Returns the average
int averageAnalogRead(int pinToRead)
{
byte numberOfReadings = 8;
unsigned int runningValue = 0;

for (int x = 0 ; x < numberOfReadings ; x++)
runningValue += analogRead(pinToRead);
runningValue /= numberOfReadings;

return (runningValue);
}

//The Arduino Map function but for floats
//From: map to floating point - Frequently-Asked Questions - Arduino Forum
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

@KingArt those lines are for Arduino’s with just one physical serial port. You can hook up an extra serial port to pins 2 and 3 (or most of the pins) to see the debug info etc.