CD4051BE CMOS Analog Multiplexers/Demultiplexers with Logic Level Conversion (Analog Input)

used products:

1. Arduino UNO
2. CD4051BE
3. CD74HC4051E
4. HCF4051BE

content:

CD4051BE or CD74HC4051E Çeşme: https://learn.sparkfun.com/tutorials/multiplexer-breakout-hookup-guide http://playground.arduino.cc/Learning/4051 https://i.stack.imgur.com/TuLBX.gif rq6wMBjASFE

Arduino:

/****************************************************************************** Mux_Analog_Input SparkFun Multiplexer Analog Input Example Jim Lindblom @ SparkFun Electronics August 15, 2016 https://github.com/sparkfun/74HC4051_8-Channel_Mux_Breakout This sketch demonstrates how to use the SparkFun Multiplexer Breakout - 8 Channel (74HC4051) to read eight, separate analog inputs, using just a single ADC channel. Hardware Hookup: Mux Breakout ----------- Arduino S0 ------------------- 2 S1 ------------------- 3 S2 ------------------- 4 Z -------------------- A0 VCC ------------------- 5V GND ------------------- GND (VEE should be connected to GND) The multiplexers independent I/O (Y0-Y7) can each be wired up to a potentiometer or any other analog signal-producing component. ******************************************************************************/ ///////////////////// // Pin Definitions // ///////////////////// const int selectPins[3] = {2, 3, 4}; // S0~2, S1~3, S2~4 const int zInput = A0; // Connect common (Z) to A0 (analog input) void setup() { Serial.begin(9600); // Initialize the serial port // Set up the select pins as outputs: for (int i=0; i<3; i++) { pinMode(selectPins[i], OUTPUT); digitalWrite(selectPins[i], HIGH); } // Print the header: Serial.println("Y0\tY1\tY2\tY3\tY4\tY5\tY6\tY7"); Serial.println("---\t---\t---\t---\t---\t---\t---\t---"); } void loop() { // Loop through all eight pins. for (byte pin=0; pin<=7; pin++) { for (int i=0; i<3; i++) { digitalWrite(selectPins[i], pin & (1<<i)?HIGH:LOW); } int inputValue = analogRead(zInput); Serial.print(String(inputValue) + "\t"); } Serial.println(); delay(100); }

C:

#include <avr/io.h> #include <util/delay.h> #include <Arduino.h> const int selectPins[3] = {PD2, PD3, PD4}; // S0~2, S1~3, S2~4 const int zInput = PC0; // Connect common (Z) to A0 (analog input) int main(void) { Serial.begin(9600); for (int i=0; i<3; i++) { DDRD |= (1<<selectPins[i]); } Serial.println("Y0\tY1\tY2\tY3\tY4\tY5\tY6\tY7"); Serial.println("---\t---\t---\t---\t---\t---\t---\t---"); setupADC(); while (1) { for (byte pin = 0; pin <= 7; pin++) { for (int i=0; i<3; i++) { if (pin & (1<<i)) { PORTD |= (1<<selectPins[i]); } else { PORTD &= ~(1<<selectPins[i]); } } int inputValue = readADCValue(zInput); Serial.print(String(inputValue) + "\t"); } Serial.println(); _delay_ms(50); } return 0; } void setupADC() { ADMUX = (1<<REFS0); // For Aref=AVcc; ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); //Rrescalar div factor =128 } uint16_t readADCValue(uint8_t ch){ //Select ADC Channel ch must be 0-7 ADMUX |= ch & 0b00000111; //Start Single conversion ADCSRA |= (1<<ADSC); //Wait for conversion to complete while(!(ADCSRA & (1<<ADIF))); //Clear ADIF by writing one to it //Note you may be wondering why we have write one to clear it //This is standard way of clearing bits in io as said in datasheets. //The code writes '1' but it result in setting bit to '0' !!! ADCSRA|=(1<<ADIF); return(ADC); }