Go Down

Topic: Arduino MEGA analog problem (Read 544 times) previous topic - next topic

caitlyn

hey, guys
I'm a noob...
so my problem is that I designed the arduino as Voltmeter by using voltage divider.
Firstly, it was ok to measure the voltage... thereafter, i wanted to try it again,
and i discovered that the value was not accurate and 0 ( such as V= 11.xxV) when even i didn't plug anything on it....
its supposed to be 0 if i haven't plug everything on it... i m wondering if the arduino is kind of broken of fried out......
plz help me...

caitlyn

here is my code ....


int analogInput = 2;
float vout = 0.0;
float vin = 0.0;
float R1 = 50000.0;   
float R2 = 4400.0;     


int value = 0;

void setup(){
  Serial.begin(9600);

  pinMode(analogInput, INPUT);



}

void loop(){
  value = analogRead(analogInput);

  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2/(R1+R2)); 

  Serial.print("Vin=");
  Serial.print(vout);



  delay(1000);
}


spirilis

I think analog ports start with A, they're just a macro that boils down to a number, so try int analogInput = A2;  (up top)

spirilis

also your "Serial.print(vout);" you might want to change to "Serial.println(vout);" just so it prints a newline character to the serial monitor after the number, i.e. then each reading will show up on its own line.

James C4S


Firstly, it was ok to measure the voltage...

Okay...


thereafter, i wanted to try it again,

So what changed?

Use another analog input.  If it behaves the same, you probably have something in your circuit wrong.



I think analog ports start with A, they're just a macro that boils down to a number, so try int analogInput = A2;  (up top)

Calling analogRead() with A2 or 2 results in the same pin being used. 
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Nick Gammon


the value was not accurate and 0 ( such as V= 11.xxV) when even i didn't plug anything on it....
its supposed to be 0 if i haven't plug everything on it...


No it isn't. With nothing plugged in it reads random noise.

Go Up