Problems with buttons, battery charge, LED

So here is my simple sketch just to make sure everything works as intended. I believe the battery is now charging since the display is on while not connected to any power. I am still not able to get the buttons to do anything nor can i see the battery charge levels to display or the LED, hopefully it is just something in the code.

#include <M5GFX.h>

M5GFX display; // Create a display instance

NessoBattery battery; // ---- Battery ----

void setup() {
  Serial.begin(115200);
  display.begin();
  display.setRotation(1); // Set to landscape mode
  // Configure the built-in LED as an output
  pinMode(LED_BUILTIN, OUTPUT);
  // ----Buttons ----
  pinMode(KEY1, INPUT_PULLUP);
  pinMode(KEY2, INPUT_PULLUP);
  
  // Set text properties
  display.setTextDatum(MC_DATUM); // Middle-Center datum for text alignment
  display.setTextColor(TFT_YELLOW, TFT_BLUE); // White text, black background
  display.setTextSize(2);

  // Clear the screen and draw the string
  display.fillScreen(TFT_BLUE);
  display.drawString("Hello, Nesso N1!", display.width() / 2, display.height() / 2);

  // ---- Battery ----
  battery.enableCharge();
  uint16_t chargeLevel = battery.getChargeLevel();
  Serial.print("Charge Level: ");
  Serial.print(chargeLevel);
  Serial.println(" %");
}

void loop() {
  //---- Buttons -----
  if (digitalRead(KEY1) == LOW) {
    Serial.println("Button 1 pressed!");
    delay(200); // Simple debounce
  }
  if (digitalRead(KEY2) == LOW) {
    Serial.println("Button 2 pressed!");
    delay(200); // Simple debounce

    // Blink the LED
  digitalWrite(LED_BUILTIN, LOW); // Turn LED ON
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH); // Turn LED OFF
  delay(500);
  }
}
"

oops, helps if i include Drawstrings... This one shows me the battery charge but still no buttons clicks

#include <M5GFX.h>

M5GFX display;
NessoBattery battery;

// Button pins on Nesso N1
#define BTN1 KEY1
#define BTN2 KEY2

void setup() {
  Serial.begin(115200);

  display.begin();
  display.setRotation(1);
  display.fillScreen(TFT_BLACK);

  display.setTextSize(2);
  display.setTextDatum(TL_DATUM);
  display.setTextColor(TFT_WHITE, TFT_BLACK);

  pinMode(KEY1, INPUT_PULLUP);
  pinMode(KEY2, INPUT_PULLUP);

  battery.enableCharge();

  display.drawString("Battery & Button Test", 10, 10);
}

void loop() {
  // -------- BATTERY ----------
  static uint32_t lastBattUpdate = 0;
  if (millis() - lastBattUpdate > 1000) {   // update every 1 second
    lastBattUpdate = millis();

    uint16_t level = battery.getChargeLevel();

    // Clear area before redrawing
    display.fillRect(10, 40, 200, 30, TFT_BLACK);

    char buf[32];
    sprintf(buf, "Battery: %d %%", level);
    display.drawString(buf, 10, 40);
  }

  // -------- BUTTONS ----------
  if (digitalRead(KEY1) == LOW) {
    display.fillRect(10, 80, 200, 30, TFT_BLACK);
    display.drawString("BTN1 pressed", 10, 80);
    Serial.println("BTN1 pressed");
    delay(200);
  }

  if (digitalRead(KEY2) == LOW) {
    display.fillRect(10, 120, 200, 30, TFT_BLACK);
    display.drawString("BTN2 pressed", 10, 120);
    Serial.println("BTN2 pressed");
    delay(200);
  }
}

It may not be, post an annotated schematic showing exactly how you wired it. Show all connections, power, ground and power sources. Also post links to technical information on each of the parts.

Hi @Sonicboom ,

You’re not doing anything wrong.

Seems there’s a few things wrong in the esp32 core and m5gfx, it’s being worked on.

Check this thread here..

I did manage to get the buttons working using the m5unified lib but still waiting like you for the batt..

good luck.. ~q

I am able to reproduce the bug. Here is a minimal demonstration sketch:

#include <M5GFX.h>

M5GFX display;

int previousPinState = HIGH;

void setup() {
  Serial.begin(115200);
  pinMode(KEY1, INPUT_PULLUP);
  display.begin();  // This causes `digitalRead(KEY1)` to always return `HIGH`.
}

void loop() {
  int pinState = digitalRead(KEY1);
  if (pinState != previousPinState && pinState == LOW) {
    Serial.println("Button 1 pressed!");
    delay(50);  // Crude debounce
  }
  previousPinState = pinState;
}

The "Button 1 pressed!" message is not printed even though I press the button. The sketch works exactly as expected if I comment out this line:

  display.begin();  // This causes `digitalRead(KEY1)` to always return `HIGH`.

I apologize for any inconvenience this bug has caused you.

The developers are aware of the problem and already working on a fix. For now, the workaround will be to use the bespoke API M5Stack provide via their "M5Unified" library:

https://docs.m5stack.com/en/arduino/arduino_nesso_n1/button

The library is available for installation via the Arduino IDE Library Manager.

I believe this is the equivalent of your sketch rewritten using the "M5Unified" library:

#include <M5Unified.h>

