74HC4051

    This page is to Setup and Config 74HC4051
 

Hardware and Software

Hardware 
    
74HC4051 : in/out A0-A7 Analog Signal by Selected S0-S2 Control

Software 
    Arduino IDE


Sanki Notes


Examples

          This example is for Arduino Write a Signal to A0 to A7


  1. int AIO = A0;    // LED connected to digital pin 9
    int S0 = 4;
    int S1 = 5;
    int S2 = 6;


    void setup() {
      // nothing happens in setup
      pinMode(AIO, OUTPUT);
      pinMode(S0, OUTPUT);
      pinMode(S1, OUTPUT);
      pinMode(S2, OUTPUT);
    }

    int writeSignal (const byte which)
      {
      // select correct MUX channel
      digitalWrite (S0, (which & 1) ? HIGH : LOW);  // low-order bit
      digitalWrite (S1, (which & 2) ? HIGH : LOW);
      digitalWrite (S2, (which & 4) ? HIGH : LOW);  // high-order bit
      // now read the sensor
      showSignal();
      //analogWrite(AIO, 255);
      }  // end of readSensor
      

    void showSignal() {
      // fade in from min to max in increments of 5 points:
      for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 25) {
        // sets the value (range from 0 to 255):
        analogWrite(AIO, fadeValue);
        // wait for 30 milliseconds to see the dimming effect
        delay(30);
      }

      // fade out from max to min in increments of 5 points:
      for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 25) {
        // sets the value (range from 0 to 255):
        analogWrite(AIO, fadeValue);
        // wait for 30 milliseconds to see the dimming effect
        delay(30);
      }
    }

    void loop ()
      {
      // show all 8 sensor readings
      for (byte i = 0; i < 8; i++)
        {
        Serial.print ("Sensor ");
        Serial.print (i);
        Serial.print (" Show Signal: ");
        writeSignal(i);
        }
      delay (200);
      }  // end of loop



Examples

          This example is for Arduino Reada Signal fromA0 to A7
  1. // Example of using the 74HC4051 multiplexer/demultiplexer

    // Author: Nick Gammon
    // Date:   14 March 2013

    const byte sensor = A0;  // where the multiplexer in/out port is connected

    // the multiplexer address select lines (A/B/C)
    const byte addressA = 6; // low-order bit
    const byte addressB = 5;
    const byte addressC = 4; // high-order bit

    void setup ()
      {
      Serial.begin (115200);
      Serial.println ("Starting multiplexer test ...");
      pinMode (addressA, OUTPUT); 
      pinMode (addressB, OUTPUT); 
      pinMode (addressC, OUTPUT); 
      }  // end of setup

    int readSensor (const byte which)
      {
      // select correct MUX channel
      digitalWrite (addressA, (which & 1) ? HIGH : LOW);  // low-order bit
      digitalWrite (addressB, (which & 2) ? HIGH : LOW);
      digitalWrite (addressC, (which & 4) ? HIGH : LOW);  // high-order bit
      // now read the sensor
      return analogRead (sensor);
      }  // end of readSensor
      
    void loop ()
      {
      // show all 8 sensor readings
      for (byte i = 0; i < 7; i++)
        {
        Serial.print ("Sensor ");
        Serial.print (i);
        Serial.print (" reads: ");
        Serial.println (readSensor (i));
        }
      delay (1000);
      }  // end of loop