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 !
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 !
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.
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.
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.