ESP32 with a tons of switch

Yosuke
Posts: 8
Joined: Sat Oct 06, 2018 6:59 am

ESP32 with a tons of switch

Postby Yosuke » Sat Oct 06, 2018 8:59 am

I'm trying to build key matrix circuit by ESP32 and 74HC138, but it doesn't work.
The circuit is below.
IMG_20181006_171559.jpg
IMG_20181006_171559.jpg (2.38 MiB) Viewed 12117 times
IMG_20181006_171658.jpg
IMG_20181006_171658.jpg (2.63 MiB) Viewed 12117 times
When I push one of the switches, I want to get their own number(0~7).
This circuit has two problems.

1. Even if I properly connected 74HC138 with the switches, I pushed #0, #4 and #6 switch and I got wrong number(4, 6 and 0).
2. When I pushed #3 switch, I got 1 and 3 one after the other.

When I pushed #0 switch, I got 4. ←wrong
When I pushed #1 switch, I got 1. ←right
When I pushed #2 switch, I got 2. ←right
When I pushed #3 switch, I got 1 or 3. ←wrong
When I pushed #4 switch, I got 6. ←right
When I pushed #5 switch, I got 5. ←wrong
When I pushed #6 switch, I got 0. ←wrong
When I pushed #7 switch, I got 7. ←right

Note that I properly connected 74HC138 with the switches and I could build key matrix circuit by Arduino UNO 3 in the same way.

My code is below.

Code: Select all

//Mux control pins
int s0 = 12;
int s1 = 13;
int s2 = 14;
int controlPin[] = {s0, s1, s2};

int muxChannel[8][3] = {
  {0, 0, 0}, //channel 0
  {1, 0, 0}, //channel 1
  {0, 1, 0}, //channel 2
  {1, 1, 0}, //channel 3
  {0, 0, 1}, //channel 4
  {1, 0, 1}, //channel 5
  {0, 1, 1}, //channel 6
  {1, 1, 1}, //channel 7
};

int loop_0_7 = 0;

void setup() {
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);

  pinMode(16, INPUT_PULLUP);
  
  Serial.begin(115200);

}

void loop() {
  
  for (int i = 0; i < 3; i ++) {
    if(muxChannel[loop_0_7][i]) {
      if(i==0) GPIO.out_w1ts = ((uint32_t)1 << 12);
      else if(i==1) GPIO.out_w1ts = ((uint32_t)1 << 13);
      else if(i==2) GPIO.out_w1ts = ((uint32_t)1 << 14);
    }else{
      if(i==0) GPIO.out_w1tc = ((uint32_t)1 << 12);
      else if(i==1) GPIO.out_w1tc = ((uint32_t)1 << 13);
      else if(i==2) GPIO.out_w1tc = ((uint32_t)1 << 14);
    }
  }
  byte res = (GPIO.in >> 16) & 0x1;
  
  if(!res){Serial.println(loop_0_7);delay(100);}
  
  if(loop_0_7==7) loop_0_7=0;
  else loop_0_7++;
  
}
I don't have any idea.
Could you give some advice please?

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 with a tons of switch

Postby ESP_Sprite » Sun Oct 07, 2018 2:10 am

If anything, I'd put some delay between setting the mux outputs and reading the input: there's probably some propagation delay and capacitance you may need to wait for.

Yosuke
Posts: 8
Joined: Sat Oct 06, 2018 6:59 am

Re: ESP32 with a tons of switch

Postby Yosuke » Sun Oct 07, 2018 4:41 am

Hi, ESP_Sprite.
Thank you for advicing me.
Here is my code(Version 2).

Code: Select all

//Mux control pins
int s0 = 12;
int s1 = 13;
int s2 = 14;
int controlPin[] = {s0, s1, s2};

int muxChannel[8][3] = {
  {0, 0, 0}, //channel 0
  {1, 0, 0}, //channel 1
  {0, 1, 0}, //channel 2
  {1, 1, 0}, //channel 3
  {0, 0, 1}, //channel 4
  {1, 0, 1}, //channel 5
  {0, 1, 1}, //channel 6
  {1, 1, 1}, //channel 7
};

int loop_0_7 = 0;

void setup() {
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);

  pinMode(16, INPUT_PULLUP);
  
  Serial.begin(115200);

}

void loop() {
  
  for (int i = 0; i < 3; i ++) {
    if(muxChannel[loop_0_7][i]) {
      if(i==0) GPIO.out_w1ts = ((uint32_t)1 << 12);
      else if(i==1) GPIO.out_w1ts = ((uint32_t)1 << 13);
      else if(i==2) GPIO.out_w1ts = ((uint32_t)1 << 14);
    }else{
      if(i==0) GPIO.out_w1tc = ((uint32_t)1 << 12);
      else if(i==1) GPIO.out_w1tc = ((uint32_t)1 << 13);
      else if(i==2) GPIO.out_w1tc = ((uint32_t)1 << 14);
    }
  }
 delay(500);
 byte res = (GPIO.in >> 16) & 0x1;
  
  if(!res){Serial.println(loop_0_7);delay(100);}
  
  if(loop_0_7==7) loop_0_7=0;
  else loop_0_7++;
  
}
Then I make it better thanks to your advice.

When I pressed #0 switch, I got 0. ←right
When I pressed #1 switch, I got 1. ←right
When I pressed #2 switch, I got 2. ←right
When I pressed #3 switch, I got 3. ←right
When I pressed #4 switch, I got no response. ←wrong
When I pressed #5 switch, I got 5. ←right
When I pressed #6 switch, I got 6 or 4. ←wrong
When I pressed #7 switch, I got 7. ←right


