Hey Guys,
I am new to this board and the Arduino in general and I was trying to built a soil moisture logger powered by a 9V battery.
I wanted to log the battery voltage. My Idea was to use a voltage divider to measure the voltage of the battery. Because I do not want to drain the battery over the voltage divider I used an optocoupler turning on and of the voltage divider by digital PIN 13. The voltage is measured by the A0 pin.
The soil moisture sensor is powered by the 5V power supply from the arduino. The signal ist read by the A1 pin.
So the thing is that when I only measure the battery voltage it reads 8.3 Volts.
At the moment I am measuring the analog voltage output of the soil moisture sensore the measured battery voltage drops to 6.9 Volts.
Why is that?
This is the sketch:
int const PowerPin=13;
int const VoltagePin = A0;
int VoltagePinVal;
float Voltage;int const SoilMoist1Pin = A1;
int SoilMoist1Val;float SoilMoist1Voltage;
unsigned long prevmilis = 0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(PowerPin, OUTPUT);}
void loop() {
// put your main code here, to run repeatedly:
unsigned long curmilis = millis();if (curmilis - prevmilis >= 2000)
{
//Spannung messen
digitalWrite(PowerPin, HIGH);
delay(10);
VoltagePinVal = analogRead(VoltagePin);
delay(1);
digitalWrite(PowerPin, LOW);
Voltage = ((VoltagePinVal / 1024.0)5.02)-0.1;
Serial.println(Voltage);//Messen
SoilMoist1Val = analogRead(SoilMoist1Pin);
delay(100);
SoilMoist1Voltage = (SoilMoist1Val / 1024.0)*5.0;
Serial.print(" SoilMoist1: ");
Serial.println(SoilMoist1Voltage);prevmilis = curmilis;
}}