Arduino weird behavior

Hi, im playing with my arduino nano v3.0

This is the code I am using:

#include "LowPower.h"
#define LED_pin 9

// Track LED state
bool LED_state = false;

// Set frequency and duty cycle of LED
float blinkrate = 200; // In Hz
int dutyCycle = 80; // in percent (%)

// Since time slices are much smaller, use microseconds
int granularity = 100;
float delay_between_check = 1000000.0 / blinkrate / granularity; // 1 sec/rate, divided into 100 segments
int cyclePosition = 0; // out of 100

// Track blinking cycle times of LED
long lastCheckedTime;

void setup() {
  pinMode(LED_pin, OUTPUT);
  lastCheckedTime = micros();

}

void onda() {
    long now = micros();
    long tiempo = millis();

    if (now - lastCheckedTime > delay_between_check) {
      // Only turn LED on if current cycle position is less than the duty cycle
      if (cyclePosition < dutyCycle) {
        digitalWrite(LED_pin, HIGH);
      } else {
        digitalWrite(LED_pin, LOW);
      }A

      // Increment cycle position, zero it out if it's too big
      cyclePosition++;
      cyclePosition %= granularity;
      lastCheckedTime = now;
     
    }
    if (millis() >= tiempo+10000) {
     LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
     delay(1000);
    }
}

void generar() {
  tone(9,random(40,1000));
  delay(random(100,400));
  noTone(9);  
}
void loop() {
generar();
generar();
generar();
generar();
generar();
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  delay(1000);
}

The code creates 5 pulses in pin 9, on that pin i have connected 3 speakers in series, then after that, the arduino will sleep until i need them to start over.

The three speakers are conected to 2 18650 lithium battery. Also, arduino will be powered by them using Vin and GND pins.

The problem is:

If I measure current in Vin pin with batteries, im getting 60mA and 40mA in sleep (which i find very high... it should be 15 or 19mA asleep)

While conected to the batteries, i connect also via usb to load the sketch, and when unpluging usb from PC the current drops to 25mA asleep and 9mA sleep (the sketch loads perfect)

If instead of PC i use a powerbank (keeping the 18650 on Vin) when unpluging, the current never drops, its the same as if the powerbank was never plugged.

What im doing wrong? first, the normal current is very high, but, i find weird that plugin batteries alone gives me a higher current that if with batteries connected to Vin, I plug to PC and then, unplug.

Lowering current is vital to my project, and a diference from 40mA to 9mA in sleep mode its a very big diference.

Regards, and thank you.

updated:

Then, with that like the picture, current in 0R1 resistor is 60mA and 40mA in sleep.

If, in that state, I plug arduino to PC, and after 3-4 secs I unplug usb, current in 0R1 drops to 25mA and 9mA in sleep.

If, instead of PC i connect arduino to a powerbank, nothing changes after unplug, 60mA and 40mA in 0R1.

So, i dont know whats happening, which is the difference between pc and powerbank? the data usb pins?

Sorry, but your description makes no sense at all. Please draw a block diagram of what you have and how the parts are connected together. Take a picture or scan the diagram and post it, please.
Paul

And, show us a good schematic of your circuit.

I have edited my post to add an schematics.

Something is wrong, but the circuit diagram doesn't give a hint.

To isolate the problem, remove the speakers and transistor from the circuit and measure the current again. If it is more than about 20 mA, the Nano board is defective.

In any case, the Nano is a very poor choice for sleep operation. The Pro Mini, with the power LED and voltage regulator removed, would be vastly better.

I have removed all, now, I only have the arduino with the battery in Vin and GND, and the current in 0R1 is: 60mA and when going to sleep 41mA

If now i plug the usb to my PC: and then, unplug it, the current drop like before, 25mA and 9mA when sleeping.

I know that its very high, may i say that its not an official nano (a CH340 model)

maybe its that the problem, with an official nano current will be 20mA or lower.

Also, I know that pro mini is better, im planning on buying it, but now i wanted to test the circuit, the sketch etc before buying, and in that process i found that problem unpluging from PC xD

thanks

It probably even has a counterfeit or reject ATMega328.

Hi,

The Nano has a linear voltage regulator to regulate your Vin to 5V.
Linear regulators have a running or quiescent current that they need to operate.

The on board power LED is probably also drawing a significant amount of that Vin current, it is drawing current from the 5V supply so that currentt will show when you power directly to 5V pin or USB.

Tom... :smiley: :+1: :coffee: :australia:

I have changed the nano board with another (same model, also ch340) and i got the same readings, so... if something is wrong, is wrong from factory xD

I have removed the power led, but cant remove the regulator, because i need to feed Vin with 2 18650 battery.

But as far as i know from datasheet, android nano, current should be 20mA or im wrong?

or that current is from usb?

Also, on a Nano, the USB to serial converter chip (CH340, FT232, etc.) will pull current, too.

but if i remove the chip... i wont be able to load any sketch

is this correct?

thanks

You won't be able to upload via the serial port.

You could program your Nano through the ICSP port with a programmer or another Arduino running the ArduinoISP sketch, though.

Or switch to a Pro Mini that already has most of the power using peripherals removed.

I know....

But shipping to Spain will crush me xd

I was searching a good price to ship to Spain but.. no luck.

Thanks

Then you picked the wrong board for sleep mode.

It has a USB interface chip which is always powered - does not sleep.

A "3.3 V" Pro Mini with regulator and pilot light removed will operate directly from a single 18650.

Does Aliexpress not ship to Spain?

You may have to wait a while however ...

Hi

Sleep or not sleep is not the problem but the current.

I mean, arduino nano should have a 20mA in awaked state, then, with regulator, usb chip, power led and sleep mode, drop a lot.

But, my two units of a nano clon (bought in aliexpress) starts in 60mA, then, without power led and sleep mode, i got 40mA, and using a trick (powering with Vin, then plug and unplug from pc) awaked state gives 25mA (not bad) and in sleep mode 9mA.

My question is, why I start in 60mA instead of 20mA?

And, yes, aliexpress ships to spain, in 15 days more or less i will receive 4 pro mini :slight_smile: but, they are clones.. as my two nano boards... My fear is that... what will be the current? the same as the datasheet, or something bigger?

thanks.

PS: I have not picked the wrong board, arduino nano is the board i had, and for testing circuit, and code should to the job, then when all is tested, i wanted to mount all of it with a pro mini.

Also, I tried the atmega328 itself (from an official UNO), with a 4MHz quartz, it works! 9mA awaked, and... 0,00mA in sleep mode, my multimeter cant measure it xD so... im happy with it