Digital pins turns HIGH on my NodeMcu V3

Sorry Pete :wink:
Can’t get to work your ‘’'cpp code
Maybe becauce of apple?

did it with Ctrl+C/V
my keyboard backtick symbols didn’t work.

Pete.

1 Like

Thanks. Now got it - Alt+~=`

1 Like

And your wiring diagram?

Pete.

BMP180
VIN - 3V
GND - G
SCL - D1
SDA - D2

DHT11
VIN - 3V
GND - G
OUT - D4

Relay V5 (with seperate power, ground together with Nodemcu G)
VCC - seperate 5V DC
GND - connected together with 5V DC GND and G of NodeMcu
IN - D3 (the same problem with D5, D6, D7 pin’s)

Okay, first of all, pin D3 (GPIO 0) isn’t a good choice of pin for your relay. See this article:

Also, you’re not defining any pinmode for the relay pin, or have any code to control it. You may be doing some direct digital pin manipulation from a widget in the app, but you don’t mention this.

You don’t say if your relay is active HIGH or active LOW, but a pinmode statement in your void setup, followed by a digitalwrite command to set the pin to the opposite of what your relay needs to activate, should do the trick.

Pete.

1 Like

From this turns out that:
ESO8266
Good pins for Relays: D1, D2, D6, D8
Good pins for Sensors: D1, D2, then D5, D6, D7

I changed my pin for relay to D6 (GPIO12) and also the VCC and GND of relay to NodeMcu board (before was external 5v).
And now my relay works, although it is active at the start :frowning:
Yes, I make the pinmode statements in Blynk widget settings.

You’re probably better sticking with the external PSU and shared Ground, as you’ll probably get better stability.

You can manipulate the values of digital pins n the widgets, but setting the pinmode is done in code on the NodeMCU. It’s not necessary to set the pinmode inless you’re changing the values of the pins within the code, but as I said, a pin,ode declaration followed by a digitalwrite command to put your relay to the state you want at boot-up is one way to go. I’d probably follow that with a blink.sync command to then synchronise the relay with the state of the switch in the app.

Pete.

Looks, like I have to put some pinmode in the code.
Because if i set LOW in Blynk app, anyway relay starts as HIGH.
Then i switch button ON (nothing happens), then OFF and relay goes LOW(OFF).

But you also need to do Blynk.sync because if the switch in the app is On when power is restored to te NodeMCU then they’ll be out of sync again.

Pete.

Yes pin declarations are almost always needed, but you should also look into Virtual Pins and how to properly use them. Direct pin manipulation works, kinda, but has a lot of limitations.

I don’t think a sync will work in this case due to the fact that his switch in the APP is tied directly to the digital pin, and not a virtual one. Although I may be wrong about this.

1 Like

I did put some code in (int reley and pinmode&digitalwrite in setup)
But it didn’t work. when i switch the NodeMcu on, the relay immediately turns HIGH.
If i take the power from Nodemcu 3v, then i can turn the relay off from app.
Off course first i press ON (in the Blynk app) and nothing heppens, and then if I press OFF, then relay turns LOW.
But if I take some external 5v power for the relay, then it turns HIGH and i can’t turn it OFF from Blynk app.

#include <SPI.h>
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>

const int relejs1 = 12;

char auth[] = "xxxx";

char ssid[] = "xxx";
char pass[] = "xxx";

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
Adafruit_BMP085 bmp;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float c = bmp.readTemperature();
  float p = bmp.readPressure();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V5, h);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature
  Blynk.virtualWrite(V7, c); //bmp180
  Blynk.virtualWrite(V8, (bmp.readPressure() / 133.3F));
}

void setup()
{
  pinMode(relejs1, OUTPUT);
  digitalWrite(relejs1, LOW);
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  dht.begin();
  bmp.begin();
  
  timer.setInterval(1000L, sendSensor);
}

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

Try changing this to:

digitalWrite(relejs1, HIGH);

as you may be getting confused about whether your relay is an active HIGH or LOW device.

You could also try moving the pinmode so that its after the ```Blynk.begin`` command.

