Which is pin 3?

I'm trying to use this code in my sketch, the problem is i'm noob about this. Which is pin 3 actually?

Is the code activated only by pin 3? Is pin 3 are actually pin 13 like in the code 13, HIGH? My project not using pin 13 but other digital pin (D2-D7).

Anyone can guide me on this?

Edit: arduino nano

#include <Battery.h>

Battery battery(3400, 4200, A0);
/**
 * 1 cell li-ion/li-poly battery wired to A0 before voltage booster, on demand sensing on pin 3, linear mapping function
 * https://github.com/rlogiacco/BatterySense#lesser-than-5v-with-voltage-booster
 **/
void setup() {
	Serial.begin(9600);
	while (!Serial);
	battery.begin(5000, 1.0);
}

void loop() {
	digitalWrite(13, HIGH);
	delay(500);
	digitalWrite(13, LOW);
	delay(500);

	Serial.print("Battery voltage is ");
	Serial.print(battery.voltage());
	Serial.print(" (");
	Serial.print(battery.level());
	Serial.println("%)");
}

Which Arduino board are you using ?

Arduino Nano

Hello
You shall used a pinMode to activate an output.

So this code not required pin 3 (13?) To activated it?

The library is here

Did you try it to do it?

Can you guide me? Still figuring out

Where do you get the idea that pin 3 is in any way involved with the code you posted? The code only talks of pin 13 which is the on board LED, and all it does is blink.

Its on the comment in the example sketch "on demand sensing on pin 3"

And have you wired up the hardware for the on demand sensing?

No because im not understand the code needs

The code you showed is not using on demand sensing so you do not need to bother with pin 3.
To use on demand you need to add this to your setup command.

void setup() {
  // specify an activationPin & activationMode for on-demand configurations
  batt.onDemand(3, HIGH);
  batt.begin(3300, 1.47, &sigmoidal);
}

And then wire pin 3 to the input of the on demand circuit.
This is pin 3 as in data 3 and should have its name next to it on the appropiate physical pin on the Arduino.

It is all there in the read me file of the GitHub link you provided.

Whay is actually digitalWrite(13)?

Do you mean "Where is the digitalWrite(3)"
It is in the Battery library. You can find an example of its use in the example OnDemandDoubleLiIon
battery.onDemand(3, HIGH);

It is defined in the class function

void Battery::onDemand(uint8_t activationPin, uint8_t activationMode) {
	if (activationPin < 0xFF) {
		this->activationPin = activationPin;
		this->activationMode = activationMode;
		pinMode(this->activationPin, OUTPUT);
		digitalWrite(activationPin, !activationMode);
	}
}

Note it doesn't have to be pin 3, it can be any other unused pin, that you tell the library to use via the battery.onDemand method call.

I was referring to this line

digitalWrite(13, HIGH);

digitalWrite(13, LOW);

do i need it in my sketch?

Only if you want the on board LED to blink. That is all it does.

Thanks for that :slight_smile: so just discard the lines and im good to go with other lines

Another confusing lines, (for me) and theres no explanation in the library,

From example sketch 1

battery.begin(5000, 1.0);

And what is the different here and up?

batt.begin(3300, 1.47, &myMapFunction);

i understand map function, but why is 3300, 1.47?

And where is to put low/max volt? In mapping?

The library requires at least 1 analog pin (we will
call this the `sense pin` ) and no less
than 2 pieces of info on your
battery: the voltage you will consider the
minimum acceptable level, below which your project/product becomes unreliable and
should be shut down, and the maximum
voltage you can expect when the
battery is fully charged.

an update :

Finally i got the code working in my sketch

But still figuring out how to use the myMapFunction, the idea is to make the battery icon as follows

100% = full
80% = ⅘
60% = ½
40% = ⅖
20% = low

Anyone?

All your questions are answered in that link you posted, you have the whole code in there so you can see how it works. The begin method takes three parameters, the reference voltage, the division ratio and the address of your map function.

void begin(uint16_t refVoltage, float dividerRatio, mapFn_t = 0);

It is up to you to make a function that returns these values for your specific circumstances.
We can’t tell you what your circumstances are.
look at the RGBBatteryIndicator.ino example, and adapt it to your changing icons instead of turning on one of three LEDs.

What you have to remember is that this library only measures a voltage, you have to give that measurement a meaning. Seeing as all the trouble that Apple takes on giving you a battery indication of charge and how rubbish the actual percentage is in estimating how long it will run for, what you can do with a simple voltage reading is not going to be very good is it?