Arduino Nano with 9V Battery

Hey guys, I am working on a project where I’m using an Arduino Nano with an external power supply of a 9V Battery to read the moisture levels in soil. When the soil is above a certain humidity level a green light is triggered and when below this level, a red light. However, when I connect my Nano using the 9V battery Only the green light turns on regardless of the humidity level of the soil. Whereas when the nano is connected to the USB port on my computer, the code runs perfectly. Any suggestions?

Post code.
Post a schematic.

Sorry I dont have the software to make a proper schematic. I drew one here.

Schematic1.pdf (343 KB)

When powered from the VIN pin the voltage on the 5V pin and the analog reference voltage is 5 volts, when powered by USB it is only about 4.7 volts. Could that make a difference? Post a link to your sensor.

here is my code:

int redLed = 5; //The red led pin
int greenLed = 9; //The green led pin
int moisture; //The moisture value

void setup() {
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);

}

void loop() {
moisture = analogRead(A4); //Read the moisture from sensor

moisture = map(moisture, 0, 1023, 100, 1); //Map the moisture values to 1-100.

if (moisture < 50) { //If moisture is low

digitalWrite(redLed, HIGH); //Turn on the led light

digitalWrite(greenLed, LOW );

}

if (moisture > 50) {

digitalWrite(greenLed, HIGH);

digitalWrite(redLed, LOW);

}

}

I added a serial print to see the value of “moisture”, load this program and enable serial monitor.

uint32_t tStart, tEnd = 2000;

int redLed = 5;     //The red led pin
int greenLed = 9;  //The green led pin
int moisture;       //The moisture value

void setup() {
  Serial.begin(9600);
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);

}

void loop() {
  moisture = analogRead(A4);             //Read the moisture from sensor

  moisture = map(moisture, 0, 1023, 100, 1); //Map the moisture values to 1-100.

  if (moisture < 50) {                   //If moisture is low

    digitalWrite(redLed, HIGH);          //Turn on the led light

    digitalWrite(greenLed, LOW );

  }

  if (moisture > 50) {

    digitalWrite(greenLed, HIGH);

    digitalWrite(redLed, LOW);

  }
  if(millis() - tStart > tEnd)
  {
    Serial.println(moisture);
    tStart += tEnd;
  }  
 
}

I appreciate you doing this and updated my code. However, the main issue is still when I switch from using my laptop as a power source to using a 9V battery. When using the 9V battery, the entire code doesn't work and just lights up the green LED. Suggestions?

My suggestion is don’t use the 9V battery.

What happens if you change your set point to 40 or 60?

I have to use the 9V battery for an external power supply because I can't keep it plugged up to my laptop for this project. As for changing the PWM signal, I am having the same problem when I tried this. The funny part is that the system works when the arduino is connected to my computer but not on an external power supply. I've never seen it haha.

jgimler:
when the nano is connected to the USB port on my computer, the code runs perfectly..........
I have to use the 9V battery

If that is true it is probably because you are too lazy to use anything else, and therefore deserve all the grief you are getting. There are other ways of powering an Arduino with a battery, and about the worst possible choice is a 9v. Your nano and peripherals don't require much power, but a 9v battery delivers virtually none.

Read reply #7 again - twice. You have already proven your code is kosher, and no amount of fartarsing around with set points is going to solve the problem.

There is plenty of stuff on this forum about battery power. It usually starts with using a decent regulator.

jgimler:
I have to use the 9V battery for an external power supply

No you don't! :roll_eyes:

That's a 12 Volt battery, which is even worse!

Given his simple schematic, which for some insane reason he has attached as a useless PDF, it should work just fine with a 4.5 V battery - three alkaline "AA" cells - connected of course to the "5V" pin.

Fixed (sort of):
Selection_034.png

Note however this current reference to a proper soil moisture sensor. :roll_eyes:

Paul__B:
That's a 12 Volt battery, which is even worse!

Given his simple schematic, which for some insane reason he has attached as a useless PDF, it should work just fine with a 4.5 V battery - three alkaline "AA" cells - connected of course to the "5V" pin.

Fixed (sort of):
Selection_034.png

Note however this current reference to a proper soil moisture sensor. :roll_eyes:

Heh, yeah I can't count. But they make the same kind of thing with six cells too. I've never seen one with 5 cells though, which would be ideal to use with Arduino's on board linear regulators. However I completely agree that 3 aa or even aaa cells into the 5V and ground pins would be the way to go. I do this with a lot of my own projects.

Lately however, I have been using 3.3 volt boards, and two cells (3 nominal volts) is what I use with them, unless I want the device to also recharge the battery. Then I use a LiPo. One day I'll learn to make a NiMH recharging circuit, cause those are also attractive as a rechargeable battery (all of the LED lawn ornaments my wife loves have NiMH batteries in them.)

To the OP: if you ever get the chance to dissect a 9 volt battery you will see that it's made of six coin cells in series. A coin cell doesn't last very long, and cells in series won't last any longer than a single cell, so that is why a 9 volt battery is not a good choice. They are best for devices that operate on extremely low current or only are on intermittently. Or if you have a bunch of them and can change the battery often.

ChrisTenone:
To the OP: if you ever get the chance to dissect a 9 volt battery you will see that it's made of six coin cells in series.

Or if alkaline (and that is the only sort of 9 V battery you should use for anything), it is constructed from six cylindrical "AAAA" cells. :grinning:

Assuming you have a good battery, and with nothing else connected - if the Nano's power LED doesn't light with "VIN" applied then
A) the regulator got smoked or
B) your wiring isn't right (wrong polarity or bad connection)

People are kind of jerks here, eh?

To the OP, runaway_pancake may be on the right track. If possible, try connecting a different power source - like 4x 1.5v batteries - and see if that works.

Other thoughts: Are you sure the sensor can handle 9v? Double check the specifications. Does it have current limiting? You may need a resistor on the + or - side to prevent current overdraw.

Stupid question, but have you confirmed your 9v battery is fresh? Tested it with a multimeter?

How much current does that sensor draw? Your USB connector will have a better regulated supply than the 9v battery. Your LEDs plus the sensor may drop the voltage under load enough to cause problems. Stick some probes on it when active and see how much voltage it's putting out when everything is on!

If you had bothered to look at OP's diagram, clearly reproduced here, you would note that the sensor is wired to 5v. Indeed, the wiring looks 100% kosher, and exactly as anybody would expect. There is no need to suspect the regulator, or any other hardware. Just the 9v battery, the "freshness" of which disappears with depressing speed, but simply replacing it with 4xAA may well be worse.

Ibanix:
People are kind of jerks here, eh?

Indeed they often are, but if they are "newbies" and have only just started posting here, we generally tolerate them. :grinning: