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?