Using pins in Olimex ESP32-EVB board

Started by kordhrufan, September 24, 2020, 04:55:54 PM

Previous topic - Next topic

kordhrufan

Hello I recently adquire an Olimex Board and want to attach some interruptors and connectors for a major project, this is what I have:


 
As you can see I weld two connectos to the GPIO 40 pin connector with all ESP32 ports and connected an interruptor



I want to test the interruptor so I connect the 37 pin and the 5 pin, but the problem I have is that doesn't seem to detect when I press the interrupt, I tested other pin and leds and the board acts strange, this is my code

#include <Arduino.h>

const int timeThreshold = 150;
volatile int ISRCounter = 0;
int counter = 0;
long startTime = 0;
 
int ledPin = 32;  // LED connected to digital pin 13
int inPin = 5;    // pushbutton connected to digital pin 7
int val = 0;      // variable to sto   the read value

struct Button {
  const uint8_t PIN;
  uint32_t numberKeyPresses;
  bool pressed;
};

Button button1 = {5, 0, false};

void IRAM_ATTR isr() {
  button1.numberKeyPresses += 1;
  button1.pressed = true;
}

void debounceCount()
{
if (millis() - startTime > timeThreshold)
{
ISRCounter++;
startTime = millis();
}
}

void setup() {
  Serial.begin(115200);
  pinMode(button1.PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(inPin), debounceCount, FALLING);
  pinMode(ledPin, OUTPUT);  // sets the digital pin 13 as output
  pinMode(inPin, INPUT);    // sets the digital pin 7 as input
  attachInterrupt(button1.PIN, isr, FALLING);
  Serial.printf("Presione un botón");
}

void loop() {
  if (button1.pressed) {
      Serial.printf("Button 1 has been pressed %u times\n", button1.numberKeyPresses);
      button1.pressed = false;
      val = digitalRead(inPin);   // read the input pin
      digitalWrite(ledPin, val);  // sets the LED to the button's value
  }

  //Detach Interrupt after 1 Minute
  static uint32_t lastMillis = 0;
  if (millis() - lastMillis > 60000) {
    lastMillis = millis();
    detachInterrupt(button1.PIN);
     Serial.println("Interrupt Detached!");
  }

  if (counter != ISRCounter)
{
counter = ISRCounter;
Serial.println(counter);
}
}

What I'm doing wrong?

Stanimir5F

Hello kordhrufan!

From the photos you have posted I can see that you have connected button wires to pins 5 and 38 (not 37) on the extension. Pins 37 and 38 pins are both 3.3V so this shouldn't make any difference. But pin 5 on the extension is actually GPIO4 (the first pin is GPIO0 and that's why this offset exist), while GPIO5 (the one you have initialized in code) is extension pin 6.

Also this these three comments confuse me a little bit:
int inPin = 5;    // pushbutton connected to digital pin 7
int ledPin = 32;  // LED connected to digital pin 13
....
pinMode(inPin, INPUT);    // sets the digital pin 7 as input
Obviously comments are comments and they don't affect the code itself but is your idea to attach it (the button) to GPIO 5 or 7? And similar for the LED should it be on 32 or 13? Are these comments just a leftover from before?

As for the code section itself it seems fine at first glance. And if this change of pin doesn't solve the problem I would recommend you to start with a simplified version of the sketch until you sort out the issue. You can check the example Arduino provide about interrupt service routine (ISR).

Stan, Olimex
May the Source be with You!

kordhrufan

Thanks for the reply Stanimir, I forgot to update the comments so you can ignore them, I realized that you have to read the pins following the GPIO and not the physical pin.

Right now I want to make a simple counter additon for each time I activate the interruptor, making the led of the REL1 blink. But I encounter some problems in the making of the led blink.

Also I make some mistakes in the making of the code since I call the attachInterrupt function two times and configure 2 inputs in pinMode, so I fixed that and this is the code I have:

#include <Arduino.h>

const int timeThreshold = 150;
int ledPin = 32;
const int intPin = 4;
volatile int ISRCounter = 0;
int counter = 0;
long startTime = 0;
int val = 0;   
 

struct Button {
  const uint8_t PIN;
  uint32_t numberKeyPresses;
  bool pressed;
};

Button button1 = {intPin, 0, false};

void IRAM_ATTR isr() {
  button1.numberKeyPresses += 1;
  button1.pressed = true;

}

void debounceCount()
{
   if (millis() - startTime > timeThreshold)
   {
      ISRCounter++;
      startTime = millis();
   }
}

void setup() {
  Serial.begin(115200);
  pinMode(button1.PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(intPin), debounceCount, FALLING);
  pinMode(ledPin, OUTPUT);
//  pinMode(intPin, INPUT);   
}

void loop() {
  if (button1.pressed) {
      Serial.printf("Button 1 has been pressed %u times\n", button1.numberKeyPresses);
      button1.pressed = false;
  }

  if (counter != ISRCounter)
   {
      counter = ISRCounter;
      Serial.println(counter);
      val = digitalRead(intPin);   // read the input pin
      digitalWrite(ledPin, intPin);  // sets the LED to the button's value
   }
}

I appreciate all the help you could give me