Pages: [1]   Go Down
Author Topic: Voltmeter in program not working correctly  (Read 763 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey there,

I've built a project and am just trying to work out the bugs...thought I had the code to measure voltage (that is also powering the Atmega328) correct on the breadboard, but realized it wasn't working correctly when I checked my voltage with a multimeter.
What is happening is that voltage reading is increasing (Serial.print reading), but voltage is actually decreasing (multimeter).

When I view the analogPin reading, I'm getting a Higher number when voltage is decreased, and a lower number when voltage is increased. 

I have 4 rechargable AA's connected.

I've attached a PDF of my setup. (I'm really new at this...so please let me know if anything is confusing)

Here is my code.
Code:
/*
 * Gardening Project v1.2
 * Combining the capacitive sensor, moisture sensor, pump and led indicator.
 * Reading how much water is in the container.
 * Reading how much moisture is in the ground.
 * Pumping water if moisture is dry.
 * LED
 * Red - low battery , blink
 
 */
// LED
#define LED 9
//  Reed switch monitoring
#define SWITCH 10  // Defines the Reed switch on digital pin 10
int reed_val = 0;  // used to store the input of the reed switch

//  Volt monitoring
  int batteryPin = 1;
  float vout = 0.0;
//  float vout = (Vin * R2) / (R1 + R2);
  int value = 0;
  float R1 = 995.0;    // !! resistance of R1 !!
  float R2 = 991.0;     // !! resistance of R2 !!
  float vin = 0.0;

// Moisture Sensor
  int moistureSensor = 5;  // Sensor pin 5, other pin connected to GRN through a 10k resistor
  int moisture_val;  // Storing the current value of the Sensor pin
  int waterpump = 7;  // Initializing pin 7 for waterpump

// Trying to avoid delays using Millis()
  long previousMillis = 0;  // will store the last time it was updated
  long interval = 10000;  // interval instead of delay time
//long leddelay = 10000;  // interval for led


 
void setup()  {
  Serial.begin(9600);
pinMode(SWITCH, INPUT);  // Defines the switch as an input
pinMode(LED,OUTPUT);  // LED as an output
pinMode(waterpump,OUTPUT);  // Setting waterpump to output
digitalWrite(waterpump,LOW);  // waterpump off
pinMode(batteryPin,INPUT);
}

void loop()  {
  analogReference(EXTERNAL);
    // Volt Monitoring
 // read the value on analog input
  value = analogRead(batteryPin);
  vout= (value * 3.33)/1011.0;  //voltage coming out of the voltage divider
//    vout = (Vin * R2) / (R1 + R2);
  vin = vout / (R2/(R1+R2));  //voltage to display
 

  // Debugging with LCD
  Serial.write(0x11);  // Clear the LCD
  Serial.write(0x16);  // Turn the display on, with cursor off and no blink
  Serial.print("V: ");  // Print out V:
  Serial.println(vin);  // This is where the number of volts are displayed
  Serial.write(0x89);  // Move cursor to line 0, position 9
  Serial.print("M: ");
  Serial.println(moisture_val);
  Serial.print("Bat: ");
  Serial.print(value);
  Serial.write(0x94);  // Move cursor to line 1, position 0

  delay(100);

  if (vin < 5.1)  {
   
    digitalWrite(waterpump,LOW);
    digitalWrite(LED,HIGH);  // LED turns on
    delay(200);
    digitalWrite(LED,LOW);
    delay(200);

 }

// Reed Switch monitoring
 
  if (reed_val==LOW)  {
    Serial.println("Fill Water");
    digitalWrite(waterpump,LOW);
    digitalWrite(LED,HIGH);
    delay(200);
    digitalWrite(LED,LOW);
    delay(200);
   
  }


  // Check whether input is HIGH (switch closed)
  if (moisture_val < 900 && reed_val==HIGH)  {
    Serial.println ("Turning on pump");   // Debugging or adding add to LCD
    digitalWrite(waterpump,HIGH);  // waterpump on
   delay(5000);
   moisture_val = analogRead(moistureSensor);  // read the Sensor value
   delay(500);
  }
   digitalWrite(waterpump,LOW);
   reed_val=digitalRead(SWITCH);  // Read the input value and store it
 
  }
 
 

Hopefully this is enough to go on, I've been racking my head trying to figure this out.

Thanks again for any help.

Cheers!

* watering1.2_working.pdf (15.49 KB - downloaded 23 times.)
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure about
Code:
pinMode(batteryPin, INPUT);

Isn't that configuring digital pin 1 as an input?
I guess that won't have any effect, and in any case the analog pin A1 should default to an input, but given the unexpected behaviour you might want to check that.
« Last Edit: December 13, 2011, 10:12:23 am by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50157
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Isn't that configuring digital pin 1 as an input?
Yes, it is, which will mess with Serial.

Quote
I guess that won't have any effect
Not on the analog pin.

Quote
and in any case the analog pin A1
A1 is not an analog pin. It is a digital pin.

Quote
thought I had the code to measure voltage (that is also powering the Atmega328) correct on the breadboard, but realized it wasn't working correctly when I checked my voltage with a multimeter.
The value returned by analogRead() is the measured voltage divided by the reference voltage, times 1023. In your case, your measured voltage is declining, meaning that the voltage before the voltage regulator is declining, too. The voltage regulator needs about 7 volts to output a steady 5V, on a 5V device.

You are multiplying the value read from the pin by 3.33 then dividing by 1011. Why? Is your Arduino not a 5V device? Or, do you have some other reference voltage being supplied? Why 1011? The correct value, regardless of voltage, is 1023.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PeterH and PaulS,
Thanks for your reply.

PeterH - I've taken out
Code:
pinMode(batteryPin, INPUT);
but still having the same problem.

PaulS - I'm really new so please disregard my ignorance, but from what I've read the Atmega328 has 6 analog pins (A0 - A5), shouldn't I call the analog pin A1 if I'm using Analog pin #1?

I understand what you're saying about the voltage needing to be a constant 7 volts in order to deliver 5v.  I was hoping that I could use only 4 AA's because it fits in my project box nicely.  A 9 volt battery doesn't have enough mA (roughly 500) which means I would have to change my 9v battery every 1 - 2 days.  Whereas 1 AA battery has 2300mA.
Can you suggest how I might be able to use 4 AA's for this project?

I was multiplying the value read by 3.33 because that was the voltage I was getting to the Aref pin when I put a multimeter to it.  Dividing by 1011 was just playing with the numbers to try and get an accurate read.  I figured because I was using a voltage divider intended for up to 10 volts, that this number could be adjusted.

Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 633
Posts: 34517
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I was hoping that I could use only 4 AA's because it fits in my project box nicely.
Sadly that will give you 4 X 1.2V = 4.8V with rechargeable batteries.

Quote
3.33 because that was the voltage I was getting to the Aref pin when I put a multimeter to it.
Something wrong here. This pin is normally connected to 5V so are you running the whole thing off 3V3 deliberately or have you simply not got enough voltage going in to the regulator for it to work correctly?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all for your help.

In the end it had to do with not having enough volts to regulate 5v properly.
As soon as I made it 6 AA re-chargables (7.2v) sensors are working properly.

Now I'm having trouble with the Time library...so I'm about to post in a different thread.

Thanks again!
Logged

Pages: [1]   Go Up
Jump to: