Arduino33 BLE external interrupts

Hi,
I've searched forums, and lots of people seem to have problems with external hardware interrupts when using the Nano33 BLE, but I can't find a 'simple' solution. Member 'ohazi' seems to have a suggestion, but I'm afraid my knowledge isn't up to understanding it.
I'm new to Arduino, done a couple of simple things using Digispark.

At it's most basic, I've tried to get the code from the arduino reference page on attachinterrupt() to work (attachInterrupt() - Arduino Reference)
but it seems to accept one button press and then crash. There is a similar question on this forum, but no solution.
I can't believe that external interrupts aren't possible - has anyone found a solution ?
I don't really want to have to poll the input !

@stevedillon, your topic was movd to the dedicated Nano 33 BLE section.

Please don't expect us to find that suggestion; you can post a link.

Please don't expect us to find that question; you can post a link.

Hi,
The 'ohazi' solution was discussed in thread:

The problem that sounded similar to mine is:

Welcome to the forum.

What external hardware have you connected to the pins? IMHO polling is the better solution for slow signals like switches and buttons.

Hi Klaus_K,
The hardware is from a rotation speed sensor. The issue is that the sensor is from Aliexpress, so the documentation is flakey and the exact timings of the pulse width are not clear (I would ideally be acting on both edges). I would guess that the signal from the sensor is from some sort of hall effect sensor arrangement, in which case I can't see that the pulse width would be less than a few milliseconds so polling may well work. It's just that I can't be sure on how long my code takes to execute.

I did a few test and external interrupts seem to work as expected.
Here is a short example you can use to test this yourself. There is one task creating pulses on one pin and another task printing the number of interrupts on a second pin. Just connect the pins with a wire. You should be able to attach your sensor to the BUTTON_PIN without changing the sketch and get a pulse count.

/*
  This example shows the use of external interrupts.

  The circuit:
  - Arduino Nano 33 BLE and BLE Sense
  - connect BUTTON_PIN and PULSE_PIN using a wire or low value resistor

  This example code is in the public domain.
*/

#define BUTTON_PIN              10
#define PULSE_PIN               2
#define LED_PIN                 LED_BUILTIN

volatile uint32_t counter = 0;

void setup()
{
  Serial.begin( 9600 );
  while ( !Serial );

  pinMode( BUTTON_PIN, INPUT );
  pinMode( PULSE_PIN, OUTPUT );

  attachInterrupt( digitalPinToInterrupt( BUTTON_PIN ), buttonHandler, CHANGE );
}


void loop()
{
  printCounterTask();
  pulseTask();
}


void buttonHandler( void )
{
  counter++;
}


void printCounterTask()
{
  #define PRINT_COUNTER_INTERVAL  1000

  static uint32_t previousMillis = 0;

  uint32_t currentMillis = millis();
  if ( currentMillis - previousMillis < PRINT_COUNTER_INTERVAL )
  {
    return;
  }
  previousMillis = currentMillis;
  Serial.print( "Counter: " );
  Serial.println( counter );
  counter = 0;
}


void pulseTask()
{
  #define PULSE_INTERVAL          80
  static bool state = LOW;

  static uint32_t previousMillis = 0;

  uint32_t currentMillis = micros();
  if ( currentMillis - previousMillis < PULSE_INTERVAL )
  {
    return;
  }
  previousMillis = currentMillis;
  digitalWrite( PULSE_PIN, state );
  state = !state;
}

Thanks Klaus for the reply - unfortunately I can't seem to get the sketch you suggested to work. I've changed tack now to poll my input pin, and have got that part working now.
In going through the forum on another topic (changing PWM frequency), and keep on coming across your helpful posts. I tried another of your test sketches which again I had problems with - this time it wouldn't compile.
My guess is that I have something wrong in my setup, so I reinstalled Arduino IDE to the latest, removed and reinstalled the board manager (interesting that the nRF528x was not listed - I installed the 'Arduino Mbed OS Nano Boards'). Still no joy, the verify falls over at the digitalPinToPinName line.

It's a shame I can't seem to get some of what I assumed would be the simple things to work, the stuff I thought I'd struggle with (the BLE comms) all went smoothly !

#include "mbed.h"

#define PWM_PIN 10
#define PWM_FREQUENCY 31000

mbed::PwmOut pwmPin( digitalPinToPinName( PWM_PIN ) );

void setup()
{
pwmPin.period( 1.0 / PWM_FREQUENCY );
pwmPin.write( 0.5 );

Serial.begin( 9600 );
while ( !Serial );
Serial.println( "mbed PWM pin example" );
}

void loop()
{
static uint32_t dutyCycle = 50;

dutyCycle = ( dutyCycle + 1 ) % 100;
pwmPin.write( dutyCycle / 100.0 );

delay( 10 );
}

