Skip to content
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

Arduino pin remapping is not considered #148

Open
SinusJayCee opened this issue Nov 15, 2024 · 0 comments
Open

Arduino pin remapping is not considered #148

SinusJayCee opened this issue Nov 15, 2024 · 0 comments

Comments

@SinusJayCee
Copy link

Description

For certain boards (e.g. Arduino Nano ESP32), the Arduino functions (e.g. pinMode()) apply a remapping to match the pin labels printed on the board. However, this remapping is not considered by low-level functions used e.g. by OneWire. This causes the lib to not work properly when used with one of those boards.

For example, the pin labeled with D5 is actually connected to GPIO pin 8. This pin is accessed using Arduino function with 5, e.g. pinMode(5, INPUT) will configure D5 as input pin. But when using low-level functions, the pin needs to be addressed using 8.

This behavior can be disabled by setting "Pin Numbering" to "By GPIO number (legacy)" in the Tools menu in the Arduino IDE or adding -DBOARD_USES_HW_GPIO_NUMBERS to the build flags (e.g. in Platform IO). But by default, it is enabled, and thus OneWire doesn't work correctly.

I suggest to either apply the same pin remapping that is used by the Arduino functions, e.g. by using gpio_pin = digitalPinToPort(arduino_pin); or at least raise a compile time error if remapping is enabled, which can be detected using #if defined(BOARD_HAS_PIN_REMAP) && !defined(BOARD_USES_HW_GPIO_NUMBERS).

References:
https://forum.arduino.cc/t/unable-to-get-ds18b20-to-read-on-nano-esp32/1165044
https://forum.arduino.cc/t/solved-ds18b20-not-working-reliably-on-nano-esp32/1320253

Steps To Reproduce Problem

  1. Connect the data wire of a DS18B20 to the pin D5 of the Nano ESP32, connect power, ground, and pull-up resistor as well.
  2. Upload the first sketch below
  3. Result: doesn't work, because OneWire calls pinMode(8, INPUT) which is remapped to a wrong pin
  4. Upload the second sketch
  5. Result: works correctly
  6. Set "Pin Numbering" to "By GPIO number (legacy)" in Arduino IDE
  7. Upload the first sketch again
  8. Result: works correctly, because pin remapping is disabled

Hardware & Software

Board: Arduino Nano ESP32
Shields / modules used: DS18B20 temperature sensor
Arduino IDE version: 2.3.3
Plattform IO version: 6.1.16
OneWire version: 2.3.8
DallasTemperature version: 3.9.0
Operating system & version: Ubuntu 22.04

Arduino Sketch

Not working variant:

#include <OneWire.h>
#include <DallasTemperature.h>

OneWire oneWire;
DallasTemperature sensors( &oneWire );

void setup() {
  Serial.begin( 9600 );
  oneWire.begin( 8 );   // GPIO pin number of D5
  sensors.begin();
}

void loop() {
  Serial.println( "Number of devices:       " + String( sensors.getDeviceCount() ) );
  Serial.println( "Number of DS18* devices: " + String( sensors.getDS18Count() ) );
  sensors.requestTemperatures(); 
  float temperature = sensors.getTempCByIndex( 0 );
  Serial.println( temperature );
  delay( 5000 );
}

Working variant:

#include <OneWire.h>
#include <DallasTemperature.h>

OneWire oneWire;
DallasTemperature sensors( &oneWire );

void setup() {
  Serial.begin( 9600 );
  pinMode( 5, INPUT );  // Arduino pin number of D5
  oneWire.begin( 8 );   // GPIO pin number of D5
  sensors.begin();
}

void loop() {
  Serial.println( "Number of devices:       " + String( sensors.getDeviceCount() ) );
  Serial.println( "Number of DS18* devices: " + String( sensors.getDS18Count() ) );
  sensors.requestTemperatures(); 
  float temperature = sensors.getTempCByIndex( 0 );
  Serial.println( temperature );
  delay( 5000 );
}

Errors or Incorrect Output

In the not working version, the output is

Number of devices:       0
Number of DS18* devices: 0
-127.00

In the working version, the output is

Number of devices:       1
Number of DS18* devices: 1
21.75
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant