Using G33 analogRead while connected to Wi-Fi



  • First of all, this is my first post so please let me know if there is problem


    So I thought I'd do an analog read with the G33 pin, but it worked fine at first, but when I added the code to do the wifi communication, the G33 analog read always returned 4098.

    I searched and found that using ADC2 with analogread during wifi communication always returns 4098, but the G33 pin is ADC1.

    I don't know what's wrong, please help!



  • Hello @dollychun

    using GPIO33 as analog input and WiFi works for me. M5Atom is connected to WiFi and I get values from 0 to 4095 from GPIO33. Care to share your code?

    Thanks
    Felix



  • @felmue Heres my code

    #include "M5Atom.h"
    #include "WiFi.h"
    
    int analogIn = 33;
    
    const char* ssid       = "PatoWiFi";
    const char* password   = "PatoWifi_Password";
    
    WiFiClient client;
    
    void setup() {
      // put your setup code here, to run once:
      M5.begin(true, false, true);
      Serial.begin(115200);
      pinMode(analogIn, INPUT);
      WiFi.begin(ssid, password);
      Serial.print("Connecting to WiFi");
      while(WiFi.status()!=WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.print("OK");
      Serial.print("\r\nWiFi connected\r\nIP address: ");
      Serial.println(WiFi.localIP());
      client.connect("www.someurl.com", 80);
      Serial.println("sending data to server");
      client.println("POST / HTTP/1.1");
      client.println("Host: www.someurl.com");
      client.println("User-Agent: ESP32/M5Atom");
      client.println("Accept */*");
      client.println();
      
      unsigned long timeout = millis();
      while (client.available() == 0) {
        if (millis() - timeout > 5000) {
          Serial.println(">>> Client Timeout !");
          client.stop();
        }
      }
    
      Serial.println("receiving from remote server");
      // not testing 'client.connected()' since we do not need to send data here
      while (client.available()) {
        char ch = static_cast<char>(client.read());
        Serial.print(ch);
      }
    
      // Close the connection
      Serial.println();
      Serial.println("closing connection");
      client.stop();
      
      WiFi.disconnect();
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      int val = analogRead(analogIn); // Always return 4095
      Serial.printf("AnalogRead = %d\n", val);
      delay(100);
    }
    


  • Hello @dollychun

    Hmm, I am running your code (with my WiFi credentials) on an M5Atom Matrix and reading GPIO33 is fine.

    I get 0 when GPIO33 is connected to GND and 4095 when connected to +3.3V and values in between when floating as expected.

    AnalogRead = 0
    AnalogRead = 544
    AnalogRead = 16
    AnalogRead = 4095
    

    Have you confirmed that reading GPIO33 works again if you comment out the WiFi code? Maybe adding that isn't the cause that made reading GPIO33 stop working?

    Thanks
    Felix



  • @felmue said in Using G33 analogRead while connected to Wi-Fi:

    Have you confirmed that reading GPIO33 works again if you comment out the WiFi code? Maybe adding that isn't the cause that made reading GPIO33 stop working?

    Yes. I have confirmed that it works when I remove the Wifi code...
    I will try this with another atom...