UNO R4 WiFi disconnecting/reconnecting continuously when running sketch w/ "LED_Matrix" and "ArduinoBLE" libraries

I figured it was a long shot, again assuming that your call the begin method which starts up the fast timer.

Probably one of the main Arduino developers will need to debug this.

But so far, I have not found an easy way to debug what is happening on the Uart data between ESP32 and the main processor.

Will ask more about that on a thread more specific on suggestions on how best to connect logic analyzer to some of the test points on the board.

1 Like

As it now seems to be definitly confirmed that under firmware 0.2.0 the ArduinoUno R4 Wifi's LED Matrix Library (Arduino_LED_Matrix.h) conflicts with the BLE Library (ArduinoBLE.h) causing the BLE connection to disconnect and re-connect continually. Because with firmware 0.1.0 there is no BLE compatiblity (despite it being adverised on the box the Uno R4 Wifi came in from the Arduino store) it is, at the moment, not possible to use the LED Matrix Library with the BLE Library thereby making the Uno R4 Wifi unusable for the project I bought it for.

I was wondering if it would be possible to use an Adafruit LED Matrix library on the Uno R4 Wifi? Or alternativey if simplified instructions could be given as to how to address the matrix without a library.

1 Like

I could be wrong, but I don't believe so. I did not know that Adafruit is now selling some charlieplexed LED displays.
LED Charlieplexed Matrix - 9x16 LEDs - Red : ID 2947 : $5.95 : Adafruit Industries, Unique & fun DIY electronics and kits
A couple of differences. Theirs is 16x9, and they also sell a driver chip, to control them over I2C. And I believe their library is setup for their driver chip.

There are some web pages that describe how Charlieplexing work, like:
What is Charlieplexing, and what can I do with it? - Design Tools and Resources - Electronic Component and Engineering Solution Forum - TechForum │ Digi-Key (digikey.com)

I often look at the interactive schematic viewer from the product page:
UNO R4 WiFi | Arduino Documentation
The top page shows:
image
So you can see how the logical numbers of the matrix from the next page maps to which port pin on the processor. Like 0 -> P003 Port 0 pin 3

They have a second page about the LED matrix.


And I know, that these 11 pins map to pins 28-38.

28 P003
29 P004
30 P011
31 P012
32 P013
33 P015
34 P204
35 P205
36 P206
37 P212
38 P213

However, my wild guess is, that the issue is not that the matrix code is running from a library. But probably by something that the code is doing. I threw out a few darts on possibilities earlier.

For the fun of it, I am experimenting with the LED examples. I have a NANO 33 BLE programed as the LED device and I am running a modified version of the LED_Control on the WIFI. So far I changed from using button, to use Sparkfun QWIIC button, as it was convenient. The two are still talking. Next up will add the matrix library.

Note: I updated the analog.cpp to the one that fixed the heap corruption bug.
Also should mention, running with the updated wifi firmware that fixed the issue about not being able to program the WIFI reliably.

EDIT The modified example appears to work OK with the matrix.
It starts off that each time you press or release the button, it changes which LED in the matrix is lit... If you hold down the button for over 5 seconds (Appears to take longer than that), it does a matrix.begin(). And then each time you press and release it updates the display with the frames from the DisplaySingleFrame sketch. Here is the current version of the sketch. Not sure if it will help or not. Will try a Wi-Fi example next.
BLE_LedControl-230812a.zip (2.0 KB)

Thanks for your time with this. I tried your sketch but I couldn't get it to work, I have the Uno R4 Wifi running your sketch and a Nano 33 BLE Sense runnin the example LED sketch. The monitor stops after these comments:

*** Button did not start ***Bluetooth® Low Energy Central - LED control
Found ba:8e:08:04:a3:4d 'LED' 19b10000-e8f2-537e-4f6c-d104768a1214
Connecting ...
Connected
Discovering attributes ...
Attributes discovered

Pressing the button does nothing on either board. I noticed that in your sketch the line #39 pinMode(buttonPin, INPUT); was commented out. I removed the // but the button still seems to have no effect.

Sorry, I set it up using a Sparkfun Qwiic button, something like:
SparkFun Qwiic Button - Green LED - BOB-16842 - SparkFun Electronics

Mainly because it was easy for me to do as the board has a qwiic connector on it. Should easily be able to convert it back to simple button. I hacked up the main page:

/*
  LED Control

  This example scans for Bluetooth® Low Energy peripherals until one with the advertised service
  "19b10000-e8f2-537e-4f6c-d104768a1214" UUID is found. Once discovered and connected,
  it will remotely control the Bluetooth® Low Energy peripheral's LED, when the button is pressed or released.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
  - Button with pull-up resistor connected to pin 2.

  You can use it with another board that is compatible with this library and the
  Peripherals -> LED example.

  This example code is in the public domain.
*/
//#define USE_SPARKFUN_QWIIC_BUTTON

#include <ArduinoBLE.h>
#ifdef USE_SPARKFUN_QWIIC_BUTTON
#include <SparkFun_Qwiic_Button.h>
QwiicButton button;
#else
const int buttonPin = 2;
#endif
#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;
#include "frames.h"

bool oldButtonState = false;
int matrix_led = -1;
bool matrix_begin_called = false;
uint32_t button_pressed_start_time = 0;

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

  #ifdef USE_SPARKFUN_QWIIC_BUTTON
  Wire1.begin();
  if (!button.begin(0x6F, Wire1)) {
    Serial.print("*** Button did not start ***");
  }
  #else
  // configure the button pin as input
  pinMode(buttonPin, INPUT);
  #endif

  // initialize the Bluetooth® Low Energy hardware
  BLE.begin();

  Serial.println("Bluetooth® Low Energy Central - LED control");

  // start scanning for peripherals
  BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}

void loop() {
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();

  if (peripheral) {
    // discovered a peripheral, print out address, local name, and advertised service
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();

    if (peripheral.localName() != "LED") {
      return;
    }

    // stop scanning
    BLE.stopScan();

    controlLed(peripheral);

    // peripheral disconnected, start scanning again
    BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
  }
}

void controlLed(BLEDevice peripheral) {
  // connect to the peripheral
  Serial.println("Connecting ...");

  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }

  // discover peripheral attributes
  Serial.println("Discovering attributes ...");
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }

  // retrieve the LED characteristic
  BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");

  if (!ledCharacteristic) {
    Serial.println("Peripheral does not have LED characteristic!");
    peripheral.disconnect();
    return;
  } else if (!ledCharacteristic.canWrite()) {
    Serial.println("Peripheral does not have a writable LED characteristic!");
    peripheral.disconnect();
    return;
  }

  while (peripheral.connected()) {
    // while the peripheral is connected

    // read the button pin
  #ifdef USE_SPARKFUN_QWIIC_BUTTON
    bool buttonState = button.isPressed();
  #else  
    bool buttonState = digitalRead(buttonPin) == 0;  // using PU so 0 is pressed
  #endif
    if (oldButtonState != buttonState) {
      // button changed
      oldButtonState = buttonState;

      if (buttonState) {
        Serial.println("button pressed");

        // button is pressed, write 0x01 to turn the LED on
        ledCharacteristic.writeValue((byte)0x01);
        button_pressed_start_time = millis();
      } else {
        Serial.println("button released");

        // button is released, write 0x00 to turn the LED off
        ledCharacteristic.writeValue((byte)0x00);
      }
      if (!matrix_begin_called && ((millis() - button_pressed_start_time) > 5000)) {
        // button pressed more than 5 seconds.
        Serial.println("\n***Call matrix.begin() ***");
        matrix.begin();
        matrix_begin_called = true;
        matrix_led = 0;
      }
      if (matrix_begin_called) {
        matrix_led++;
        if (matrix_led >= (int)(sizeof(frame_list)/ sizeof(frame_list[0]))) matrix_led = 0;
        matrix.loadFrame(frame_list[matrix_led]);
      } else {
        matrix_led = (matrix_led == 95)? 0 : matrix_led + 1;
        matrix.on(matrix_led);
      }      
    }
  }

  Serial.println("Peripheral disconnected");
}

Still need the other tab with the frames...
It compiles but I have not tried it.

It compiles but I have not tried it.

I have, it works! I don't know what this shows the experts but for me it shows that I can use BLE and the Matrix in the same sketch, which is important to my little project.

:smiley:

For what is worth, I also pushed up example, where instead of R4 WIFI running the LedControl sketch, it runs the LED sketch. And have version that does similar to the other test. I tested it then with Nano 33IOT running a very slightly modified version of the LedControl. Only changes I made was to change
pinMode(buttonPin, INPUT_PULLUP);
and changed the check for state:
int buttonState = (digitalRead(buttonPin) == 0);

And it appears to work this way as well.

As I said in a previous comment this sketch works on my Uno R4 WiFi and I haven't updated the analog.cpp to the one that fixed the heap corruption bug.

So looking at your sketch you are using the Arduino_LED_Matrix.h and ArduinoBLE.h which caused my original sketch above to keep disconnecting-connecting. What is it in your sketch that fixes this issue?

Good question. Could be a timing thing, could be a memory corruption issue, ,,,

Hopefully one can start with one that works and keep adding a little and see if it still works, then try next... until done or fails.

I am following your advice but before I can use your code for other applications I would like to alter it so that the button remains in a released state until it is pressed and remains in a pressed state until it is released.

At the moment the code causes the button to alternate between button pressed and button released continually until the button is actually pressed when the monitor reads button pressed and everything stops until the button is released again. How can I stop the runaway pressed/released mode in the released state.

I realise this should be simple but I have been working so hard with this BLE/Matrix issue my brain is beginning to melt :wink:

One other question if I may, would including the two libraries with "" rather than <> make any difference? ie

#include <ArduinoBLE.h>
#include <Arduino_LED_Matrix.h>

Rather than

#include "ArduinoBLE.h"
#include "Arduino_LED_Matrix.h"

This may depend on your button, the wiring of the button and the code.

Short answer: You probably don't have a pull-up resistor, so the signal going into pin 2 is floating, and unpredictable.

Longer version:
If you look at their example LedControl - it says:

  • Button with pull-up resistor connected to pin 2.

And the code shows

  // configure the button pin as input
  pinMode(buttonPin, INPUT);
