1

Тема: Увеличиваем количество аналоговых входов на ИМС 4051

http://forum.rcl-radio.ru/uploads/images/2020/08/f0155ef47ea42f7ab8ba5bc3caa32f20.png


скетч без библиотеки

#define PIN_Y0 2
#define PIN_Y1 3
#define PIN_Y2 4
#define PIN_ANALOG A0


void setup(){
  Serial.begin(9600);
  pinMode(PIN_ANALOG,INPUT); // A0
  pinMode(PIN_Y0,OUTPUT); // Y0
  pinMode(PIN_Y1,OUTPUT); // Y1
  pinMode(PIN_Y2,OUTPUT); // Y2
  }

void loop(){
  Serial.print("ADC_0 = "); Serial.println(hef4051Read(0));
  Serial.print("ADC_1 = "); Serial.println(hef4051Read(1));
  Serial.print("ADC_2 = "); Serial.println(hef4051Read(2));
  Serial.print("ADC_3 = "); Serial.println(hef4051Read(3));
  Serial.print("ADC_4 = "); Serial.println(hef4051Read(4));
  Serial.print("ADC_5 = "); Serial.println(hef4051Read(5));
  Serial.print("ADC_6 = "); Serial.println(hef4051Read(6));
  Serial.print("ADC_7 = "); Serial.println(hef4051Read(7)); 
  Serial.println();
   delay(1000);
  
  }

int hef4051Read(byte adc){
  switch(adc){
    case 0: digitalWrite(PIN_Y2,LOW);digitalWrite(PIN_Y1,LOW);digitalWrite(PIN_Y0,LOW);break;
    case 1: digitalWrite(PIN_Y2,LOW);digitalWrite(PIN_Y1,LOW);digitalWrite(PIN_Y0,HIGH);break;
    case 2: digitalWrite(PIN_Y2,LOW);digitalWrite(PIN_Y1,HIGH);digitalWrite(PIN_Y0,LOW);break;
    case 3: digitalWrite(PIN_Y2,LOW);digitalWrite(PIN_Y1,HIGH);digitalWrite(PIN_Y0,HIGH);break;
    case 4: digitalWrite(PIN_Y2,HIGH);digitalWrite(PIN_Y1,LOW);digitalWrite(PIN_Y0,LOW);break;
    case 5: digitalWrite(PIN_Y2,HIGH);digitalWrite(PIN_Y1,LOW);digitalWrite(PIN_Y0,HIGH);break;
    case 6: digitalWrite(PIN_Y2,HIGH);digitalWrite(PIN_Y1,HIGH);digitalWrite(PIN_Y0,LOW);break;
    case 7: digitalWrite(PIN_Y2,HIGH);digitalWrite(PIN_Y1,HIGH);digitalWrite(PIN_Y0,HIGH);break;
    }
    int value = analogRead(PIN_ANALOG);
  //  delay(1);
    return value;
  }
    

скетч с библиотекой

Данный контент доступен только зарегистрированным пользователям.


#include <HEF4051.h> 

HEF4051 hef(A0,2,3,4);// analog_input, 0,1,2

void setup() {
  Serial.begin(9600);

}

void loop() {
  for(byte i=0;i<8;i++){
  Serial.println("ADC" + String(i) + " = " + String(hef.Read(i)));
  }
  Serial.println();
   delay(1000);
}