void setup() {
  // See: https://docs.m5stack.com/en/arduino/m5unified/m5unified_appendix#configs
  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);

  // See https://docs.m5stack.com/en/arduino/arduino_nesso_n1/display
  M5.Display.begin();
  M5.Display.setRotation(1);
  M5.Display.fillScreen(TFT_BLACK);
  M5.Display.setTextSize(2);
  M5.Display.setTextDatum(TL_DATUM);
  M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);

  // See: https://docs.m5stack.com/en/arduino/arduino_nesso_n1/power
  M5.Power.setBatteryCharge(true);
  M5.Power.setChargeCurrent(100);
  M5.Power.setChargeVoltage(4200);

  M5.Display.drawString("Battery & Button Test", 10, 10);
}

void loop() {
  // Monitor button presses.
  // See: https://docs.m5stack.com/en/arduino/arduino_nesso_n1/button
  M5.update();

  // -------- BATTERY ----------
  static uint32_t lastBattUpdate = 0;
  if (millis() - lastBattUpdate > 1000) {  // update every 1 second
    lastBattUpdate = millis();

    uint16_t level = M5.Power.getBatteryLevel();

    // Clear area before redrawing
    M5.Display.fillRect(10, 40, 200, 30, TFT_BLACK);

    char buf[32];
    sprintf(buf, "Battery: %d %%", level);
    M5.Display.drawString(buf, 10, 40);
  }

  // -------- BUTTONS ----------
  // See: https://docs.m5stack.com/en/arduino/arduino_nesso_n1/button
  if (M5.BtnA.wasPressed()) {
    M5.Display.fillRect(10, 80, 200, 30, TFT_BLACK);
    M5.Display.drawString("BTN1 pressed", 10, 80);
    Serial.println("BTN1 pressed");
    delay(200);
  }

  if (M5.BtnB.wasPressed()) {
    M5.Display.fillRect(10, 120, 200, 30, TFT_BLACK);
    M5.Display.drawString("BTN2 pressed", 10, 120);
    Serial.println("BTN2 pressed");
    delay(200);
  }
}

hey everyone, just chiming in

while the M5Unified library makes things work, it impedes the i2c bus from working in other libraries because the bus remains on hold (it is actually reset and rearranged multiple times over). This seems to annihilate Wire :smiling_face_with_tear:
We (Arduino and M5 Stack) are working out a solution that remains compatible with Arduino’s implementation of Wire and still lets you use everything in the Nesso N1 and wraps M5GFX nicely letting you use all the APIs to draw to screen.
Our priority is to make sure that hundreds of other peripherals using Wire (any i2c device) continue to work.

keep you posted with progress

Thank you all, I am setting the Nesso aside for now until everything is usable. As it seems, tweaking one thing for it to work impedes or breaks another. I will watch the threads for updates.

thank you for bearing with us, @Sonicboom

we have ran very successful tests with the next iteration of the core.
offloading the IO expander duties to a library to be published next week has resolved the breakage caused by M5’s lgfx::io.
This means that users will be able to use M5 Stack libraries but for display, battery and IO they’ll instantiate from the library.
we wrap M5GFX, hence all the display API remains the same but rather than doing

#include <M5GFX.h>

M5GFX display;

it will be

#include <Arduino_Nesso_N1.h>
NessoDisplay display;

We’ll also update the examples accordingly and provide some examples in the library itself.

Thanks everyone for helping us find bugs and supporting during resolution

u.

hey everyone

glad to inform you that the PR has been merged.

next release will bring all the fixes.
I’ll update when the Arduino_Nesso_N1 library will be published.

thank you for your patience :slight_smile:

Thank you so much!, would this be where VS code shines? I don't see where in the Arduino IDE i can edit the code directly. I did this in the past quite often with Marlin 3D printer firmware.

@Sonicboom if you want to modify the "esp32" boards platform source code, you can open it in any text editor. Since you have experience using PlatformIO, you may already have VS Code installed and find that to be a convenient editor for the purpose.

You can also perform a "manual installation" of the platform (which contains all changes made since the time of the 3.3.4 release) if you like:

https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html#windows-manual-installation

Note that either of these things are very much "at your own risk". Advanced users may find it useful to modify the platform or install the development version, but doing either thing is fairly complex and prone to resulting in problems that are difficult to troubleshoot. So we definitely do not recommend it for any less experienced users who may read this. If you do either, please make sure to take note of the changes you make so that you don't end up in a confusing situation where you later end up forgetting and ending up with unexpected behavior caused by using a non-standard "esp32" platform.

don’t bother..

while you could recompile the core and have the batt and io expander removed..

we’re still waiting for some code in here..

getting closer though..

~q

Thank you kindly, yes i do already have VS code installed and mostly configured with PIO, but mainly for Marlin. I often test changes so yes i would copy in the changes and report.

Based on @qubits-us reponse i will stand by. No rush

last few days as we all wait..

been using my vscode to simulate the nesso screen..

should make gui development a bit easier as compile time is less than a second..

fun stuff.. ~q

ok that does it...lol now i will ping you offline as i need to know which board you selected in PIO... :grin:

i’m compiling to windows here..

not uploading to the board..

can simulate screen drawing and touch screen..

i followed these instructions..

which were rather difficult to follow to say the least..

~q

FYI :slight_smile: