logo elektroda
logo elektroda
X
logo elektroda

[Solved] ESP8266 NodeMcu v3 - High state on pin without reed switch connected

mak188 621 8
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 21096391
    mak188
    Level 8  
    Good morning.
    While programming a weather station on an ESP8266 NodeMcu v3, I encountered a problem. When nothing is connected on the pin there is a high state, despite changing the state to 0. Here is the program code:
    #include <Wire.h>
    #include <SPI.h>
    #include <Adafruit_Sensor.h>
    #include <Adafruit_BMP280.h>
    #include <DHT.h>
    #include <Rtc_Pcf8563.h>

    #define DHT_SENSOR_PIN D7 // The ESP8266 pin D7 connected to DHT22 sensor
    #define DHT_SENSOR_TYPE DHT22


    DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);

    #define BMP_SCK 13
    #define BMP_MISO 12
    #define BMP_MOSI 11
    #define BMP_CS 10
    Rtc_Pcf8563 rtc;
    Adafruit_BMP280 bme; // I2C
    //Adafruit_BMP280 bme(BMP_CS); // SPI hardware
    //Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
    int Fc_37 = D6;
    int greenLED = 226;
    int redLED = 2227;
    // you can adjust the threshold value
    int thresholdValue = 500;
    int sensor = A0; //pin analog A0 connected to the photoresistor leg
    int direction wind = 15; //pin D8
    int wind direction1 = 5; // Pin D5
    int stronawiatru = 0;
    int stronawiatru1 = 0;

    void setup() {
    Serial.begin(9600);
    Serial.println(F("BMP280 test"));
    dht_sensor.begin();

    if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1);
    }
    pinMode(directionwiatr, INPUT);
    pinMode(directionwiatr1, INPUT);
    pinMode(Fc_37, INPUT);
    }

    void loop() {
    //RTC
    Serial.print("Time:");
    Serial.print(rtc.formatTime());
    Serial.print("Date:");
    Serial.println(rtc.formatDate());
    //rtc
    //direction sensor
    stronawiatru=digitalRead(directionwind);
    Serial.println(stronawiatru);
    stronawiatru1=digitalRead(directionwiatr1);
    Serial.println(stronawiatru1);

    if(stronawiatru1 == HIGH && stronawiatru == HIGH){
    Serial.println("WIEJEEEE from the north!!!");
    }
    else if(stronawiatru1 == HIGH){
    Serial.println("WEEEEE from the west!!!")
    }
    else if(stronawiatru == HIGH){
    Serial.println("KNOWEEE northwest!");
    }
    else if (stronawiatru1 == LOW && stronawiatru == LOW){
    Serial.println("No wind@");
    }
    // direction sensor
    // photoresistor
    int war = analogRead(sensor); //read value from A1
    Serial.print(war); //display it on the monitor
    //when the value exceeds a certain fixed threshold, then the LED on pin 13 will light up

    Serial.println(""); // photoresistor
    int sensorValue = analogRead(Fc_37);//Fc_37
    Serial.print(sensorValue);
    if(sensorValue < thresholdValue){
    Serial.println(" - It's wet");
    }
    else {
    Serial.println(" - It's dry");
    } //Fc_37
    // read humidity
    float humi = dht_sensor.readHumidity();
    // read temperature in Celsius
    float temperature_C = dht_sensor.readTemperature();
    // read temperature in Fahrenheit
    float temperature_F = dht_sensor.readTemperature(true);

    Serial.print(Fc_37);
    // check whether the reading is successful or not
    if ( isnan(temperature_C) || isnan(temperature_F) || isnan(humi)) {
    Serial.println("Failed to read from DHT sensor!");
    } else {
    Serial.print("Humidity: ");
    Serial.print(humi);
    Serial.print("%");

    Serial.print(" | ");

    Serial.print("Temperature: ");
    Serial.print(temperature_C);
    Serial.print("°C ~ ");
    Serial.print(temperature_F);
    Serial.println("°F");
    }

    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" *C");

    Serial.print("Pressure = ");
    Serial.print(bme.readPressure());
    Serial.println(" Pa");

    Serial.print("Approx altitude = ");
    Serial.print(bme.readAltitude(1013.25)); // this should be adjusted to your local forcase
    Serial.println(" m");

    Serial.println();
    delay(2000);
    }
    It's about the reed switch on D5, On D8 it reacts as it is supposed to (when a magnet is approaching it shows a high state, when no magnet is present it shows a low state) When I tried to change to a different location it still shows a high state everywhere (D5). Even when I do not connect anything to the pin I am programming.
  • ADVERTISEMENT
  • #2 21096464
    stefan_marek
    Level 18  
    Did you make a mistake in writing pin D5?
    Is: int directionwind1 = 5; // Pin D5
    Should be: int directionwind1 = 14; // Pin D5
  • ADVERTISEMENT
  • #3 21096465
    mak188
    Level 8  
    I also checked this option, it continues to show a high state, with a meter I also checked if there was a physical short circuit of some kind maybe between the pins. But the voltage is 0V

    Added after 4 [minutes]:

    https://nodemcu.readthedocs.io/en/release/modules/gpio/ pins I looked at according to this documentation
  • ADVERTISEMENT
  • #4 21096472
    stefan_marek
    Level 18  
    And have you checked the reed switch - after all, if it has shorted contacts (magnet nearby) and is connected to D5 and ground, it must force a low state on D5.
  • #5 21096478
    mak188
    Level 8  
    I have checked the program code without the reed switch connected (open circuit) as well as with it connected (on open circuit and closed circuit-approximate magnet) in each case a state of 1 instead of 0 despite assigning a state of 0 to a variable, as in the program. It all works as far as the reed switch which is disconnected to D8 is concerned. The reed switch is also high all the time after being moved from D8 to D5. So something must be wrong with the program or the board.
  • Helpful post
    #6 21096536
    xury
    Automation specialist
    Pin D8 has a pull down to GND. D5 does not have this. Give a pull down resistor of 10k . And even better use reverse logic i.e. short circuit gpio to GND.
  • ADVERTISEMENT
  • #7 21096566
    mak188
    Level 8  
    How to do such reverse logic? I just have to instead of 3.3v --> Reed --> D5, make GND --> Reed --> D5? Do I have to do D5 as output in this case? And I understand that there is no e.g. pull down to GND, but why do I see in the serial monitor that the value is high? Even though the pin is physically empty, because I do not even connect the reed switch to it?
    Serial monitor display with sensor data. Breadboard with connected wires and an ESP8266 module.

    Under the hour, the first value shows the state of pin D8.
    The second value shows the state of pin D5 (which is currently empty).

    Added after 32 [minutes]:

    Adding a resistor did nothing

    Added after 9 [minutes]:

    will someone write me how the reverse logic looks like? or how to do it? I can't very much from the information I've found draw conclusions that will guide me to it.
  • Helpful post
    #8 21096785
    xury
    Automation specialist
    You have information on how to use each pin on the following page: https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/
    https://forum.arduino.cc/t/how-to-use-gpio2-14-12-of-esp8266-as-input/1181810/2

    So it's better to use the input pulled up to 3.3V, and let the reed switch short them to ground.
    I don't trust internal resistors and always use physical 10kOm to 3.3V.
    Also note that you are using D8 which is already physically pulled down to GND. If it is pulled up to plus during boot then the ESP8266 will not boot.
    You will not change the state on the input programmatically. You can only do this on pins declared as output. That is, you can, but its state read by digitalRead will be the voltage on it at the time of reading.

Topic summary

The discussion revolves around an issue with the ESP8266 NodeMcu v3 where a pin (D5) shows a high state when no device is connected, despite programming it to a low state. The user has verified the pin configuration and checked for physical short circuits but continues to experience the problem. Suggestions include using a pull-down resistor to ground, as D5 lacks an internal pull-down, and employing reverse logic by connecting the reed switch to ground instead of 3.3V. The importance of using external resistors for reliable readings is emphasized, along with the need to understand pin configurations to avoid boot issues with the ESP8266.
Summary generated by the language model.
ADVERTISEMENT