Good day,
For a project i need to measure the voltage of 8 batteries, since the Atmega1284 (which i am using for this) doesn't have enough analog inputs (it has 8 but i also need to connect other things to the analog input) i want to use a multiplexer to measure the 8 voltages.
I have bought the 74HC4051 multiplexer.
I have connected the multiplexer as this and connected 1 power supply as a test:
I use this code as a test:
/*
This code relies on the fact that PORTB on the ATMega328 chip has consecutive pins
which we can take advantage of to set the channel on the mux.
This code has been tested with Sparkfun's 16-ch mux (https://www.sparkfun.com/products/9056).
*/
#define MUX_CH_COUNT 8 // Reduce this number if you use less channels
#define PIN_D_MUX_S0 5 // bit 7 of PORTB
#define PIN_D_MUX_S1 6 // bit 6 of PORTB
#define PIN_D_MUX_S2 7 // bit 5 of PORTB
#define PIN_A_MUX_SIG A0 // This pin will read the input from the mux.
void setup() {
pinMode(PIN_D_MUX_S0, OUTPUT);
pinMode(PIN_D_MUX_S1, OUTPUT);
pinMode(PIN_D_MUX_S2, OUTPUT);
Serial.begin(9600);
}
void loop() {
for (byte i=0; i<MUX_CH_COUNT; i++) {
PORTB = (PORTB & B11110000) | i;
short val = analogRead(PIN_A_MUX_SIG);
// "val" holds the value for input "i", so you can insert your custom code here.
// Print the values...
Serial.print(i);
Serial.print(": ");
Serial.print(val);
Serial.print(" | ");
}
Serial.println("");
delay(1000);
}
The values which i am getting with the battery not connected is:
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
but somethimes i get something like this:
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 178 | 1: 183 | 2: 185 | 3: 189 | 4: 187 | 5: 156 | 6: 161 | 7: 178 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 180 | 1: 168 | 2: 159 | 3: 161 | 4: 174 | 5: 176 | 6: 148 | 7: 143 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 1023 | 1: 1023 | 2: 1023 | 3: 1023 | 4: 1023 | 5: 1023 | 6: 1023 | 7: 1023 |
0: 1023 | 1: 1023 | 2: 1023 | 3: 1023 | 4: 1023 | 5: 1023 | 6: 1023 | 7: 1023 |
0: 1023 | 1: 1023 | 2: 1023 | 3: 1023 | 4: 1023 | 5: 1023 | 6: 1023 | 7: 1023 |
0: 1023 | 1: 1023 | 2: 1023 | 3: 1023 | 4: 1023 | 5: 1023 | 6: 1023 | 7: 1023 |
0: 1023 | 1: 1023 | 2: 1023 | 3: 1023 | 4: 1023 | 5: 1023 | 6: 1023 | 7: 1023 |
0: 1023 | 1: 1023 | 2: 1023 | 3: 1023 | 4: 1023 | 5: 1023 | 6: 1023 | 7: 1023 |
0: 1023 | 1: 1023 | 2: 1023 | 3: 1023 | 4: 1023 | 5: 1023 | 6: 1023 | 7: 1023 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 0 | 1: 0 | 2: 0 | 3: 144 | 4: 173 | 5: 187 | 6: 181 | 7: 177 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
When i connect the battery, i get values like this:
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
0: 132 | 1: 201 | 2: 160 | 3: 243 | 4: 288 | 5: 252 | 6: 278 | 7: 232 |
0: 774 | 1: 756 | 2: 770 | 3: 785 | 4: 770 | 5: 746 | 6: 742 | 7: 764 |
0: 140 | 1: 163 | 2: 126 | 3: 175 | 4: 238 | 5: 214 | 6: 263 | 7: 247 |
0: 133 | 1: 147 | 2: 157 | 3: 144 | 4: 147 | 5: 134 | 6: 166 | 7: 194 |
0: 782 | 1: 778 | 2: 771 | 3: 777 | 4: 773 | 5: 774 | 6: 783 | 7: 763 |
0: 134 | 1: 152 | 2: 144 | 3: 132 | 4: 160 | 5: 156 | 6: 167 | 7: 224 |
0: 764 | 1: 772 | 2: 785 | 3: 764 | 4: 779 | 5: 781 | 6: 752 | 7: 779 |
0: 140 | 1: 137 | 2: 179 | 3: 149 | 4: 180 | 5: 153 | 6: 163 | 7: 210 |
0: 794 | 1: 769 | 2: 763 | 3: 767 | 4: 782 | 5: 781 | 6: 776 | 7: 768 |
0: 146 | 1: 156 | 2: 142 | 3: 149 | 4: 163 | 5: 145 | 6: 203 | 7: 228 |
0: 154 | 1: 144 | 2: 160 | 3: 129 | 4: 764 | 5: 192 | 6: 748 | 7: 173 |
0: 0 | 1: 0 | 2: 0 | 3: 0 | 4: 0 | 5: 0 | 6: 0 | 7: 0 |
What am i doing wrong? I expected to get the same values on 1 input (in this case 0 because i connected the battery to Y0.
Why do all the other numbers get values too, and why is the value for number 0 different every reading?
I only connected 1 battery to test, i am not sure if this causes the problem
Thanks for helping