So I would like to begin to build an on-board-computer for my diy electric longboard. My first step was to make something that could read the voltages of six 4.2v batteries. So far my code looks fine. I am using a multiplexer to expand my analog inputs. My issue is that when I read a single battery directly I get an accurate measurement but when using the multiplexer I can not get an accurate reading with a single or multiple batteries.
Here is my code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int s0 = 6;
int s1 = 7;
int s2 = 8;
float battery1_voltage = 0.0;
float battery2_voltage = 0.0;
float battery3_voltage = 0.0;
float battery4_voltage = 0.0;
float battery5_voltage = 0.0;
float battery6_voltage = 0.0;
void setup()
{
Serial.begin(9600);
//Defining analog pins
analogReference(DEFAULT);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
//Fake loading bar used to make user wait for ESC boot-up
lcd.begin(16, 2);
lcd.print("|");
delay(250);
lcd.print("||");
delay(250);
lcd.print("|||");
delay(250);
lcd.print("||||");
delay(250);
lcd.print("||||");
delay(250);
lcd.print("|||||");
delay(250);
lcd.print("||||||");
delay(250);
lcd.print("|||||||");
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(" SNYXMOBILE ");
delay(1000);
lcd.setCursor(6,1);
lcd.print("READY");
delay(1000);
}
void loop()
{
//Conversion formula, pin activation and lcd.print for voltage
//CELL 1
digitalWrite(s0, LOW);
battery1_voltage = analogRead(A0);
delay(10);
battery1_voltage = analogRead(A0);
delay(10);
battery1_voltage = (battery1_voltage * 5.0) / 1024.0;
if (battery1_voltage < 0.1)
{
battery1_voltage=0.0;
}
Serial.print("C1v= ");
Serial.println(battery1_voltage);
lcd.setCursor(0,0);
lcd.print("Cell 1 = ");
lcd.print(battery1_voltage);
delay(1000);
digitalWrite(s0, LOW);
//CELL2
digitalWrite(s1, HIGH);
battery2_voltage = analogRead(A0);
delay(10);
battery2_voltage = analogRead(A0);
delay(10);
battery2_voltage = (battery2_voltage * 5.0) / 1024.0;
if (battery2_voltage < 0.1)
{
battery2_voltage=0.0;
}
Serial.print("C2v= ");
Serial.println(battery1_voltage);
lcd.setCursor(0,1);
lcd.print("Cell 2 = ");
lcd.print(battery2_voltage);
delay(1000);
digitalWrite(s1, LOW);
//CELL 3
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
battery3_voltage = analogRead(A0);
delay(10);
battery3_voltage = analogRead(A0);
delay(10);
battery3_voltage = (battery3_voltage * 5.0) / 1024.0;
if (battery3_voltage < 0.1)
{
battery3_voltage=0.0;
}
Serial.print("C3v= ");
Serial.println(battery3_voltage);
lcd.setCursor(0,0);
lcd.print("Cell 3 = ");
lcd.print(battery3_voltage);
delay(1000);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
//CELL4
digitalWrite(s2, HIGH);
battery4_voltage = analogRead(A0);
delay(10);
battery4_voltage = analogRead(A0);
delay(10);
battery4_voltage = (battery4_voltage * 5.0) / 1024.0;
if (battery4_voltage < 0.1)
{
battery4_voltage=0.0;
}
Serial.print("C4v= ");
Serial.println(battery1_voltage);
lcd.setCursor(0,1);
lcd.print("Cell 4 = ");
lcd.print(battery4_voltage);
delay(1000);
digitalWrite(s2, LOW);
//CELL 5
digitalWrite(s0, HIGH);
digitalWrite(s2, HIGH);
battery5_voltage = analogRead(A0);
delay(10);
battery5_voltage = analogRead(A0);
delay(10);
battery5_voltage = (battery5_voltage * 5.0) / 1024.0;
if (battery5_voltage < 0.1)
{
battery5_voltage=0.0;
}
Serial.print("C5v= ");
Serial.println(battery5_voltage);
lcd.setCursor(0,0);
lcd.print("Cell 5 = ");
lcd.print(battery5_voltage);
delay(1000);
digitalWrite(s0, LOW);
digitalWrite(s2, LOW);
//CELL6
digitalWrite(s1, HIGH);
digitalWrite(s2, HIGH);
battery6_voltage = analogRead(A0);
delay(10);
battery6_voltage = analogRead(A0);
delay(10);
battery6_voltage = (battery6_voltage * 5.0) / 1024.0;
if (battery6_voltage < 0.1)
{
battery6_voltage=0.0;
}
Serial.print("C6v= ");
Serial.println(battery6_voltage);
lcd.setCursor(0,1);
lcd.print("Cell 6 = ");
lcd.print(battery6_voltage);
delay(1000);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
}