0

I was wondering if anyone would be willing to teach me how to logically choose/calculate which battery I should/can use to power my Arduino nano project (so I don't have to keep it connected to my computer to use it)? I am interested in learning, not just being given an answer because my goal is to be able to figure this out on my own for my next project. (Also, I got this far by myself because of a lot of trial and error in addition to reading as much as possible, but I don't want to buy an assortment of batteries for my trial and process of learning.) Thank you kindly.

If you are willing to teach, I have included all of the details about my project in the code below or at least I think I did.

Before I get to my code, one problem (I think) is that the two digit seven segment LED display that I bought for my project does not have a data sheet. However, even if it did, I am not sure that I would understand how to use it. I looked up other similar LED displays and if I understand what I am looking at, they seem to use between 2-3 V and 20-30 mA.

I thought that because I saw 2-3 V on the similar looking LED displays, I would be able to use a 3V battery to power my project. However, when I tried the following two batteries to power my project they did not work:

  1. 3V CR2032 lithium cell battery (for LilyPad) - this was enough power to dimly turn the power light on the nano, but not enough to turn the lights on for my project.

  2. E-Textiles Battery 3.7 V - 110mAh (2C Discharge) - this was also enough to dimly turn the power light on the nano, but not enough to turn the lights on for my project.

Because those did not work and it's all I had left, I tried a 9V battery:

  1. 9V battery - this powers the project just fine, but I was hoping to calculate the actual voltage and/or current because I was hoping to use one of the tiny lithium ion batteries from SparkFun.com (or elsewhere) to power my project if this is possible.

