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

used products:

1. Arduino UNO
2. CD4052BE
3. CD74HC4052E

content:

CD4052BE or CD74HC4052E ux5yIB_GEkA

Arduino:

const int selectPins[2] = {2, 3}; // S0~2, S1~3 const int zInput = A0; // Connect common (Z) to A0 (analog input) void setup() { Serial.begin(9600); for (int i=0; i<2; i++) { pinMode(selectPins[i], OUTPUT); digitalWrite(selectPins[i], LOW); } } void loop() { for (byte pin=0; pin<=3; pin++) { for (int i=2; i>=0; i--) { int stat = pin & (1<<i)?HIGH:LOW; digitalWrite(selectPins[i], stat); } int inputValue = analogRead(zInput); Serial.print(String(inputValue) + "\t"); } Serial.println(); delay(50); }

C:

#include <avr/io.h> #include <util/delay.h> #include <Arduino.h> const int selectPins[3] = {PD2, PD3}; // S0~2, S1~3 const int zInput = PC0; // Connect common (Z) to A0 (analog input) int main(void) { Serial.begin(9600); for (int i=0; i<2; i++) { DDRD |= (1<<selectPins[i]); } setupADC(); while (1) { for (byte pin = 0; pin <= 3; pin++) { for (int i=2; i>=0; 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); }