I was trying to use a Wemos D1 mini to read the value of a LDR using an analog input, and then sending a request to a server. The module managed to connect to the WiFi, however the module could not read the LDR. I tried replacing the LDR with a potentiometer and it still cannot seem to read the analog pin - instead the value it returns seems to be either a 1, 2, 3 or 8. Even after removing the wire, in which there should not be any voltage, it keeps on printing out those values. Before that, (an hour ago with the same code) it was returning 13. Why would this be happening, and is there anything I can do to fix it?
Welcom to the forum.
Sorry but this comment is just wrong. With nothing connected to an analogue pin is is said to float. This means it picks up interference and can return any value.
To measure an LDR you need a resistor as well to act as a potential divider.
From your description you don't seem to have one.
I did use a 10k ohm resistor to make a potential divider. Sorry for not mentioning it before. I used this schematic:
Is the A0 pin not connecting to the wire for some reason?
That is possible, but that circuit should work.
Can you post your code please?
The "native" 8266 adc can acept an input of 0-1V
The development boards - wemos etc -
add a potential divider to match the input level to the supply -
So you can safely connect the 3v3 pin to the A0 input, and you should then read 1023
The potential divider also offers a good level of protection to the adc input. So its unlikely its broken.
Here is the code I was using:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#define LDR_PIN A0
const char* ssid = "SSID";
const char* password = "PWD";
int sensorValue;
void setup() {
Serial.begin(9600);
WiFi.hostname("Sensor");
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.print(WiFi.localIP());
pinMode(LDR_PIN, INPUT);
}
void loop() {
sensorValue = analogRead(LDR_PIN);
Serial.println(sensorValue);
delay(1000);
// TODO: Send data to webserver
}
I don't think that the WiFi code has any effect because even when I removed the Wifi code, the expected values were still not given.
Also, connecting it to the 3v3 had no effect... The values are still floating. I have verified that the 3v3 is working since I can light a bulb with it. Same thing with the LDR - I have used it to dim an LED, so the LDR is also functional.
Those values are normal (even 8) when nothing is connected to A0. If you're sure that you use the right pin, then check if all the resistors are actually there on the back of the WeMos.
Leo..
So why did you include it in the post? The code to my untutored eye looks OK.
I believe from your post you have tried connecting A0 directly to 3V3 and still got no valid result.
When you have eliminated the impossible, whatever remains, however improbable, must be the truth.
I guess the Wemos is defective.
Try another
Ok. Thanks for your help.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.