ESP32 analogRead always 4095
23 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Christopher Stapels
le 18 Sep 2023
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.
Réponses (1)
Rene Chan
le 18 Sep 2023
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)
0 commentaires
Communautés
Plus de réponses dans ThingSpeak Community
Voir également
Catégories
En savoir plus sur Write Data to Channel dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!