2018/01/15

ESP32 - Working with 74HC4067 (16-channel MUX/DEMUX)

This post is a quick summary of how to work with 74HC4067 (16-channel MUX/DEMUX) using ESP32.

Schematic


Connect ESP32 NodeMCU v1.0 to 74HC4067 breakout board as shown below.

Arduino Sketch

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/******************************************************************************
Source: https://learn.sparkfun.com/tutorials/multiplexer-breakout-hookup-guide

This sketch is modified from the code in the link above to demonstrate how to use 
the 16 Channel (74HC4067) Multiplexer Breakout to drive 16 outputs using
5 digital pins.

74HC4067 Hardware Hookup:
Mux Breakout ----------- ESP32 NodeMCU-V1.0
     S0 ------------------- 26
     S1 ------------------- 25
     S2 ------------------- 33
     S3 ------------------- 32
     Z -------------------- 3V3
    VCC ------------------- 3V3
    GND ------------------- GND
    ~EN ------------------- GND

Note,
analogWrite is not implemented on ESP32. See below link for more detail:
https://github.com/espressif/arduino-esp32/issues/4

A work around example can be found at 
https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/AnalogOut/ledcWrite_RGB/ledcWrite_RGB.ino
******************************************************************************/
/////////////////////
// Pin Definitions //
/////////////////////
const int selectPins[4] = {26, 25, 33, 32}; // S0~26, S1~25, S2~33, S3~32
const int zOutput = 27;                     // Connect common (Z) to 27 (PWM-capable)

void setup() {
  // Set up the select pins, as outputs
  for (int i = 0; i < 4; i++) {
    pinMode(selectPins[i], OUTPUT);
    digitalWrite(selectPins[i], LOW);
  }
  pinMode(zOutput, OUTPUT); // Set up Z as an output
}

void loop() {
  
  // Cycle from pins Y0 to Y15 first
  for (int pin = 0; pin <= 15; pin++) {
    // Set the S0, S1, S2, and S3 pins to select our active output (Y0-Y15):
    
    selectMuxPin(pin);

    digitalWrite(zOutput, HIGH);
    delay(500);
    
  }
  // Now cycle from pins Y14 to Y1
  for (int pin = 14; pin >= 1; pin--) {
    
    selectMuxPin(pin); // Select the pin

    digitalWrite(zOutput, HIGH);
    delay(500);
    
  }
}

// The selectMuxPin function sets the S0, S1, S2, and S3 pins
// accordingly, given a pin from 0-15.
void selectMuxPin(byte pin)
{
  if (pin > 15) return; // Exit if pin is out of scope
  for (int i = 0; i < 4; i++) {
    // 1 byte = 8 bits
    // Assuming pin = 6 -> 00000110
    // 00000110 & 00000001 -> 0, pin 26 = 0
    // 00000110 & 00000010 -> 1, pin 25 = 1
    // 00000110 & 00000100 -> 1, pin 33 = 1
    // 00000110 & 00001000 -> 0, pin 32 = 0
    if (pin & (1<<i))
      digitalWrite(selectPins[i], HIGH);
    else
      digitalWrite(selectPins[i], LOW);
  }
}

The Result


--------------------------------------------------------------------------------------------------------------------------

ESP-IDF

To be implemented...

Reference:

http://tronixstuff.com/2013/08/05/part-review-74hc4067-16-channel-analog-multiplexerdemultiplexer/

http://blog.codebender.cc/2014/01/30/mux74hc4067/

Multiplexer Breakout Hookup Guide - 74HC4051 (8-channel analog multiplexer/demultiplexer)
https://learn.sparkfun.com/tutorials/multiplexer-breakout-hookup-guide

SparkFun Analog/Digital MUX Breakout - CD74HC4067
https://www.sparkfun.com/products/9056

Muxing Around With The CD74HC4067 + Arduino
http://bildr.org/2011/02/cd74hc4067-arduino/

https://github.com/espressif/arduino-esp32/issues/914

74HC4051D, 8-channel analog multiplexer/demultiplexer
https://www.nexperia.com/products/logic/switches-multiplexers-de-multiplexers/analog-switches/74HC4051D.html

--------------------------------------------------------------------------------------------------------------------------
Alternative Part

Remote 8-Bit I/O Expander for I2C Bus
https://www.ti.com/lit/ds/symlink/pcf8574.pdf

No comments:

Post a Comment