Skip to content

analogRead on certain pins breaks u8g2 library in 3.0.0 alpha dev (mysterious) #9040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
ForrestFire0 opened this issue Dec 26, 2023 · 8 comments
Closed
1 task done
Assignees

Comments

@ForrestFire0
Copy link

ForrestFire0 commented Dec 26, 2023

Board

LOLIN S2 Mini (ESP32 S2)

Device Description

Hardware is esp32 lolin s2 mini board attached to a cheap typical joystick module and to a oled screen. The pinout is in the code. The individual components work separately (and worked together on previous versions of arduino-esp32 framework) to I don't think it's the wiring, happy to share.

Hardware Configuration

used for u8g2 SPI connection
#define SPI_CS 35
#define SPI_DC 37
#define SPI_RST 33

The joystick axis X (pin 5) and Y (pin 9) is hooked up to the ESP32 S2.

Version

3.0.0 alpha 3

IDE Name

Arduino IDE

Operating System

Windows 11 home

Flash frequency

40 MHz

PSRAM enabled

yes

Upload speed

921600

Description

I am trying to connect my joystick (analogRead single shot) to my u8g2 screen.

Both components work independently.

See the sketch. When pin 5 is read, all good, when pin 9 is read, nothing is printed to the screen.

Sketch

#include <Arduino.h>
#include <U8g2lib.h>

#include <SPI.h>

#define SPI_CS 35
#define SPI_DC 37
#define SPI_RST 33

// Green wire
#define X_PIN 5
// Orange Wire
#define Y_PIN 9

U8G2_SH1106_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, SPI_CS, SPI_DC, SPI_RST); //ESP32S2 board with SPI

void setup(void) {
    u8g2.begin();
}

void loop(void) {
    u8g2.clearBuffer();					
    u8g2.setFont(u8g2_font_ncenB08_tr);
    char buff[100];
    int val = analogRead(5); //5 works, 9 DOES NOT WORK
    sprintf(buff, "val: %d\n", val);
    u8g2.drawStr(0, 10, buff);	
    u8g2.sendBuffer();
}

Debug Message

I set the core debug level to verbose and did not get any output on the serial monitor other than the prints in my sketch. I think I am doing the debug incorrectly or maybe there is an issue with the esp32s2s ports.

Other Steps to Reproduce

Was working on the stable version of arduino-esp32, however I need esp idf 5.1 functionality so need to use the dev version. I don't see any of my needed functionality as stuff that is being messed with.

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@ForrestFire0 ForrestFire0 added the Status: Awaiting triage Issue is waiting for triage label Dec 26, 2023
@ForrestFire0 ForrestFire0 changed the title Analog Read sometimes breaks u8g2 library in 3.0.0 alpha dev Analog Read sometimes breaks u8g2 library in 3.0.0 alpha dev (mysterious) Dec 26, 2023
@ForrestFire0
Copy link
Author

ForrestFire0 commented Dec 26, 2023

UPDATE

Of course it had nothing to do with the function.

analogRead on pin 5 works great, on 9 it breaks the screen.

void loop(void) {
    u8g2.clearBuffer();					
    u8g2.setFont(u8g2_font_ncenB08_tr);
    char buff[100];
    int val = analogRead(5); // WORKS, 9 DOES NOT WORK!!!
    sprintf(buff, "val: %d\n", val);
    u8g2.drawStr(0, 10, buff);	
    u8g2.sendBuffer();
}

Updated the original issue.

@ForrestFire0 ForrestFire0 changed the title Analog Read sometimes breaks u8g2 library in 3.0.0 alpha dev (mysterious) analogRead on certain pins breaks u8g2 library in 3.0.0 alpha dev (mysterious) Dec 26, 2023
@ForrestFire0
Copy link
Author

UPDATE

void setup(void) {
    
    pinMode(3, INPUT);
    pinMode(5, INPUT);
    pinMode(9, INPUT);
    u8g2.begin();
}

The above code works (with analogRead on pin 5)
but if you move the pinmode for 9 after the begin statement, then it does not work.

It seems like the error is with configuring the analog pin for input.

@ForrestFire0
Copy link
Author

ForrestFire0 commented Dec 27, 2023

I found a workaround for my project by using pins 3 and 5 for my joystick.

@SuGlider SuGlider added Type: Question Only question and removed Status: Awaiting triage Issue is waiting for triage labels Jan 3, 2024
@SuGlider
Copy link
Collaborator

SuGlider commented Jan 3, 2024

@ForrestFire0 - I just checked the ESP32-S2 Lolin Mini board about its pin definition.
I found out that GPIO 9 is used as MISO (SPI - Master Input Slave Ouput).
Therefore, given that u8g2 Library uses SPI, this GPIO will be busy as MISO in the SPI function, not available for Analog INPUT.

static const uint8_t SS = 12;
static const uint8_t MOSI = 11;
static const uint8_t MISO = 9;
static const uint8_t SCK = 7;

@SuGlider
Copy link
Collaborator

SuGlider commented Jan 3, 2024

Mysterious solved! Have fun with your project!

@SuGlider SuGlider self-assigned this Jan 3, 2024
@ForrestFire0
Copy link
Author

Wonderful. Purely for my own understanding, why would configuring the pin with pin mode cause the SPI to break? Does it disable the SPI interface?

@SuGlider
Copy link
Collaborator

SuGlider commented Jan 3, 2024

Wonderful. Purely for my own understanding, why would configuring the pin with pin mode cause the SPI to break? Does it disable the SPI interface?

In Arduino 3.0.0 there is a new feature called Peripheral Manager.
In the specific case of SPI, when any of its 4 pins (MOSI, MISO, CLK and CS) are used for any other function, for instance, with pinMode(), the whole SPI is detached. Therefore, SPI stops working, being necessary to restart it with SPI.begin().

I think that your example brings a good questioning because most displays will only care about using MOSI and not MISO.
I could also use CS pin for something else and only set it as CS whenever it is necessary.

Therefore, I would say that we may review it in order to allow using SPI pins partly.

@ForrestFire0
Copy link
Author

I’m happy to close this issue, it seems like at the moment this is an intended (maybe undocumented?) feature. Thanks to everyone doing all this great work, I have been an ESP-Arduino appreciator for a few years now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants