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
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:
// Example of using the 74HC4051 multiplexer/demultiplexer
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 25) {
// sets the value (range from 0 to 255):
// Author: Nick Gammon
analogWrite(AIO, fadeValue);
// Date: 14 March 2013
// wait for 30 milliseconds to see the dimming effect
delay(30);
const byte sensor = A0; // where the multiplexer in/out port is connected
}
// the multiplexer address select lines (A/B/C)
// fade out from max to min in increments of 5 points:
const byte addressA = 6; // low-order bit
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 25) {
const byte addressB = 5;
// sets the value (range from 0 to 255):
const byte addressC = 4; // high-order bit
analogWrite(AIO, fadeValue);
// wait for 30 milliseconds to see the dimming effect
void setup ()
delay(30);
{
}
Serial.begin (115200);
}
Serial.println ("Starting multiplexer test ...");
pinMode (addressA, OUTPUT);
void loop ()
pinMode (addressB, OUTPUT);
{
pinMode (addressC, OUTPUT);
// show all 8 sensor readings
} // end of setup
for (byte i = 0; i < 8; i++)
{
int readSensor (const byte which)
Serial.print ("Sensor ");
{
Serial.print (i);
// select correct MUX channel
Serial.print (" Show Signal: ");
digitalWrite (addressA, (which & 1) ? HIGH : LOW); // low-order bit
writeSignal(i);
digitalWrite (addressB, (which & 2) ? HIGH : LOW);
}
digitalWrite (addressC, (which & 4) ? HIGH : LOW); // high-order bit
delay (200);
// now read the sensor
} // end of loop
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