- Prolog
- Button (Polling)
- Button (Interrupt Handler)
- Button (Interrupt Handler and Debounce)
- Potentiometer
- Potentiometer and SSD1306 OLED
- Joystick (XY + Button)
- RFID read (RC522 13.56 MHz)
- Matrix Keypad
- Control LED by capacitive touch
Till now, mostly all examples were without any direct human interaction (all was done just by code). This will change now! The following programs will respond to input.
The objective is to turn an LED ON (if LED is OFF) or OFF (if LED is ON) with a button. In addition, the number of times the button was pressed should be counted and displayed.
As soon as you start the program, you will understand that it will not work. Even if you change the value of constant
DELAY_MILLISECONDS
.
- mandatory 1x RGB
- mandatory 1x button
- mandatory 1x resistor (min. 220 ohms)
- few cables
- optional breadboard
# create new subdirectory
$ mkdir -p ~/Projects/ESP/examples/user_input
# create script
$ touch ~/Projects/ESP/examples/user_input/btn_led_polling.py
Source Code for
btn_led_polling.py
Check your circuit and copy the script to the microcontroller as main.py
.
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/user_input/btn_led_polling.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] repl
Start with keys Control
+ d
. Stop the loop with keys Control
+ c
. To leave the REPL, press keys Control
+ x
.
There is also another disadvantage in case of
polling
, it does take a lot of the CPU resources!
The objective is exactly the same as in previous example. Just the code is changed to use isr
(interrupt service routines).
... same as previous example ...
... same as previous example ...
# create script
$ touch ~/Projects/ESP/examples/user_input/btn_led_interrupt_handler.py
Source Code for
btn_led_interrupt_handler.py
Check your circuit and copy the script to the microcontroller as main.py
.
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/user_input/btn_led_interrupt_handler.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] repl
Start with keys Control
+ d
. To leave the REPL, press keys Control
+ x
.
If you observe the output, you see that the counter shows the problem with the button. Don't worry that will be solved in next example.
Again, same objective but with measurement of milliseconds since button was last pressed. If the time difference is under 200 milliseconds, nothing will happen.
... same as first example ...
... same as first example ...
# create script
$ touch ~/Projects/ESP/examples/user_input/btn_led_interrupt_handler_debounce.py
Source Code for
btn_led_interrupt_handler_debounce.py
Check your circuit and copy the script to the microcontroller as main.py
.
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/user_input/btn_led_interrupt_handler_debounce.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] repl
Start with keys Control
+ d
. To leave the REPL, press keys Control
+ x
.
Another solution for debouncing could be, to deactivate the IRQ for few milliseconds (inside function interrupt_handler(pin)). This would save you from the time measurement and calculation but (like any sleep) blocks the program flow.
... btn.irq(handler=None) sleep_ms(200) btn.irq(trigger=Pin.IRQ_FALLING, handler=interrupt_handler) ...Decide for yourself which is the best solution on your current particular problem!
With this passive component you can regulate the resistance (in ohms). On the ESP microcontroller, the ADC (analog-digital converter) then helps to display the value with a little MicroPython.
- mandatory 1x potentiometer (e.g. 10 kilo ohms)
- few cables
- optional breadboard
# create script
$ touch ~/Projects/ESP/examples/user_input/potentiometer.py
Source Code for
potentiometer.py
Check your circuit and copy the script to the microcontroller as main.py
.
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/user_input/potentiometer.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] repl
Start with keys Control
+ d
. Stop the loop with keys Control
+ c
. To leave the REPL, press keys Control
+ x
.
Now the values should not be shown in the REPL but on the OLED display (ssd1306) - as graphics and text.
If you miss the basic information about the OLED (ssd1306), have a look here.
- mandatory 1x potentiometer (e.g. 10 kilo ohms)
- mandatory 1x I2C OLED (example 0.96" 128x64)
- few cables
- optional breadboard
# create script
$ touch ~/Projects/ESP/examples/user_input/potentiometer_display.py
Source Code for
potentiometer_display.py
Check your circuit and copy the script to the microcontroller as main.py
.
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/user_input/potentiometer_display.py /pyboard/main.py
Press reset
button and turn the potentiometer. If you don't have a reset
button, just unplug and reconnect the USB cable.
What would games be without joysticks? This type of user input is very old (I had my first joysticks on the AMIGA500). Actually, most joysticks are two potentiometers for the XY-axes. So here is a very simple example.
Note: If you have a joystick without a button function, you can modify the relevant code.
Important: Even if the joystick can actually be operated with 5V, use 3.3V!
- mandatory 1x analog joystick
- few cables
- optional breadboard
# create script
$ touch ~/Projects/ESP/examples/user_input/joystick.py
Source Code for
joystick.py
Check your circuit and copy the script to the microcontroller as main.py
.
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/user_input/joystick.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] repl
Start with keys Control
+ d
. Stop the loop with keys Control
+ c
. To leave the REPL, press keys Control
+ x
.
With the RFID RC522 module, it is very easy to read and write cards and tags for shortwave (HF, 3-30 MHz). The RFID module itself is based on NXP MFRC-5222 with a frequency of 13.56 MHz and is operated with 3.3V.
- Smart cards (ISO/IEC 15693, ISO/IEC 14443 A, B),
- ISO-non-compliant memory cards (Mifare Classic, iCLASS, Legic, FeliCa ...),
- ISO-compatible microprocessor cards (Desfire EV1, Seos)
- mandatory 1x RFID RC522 device (SPI)
- few cables
- optional breadboard
RC522 | ESP32 |
---|---|
VCC | 3V3 |
RST | 4 |
GND | GND |
MISO | 19 |
MOSI | 23 |
SCK | 18 |
NSS | 5 |
IRQ | - |
# create script
$ touch ~/Projects/ESP/examples/user_input/rfid_reader.py
# download driver module
$ curl -l 'https://raw.githubusercontent.com/cefn/micropython-mfrc522/master/mfrc522.py' -o lib/mfrc522.py
Source Code for
rfid_reader.py
Source Code for module
lib/mfrc522.py
Check your circuit and copy the script to the microcontroller as main.py
.
# copy module to ESP32
(venv) $ rshell -p [SERIAL-PORT] cp lib/mfrc522.py /pyboard/lib/mfrc522.py
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/user_input/rfid_reader.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] repl
Start with keys Control
+ d
. Stop the loop with keys Control
+ c
. To leave the REPL, press keys Control
+ x
.
Keypads are ideal for user inputs. They are very flat, have very low power consumption and are easy to understand. They are available in various designs and are still the best solution in some areas.
- mandatory 1x Matrix keypad (0 - 9, *, #, A - D)
- few cables
- optional breadboard
# create script
$ touch ~/Projects/ESP/examples/user_input/keypad.py
Source Code for
keypad.py
Check your circuit and copy the script to the microcontroller as main.py
.
# copy script as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/user_input/keypad.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] repl
Start with keys Control
+ d
. Stop the loop with keys Control
+ c
. To leave the REPL, press keys Control
+ x
.
The ESP32's capacitive touch allows for unique controls of, for example, LEDs. The usage is very simple. Here is a very simple example.
- mandatory 1x RGB
- mandatory 1x resistor (min. 220 ohms)
- few cables
- optional breadboard
The dotted line is simply a male cable, which you can touch by hands.
# create script
$ touch ~/Projects/ESP/examples/user_input/capacitive_touch.py
Source Code for
capacitive_touch.py
Check your circuit and copy the script to the microcontroller as main.py
.
# copy script as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/user_input/capacitive_touch.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] repl
You can touch the cable without having fear to get an electroshock!
Start with keys Control
+ d
. Stop the loop with keys Control
+ c
. To leave the REPL, press keys Control
+ x
.