Ultimately, I think you’d be better using virtual pins rather digital.

Pete.

1 Like

Yup, thanks Pete!
I put HIGH and now works fine if power from NodeMcu.

{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(relejs1, OUTPUT);
  digitalWrite(relejs1, HIGH);
  dht.begin();
  bmp.begin();

But anyway stays HIGH with external 5V power (common GND) and i can’t change it with Blynk app.
Will try the virtual pins.

No, i give up! :smile:
Somehow i can’t get it to work with Blynk virtual pins.
Here’s my code:

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>

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

const int RELAY1 = 12; //D6

#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
Adafruit_BMP085 bmp;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float c = bmp.readTemperature();
  float p = bmp.readPressure();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Blynk.virtualWrite(V5, h);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature
  Blynk.virtualWrite(V7, c); //bmp180
  Blynk.virtualWrite(V8, (bmp.readPressure() / 133.3F));
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(RELAY1, OUTPUT);
  digitalWrite(RELAY1, HIGH);
  Blynk.syncVirtual(V4);
//?- Blink.virtualWrite(V4, HIGH);
  dht.begin();
  bmp.begin();
  
  timer.setInterval(1000L, sendSensor);
}

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

Maybe if you included a screenshot of your app in “stopped” mode, and a description of exactly what you’re trying to achieve, what is working (if anything) and what isn’t; then we might be able to help.

Pete.


relay

I’m trying to get the relay work together with BMP180 and DTH11 sensors.
The relay worked fine without these sensors and without extra code in sketch (just Blynk app settings and digital pins).
It was working fine also on digital pin, with extra coding in sketch, but only with power from NodeMcu (not with external 5V power).
Would like to get it work with virtual pins.

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>

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

const int RELAY1 = 12; //D6

#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
Adafruit_BMP085 bmp;

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V4); //make relay state of button upon connection to BLYNK

}

// When App button is pushed - switch the state
BLYNK_WRITE(V4) {
 int relayState = param.asInt();
  if (relayState == 1){
 digitalWrite(RELAY1, LOW);//turn on relay
 }
else if (relayState == 0){
 digitalWrite(RELAY1, HIGH);//turn off relay
  }
}

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float c = bmp.readTemperature();
  float p = bmp.readPressure();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Blynk.virtualWrite(V5, h);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature
  Blynk.virtualWrite(V7, c); //bmp180
  Blynk.virtualWrite(V8, (bmp.readPressure() / 133.3F));
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(RELAY1, OUTPUT);
  digitalWrite(RELAY1, HIGH); //make relay off at startup
  dht.begin();
  bmp.begin();
  
  timer.setInterval(1000L, sendSensor);
}

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

This assumes relay is active LOW, and the button widget outputs a 1 when pressed and a 0 when not-pressed.

1 Like

I put this code, but no changes :frowning:
I noticed that as long as the sketch is loading, the relay goes LOW, when starting - turns again HIGH and I can’t change it from Blynk app.
Then I commented out line 66 (// pinMode(RELAY1, OUTPUT):wink: in setup sector.
And after restart relay stays LOW. Of course I still cant control it.

I’d start by getting to the bottom of the relay control, and working-out if it’s active HIGH or LOW.

Remove all the code relating to the sensors and disconnect them, just keep the relay connected and the relay code in the sketch.

Also, keep an eye on the serial monitor to check that the NodeMCU isn’t restarting without you realising it.

The relay can take quite a bit of current to power it, so it’s not a good idea to connect it to the NodeMCU as you could end-up frying the onboard voltage regulator or starving the NodeMCU of power causing it to restart, or starving the relay of power so that it can’t operate even if it’s being activated correctly.

Pete.

1 Like