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:
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.
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:
- 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;
}
}