I pressed #4 switch over and over but it didn't respond. And #6 switch didn't work yet.
hmmm...... :|

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: ESP32 with a tons of switch

Postby tommeyers » Sun Oct 07, 2018 1:17 pm

My first thought is key bounce.

Can you post a schematic?

Tom Meyers
IT Professional, Maker
Santiago, Dominican Republic

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: ESP32 with a tons of switch

Postby tommeyers » Sun Oct 07, 2018 2:24 pm

My second thought is pull-ups.

Do the mux inputs need a pull-up? pin<-->resistor<-->+ and pin<--> switch <--> gnd

So, maybe 2 issues no-debounce and no-pull-ups. That will give you strange output on mux.

In addition, your code is too complicated; clear, get bit (de-bounced), or, shift, get bit (de-bounced), or, shift, get bit (de-bounced),or --> table lookup/1 dimensional table.

I looked for some code examples; sorry could not find any.

Tom Meyers
IT Professional, Maker
Santiago, Dominican Republic

Yosuke
Posts: 8
Joined: Sat Oct 06, 2018 6:59 am

Re: ESP32 with a tons of switch

Postby Yosuke » Mon Oct 08, 2018 2:35 pm

Hello, Tom Meyers.
Thanks for taking the time to reply.
It took a long time to edit schematic. But I managed to do it.
Here is my schematic.
WS000000.JPG
WS000000.JPG (43.31 KiB) Viewed 11978 times
My key matrix circuit doesn't work yet.
So, please tell me if you know something about this.

Yosuke

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: ESP32 with a tons of switch

Postby tommeyers » Tue Oct 09, 2018 4:27 pm

First, put pull-ups on the buttons. That will assure that the value will be either Low or High for Up or Down.

Second, observe bounce with a simple program that reads the inputs loop: (read, display 'U' or 'D') what you should see is:UUUUUUUDUDUDUDDDDDDDDDDDDDDDDDDD. In the transition from U to D the connection bounces: UDUDUD...

Finally: You need to program for bounce. The code is simple and many examples are available. Basically, when you detect a transition like UD you wait for the bounce to stop and then use the value ddddduduuduududUUUUUU -> U; ddddddududuuddddd - D; UUUUUUUUUUUududududuDDDDDDDDDDD - D; UUUUUUududududuUUUUUUU -> U; Take a look at someones debounce code to see how.

Tom Meyers
IT Professional, Maker
Santiago, Dominican Republic

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: ESP32 with a tons of switch

Postby tommeyers » Tue Oct 09, 2018 6:33 pm

Holy shit! 74HC138 is for 3 input and 8 outputs. not 8 to 3; 3 to 8!

datasheet: http://www.futurlec.com/74HC/74HC138.shtml

How about a 74HC148 instead?

datasheet: http://www.futurlec.com/74HC/74HC148.shtml

Sorry, I didn't check the datasheet.

Tom Meyers
IT Professional, Maker
Santiago, Dominican Republic

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 with a tons of switch

Postby ESP_Sprite » Wed Oct 10, 2018 2:13 am

I disagree, what Yosuke is doing should work perfectly well.

First of all, the use of an '138 here is unconventional, but should work perfectly fine, given that only one button is pressed at the same time. The ESP32 uses the '138 to make each output low sequentially. Note that Yosuke has turned on the pull-up on GPIO16 in software. This means that when a button is pressed and the line for the button is not selected *or* no button is pressed, GPIO16 is high. Only when the line for the button is selected *and* the button is pressed, the line goes low. When software detects this, it knows which line it has selected, and as such knows which button is pressed.

(Yosuke: you may still want to modify your schematic a little bit. If you press two buttons at the same time, you're essentially shorting your selected low output with an unselected, high output at times. This is Not Good in general. You could work around this by e.g. connecting 1K resistors between the buttons and the '138; that's enough to limit the current, but will still easily over-power the internal pull-up, which is 50K or so. Even better: use diodes instead of resistors so current can only run from the buttons into the '138; that way your setup can actually detect all the buttons pressed as well.)

For the subject of bounce... well, it's not really an issue here. Bounce is only an issue if you have edge-triggered logic, with either a direct connection or a scan rate faster than the bounce time of the buttons (say, 100ms). Both are not true here; the reads happen with an 100ms delay, which is enough time for a button to finish bouncing, and the output is a simple print of the state; nothing that triggers on an edge.

Yosuke: I'm not quite sure why you get the results you do... the only thing that springs to mind immediately is that your breadboard setup has a fairly big parasitic capacitance, and the pull-up in GPIO16 may not be enough to overpower it. You can try adding an external resistor (anywhere between 1K and 10K should work) from that pin to 3.3V. If that doesn't work: if you have an oscilloscope or logic analyzer, it may be helpful to visualize the signals and see what's happening.

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: ESP32 with a tons of switch

Postby tommeyers » Wed Oct 10, 2018 1:56 pm

esp sprite,thanks for jumping in. I have not used the 138 nor 148.

I am curious about how just 0 button pressed and no buttons pressed will be sorted-out. Do they both return 0? The 3 bit value has 8 possible key values and the switches have 9: none,0,1,2,3,4,5,6,7,8 .

Here is the TI functional diagram and the 8 lines look like output not input: http://www.ti.com/general/docs/datashee ... d=SCLS107F

Do you have a reference/example/app note that I can study?

Thanks, Tom Meyers
IT Professional, Maker
Santiago, Dominican Republic

Who is online

Users browsing this forum: No registered users and 111 guests