tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

Arduino Nano 33 IoT floating pin when using interrupts

Open bpoetzschke opened this issue 2 years ago • 0 comments

Hi there,

I was playing around with configuring pins to input mode and came across the GPIO documentation here https://tinygo.org/docs/concepts/peripherals/gpio/.

So I configured my pin as PinInputPullup and afterward, I configured the interrupt. And in the callback of the interrupt, I can see the pin floating. I need to connect an external PullUp resistor to make it work. Interestingly I can also just configure the interrupt without setting any input mode before. So it seems that configuring an interrupt overrides the previous pin config.

However, the documentation of SetInterrupts states I need to configure the pin first.

I attached 3 examples of the behaviors I observed.

Example 1: Floating pin.

func main() {
	waitSerial()

	led.Configure(machine.PinConfig{Mode: machine.PinOutput})
	input.Configure(machine.PinConfig{Mode: machine.PinInputPullup})

	input.SetInterrupt(machine.PinToggle, func(pin machine.Pin) {
		println(fmt.Sprintf("Pin toggled to %t", pin.Get()))
		led.Set(pin.Get())
	})

	for {
		time.Sleep(time.Second)
	}

Output:

21:37:17.094 -> Pin toggled to true
21:37:17.094 -> Pin toggled to false
21:37:17.094 -> Pin toggled to true
21:37:17.094 -> Pin toggled to false
21:37:17.094 -> Pin toggled to true
21:37:17.121 -> Pin toggled to false
21:37:17.121 -> Pin toggled to true
21:37:17.121 -> Pin toggled to false
21:37:17.121 -> Pin toggled to true
21:37:17.158 -> Pin toggled to false
21:37:17.158 -> Pin toggled to true
21:37:17.158 -> Pin toggled to false
21:37:17.158 -> Pin toggled to true
21:37:17.192 -> Pin toggled to false
21:37:17.192 -> Pin toggled to true
21:37:17.192 -> Pin toggled to false
21:37:17.192 -> Pin toggled to true
...

Example 2: Floating pin without configuring it as input at all. Produces the same output.

func main() {
	waitSerial()

	led.Configure(machine.PinConfig{Mode: machine.PinOutput})

	input.SetInterrupt(machine.PinToggle, func(pin machine.Pin) {
		println(fmt.Sprintf("Pin toggled to %t", pin.Get()))
		led.Set(pin.Get())
	})

	for {
		time.Sleep(time.Second)
	}

Example 3: No interrupts and pin configured as PinInputPullup; works as expected

func main() {
	waitSerial()

	led.Configure(machine.PinConfig{Mode: machine.PinOutput})
	input.Configure(machine.PinConfig{Mode: machine.PinInputPullup})

	for {
                println(fmt.Sprintf("Pin toggled to %t", input.Get()))
		time.Sleep(time.Second)
	}

Output for example 3:

21:40:54.231 -> Pin toggled to true
--> Button pressed
21:40:55.233 -> Pin toggled to false
21:40:56.259 -> Pin toggled to false
21:40:57.249 -> Pin toggled to false
21:40:58.254 -> Pin toggled to false
--> Button released
21:40:59.255 -> Pin toggled to true

bpoetzschke avatar Jul 14 '22 01:07 bpoetzschke