ESP32 manipulate port

I try to manipulate the esp32 port to run 74HC4067. However, I only found an example of arduino uno . Could you guy show me how to manipulate the port of esp32 to run 74HC4067 ?

how internet example manipulate the port of uno

void setup()
{
 Serial.begin(9600);
 DDRD = B11111111; // set PORTD (digital 7~0) to outputs
}

void setPin(int outputPin)
// function to select pin on 74HC4067
{
 PORTD = controlPins[outputPin];
}

Full code found on internet:

// 74HC4067 multiplexer demonstration (16 to 1)

// control pins output table in array form
// see truth table on page 2 of TI 74HC4067 data sheet
// connect 74HC4067 S0~S3 to Arduino D7~D4 respectively
// connect 74HC4067 pin 1 to Arduino A0
byte controlPins[] = {B00000000, 
                  B10000000,
                  B01000000,
                  B11000000,
                  B00100000,
                  B10100000,
                  B01100000,
                  B11100000,
                  B00010000,
                  B10010000,
                  B01010000,
                  B11010000,
                  B00110000,
                  B10110000,
                  B01110000,
                  B11110000 }; 

// holds incoming values from 74HC4067                  
byte muxValues[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};

void setup()
{
  Serial.begin(9600);
  DDRD = B11111111; // set PORTD (digital 7~0) to outputs
}

void setPin(int outputPin)
// function to select pin on 74HC4067
{
  PORTD = controlPins[outputPin];
}

void displayData()
// dumps captured data from array to serial monitor
{
  Serial.println();
  Serial.println("Values from multiplexer:");
  Serial.println("========================");
  for (int i = 0; i < 16; i++)
  {
    Serial.print("input I"); 
    Serial.print(i); 
    Serial.print(" = "); 
    Serial.println(muxValues[i]);
  }
  Serial.println("========================");  
}

void loop()
{
  for (int i = 0; i < 16; i++)
  {
    setPin(i); // choose an input pin on the 74HC4067
    muxValues[i]=analogRead(0); // read the vlaue on that pin and store in array
  }

  // display captured data
  displayData();
  delay(2000); 
}```

What is your actual problem? Such a change usually only requires that you specify different pin numbers in the code, since the peripherals are connected to different pins on the board.

You didn't provide any wiring diagram.

This code uses direct port I/O instead of using the high level digitalRead() and digitalWrite(). That will make it a nightmare to port to the ESP. You are best advised to first find a version that uses those instead of port manipulation.

Porting this sketch, as is, to an ESP32 is a job for a master, experienced coder. Even then it would be extremely time consuming.

If you are experienced you could read and understand the code, and then write a high level I/O based version for the ESP32.

What does this code do, actually?

The ESP32 is not an Arduino.

gpio_set_level( GPIO_NUM_4, LOW); // set air particle sensor trigger pin to LOW
1 Like

Yes, it is. (argument sketch begins) :slight_smile:

This thread is a non-starter without a circuit diagram!!

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/gpio.html

2 Likes

I forgot to ask, do you understand how an analog multiplexer works? Have you read the data sheet?

Is this a learning exercise, or will the mux circuit perform some assigned task?

The error I have which is PORTD and DDRD not available for esp32(which is should be because esp32 is not same as Arduino) . I just want to know how i could access I/O directly on ESP32 ? such as any equivalent for PORTD and DDRD in ESP32

I use the mux to increase port ADC1 for esp32. Cause I want to use WIfi and if I turn on wife ADC2 will turn off.

P/S: I need about 10 adc port

Then show the schematic. Also please answer all the other questions.

How quickly do you need to switch analog ports? Analog input reads are not very fast. I really don't believe you need ports for this.

To access the ESP32's GPIO ports directly go to ESP32.com. You may need to install the IDF.

Finding the ports is only the beginning, because of the multifunction pins. The masking code that you will need for that will be a nightmare.

I will check it out.

I understand that. But use digital (low ) and (high ) for 10 port would not good choice also .

Why not? Digital I/O is pretty fast on a 180MHz processor.

Big problem here, you are throwing out statements, and not answering questions. Why does it have to be fast?

Also the number of control pins would not be equal to the number of analog ports (10), as you imply.

Sorry, that sounds really nonsensical. Please explain why, and post a schematic.

I dont have a schematic. I just look for the equivalent of PORTD and DDRD in esp32.

If that is all you want, have fun and good luck with your project.

Personally, I would never begin anything like this without a schematic. At least a diagram on an old envelope.

If you are unfamiliar with the ESP32, you have an extremely steep learning curve to get to what you are asking for.

there is none. I/O are managed differently.

read this GPIO & RTC GPIO - ESP32 - — ESP-IDF Programming Guide latest documentation

https://portal.vidadesilicio.com.br/manipulando-os-registradores-esp32/

1 Like