First post here and I have very little Arduino/c programming knowledge so forgive me my ignorance.
I am processing 18650 cells for a little powerwall project. I am using an Arduino pro mini atmega328p(5v) to read battery slot voltages and to turn on/off the tp4506 when the voltage reaches 3.85 volts for long term storage.
I am getting good voltage readings and after some trouble having a common ground with the tp4506 I think I have isolated it with a couple diodes.
I am using a 30N06L n channel mosfet to switch the tp4506's on and off, shutting them off to get the reading then back on till next voltage reading. I am learning a lot and having fun with it.
My problem stems from me not really having firm grasp of terminology or Arduino C. I cannot find my solution from search engine results after a few hours searching on and off.
I am using a for loop over 3(for now) analogue pins to get my voltage readings. What I can't figure out is how to map those pins to the digital pins controlling the mosfets from the loop. I have a few different versions of this project just trying to get a feel for what is the most efficient way to do it(I like the loop). How do I map or correlate AO with D2 and so forth. It's very frustrating not being able to type what I want out in a way that get's me some code I can learn from so I hope another human can understand my issue.
const int aRefVoltage = 5.00;
float voltage = 0;
void setup() {
// put your setup code here, to run once:
pinMode(2, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
// read the input on analog pin 0-2
for (int i = 0; i < 3; i++) {
int sensorValue = analogRead(i);
delay(700);
// float voltage = sensorValue * (aRefVoltage / 1024.0);
voltage = sensorValue*aRefVoltage/1024.0;
// print out the value you read:
Serial.print( F(" Slot-") );
Serial.print(i + 1); // +1 to match slot number
Serial.print( F(" = ") );
Serial.print(voltage);
Serial.print( F(" Volts") );
if (i == 2) Serial.println(" ");
Serial.println( F("") );
}
}
What is the smartest way to map analogue "i[0]" to the digital pin 2 to turn on that slots mosfet while using the for loop? Switch case? I hope I was clear enough in my question.
Thank you
edit: inb4 I get chewed out.
I am not looking for the answer to be handed to me. I would like a link to a tutorial that will show me how to use i[1] either in or out of the loop so I can use it....or how to use it. An explanation from my would be hero would be nice too.
My searches all got me very rudimentary for loop examples and endless tutorials on how to turn analogue pins into digital pins. Surprising amount of tutorials on how to do that out there.
edit 2
A ce pin? That sounds handy I will have to look into that. I am sure my use of diodes would make an engineer shake his head. I have limited supplies on hand and this is really my first project where I have a use for my tinkering.
On each analogue pin I have two 10k resistors. One to pull down and the other to prevent the batteries from sending juice back through the analogue pin.
I spent several hours trying to use the 1.1 volt internal reference but even after calculating what my 1.1 actually was my results were not as accurate and it just seemed like slapping a dmm across the rail and dialing the knob on the buck converter was practical.
https://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/ and internal1.1Ref = 1.1 * Vcc1 (per voltmeter) / Vcc2 (per readVcc() function)
For diodes I have a 1N4001 between the battery negative on tp4506 chip and ground. If I did not have it there power was flowing back through the chip. I was also playing with a 3v zener on the positive side to block .5 volts to my analogue reading when no battery present in slot.
I have no doubt I am doing it incorrectly I wasn't expecting those issues and was just trying to hack a solution together, luckily my aim is not charging to 4.2 or I would be up a creek.
Thank you again for taking the time to show me the code I needed. I should be able to figure it out from here. As long as I am moving forward I am content. I had been at it for ten hours by the time I broke down and made this post. I was pretty burnt out and knew if I could just see an example I would be good to go.
edit 3
const int aRefVoltage = 5.00;
const int readVoltagePins[3] = { A0, A1, A2 };
const int chargePin[3] = { 2, 3, 4 };
const int hysteresis = 0.10;
float voltage = 0;
void setup() {
// put your setup code here, to run once:
for (int i = 0; i < 3; i++) {
pinMode(chargePin[i], OUTPUT);
}
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
// read the input on analog pin 0-2
for (int i = 0; i < 3; i++) {
digitalWrite(chargePin[i], LOW);
delay(1500);
int sensorValue = analogRead(readVoltagePins[i]);
float voltage = (float) sensorValue * (5.0 / 1024.0);
// Write here code to turn on the digital pin on (once) if the voltage
// is too low and off (once) with hysteresis.
// The digital pin is: chargePin[i]
if (voltage <= 1.50) {
digitalWrite(chargePin[i], LOW);
Serial.print( F(" Slot-") );
Serial.print(i + 1); // +1 to match slot number
Serial.println( F(" = NFG || Empty ") );
break;
}
else if (voltage <= (3.85 - hysteresis)) {
digitalWrite(chargePin[i], HIGH);
Serial.print( F(" Slot-") );
Serial.print(i + 1); // +1 to match slot number
Serial.print( F(" = ") );
Serial.print(voltage);
Serial.println( F(" Volts & Charging") );
}
else if (voltage >= (3.85 + hysteresis)) {
digitalWrite(chargePin[i], LOW);
Serial.print( F(" Slot-") );
Serial.print(i + 1); // +1 to match slot number
Serial.print( F(" = ") );
Serial.print(voltage);
Serial.println( F(" Volts & !Charging") );
}
}
delay(5000); // increase later
for (int i = 0; i < 3; i++) {
if (i == 2)
Serial.println(" ");
}
}
This is what I have for code for now. I am just leaving the b- disconnected on the cloned charging boards. Let me know what you think @Jot