create new tag
view all tags

Solutions to the Exercise 3: Switches

Exercise 1:

This is very similar to switching a LED on or off. Again a single GPIO line is used. This time however the pin in programmed as input and we can add a pullup or pulldown resistor:

_PB_PIN = 22
pushButton = Pin(_PB_PIN,Pin.IN,Pin.PULL_UP)  

Once the pushButton pin is defined we can get its current state with:

state = pushButton.value()


Exercise 2:

The MicroPython documentation includes a chapter on writing interrupt service routines


How to do this with GPIO lines is described in


When using interrupts you may run into problems with bouncing of mechanical switches. The transition is not a single clear cut step function but may flip back and forth. Have a look at http://docs.micropython.org/en/latest/pyboard/tutorial/debounce.html for an explanation on how to eliminate this problem (software debounce algorithm).


Exercise 3:

Nothing much to be said here. Have a look at the solution:


Exercise 4:

There is no difference in programming between a mechanical and a PIR switch. Just the GPIO line on which the switch is connected must be adapted.


-- Uli Raich - 2020-05-06


Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng builtinLED.png r1 manage 17.8 K 2020-05-06 - 09:32 UliRaich  
Texttxt pbIntLED.py.txt r1 manage 0.9 K 2020-06-15 - 09:39 UliRaich  
Texttxt pbInterrupt.py.txt r1 manage 0.8 K 2020-06-15 - 09:39 UliRaich  
Texttxt pbPoll.py.txt r1 manage 1.0 K 2020-06-15 - 09:39 UliRaich  
Texttxt pirPoll.py.txt r2 r1 manage 1.0 K 2021-11-26 - 21:13 UliRaich  
Edit | Attach | Watch | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r4 - 2021-11-26 - UliRaich
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback