I have not yet made a 16 channel version, but here is the code for a working 8 channel version. The code is an extension of the code found on:
http://www.arduino.cc/playground/Learning/4051
Details of the 4051 are available on that site:
I create a CSV data stream with 9 elements, 8 Analog values read in sequentially from the 4051 plus a digital value to read a push button on Digital Input 3. The output from the 4051 goes to Analog Input(0). When the button is pressed, data is loaded into a MSFlexgrid.
It should be easy to extend the software for a second 4051 feeding into Analog Input(1).
****** Arduino Code Ver 0019
/*
- codeexample for useing a 4051 * analog multiplexer / demultiplexer
- by david c. and tomek n.* for k3 / malm[ch65533] h[ch65533]gskola
-
- To read 8 Analog Values into a single Analog Input Pin on Arduino
-
- read a single Button Input on a Digital Input
*/
const int buttonPin = 3; //digital input 3 Pin 4
int led = 13; //just an led
int r0 = 0; //value select pin at the 4051 (s0)
int r1 = 0; //value select pin at the 4051 (s1)
int r2 = 0; //value select pin at the 4051 (s2)
int row = 0; // storing the bin code
int count = 0; // just a count
int bin [] = {000, 1, 10, 11, 100, 101, 110, 111};//bin = bin[ch65533]r, some times it is so easy
void setup(){
// initialize the serial port:
pinMode(buttonPin, INPUT); //designate buttonPin as Input
pinMode(7, OUTPUT); // s0 //Output MUX code to 4051 pin 8
pinMode(6, OUTPUT); // s1 pin 7
pinMode(5, OUTPUT); // s2 pin 6
digitalWrite(led, HIGH);
Serial.begin(9600);
}
void loop () {
for (count=0; count<=7; count++) { //count through 8 MUX codes
row = bin[count];
r0 = row & 0x01;
r1 = (row>>1) & 0x01;
r2 = (row>>2) & 0x01;
digitalWrite(7, r0); //send MUX code to 4051
digitalWrite(6, r1);
digitalWrite(5, r2);
//Serial.print(r0); // used to include 4051 code in datastring
//Serial.print(r1);
//Serial.print(r2);
//Serial.println();
//Serial.print(",");
// read the sensor into ch0
int sensorReading = analogRead(0); // Analog Input goes to Ch 0 (pin 6) after each input of 4051 is selected
// print its value out as an ASCII numeric string
Serial.print(sensorReading, DEC);
Serial.print(","); //add comma
}
//read digital input and write to end of line
int DigitalsensorReading = digitalRead(buttonPin); //normally Hi. press for Lo
Serial.print(DigitalsensorReading);
// after the sensors have been read,
// print a newline and carriage return
Serial.println();
//delay (300); //Optional
}
****** My VB6 code for a Test Program is:
Dim RowCounter As Integer
Private Sub Form_Load()
RowCounter = 1
Label1.BackColor = vbRed 'initial program run condition.
'to make the square box in red color.
End Sub
Private Sub CmdOpen_Click()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1" '9600=baud rate N=no parity 8bits 1 stop bit
MSComm1.InputLen = 0
MSComm1.PortOpen = True
Label1.BackColor = vbGreen
End Sub
Private Sub Timer1_Timer() 'Timer.Interval = 150
Dim DataValues() As String
Dim DataString() As String
Dim DataLines() As String
Dim MsComStringData As String
Dim HP, kmh As Single
If MSComm1.InBufferCount > 0 Then
MsComStringData = MSComm1.Input
Text2.Text = MsComStringData
MsComStringData = Replace(MsComStringData, Chr(13), "") 'replaces vbCr Carriage Return
'with null
DataLines = Split(MsComStringData, Chr(10)) 'Split MsComStringData
'at vbLf Line Feed
DataString = Split(DataLines(UBound(DataLines) - 1), ",") 'Split DataLines
'at comma
'for 8 channels MUX output from 4051
If UBound(DataString) < 8 Then '6 Then ''for 6 channels
Exit Sub
End If
txt0.Text = DataString(0)
txt1.Text = DataString(1)
txt2.Text = DataString(2)
txt3.Text = DataString(3)
txt4.Text = DataString(4)
txt5.Text = DataString(5)
txt6.Text = DataString(6)
txt7.Text = DataString(7)
txtButtonPin.Text = DataString(8)
If txtButtonPin = 1 Then txtButtonPin.BackColor = vbRed
'Button not Pressed
If txtButtonPin = 0 Then
txtButtonPin.BackColor = vbWhite 'Button Pressed
grdTable1.Row = RowCounter
grdTable1.Col = 0
grdTable1.Text = txt0.Text
grdTable1.Col = 1
grdTable1.Text = txt1.Text
grdTable1.Col = 2
grdTable1.Text = txt2.Text
grdTable1.Col = 3
grdTable1.Text = txt3.Text
grdTable1.Col = 4
grdTable1.Text = txt4.Text
grdTable1.Col = 5
grdTable1.Text = txt5.Text
grdTable1.Col = 0
grdTable1.Text = txt0.Text
grdTable1.Col = 1
grdTable1.Text = txt1.Text
RowCounter = RowCounter + 1
End If
End If
End Sub
Private Sub cmdClose_Click()
MSComm1.PortOpen = False
Label1.BackColor = vbRed
End Sub
Private Sub cmdExit_Click()
End
End Sub