Simple multiplexer example

This took me some time to figure out, so I thought this code sample and picture might help other people who are first attempting to use the 4051(HCF4051BE) multiplexer.

All this does is scroll through some LEDs and turn then on one at a time according to which channel of the multiplexer they are one.

// Define binary control output pins
#define S0 2
#define S1 3
#define S2 4

void setup()
{
  // Set multiplexer controller pins to output
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
}

void loop()
{
  // Loop through the 8 possible channels in the multiplexer
  for( int i = 0; i < 8; i++ ){
    setMultiplexer(i);
    delay(1000);
  }
  
}

void setMultiplexer( int i ){
  
  int s0Value = i & 0x01;      // get value of first bit
  int s1Value = (i>>1) & 0x01; // get value of second bit
  int s2Value = (i>>2) & 0x01; // get value of third bit
  
  digitalWrite( S0, s0Value ); // turn first pin on or off
  digitalWrite( S1, s1Value ); // turn second pin on or off
  digitalWrite( S2, s2Value ); // turn third pin on or off
}

Only 6 LEDs, looks like someone needs to go component shopping!

Only 6 LEDs, looks like someone needs to go component shopping!

Ha, yeah, I just ordered some off ebay :)

This one reads resistance values instead of turns LEDs on. In the setup I have, the first value is “0” because its just a wire, and the last value is very high because its hanging open with nothing connecting it. In between are resistors with increasing known values so I could make sure everything was working ok. The green wire going across the whole thing is the analogRead() wire.

I think this code and example could also be of value to people who need to know how to calculate actual resistance values from an Arduino “analogRead().” This sounds simple, but it took me a while to find the information on how to do this and then implement it properly.

The code:

// Define binary control output pins
#define S0 2
#define S1 3
#define S2 4

// The analog sensor used to find resistence
#define SENSOR 0

// Constant values
float knownResistance = 99100;
float inputVoltage = 5.0;

void setup()
{
  // Start serial, this will use digital pins 0 and 1
  Serial.begin(9600);
  
  // Set multiplexer controller pins to output
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
}

void loop()
{
  // Loop through the 8 possible channels in the multiplexer
  for( int i = 0; i < 8; i++ ){
    // Set multiplexer to the current channel
    setMultiplexer(i);
    delay(10);          // give multiplexer a chance to respond and resistance a chance to settle
    
    // Read raw analogRead value
    int A = analogRead(SENSOR);
    
    // Get actual resistance
    float Resistance = getResistance(knownResistance, inputVoltage, A);
    
    // Print results to serial
    Serial.print(i);
    Serial.print(") Resistance: ");
    Serial.print(Resistance);
    Serial.print("\n");
    
    delay(1000);
  }
  
  Serial.print("\n");
  delay(600000);
}

void setMultiplexer( int i ){
  
  int s0Value = i & 0x01;      // get value of first bit
  int s1Value = (i>>1) & 0x01; // get value of second bit
  int s2Value = (i>>2) & 0x01; // get value of third bit
  
  digitalWrite( S0, s0Value ); // turn first pin on or off
  digitalWrite( S1, s1Value ); // turn second pin on or off
  digitalWrite( S2, s2Value ); // turn third pin on or off
}

// Rn = value of known resitor
// Vn = value of input voltage
// inA = the result from "digitalRead()
float getResistance(float Rn, float Vn, int inA){
 float A = (float)inA;
 float Vx;  // calculated voltage
 float Rx;  // calculated resistance
 Vx = (Vn / 1024.0) * A;
 Rx = Rn / ((Vn/Vx)-1.0);

 return Rx;
}
float getReverseResistance( float Rn, float Vn, int inA ){
 float A = (float)inA;
 float Vx;  // calculated voltage
 float Rx;  // calculated resistance
 Vx = (Vn / 1024.0) * A;
 Rx = Rn * ((Vn/Vx)-1.0);
 
 return Rx; 
}