Hi,
I am trying to read voltages (i.e. ADC input) using a 16 channel 74HC4067, which then should feed to an MCP3208.
It doesn't seem to pass the inputs through to the "Z" pin. Here's the code:
#include <MCP3208.h>
#include <SPI.h>tp
MCP3208 adc(15);
float voltage = 0.0; // calculated voltage
float R1 = 330000;
float R2 = 1000;
float divider = 340 ; // 299.7012987;
float Vref = 4.095; // Using LM4040 reference voltage
unsigned int total; // can hold max 64 readings
int s0 = 5;
int s1 = 4;
int s2 = 0;
int s3 = 2;
void setup() {
adc.begin();
Serial.begin(9600);
Serial.println("Voltage monitor: MCP3208VoltMeter");
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
}
void loop() {
// MUX Connection
for (int i = 0; i < 16; i ++) {
//MCP3208 Code
// take a number of analog samples and add them up
for (int x = 0; x < 64; x++) { // multiple analogue readings for averaging
total = total + adc.analogRead(0); // add each value to a total
}
voltage = (total / 64.0) * divider * Vref / 4095 ; // convert readings to volt
// Serial.print("adc.analogRead(0) :");
// Serial.println(adc.analogRead(0));
Serial.print("Battery ");
Serial.print(i);
Serial.print(": ");
// Serial.print(voltage);
// Serial.print(readMux(i));
Serial.println("V");
Serial.println();
total = 0; // reset value
delay(1000); // one second between measurements
}
}
int readMux(int channel) {
int controlPin[] = {s0, s1, s2, s3};
int muxChannel[16][4] = {
{0, 0, 0, 0}, //channel 0
{1, 0, 0, 0}, //channel 1
{0, 1, 0, 0}, //channel 2
{1, 1, 0, 0}, //channel 3
{0, 0, 1, 0}, //channel 4
{1, 0, 1, 0}, //channel 5
{0, 1, 1, 0}, //channel 6
{1, 1, 1, 0}, //channel 7
{0, 0, 0, 1}, //channel 8
{1, 0, 0, 1}, //channel 9
{0, 1, 0, 1}, //channel 10
{1, 1, 0, 1}, //channel 11
{0, 0, 1, 1}, //channel 12
{1, 0, 1, 1}, //channel 13
{0, 1, 1, 1}, //channel 14
{1, 1, 1, 1} //channel 15
};
for (int i = 0; i < 4; i ++) {
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
for (int x = 0; x < 64; x++) { // multiple analogue readings for averaging
total = total + adc.analogRead(0); // add each value to a total
}
voltage = (total / 64.0) * divider * Vref / 4095 ; // convert readings to volt
Serial.print("adc.analogRead(0) :");
Serial.println(adc.analogRead(0));
Serial.print(voltage);
Serial.println("V");
// int val = adc.analogRead(0);
int val = voltage;
return val;
}
There's a 12V battery connected to Y0 (Battery 0), and this is the output:
Battery 15: adc.analogRead(0) :3200
2112.70V
2112V
Battery 0: adc.analogRead(0) :2798
2040.07V
2040V
Battery 1: adc.analogRead(0) :3015
1975.99V
1975V
Battery 2: adc.analogRead(0) :3016
2050.13V
2050V
What am I doing wrong?