Problems with a 4051 multiplexer

Hello im building a MIDI controller and im gonna need to expand my inputs so i decided to use a multiplexer. Im fairly new both in arduino related stuff and electronics stuff.

You may ask why im writing in the LED multiplexer forum when im building a midi controller. First i want to understand how the multiplexer code works and with 2 buttons i want to switch a LED on and off, so that im sure i know what im doing and move on with my project.
Until now i have read and watched lots of tutorials about the things i need. I cant find a detailed tutorial for a multiplexer though.
I know how the multiplexer works, i know what a truth table is, i know what it does. Im having trouble with the code.
I finally found an example code that i can roughly understand.
Here is the example code.

/*
multiplexer sketch
read 1 of 8 analog values into single analog input pin with 4051 multiplexer

 array of pins used to select 1 of 8 inputs on multiplexer 
 */
 int LedPin = 13;
 int buttonState;
 int lasbuttonState = 0;
const int select[] = {8,9,10}; // array of the pins connected to the 4051 input select lines
const int analogPin = A1; // the analog pin connected to the multiplexer output
// this function returns the analog value for the given channel
int getValue( int channel)
{
// the following sets the selector pins HIGH and LOW to match the binary value of channel
for(int bit = 0; bit < 3; bit++)
{
int pin = select[bit]; // the pin wired to the multiplexer select bit
int isBitSet = bitRead(channel, bit); // true if given bit set in channel
digitalWrite(pin, isBitSet);
}
return analogRead(analogPin);
}
void setup()
{
for(int bit = 0; bit < 3; bit++)
pinMode(select[bit], OUTPUT); // set the three select pins to output
pinMode (13, OUTPUT);
Serial.begin(9600);
}
void loop () {
// print the values for each channel once per second
for(int channel = 0; channel < 8; channel++)
{
int value = getValue(channel);
Serial.print("Channel ");
Serial.print(channel);
Serial.print(" = ");
Serial.println(value);
}
delay (1000);

buttonState = digitalRead(analogPin);
 if (buttonState != lastbuttonState) {
  if (buttonState == 1 && lastbuttonState == 0) {
   I WANT THE LED TO TURN ON;
  }
 }
 lastbuttonState = buttonState;
 if (buttonState!= lastbuttonState) {
  if (buttonState == 0 && lastbuttonState == 1) {
   I WANT THE LED TO TURN OFF;
}

My question what do i need to code to make 2 momentary buttons to toggle a LED on and off. I have already made the tutorial button state change in the arduino examples, so im familiar with that, but now i want to make it via a multiplexer.

Looks like an 8x1 multiplexer. Connect the output to a pin. Apply an address (3 bits) to the multiplexer. The button connected to the select input of the multiplexer is now connected to the output of the multiplexer. Read its state from the pin.

Yes its a 4051 8 channel mux. As i said i wired i perfectly im using 2 buttons on channel 0 and 1. What im having trouble with is the actual code.
My question is - can someone tell me if i can read only from channel 0 and 1. do i use channel0, channel1 to do that?. Where should i put it (maybe between some lines is the right choice and not just at the bottom. if someone can explain me the void loop section and this section i would be very greatfull.

for(int bit = 0; bit < 3; bit++)
{
int pin = select[bit]; // the pin wired to the multiplexer select bit
int isBitSet = bitRead(channel, bit); // true if given bit set in channel
digitalWrite(pin, isBitSet);
}
return analogRead(analogPin);

Thank you

One multiplexer connects 8 inputs to one analogue inputs.
So to use it you set up the three data select lines which you connect to 3 arduino outputs. Then you read that one input.
For an example of this in a project see:-
http://www.thebox.myzen.co.uk/Hardware/MIDI_Footsteps.html

Yes, i know how to connect the multiplexer. what i dont know is how to make one button to turn on the led other button to turn off the led, because if i just write digitalwrite(MYLEDPIN, VALUEFROMTHEMULTIPLEXEROUTPUT) both buttons are gonna light up the led and i want one button to turn on the led and the other button to turn off the led. thats why i dont understand it. why im using 2 buttons? i dont have 6 more buttons to wire them.

Just use this digitalReadMux() in place of digitalRead() for your buttons.

const int select[] = {8,9,10}; // array of the pins connected to the 4051 input select lines
const int analogPin = A1; // the analog pin connected to the multiplexer output

boolean digitalReadMux(int channel)
{
  // the following sets the selector pins HIGH and LOW to match the binary value of channel
  for(int bit = 0; bit < 3; bit++)
  {
    int pin = select[bit]; // the pin wired to the multiplexer select bit
    int isBitSet = bitRead(channel, bit); // true if given bit set in channel
    digitalWrite(pin, isBitSet);
  }
  pinMode(analogPin, INPUT)
  return digitalRead(analogPin);
}

// this function returns the analog value for the given channel
int analogReadMux( int channel)
{
  // the following sets the selector pins HIGH and LOW to match the binary value of channel
  for(int bit = 0; bit < 3; bit++)
  {
    int pin = select[bit]; // the pin wired to the multiplexer select bit
    int isBitSet = bitRead(channel, bit); // true if given bit set in channel
    digitalWrite(pin, isBitSet);
  }
  return analogRead(analogPin);
}

can i use only a part of the channel array - channel0 and channel1, because i dont need the others for now.
and i should write digitalWrite(ledpin,digitalRead(digitalReadMux) ?

EDIT : i will update the code to contain the lines for the led

spoocter:

can i use only a part of the channel array - channel0 and channel1, because i dont need the others for now.

To read the button on channel 0, use digitalReadMux(0).
To read the button on channel 1, use digitalReadMux(1).

and i should write digitalWrite(ledpin,digitalRead(digitalReadMux) ?

Depends on which button you want to copy to the LED. You probably want something like:

digitalWrite(ledpin, digitalReadMux(0));  // LED follows button on Mux channel 0

EDIT : i will update the code to contain the lines for the led

Thank you very very much!!! :slight_smile:

Here's my code, when i try to compile it, it returns an error (there is no line highlighted indicating where the error is) :
My code:

/*
multiplexer sketch
read 1 of 8 analog values into single analog input pin with 4051 multiplexer

 array of pins used to select 1 of 8 inputs on multiplexer 
 */
 boolean digitalReadMux(int channel);
 int LedPin = 13;
const int select[] = {8,9,10}; // array of the pins connected to the 4051 input select lines
const int analogPin = A1; // the analog pin connected to the multiplexer output
// this function returns the analog value for the given channel
int getValue( int channel)
{
// the following sets the selector pins HIGH and LOW to match the binary value of channel
for(int bit = 0; bit < 3; bit++)
{
int pin = select[bit]; // the pin wired to the multiplexer select bit
int isBitSet = bitRead(channel, bit); // true if given bit set in channel
digitalWrite(pin, isBitSet);
}
return analogRead(analogPin);
}
void setup()
{
for(int bit = 0; bit < 3; bit++)
pinMode(select[bit], OUTPUT); // set the three select pins to output
pinMode (13, OUTPUT);
}
void loop () {

 if (digitalReadMux(0) == 1) {
   digitalWrite(LedPin, HIGH);
 }
  if (digitalReadMux(1) == 1) {
    digitalWrite(LedPin, LOW);
}

}

The error :

Multiplexer2LEDs.cpp.o: In function `loop':
AppData\Local\Temp\build560401855751289364.tmp/Multiplexer2LEDs.cpp:35: undefined reference to `digitalReadMux(int)'
AppData\Local\Temp\build560401855751289364.tmp/Multiplexer2LEDs.cpp:38: undefined reference to `digitalReadMux(int)'

Somebody please :~ :roll_eyes:

How have you got this set up? Where is the Multiplexer2LEDs.cpp, should it not be in a tab or the library?
The error message talks about a loop function in this code, you can only have one loop function.
As the error is with the Multiplexer2LEDs.cpp file it is a little difficult trying to fault find on it as you haven't included it.
Try zipping up your project file and posting that.

I have no other tabs or libraries, when i zip the sketch there is only 1 file that is .ino. I copied the code from a book and added my things for the led. the book mentions no library

links?
code?
what book?
if you expect help you better be prepared to play ball.

I took the code from Arduino Cookbook.
I attached the zipped sketch.
Im writing here the most recent code

/*
multiplexer sketch
read 1 of 8 analog values into single analog input pin with 4051 multiplexer

 array of pins used to select 1 of 8 inputs on multiplexer 
 */
 boolean digitalReadMux(int channel);
 int LedPin = 13;
const int select[] = {10,9,8}; // array of the pins connected to the 4051 input select lines
const int analogPin = A1; // the analog pin connected to the multiplexer output
// this function returns the analog value for the given channel
int getValue( int channel)
{
// the following sets the selector pins HIGH and LOW to match the binary value of channel
for(int bit = 0; bit < 3; bit++)
{
int pin = select[bit]; // the pin wired to the multiplexer select bit
int isBitSet = bitRead(channel, bit); // true if given bit set in channel
digitalWrite(pin, isBitSet);
}
return analogRead(analogPin);
}
void setup()
{
for(int bit = 0; bit < 3; bit++)
pinMode(select[bit], OUTPUT); // set the three select pins to output
pinMode (13, OUTPUT);
}
void loop () {

 if (digitalReadMux(0) == 1) {
   digitalWrite(LedPin, HIGH);
 }
  if (digitalReadMux(1) == 1) {
    digitalWrite(LedPin, LOW);
}

}

Multiplexer2LEDs-120818a.zip (938 Bytes)

I found a solution to my problem. I found a library for the 4051 mux, it makes everything hell of alot more easier and understandable by new people.
I noticed there are no sticky threads in the forum so im posting it as very usefull information.

This library has now been updated with some better examples and the lib now supports daisy chaining 4051s as well.

Cheers
ajfisher

I think your problem is here

boolean digitalReadMux(int channel);

digitalReadMux is being used as a function later on so it needs a body. Instead of the semicolon there should be an open curly brace { some code which returns true or false depending on the mux input then a closing curly brace }

Also where you check for it you should not use ==1 as it is returning boolean rather than an integer

if (digitalReadMux(1)) { // ..... // }