Main Site Documentation

Use of A4 or A5, Di7 as digital pin?


#1

Can we use A4 or A5, Di7 as digital pin?

If I do->


while (true)
{
    InputPort sensor = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di7, true, Port.ResistorMode.Disabled);

    if (sensor.Read())
        Debug.Print("On");
    else
        Debug.Print("Off");

    Thread.Sleep(100);
}

I get ->
#### Exception System.Exception - CLR_E_PIN_UNAVAILABLE (1) ####


#2

Welcome to the forum!

You are doing it wrong. You are creating InputPort on every run of your loop. It works first time, but every consecutive run it will throw the exception if previous instance of the port was not garbage collected. Creating of port in the loop like yours is not effective as well. think of it as a protection mechanism. If one part of your code is using a pin, if you try to use the same pin in other part of the code you’ll get an exception.

Take creation of the port out of the loop:


InputPort sensor = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di7, true, Port.ResistorMode.Disabled);

while (true)
{ 
    if (sensor.Read())
        Debug.Print("On");
    else
        Debug.Print("Off");
 
    Thread.Sleep(100);
}

And yes, you can use A4 or A5 as digital pins as well.


#3

Why not use the examples in the free ebook?


#4

Thank, I was too focused on my work.

I used the eBook, but I copied it wrong, also I had past problem with the serial example which as-is didn’t work.