Yes, there must be something wrong with your installation because I load and run the Klaus_K test sketch with pin 2 jumpered to pin 10 and see the counts.

#include "mbed.h"

When using the Arduino ide with the os installed from the board manager you should not need this.

I installed the 'Arduino Mbed OS Nano Boards"

What version did you install.

There is something peculiar going on with the Arduino mbed core and the digitalPinToPinName() which I think is defined in pinDefinitions.h

inline PinName digitalPinToPinName(pin_size_t P) {
	return (P >= PINS_COUNT ? NC : g_APinDescription[P].name);
};

This should be the return value.

PinDescription g_APinDescription[] = {
  // D0 - D7
  { P1_3,  NULL, NULL, NULL },     // D0/TX
  { P1_10, NULL, NULL, NULL },     // D1/RX
  { P1_11, NULL, NULL, NULL },     // D2
  { P1_12, NULL, NULL, NULL },     // D3
  { P1_15, NULL, NULL, NULL },     // D4
  { P1_13, NULL, NULL, NULL },     // D5
  { P1_14, NULL, NULL, NULL },     // D6
  { P0_23, NULL, NULL, NULL },     // D7

  // D8 - D13
  { P0_21, NULL, NULL, NULL },     // D8
  { P0_27, NULL, NULL, NULL },     // D9
  { P1_2,  NULL, NULL, NULL },     // D10
  { P1_1,  NULL, NULL, NULL },     // D11/MOSI
  { P1_8,  NULL, NULL, NULL },     // D12/MISO
  { P0_13, NULL, NULL, NULL },     // D13/SCK/LED

  // A0 - A7
  { P0_4,  NULL, NULL, NULL },     // A0
  { P0_5,  NULL, NULL, NULL },     // A1
  { P0_30, NULL, NULL, NULL },     // A2
  { P0_29, NULL, NULL, NULL },     // A3
  { P0_31, NULL, NULL, NULL },     // A4/SDA
  { P0_2,  NULL, NULL, NULL },     // A5/SCL
  { P0_28, NULL, NULL, NULL },     // A6
  { P0_3,  NULL, NULL, NULL },     // A7

  // LEDs
  { P0_24, NULL, NULL, NULL },     // LED R
  { P0_16, NULL, NULL, NULL },     // LED G
  { P0_6,  NULL, NULL, NULL },     // LED B
  { P1_9,  NULL, NULL, NULL },     // LED PWR

  { P0_19, NULL, NULL, NULL },     // INT APDS

  // PDM
  { P0_17, NULL, NULL, NULL },     // PDM PWR
  { P0_26, NULL, NULL, NULL },     // PDM CLK
  { P0_25, NULL, NULL, NULL },     // PDM DIN

  // Internal I2C
  { P0_14, NULL, NULL, NULL },     // SDA2
  { P0_15, NULL, NULL, NULL },     // SCL2

  // Internal I2C
  { P1_0,  NULL, NULL, NULL },     // I2C_PULL
  { P0_22, NULL, NULL, NULL }     // VDD_ENV_ENABLE
};

I don't understand why digitalPinToPinName() appears to be broken, but there is a work around to use the g_ApinDescription nomenclature.

This will compile.

Indeed several of @Klaus_K previous posts will now not compile with the digitalPinToPinName() syntax.

This is probably a bug worth posting at GitHub, but I'd like wait for Klaus_K to comment.

1 Like

It looks like this bug was introduced in the new version of the mbed core. When you use version 1.3.2 compilation works. You can install both at the same time because they split the old package with the Portenta and the Arduino Nano 33 BLE. Then you can switch between them by selecting the board from from "Arduino Mbed OS boards" or "Arduino Mbed OS Nano boards".
In the old mbedOS core version digitalPinToPinName was part of the macros.h file.
There are a few similar macros and only digitalPinToPinName was turned into an inline function. So, this is not just a copy/past or typo error. The author of the code should be able to tell us what might be wrong.

https://github.com/arduino/ArduinoCore-mbed/blob/master/cores/arduino/pinDefinitions.h#L22

Do you have a Github account to post a bug report?

Yes. Would you like me to do it? I will refer to this thread.

However, you clearly have more information and insight into the root cause than I do, so if you would like to post this bug feel free to do so.

Let me know.

Thanks cattledog and Klaus_K, the PWM code compiles fine.
Whilst playing with the code, it looks like there is a minimum PWM frequency.

I created an account and posted an issue. Thanks for your help.

https://github.com/arduino/ArduinoCore-mbed/issues/244

The Arduino guys just updated my issue on Github. The function is now intentionally made private and to use it it you now need to include a header file.

#include "pinDefinitions.h"

I tested it with one example and the code compiled successfully.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.