Analogread problem

Hello everybody this is a first time for my post.

OK! about my problem … I use the Arduino ATMega168-20PU IC

I have to wire the potentiometer 50K [volume type] with Analog pin 2 and writing program for read the value from Analog pin 2 and show the value on LCD Display.

The problem is the value from reading by use analogRead command is zero [0.00] everytime.

I have to check my wiring is correct and device is okay it’s not broken.

and I have to use another Analog Pin already and the value is zero again.

I want to know that how can I fix it to read value from AnalogPin correct?

Thank you so much from any help and every answer.

Wit

PS. Sorry for my English and I hope you are understand my question. Thank you so much!

How have you wired the potentiometer to the Arduino? The outer pins should be 5V and gnd, the one in the middle should go to your analog input.

Post your code and a picture of your circuit. It's hard to tell what the problem is without knowing what you have.

To help you out, here a simple code. As long you wire properly.

const int anapin = A1;

int my_reading;

void setup()
{
    analogReference(DEFAULT);
    Serial.begin(9600);
}

void loop ()
{
     my_reading = analogRead(anapin);
     Serial.println(my_reading, DEC);
     delay(1000);
}

It will place a value for 0 to 1023, that depend of the voltage input of your analog pin. You can measure the AVref pin for a voltage. Do this formula : AVref / 1023 . Take the result X the ADC value = the voltage at the analog pin.

OK This is my code.

#include <LiquidCrystal.h>

LiquidCrystal lcd (12,11,5,4,3,2);
int potPin = 2;
int ledPin = 10;
float val ;

void setup ()
{

pinMode(ledPin,OUTPUT);
lcd.begin(16, 2);
lcd.clear();

}

void loop ()

{
val = analogRead(potPin);
lcd.print(val);
delay(2000);
analogWrite(ledPin,val);
lcd.clear();
delay(2000);

}

and This is my wiring diagram [ Attachment ]

I want to writing in ledPin that connect Digital Pin 10 and want to show the value from Analogread pot Pin 2

the result is the LED not have any shine and the value from Analog pin 2 = 0.00

and I have to connect the potentiometer to another pin for Analog [pin 0 , 1 , 2 , 3 , 4 , 5]

the result it’s same is equal 0.00 and LED no shine.

Thank you for every answer.

02-27-2012 09-49-14.jpg

Techone:
To help you out, here a simple code. As long you wire properly.

const int anapin = A1;

int my_reading;

void setup()
{
    analogReference(DEFAULT);
    Serial.begin(9600);
}

void loop ()
{
    my_reading = analogRead(anapin);
    Serial.println(my_reading, DEC);
    delay(1000);
}




It will place a value for 0 to 1023, that depend of the voltage input of your analog pin. You can measure the AVref pin for a voltage. Do this formula : AVref / 1023 . Take the result X the ADC value = the voltage at the analog pin.

...I have to try your code [like you write above] and Serial have to show zero [Wiring potentiometer in A1 pin]....the value to show it's zero only [I have to rotate potentiometer to 5V but it's show zero.]

#include <LiquidCrystal.h>

LiquidCrystal lcd (12,11,5,4,3,2);
int potPin = 2;
int ledPin = 10;
int val ;

void setup ()
{

pinMode(ledPin,OUTPUT);
lcd.begin(16, 2);
lcd.clear();

}

void loop ()

{
val = analogRead(potPin);
lcd.print(val);
analogWrite(ledPin,val / 4); //scale the val from 0-1023 to 0-255
delay(2000);
lcd.clear();
delay(2000);
}

Try this version without the delay():

#include <LiquidCrystal.h>

LiquidCrystal lcd (12,11,5,4,3,2);
int potPin = 2;
int ledPin = 10;
int val ;
unsigned long lastValTime;  //will be used with millis()

void setup ()
{
  pinMode(ledPin,OUTPUT); 
  lcd.begin(16, 2);
  lcd.clear();
}