...
    int buttonState = digitalRead(buttonPin);

    if (oldButtonState != buttonState) {
      // button changed
      oldButtonState = buttonState;

      if (buttonState) {
        Serial.println("button pressed");

A couple of issues with this description and code.

My internal questions about the comment about the button, is:

  1. The other oead to the button is connected to ground
  2. Wondering if they are assuming the switch is NO(Normally open) or NC(Normally closed)? Most of my switches are NO - The connection between the two pins is only made when the switch is pressed. NC the connection is made unless the button is pressed.

Assuming you have a PU resistor and other side connected to GND on a NO switch, then the state test is wrong:

 if (buttonState) {
        Serial.println("button pressed");

As in the unpressed state, the PU will have the signal high, and when pressed it will be low...

Change I would make to he code:
Avoid the need for external Pull up resistor:

pinMode(buttonPin, INPUT_PULLUP);

(I forgot to check in that to my github test project) This causes the pin to be internall pulled high.

I would also change the state of the test for when the button is pressed.

Could be done: like change the test above to: if (!buttonState)

Or what I did on the reading of the button:
`

Short Answer:
When you use the
The compiler will look for the file name in the normal search path directories. Typically used.
for header files contained in the system area and libraries.

When you use the "filename"
Like above, but also looks in the directory of where the code is that included it. Like internal header files used in the core, or in a library. Likewise for header files that you have in your sketch folder.

Answers on the web. The below one has a link also to the GCC documentation.
c++ - What is the difference between #include and #include "filename"? - Stack Overflow
`

:+1:removing the PU resistor and using pinMode(buttonPin, INPUT_PULLUP); fixed the problem.

A few comments ago you said you had also used the Nano as Central and the Uno R4 WiFi as the periheral with the Uno running the LED sketch. But to test the matrix the Uno must be running a modified LED sketch in order for the Matrix to be used.

The modified LED sketch is up in my github project with most all of the test programs:
UNOR4-stuff/test_sketches/UNOR4_WIFI_LED/UNOR4_WIFI_LED.ino at main · KurtE/UNOR4-stuff (github.com)

Again many thanks! I have uploaded you modified LED sketch, UNOR4_WIFI_LED.ino, onto the Uno
R4 but as I couldn't find a modified LED_Control sketch fot the Nano so I used the original with all the matrix stuff removed and the two modifications you suggested. Unfortunately it keeps disconnecting. According to the monitor it disconnects because the Uno as peripheral device doesn't have a LED characteristic yet the LED charateristic on the Uno is set up with
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
and on the Nano with
BLECharacteristic ledCharacteristic = peripheral.characteristic("19B10001-E8F2-537E-4F6C-D104768A1214");

I used the straight LedControl.ino sketch, with two mods:

Changed INPUT to INPUT_PULLUP

And I changed the
int buttonState = digitalRead(buttonPin);
to maybe:
int buttonState = digitalRead(buttonPin) == 0;

Using the example LedControl sketch with those mods on the Nano with the Nano connected to the monitor and the Uno R4 with the sketch UNOR4_WIFI_LED.ino fom your github project causes the Nano to report:

Found dc:54:75:d1:16:e5 'LED' 19b10000-e8f2-537e-4f6c-d104768a1214
Connecting ...
Connected
Discovering attributes ...
Attributes discovered
Peripheral does not have LED characteristic!
Found dc:54:75:d1:16:e5 'LED' 19b10000-e8f2-537e-4f6c-d104768a1214
Connecting ...
Connected
Discovering attributes ...
Attributes discovered
Peripheral does not have LED characteristic!
Found dc:54:75:d1:16:e5 'LED' 19b10000-e8f2-537e-4f6c-d104768a1214
Connecting ...

I have tried using the example LedControl sketch on the Uno and in the nRF Connect app on my phone there is a service available. after loading your UNOR4_WIFI_LED.ino from your github project on the Uno and there is no service available.

It seems likely to me that we may have come against the original problem with the matrix again.

Did you update the analog.cpp file in the core? Mine is updated.

Where can I find the correct analog.cpp file on github?

I have found the analog.cpp file in my Arduino setup under

~/.arduino15/packages/arduino/hardware/renesas_uno/1.0.2/cores/arduino

but haven't found an update on github yet

If I use the example LED sketch on the Uno R44 WiFi there is a service offered on my Android app nRF Connect. if I add the lines .....

#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;

......the service disappears.

So this is the original problem! How you got it to work in the other direction is the big question in my, admittedly limited, opinion.

Perhaps it is the analog.cpp file, but why does your Nano-peripheral and Uno-central sketches work on my devices with an unchanged analog.cpp?

Hard to say, but I simply try to remove as many bad things as possible, just so that I don't end up trying to debug something, that was already fixed.

The analog.cpp fix was causing a heap issue at startup. How issues like that manifest themself can be really fickle. Also I updated the firmware of the ESP32 to fix the issue where you could not download new sketches...

Or maybe there is a race condition that is there that you have to just hit things exactly right to happen.

You can find the updated file on github:
ArduinoCore-renesas/cores/arduino/analog.cpp at main · arduino/ArduinoCore-renesas (github.com)

You can use the download raw file button on the page, and then copy it into the core.

1 Like