Nicla Sense ME and digitalRead

Hello
I have a quite stupid problem with digitalRead
I modified example Arduino_BHY2->Standalone by just adding
pinMode(9, INPUT_PULLUP) in setup() function
and
Serial.println(String(digitalRead(9))) in loop() function
I connect the pin to ground, but digitalRead prints "1"
I tried with 10 and 20, but still have issues

Does anyone have suggestions?
Thanks in advance!

Hey there,

In the MBED board folder there is a library that contains the pin mapping:

PinDescription g_APinDescription[] = {

 

  { P0_10, NULL, NULL, NULL },    // 0: GPIO3

  { P0_9, NULL, NULL, NULL },     // 1: GPIO2/RX

  { P0_20, NULL, NULL, NULL },    // 2: GPIO1/TX

  { P0_23, NULL, NULL, NULL },    // 3: SCL1

  { P0_22, NULL, NULL, NULL },    // 4: SDA1

 

 

  { P0_24, NULL, NULL, NULL },    // 5: GPIO0

  { P0_29, NULL, NULL, NULL },    // 6: CS

  { P0_28, NULL, NULL, NULL },    // 7: CIPO

  { P0_27, NULL, NULL, NULL },    // 8: COPI

  { P0_11, NULL, NULL, NULL },    // 9: SCLK

 

  { P0_2, NULL, NULL, NULL },     // 10: A0

  { P0_30, NULL, NULL, NULL },    // 11: A1

 

  { P0_19, NULL, NULL, NULL },    // 12: INT ESLOV

  { P0_18, NULL, NULL, NULL },    // 13: Reset BHI260

  { P0_14, NULL, NULL, NULL },    // 14: INT BHI260

  { P0_25, NULL, NULL, NULL },    // 15: BQ25120 CD

  { P0_26, NULL, NULL, NULL },    // 16: CS FLASH

  { P0_31, NULL, NULL, NULL },    // 17: CS BHI260 
};

In order to check digitalRead on a pin, you need to use the above map with the pinout, available here:

I do not exactly what you want you consider as "pin 9". But if you want to read the pin called "GPIO3" (which you find in the center-right side in the image).
You can use the following 2 methods:

digitalRead(GPIO3);
digitalRead(p10);

In my opinion always use the "pnumber" method.
Good luck!

EDIT: Forgot to mention that not all pins can be used for digital read/write as they are used for other protocols. GPIO1/2 are always used for TX/RX so you cannot access them from my findings. Even if you don't initialise the hardware serial with: Serial1.begin(9600);

2 Likes

Thank you very much for your reply
With GPIO3, the behaviour is slightly different. it is read as 1, when I connect the pin to GND it is read a 0, but then stays 0 even if I disconnect the pin from GND. Do I need an external pull-up resistor?

I've encountered that type of issue.
Try using a pull-up resistor (10k, 4.7k, 1k etc.) and try reading it as:

digitalRead(P0_10);
digitalRead(p10);
digitalRead(GPIO3);

And see which one gets the job done. For me P0_10 has always been the most successful, although I have not played with the digital Read too much. It seems that the board supports proper digitalRead/Write only on GPIO0 and 3, everything else causes issues.

Always remember to use pinMode(pin, INPUT);, otherwise you will have issues.

I would recommend if you can to have all your sensors either I2C or SPI, or if not, on the 2 analog pins (including the buttons). The digital pins are sketchy.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.