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()

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToTheExerciseOnSwitches/pbPoll.py.txt

Exercise 2:

The MicroPython documentation includes a chapter on writing interrupt service routines

http://docs.micropython.org/en/latest/reference/isr_rules.html

How to do this with GPIO lines is described in

http://docs.micropython.org/en/latest/esp8266/tutorial/pins.html

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).

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToTheExerciseOnSwitches/pbInterrupt.py.txt

Exercise 3:

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

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToTheExerciseOnSwitches/pbIntLED.py.txt

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.

https://iotworkshop.africa/pub/IoT_Course_English/SolutionsToTheExerciseOnSwitches/pirPoll.py.txt

-- %USERSIG{UliRaich - 2020-05-06}%

Comments

 

I Attachment Action Size Date Who Comment
builtinLED.pngpng builtinLED.png manage 17 K 06 May 2020 - 09:32 UnknownUser  
pbIntLED.py.txttxt pbIntLED.py.txt manage 876 bytes 15 Jun 2020 - 09:39 UnknownUser  
pbInterrupt.py.txttxt pbInterrupt.py.txt manage 829 bytes 15 Jun 2020 - 09:39 UnknownUser  
pbPoll.py.txttxt pbPoll.py.txt manage 973 bytes 15 Jun 2020 - 09:39 UnknownUser  
pirPoll.py.txttxt pirPoll.py.txt manage 976 bytes 26 Nov 2021 - 21:13 UnknownUser  
This topic: IoT_Course_English > WebHome > Solutions > SolutionsToTheExerciseOnSwitches
Topic revision: 26 Nov 2021, UliRaich