フィルターのクリア

ESP32 analogRead always 4095

32 ビュー (過去 30 日間)
Aqsal Renaldi
Aqsal Renaldi 2023 年 9 月 16 日
回答済み: Rene Chan 2023 年 9 月 18 日
when i try to use analog read to read a sensor and connect it to thingspeak, the value is always 4095. but when i only use the analog read without the thingspeak program, it works normal. did anyone know whats wrong?
here is my program :
#include "WiFi.h"
#include "ThingSpeak.h"
int ldr = 14;
int nilai;
#define CHANNEL_ID "my number"
#define CHANNEL_API_KEY "my key"
WiFiClient client;
#define WIFI_NETWORK "name"
#define WIFI_PASSWORD "pass"
#define WIFI_TIMEOUT_MS 2000
void connectToWiFi(){
Serial.print("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);
unsigned long startAttemptTime = millis();
while(WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < WIFI_TIMEOUT_MS){
Serial.print(".");
delay(100);
}
if(WiFi.status() != WL_CONNECTED){
Serial.print("Failed");
}else{
Serial.print("Connected");
Serial.println(WiFi.localIP());
}
}
void setup() {
pinMode(ldr, INPUT);
Serial.begin(9600);
connectToWiFi();
ThingSpeak.begin(client);
}
void loop() {
nilai = analogRead(ldr);
Serial.print("Nilai LDR :");
Serial.println(nilai);
ThingSpeak.writeField(CHANNEL_ID, 1, nilai, CHANNEL_API_KEY);
delay(15000);
}
  1 件のコメント
Christopher Stapels
Christopher Stapels 2023 年 9 月 18 日
These things should be totally unrelated.
My first guess is that is is power related. Running the wifi consumes a decent amount of current. How are you powering the device? Also, check what happens if you use a differnt pin. I like to use pin 33 for analong input, for no particular reason.
Perhaps also add a short delay after the analogRead to make sure the number has time to settle.
I dont think the issues is related to the library at all, but you could try this just incase. It may help you find the real issue. You can try subtracting each part incrementally. Connect to wifi but dont write to thingspeak, then just connect to wifi but dont create the thingspeak client (but keep the library). Then try it without the library if you havent found the issue yet.

サインインしてコメントする。

回答 (1 件)

Rene Chan
Rene Chan 2023 年 9 月 18 日
According to https://docs.espressif.com/projects/esp-idf/en/v4.0.3/api-reference/peripherals/adc.html,
"ADC2 is used by the Wi-Fi driver. Therefore the application can only use ADC2 when the Wi-Fi driver has not started"
Pin 14 is on ADC2. Try pins that are on ADC1 channel (GPIOs 32 - 39)

コミュニティ

その他の回答  ThingSpeak コミュニティ

カテゴリ

Help Center および File ExchangeRead Data from Channel についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by