void loop ()

{
  if (millis() - lastValTime >=2000){  //if it has been 2000ms since you last read the sensor...
    lastValTime = millis();            //remember the current time

    lcd.clear();
    val = analogRead(potPin);
    lcd.print(val);
    analogWrite(ledPin,val / 4); //scale the val from 0-1023 to 0-255
  }   
}

Or this version without any delay… It will update the lcd with a change of val.

#include <LiquidCrystal.h>

LiquidCrystal lcd (12,11,5,4,3,2);
int potPin = 2;
int ledPin = 10;
int val ;
int oldVal;  //the old val;

void setup ()
{
  pinMode(ledPin,OUTPUT); 
  lcd.begin(16, 2);
  lcd.clear();
}

void loop ()

{
  val = analogRead(potPin);
  if (val != oldVal){     //if the value of val has changed, then update the lcd and LED
    oldVal = val
      lcd.clear();
    lcd.print(val);
    analogWrite(ledPin,val / 4); //scale the val from 0-1023 to 0-255
  }   
}

Thank you for every answer

I have to try the code from Mr. John_S already but it's not work anything it's show the value = 0 ;

The first code program from Mr.John it's show the val = 0 and it's not change after that I have to edit this code without the val/4 but the result is same.

The second code program LCD not show any value and I have to edit by not write if condition but it's show zero all time.

I don't sure that this problem have cause from Hardware it's right that? I think.... Not IC or board is broken.

PS. I use ATMega168-20PU but In digital pin it's work normally if I have command to control the digitalPin example...

analogWrite(digitalPin 10 , 100); it's okay.
digitalWrite(10, HIGH); it's work

I can use every command but it's not analogRead command in analogPin like this problem

analogRead(analogPin 2) ;

it's not work I have to use Potentiometer 50K to connect or 5V DC to connect directly it's always show 0 and not change.

Thank you!

Do you have a multimeter to check the pot with?

Can we see a photograph of your circuit?

So you did try my code ?

My code is just a simple example to read a value of an analog pin and show it using Serial.print( pot_value, DEC);

For an LCD, use lcd.print(my_value, DEC); to display an integer.

As for not show any value... like 0 ... well my guess it is simply a bad wiring somewhere or something is wrong, or analog pin is connected to ground. If the analog pin was open, it will show a random value.

That is my guess...

I am so sorry to reply.

But I have to fix this problem already on Feb 26 , Thank you so much for every answer.

[Cause of this problem is IC ATMega168 AnalogPin are broken.]

Thank you!

Cause of this problem is IC ATMega168 AnalogPin are broken.

I see… ( since others peoples may read this tread ), to test an analog pin, you can test it without anything connected to them. Just a plain Arduino board connect to your computer. THAT IT… If the analog pins are working, my example code will show a value to the serial monitor.

Here a code to test ALL the analog pins. The hardware is just a any plain Arduino board. The analog pins are open and they will read a value between 0 to 1023 and the value will change, that is normal. If you have those reading, than your analog pins are OK.

Here the test code :

const byte anapin[6] = {0,1,2,3,4,5};

int my_value[6];

void setup()
{
  //analogReference(EXTERNAL);  <-- AVref is connected
  // analogReference(INTERNAL); <--AVref not connected
  Serial.begin(9600);
}

void loop()
{
  for (int i=0;i<6;i++)
  {
    my_value[i] = analogRead(anapin[i]);
      
    Serial.print("Analog pin ");
    Serial.print(i, DEC);
    Serial.print(" value : ");
    Serial.println(my_value[i], DEC);
    delay(1000);
  }  
}

Thank you so much Mr.Techone

Your welcome. I hope it help. If your analog pins are not working, well it time to buy a new Arduino chip and replace the faulty one... ( for DIP chip )... IF possible... SMT ... well you are "out-of-luck", unless you know how to program a SMT ATMEGA chip, I don't :~