Here is my code (Sorry that the code is a bit long. I couldn't figure out how the SevSeg library worked, so I just wrote my own code):

/************************************************************************************************************************
Rebecca Wilson
12/17/18

This program is for using an arduino nano to run a 2 digit 7 segment LED display. I made it as a life counter for 
MTG, so it displays "20" to start and there are two buttons that can be used to change the life total by +/- 1.   

These are the pieces I used:

1x Nano 3.0 Controller compaitible with arduino nano CH340 USB driver (taydaelectronics.com; SKU A-2864)
    *You must select "ATmega328P (Old Bootloader)" for the processor (otherwise it can't upload the file to the nano).
    *This also works with an arduino UNO, but I prefer to change the negativeButtonPin to 13 for the arduino Uno (I don't 
     think pin 13 works on my nano because it is connected to the practice LED that comes attached to the board).

1x LED Display 7 segment 2 digit 0.28 Inch common anode HI RED (taydaelectronics.com; SKU A-1701)

2x Tact switch 6x6mm 5mm through hole SPST-NO (taydaelectronics.com; SKU A-5127) 

2x 330 Ohm 1/4W 5% carbon film resistor (taydaelectronics.com; SKU A-2067)

breadboard and jumper wires

This is what I understand about the LED display:
  LOW means that the segment is turned on
  HIGH means that the digit is turned on and when that digit is turned on all segments are set to LOW (turned on)
*****************************************************************************************************************************/

//declare constants
int tensDigit = 10; //330 Ohm resistor between digit pin and nano pin
int onesDigit = 11; //330 Ohm resistor between digit pin and nano pin

int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int pinDP = 9;

int positiveButtonPin = 12; //and the other side of the button is connected to the GND pin next to D2
int negativeButtonPin = A0; //and the other side of the button is connected to the GND pin next to D2

//declare variables
unsigned long lastPositiveDebounceTime = 0;
unsigned long lastNegativeDebounceTime = 0;
unsigned long debounceDelay = 100;
unsigned long previousMillis = 0;

int currentPositiveButtonState = HIGH;
int currentNegativeButtonState = HIGH;
int lastPositiveButtonState = LOW;
int lastNegativeButtonState = LOW;
int trueLastPositiveButtonState = LOW;
int trueLastNegativeButtonState = LOW;
int buttonPushCounter = 0;
int startingValue = 20;
int currentValue = startingValue;


//declare functions
void delayTime(unsigned long milliseconds);
void setDigit(int place, int number);
void setNumber(int number);

void setup() { 

  pinMode(tensDigit, OUTPUT);
  pinMode(onesDigit, OUTPUT);
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(pinDP, OUTPUT);

  pinMode(positiveButtonPin, INPUT_PULLUP);
  pinMode(negativeButtonPin, INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {

  setNumber(currentValue);

  //read the current button state
  currentPositiveButtonState = digitalRead(positiveButtonPin);
  currentNegativeButtonState = digitalRead(negativeButtonPin);

  //if the positive button state has changed then record the new current time
  if (currentPositiveButtonState != lastPositiveButtonState)
  {
    lastPositiveDebounceTime = millis();  
  }

  //if the button state did not change during the debounceDelay time, then consider it a true state change and...
  if ((millis() - lastPositiveDebounceTime) > debounceDelay) 
  {
    //if the last true button state has changed then...
    if (currentPositiveButtonState != trueLastPositiveButtonState)
    {
      trueLastPositiveButtonState = currentPositiveButtonState;

      //if the button was pressed then increment the button pressed counter
      if (currentPositiveButtonState == LOW)
      {
        currentValue++;
        setNumber(currentValue);
        Serial.println(currentValue);
      }
    }
  }

  //change the last button reading to the current button reading 
  lastPositiveButtonState = currentPositiveButtonState;

  //if the negative button state has changed then record the new current time
  if (currentNegativeButtonState != lastNegativeButtonState)
  {
    lastNegativeDebounceTime = millis();  
  }

  //if the button state did not change during the debounceDelay time, then consider it a true state change and...
  if ((millis() - lastNegativeDebounceTime) > debounceDelay) 
  {
    //if the last true button state has changed then...
    if (currentNegativeButtonState != trueLastNegativeButtonState)
    {
      trueLastNegativeButtonState = currentNegativeButtonState;

      //if the button was pressed then increment the button pressed counter
      if (currentNegativeButtonState == LOW)
      {
        currentValue--;
        setNumber(currentValue);
      }
    }
  }

  //change the last button reading to the current button reading 
  lastNegativeButtonState = currentNegativeButtonState;



}

void setNumber(int number) {

  int onesPlace = number % 10;
  setDigit(1, onesPlace); 
  delay(5);
  int tensPlace = number / 10;
  setDigit(10, tensPlace); 
  delay(5);
}


//place is 1 if setting the ones place and 10 if setting the tens place
void setDigit(int place, int number) {
  switch(place)
  {
    case 1: 
      digitalWrite(onesDigit, HIGH);
      digitalWrite(tensDigit, LOW);
      break;
    case 10:
      digitalWrite(onesDigit, LOW);
      digitalWrite(tensDigit, HIGH);
      break;
  }

  switch(number)
  {
    case 0:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, HIGH);
      digitalWrite(pinDP, HIGH);
      break;
    case 1:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      digitalWrite(pinDP, HIGH);
      break;
    case 2:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, LOW);
      digitalWrite(pinDP, HIGH);
      break;
    case 3:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, LOW);
      digitalWrite(pinDP, HIGH);
      break;
    case 4:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, LOW);
      digitalWrite(pinDP, HIGH);
      break;
    case 5:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, LOW);
      digitalWrite(pinDP, HIGH);
      break;
    case 6:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, LOW);
      digitalWrite(pinDP, HIGH);
      break;
    case 7:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      digitalWrite(pinDP, HIGH);
      break;
    case 8:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, LOW);
      digitalWrite(pinDP, HIGH);
      break;
    case 9:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, LOW);
      digitalWrite(pinDP, HIGH);
      break;
  }
}
6
  • Your post seems to have two questions. One, battery power, is well documented and you can find the answer on the Arduino website. The other is to do with understanding the 7-seg display, which is not apparent in the title of the question. Did you use current-limiting resistors? Google "7 segment display resistor" and there are plenty of tutorials (including video) on how to use.
    – MichaelT
    Dec 18, 2018 at 7:31
  • How are you attempting to connect the various batteries you have tried?
    – Majenko
    Dec 18, 2018 at 11:16
  • Stackexchange works best one question at a time. The assumed goal is to build a searchable data base of questions and answers for others with similar questions. Multiple questions and long discussions make this goal difficult to realize. That said, batteries are chosen for a number of reasons. Some, like cost and weight, you may not even care about. And more then not, several different batteries can be used for the same application. So, it may come down to opinion. Yet another format which stackexchange uses normally avoid...
    – st2000
    Dec 18, 2018 at 13:36
  • (1) Yes, as I stated in my code, I used a 330 ohm resistor between each of the two seven segment digit pins and the Arduino nano pin.
    – R. Wilson
    Dec 19, 2018 at 1:25
  • (2) I connected the positive terminal of my battery to pin 30 which is labeled as VIN on the Arduino Nano and the negative terminal of my battery to pin 29 which is right next door and labeled as GND.
    – R. Wilson
    Dec 19, 2018 at 1:28

