"Weird" values


I've been trying on a project using Arduino, but since I started I've never been able to read any sort of correct values (writing works just fine), I've tested this on both Nano and UNO. Since I haven't been able to achieve my actual objective I stepped down and tried reading values from an LDR (using a voltage divider of course) and still the values don't look any better. With analogRead I've been having a consistent response with variations from 0 to 1023 and if I try to measure the same output with a digitalRead I get variations between 0 and 1, in both situations I don't have to do anyhting for it to happen and when I do mess with the LDR nothing happens, at all, zero relevance.

This is the code I'm currently running:

int sensor = A2; int sensorValue = 0;

void setup() { // put your setup code here, to run once: pinMode(sensor,INPUT); Serial.begin(9600);


void loop() { // put your main code here, to run repeatedly: sensorValue = analogRead(sensor);

Serial.println(sensorValue); delay(100); }

I kept it as simple as I could imagine, wanting to eliminate extra code that could have been causing the "error". If anyone one has an idea of what might be the problem I would be very thakful!

Post a hand drawn circuit diagram (not Fritzing), with all the parts and pin numbers labeled.

Did you forget to connect all the grounds?

Except the missing code tags I see nothing weird in the code. So how is it wired? Please post a schematic, no Fritzing breadboard mess.

Do you get more-consistent readings in the dark (i.e. with black tape over the LDR)?

Do you have fluorescent lights? They strobe at line frequency (actually once per half-cycle) which is too fast for the eye to see but perhaps not so fast that the LDR can't detect it.

The code looks like it should work but a couple tips:

Don't use an int where a byte will do, like for pin numbers. Make that an Arduino habit.

Set Serial at a high baud rate, at least 115200, it will clear the serial output buffer quickest.

Try jumpering 5V to pin A0 to make sure it reads 1023. If the code is good, troubleshoot the hardware.

Are you sure that you connected Gnd to all sensors?

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please tell us your electronics, programming, Arduino, hardware experience?

Thanks.. Tom... :)