Switches exist in various incarnations. There are the simple mechanical switches: on/off or push button switches but there are also switches than turn on or off with various external conditions:
and a PIR sensor.
Front side | Back side |
---|---|
![]() |
![]() |
The push button switch is connected to GPIO 22 on the ESP32 board.
Write a script that polls the state of the switch every 100 ms and prints state changes (only print out a message when the switch state has changed). Catch <ctrl> C to smoothly exit the program.
Check https://docs.micropython.org/en/latest/esp8266/tutorial/pins.html and https://docs.micropython.org/en/latest/esp32/quickref.html
for information on how to accomplish this.
Do the same thing as in exercise 1 but use external interrupts and a callback routine to do the job.
Combine this exercise with the exercise on LEDs: Switch the built-in LED on when the button is pressed and off when it is released
Modify exercise 3 such that a single push-and-release will switch the LED on, and a second push-and-release will switch it off again. This type of switches you find e.g. on table lamps. You may find that your program sometimes does not work as expected, which may be due to switch bouncing. In this case please modify your program adding a debounce algorithm to it. You can write a software debouncer, waiting a short while after a switch state change (typically ~ 10 ms) making sure that the switch level has stabilized.
The ESP32 has an interface to read capacitive touch sensors (see https://docs.micropython.org/en/latest/esp32/quickref.html?highlight=touchpad). Write a program to read out the touch sensor and to find out if a wire is touched or not. You may use this again to switch on and off LEDs.
Write a program scanning the PIR sensor, indicating if a person is close. The PIR sensor, just like the push button, uses a single GPIO line as input. The boards allow choosing which GPIO to use, and I selected D1 = GPIO 22
The sensor kit in the lab contains a big number of sensors acting like switches:
I | Attachment | History | Action | Size | Date | Who![]() |
Comment |
---|---|---|---|---|---|---|---|
![]() |
button.png | r1 | manage | 279.6 K | 2020-05-06 - 09:01 | UliRaich | |
![]() |
exercise_3.odt | r1 | manage | 622.1 K | 2020-08-09 - 08:45 | UliRaich | |
![]() |
pir.png | r1 | manage | 319.4 K | 2020-05-06 - 09:01 | UliRaich | |
![]() |
pir_back.png | r2 r1 | manage | 336.6 K | 2022-06-12 - 07:27 | UliRaich |