2 Answers 2

1

Consider the length of time needed with out external power and the average current the project demands. Multiply these two giving Amp-Hours. Then, given the Amp-Hours are reasonable, choose a battery with twice the Amp-Hour capacity. Battery ratings are usually ideal. In that the slower the power is pulled from the batteries the larger the battery capacity will appear.

It goes without saying you need enough cells to generate the specified voltage. Cell voltage varies by chemistry. Consequently note the exact voltage of a cell or pack of cells. Especially when building your own serial battery packs where slight differences add up to significant voltages.

Lastly consider if primary or secondary batteries are to be used. Primary cells such as alkaline batteries are long lasting and generally safer. Secondary batteries such as Lithium-ion offer good power density but are difficult to care for.

4
  • Thank you for your help. I attempted to follow the directions that came with my multimeter and measured the current of my circuit as 28 mA. If I understand correctly, if I want my project to run for 4 hours at a time then I should buy a battery with at least 224mAh.
    – R. Wilson
    Dec 19, 2018 at 1:08
  • Concerning the voltage, I feel really stupid. For some reason, I was imagining that I should use ohm's law (or something else) to do some kind of calculation (which I was not grasping) to determine the proper voltage of the battery to use. However, would I now be correct to assume that there is no calculation that I should do? Instead, I just need to use between 7 and 12 V because that is what the instructions say that the nano needs and it doesn't actual matter what pieces I put in my circuit.
    – R. Wilson
    Dec 19, 2018 at 1:19
  • 1
    I believe both of your comments are correct. Most batteries are have better than 224mAh so it should be easy to find even small batteries that satisfy this minimum. Processors current demands can vary depending on what the processor is doing. And over engineering battery life by an order of magnitude rather than double may be prudent. When using any on board regulator, inferred by the wide range of acceptable voltages, be careful and double check you are connecting to the proper power pins. Directly powering a 3 or 5 volt processor with these higher voltages can damage the processor.
    – st2000
    Dec 19, 2018 at 13:41
  • I should add that managing portable processor based devices to extend battery life is not trivial. Professional developers spend hours if not days selecting and configuring processors to operate as expected while only "sipping" nAmps and uAmps of current 90% of the time.
    – st2000
    Dec 19, 2018 at 13:47
1

I don't think this will be a fully fledged answer, but I want to tell you a useful alternative to batteries you already used. I usually power my projects through a 5V "powerbank" that I do myself. All you need is a step-up board like for example 134N3P and any Li-Po/Li-Ion 3,7V cell. The 134N3P is very small and it contains a undervoltage and overcurrent protection circuits so you won't destroy the cell. It also allows to safely charge that cell using any USB charger. The Li-Po/Li-Ion cells come in all shapes and sizes. For miniature projects you can find one in size of a two coin batteries, but for regular size projects I suggest 18650 cells as the cheapest option.

1
  • Thank you. This is very interesting. I was not aware of this option.
    – R. Wilson
    Dec 20, 2018 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.