ESP32 用CD74HC4067使用一路ADC 采集16路模拟信号


https://www.jianshu.com/p/1f82f99e55c2

 这个芯片的使用方法非常简单,例如: S0-S3 分别是 0 0 0 0时,SIG就和 C0是导通的。

CD74HC4067 作用是选通一路对十六路模拟信号,就像一个单刀多掷开关,根据芯片上 S0-S3 四个不同管脚的组合,让SIG管脚和C0-C15导通(每次只能连接一个),它适用于数字和模拟信号,可以只用5针最多连接16传感器系统,也可以用它来管理多个设备。

 

CD74HC4067接口板就像一个单刀多掷开关 -SIG引脚通过地址选择引脚(S0-S3)与CH0~CH15之一进行连通。它适用于数字和模拟信号。我们只需连接4个数字输出芯片的地址选择引脚(S0-S3),并发送欲连接的通道地址(每次只能连接一个)。这使我们可以连接最多只用5针16传感器系统,
也可以用它来管理多个设备。例如,可以用它来连接16台设备的

 

//测量换算每个Pin的电压
int s0 = 34;
int s1 = 35;
int s2 = 32;
int s3 = 33;
int SIG_pin = 25;
void setup() {
  // put your setup code here, to run once:
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);

  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  Serial.begin(115200);

}

void loop() {
  // put your main code here, to run repeatedly:
  int v;
  for (int i = 0; i < 16; i ++)
  {
    Serial.print("Value at channel ");
    Serial.print(i);
    Serial.print(" is : ");
    v = readMux(i);
    Serial.println(v * 5.0 / 4096);
  }
  Serial.println(" ");
  delay(3000);

}
int readMux(int channel)
{
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4] =
  {
    {0, 0, 0, 0}, //channel 0
    {1, 0, 0, 0}, //channel 1
    {0, 1, 0, 0}, //channel 2
    {1, 1, 0, 0}, //channel 3
    {0, 0, 1, 0}, //channel 4
    {1, 0, 1, 0}, //channel 5
    {0, 1, 1, 0}, //channel 6
    {1, 1, 1, 0}, //channel 7
    {0, 0, 0, 1}, //channel 8
    {1, 0, 0, 1}, //channel 9
    {0, 1, 0, 1}, //channel 10
    {1, 1, 0, 1}, //channel 11
    {0, 0, 1, 1}, //channel 12
    {1, 0, 1, 1}, //channel 13
    {0, 1, 1, 1}, //channel 14
    {1, 1, 1, 1} //channel 15
  };

  //loop through the 4 sig
  for (int i = 0; i < 4; i ++)
  {
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  return val;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM