74HC4051

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

Examples

This example is for Arduino Reada Signal fromA0 to A7

          1. int AIO = A0; // LED connected to digital pin 9

          2. int S0 = 4;

          3. int S1 = 5;

          4. int S2 = 6;

          5. void setup() {

          6. // nothing happens in setup

          7. pinMode(AIO, OUTPUT);

          8. pinMode(S0, OUTPUT);

          9. pinMode(S1, OUTPUT);

          10. pinMode(S2, OUTPUT);

          11. }

          12. int writeSignal (const byte which)

          13. {

          14. // select correct MUX channel

          15. digitalWrite (S0, (which & 1) ? HIGH : LOW); // low-order bit

          16. digitalWrite (S1, (which & 2) ? HIGH : LOW);

          17. digitalWrite (S2, (which & 4) ? HIGH : LOW); // high-order bit

          18. // now read the sensor

          19. showSignal();

          20. //analogWrite(AIO, 255);

          21. } // end of readSensor

          22. void showSignal() {

          23. // fade in from min to max in increments of 5 points:

              1. // Example of using the 74HC4051 multiplexer/demultiplexer

          24. for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 25) {

          25. // sets the value (range from 0 to 255):

              1. // Author: Nick Gammon

          1. analogWrite(AIO, fadeValue);

              1. // Date: 14 March 2013

          1. // wait for 30 milliseconds to see the dimming effect

          2. delay(30);

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

          1. }

              1. // the multiplexer address select lines (A/B/C)

          1. // fade out from max to min in increments of 5 points:

              1. const byte addressA = 6; // low-order bit

          1. for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 25) {

              1. const byte addressB = 5;

          1. // sets the value (range from 0 to 255):

              1. const byte addressC = 4; // high-order bit

          1. analogWrite(AIO, fadeValue);

          2. // wait for 30 milliseconds to see the dimming effect

              1. void setup ()

          1. delay(30);

              1. {

          1. }

              1. Serial.begin (115200);

          1. }

              1. Serial.println ("Starting multiplexer test ...");

              2. pinMode (addressA, OUTPUT);

          1. void loop ()

              1. pinMode (addressB, OUTPUT);

          1. {

              1. pinMode (addressC, OUTPUT);

          1. // show all 8 sensor readings

              1. } // end of setup

          1. for (byte i = 0; i < 8; i++)

          2. {

              1. int readSensor (const byte which)

          1. Serial.print ("Sensor ");

              1. {

          1. Serial.print (i);

              1. // select correct MUX channel

          1. Serial.print (" Show Signal: ");

              1. digitalWrite (addressA, (which & 1) ? HIGH : LOW); // low-order bit

          1. writeSignal(i);

              1. digitalWrite (addressB, (which & 2) ? HIGH : LOW);

          1. }

              1. digitalWrite (addressC, (which & 4) ? HIGH : LOW); // high-order bit

          1. delay (200);

              1. // now read the sensor

          1. } // end of loop

              1. return analogRead (sensor);

              2. } // end of readSensor

              3. void loop ()

              4. {

              5. // show all 8 sensor readings

              6. for (byte i = 0; i < 7; i++)

              7. {

              8. Serial.print ("Sensor ");

              9. Serial.print (i);

              10. Serial.print (" reads: ");

              11. Serial.println (readSensor (i));

              12. }

              13. delay (1000);

              14. } // end of loop