018 - MicroPython TechNotes: Button | Reading an Input
Introduction
In this article, we will learn on how to read an input device with ESP32 using MicroPython programming language. GorillaCell ESP32 development kit comes with 4 pieces of Button modules with different colors of button caps.
Pinout
- G – for the ground pin.
- V – for the VCC or supply voltage.
- S – for the signal pin.
Bill Of Materials
- ESP32 development board flashed with MicroPython firmware. If your ESP32 has no MicroPython firmware, be sure to learn it from: https://techtotinker.com/2021/01/22/001-micropython-technotes-get-started-with-micropython/ . The ESP32 will serve as the brain for this experiment.
- ESP32 shield from GorillaCell ESP32 development kit which will extend the ESP32 pins to the pin headers for easy access and easy connection without confusion.
- 3-pin female-female dupont wires which will connects the button modules to the ESP32 shield pin headers.
- Button modules which will serve as an input device.
Hardware Instruction
- First, attach the ESP32 on top of the ESP32 shield and make sure that both USB ports are on the same side.
- Next, attach the dupont wires to the button module by following the color coding which is black wires for the ground, red wires for the VCC, and yellow wires for the signal pin.
- Next, attach the other end of the dupont wires to the ESP32 shield by matching the colors of the dupont wires to the colors of the pin headers which black to black, red to red, and yellow to yellow. For this experiment I choose GPIO 32 as the input signal pin for the Red button, GPIO 33 for the Green button, and GPIO 34 for the Blue button.
- Next, power the ESP32 shield with an external power supply with type-C USB cable and make sure that the power switch is set to ON state.
- Lastly, connect the ESP32 to the computer through the micro-USB cable. Our demo circuit should now be ready. Yehey!
Software Instruction
For the button module, I prepared 3 examples you can copy to Thonny for you to try.
Play with it, modify it according to your needs.
Enjoy learning and happy tinkering.
Video Demonstration
Call To Action
For any concern, write your message in the comment section.
You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.
Thank you and have a good days ahead.
See you,
– George Bantique | tech.to.tinker@gmail.com
Source Code
1. Example # 1, reading an input device:
1# More details can be found in TechToTinker.blogspot.com
2# George Bantique | tech.to.tinker@gmail.com
3
4from machine import Pin
5
6button = Pin(32, Pin.IN)
7led = Pin(2, Pin.OUT)
8
9# # The following lines of code can be tested in the REPL:
10# button.value() # Reads the current value of the button.
11# # Return value is 1 when button is press else,
12# # the value is 0 when button is release or not press.
2. Example # 2, multiple input device:
1# More details can be found in TechToTinker.blogspot.com
2# George Bantique | tech.to.tinker@gmail.com
3
4from machine import Pin
5from time import sleep_ms
6
7red_button = Pin(32, Pin.IN)
8grn_button = Pin(33, Pin.IN)
9blu_button = Pin(34, Pin.IN)
10led = Pin(2, Pin.OUT)
11
12while True:
13 led.value(not led.value())
14
15 if red_button.value()==1:
16 print('Red button is press')
17 if grn_button.value()==1:
18 print('Green button is press')
19 if blu_button.value()==1:
20 print('Blue button is press')
21
22 sleep_ms(200)
3. Example # 3, simple application of button modules:
1# More details can be found in TechToTinker.blogspot.com
2# George Bantique | tech.to.tinker@gmail.com
3
4from machine import Pin
5from time import sleep_ms
6
7red_button = Pin(32, Pin.IN)
8grn_button = Pin(33, Pin.IN)
9blu_button = Pin(34, Pin.IN)
10led = Pin(2, Pin.OUT)
11
12DEFAULT_COUNTER_VALUE = 0
13COUNTER_CHANGE = 1
14counter_value = DEFAULT_COUNTER_VALUE
15print('counter_value is currently {}.'.format(counter_value))
16
17while True:
18 led.value(not led.value())
19
20 if red_button.value()==1:
21 print('Red button is press:')
22 counter_value = counter_value + COUNTER_CHANGE
23 print('> counter_value incremented to {}.'.format(counter_value))
24 if grn_button.value()==1:
25 print('Green button is press:')
26 counter_value = DEFAULT_COUNTER_VALUE
27 print('> counter_value resetted to {}.'.format(counter_value))
28 if blu_button.value()==1:
29 print('Blue button is press:')
30 counter_value = counter_value - COUNTER_CHANGE
31 print('> counter_value decremented to {}.'.format(counter_value))
32
33 sleep_ms(200)
References And Credits
- Purchase your Gorillacell ESP32 development kit at: gorillacell.kr
Posts in this series
- 049 - MicroPython TechNotes: MP3 Player
- 048 - MicroPython TechNotes: Analog Touch Sensor
- 047 - MicroPython TechNotes: E108 GPS
- 046 - MicroPython TechNotes: RF433 Transceivers
- 045 - MicroPython TechNotes: Infrared Transmitter
- 044 - MicroPython TechNotes: Infrared Receiver
- 043 - MicroPython TechNotes: ESP12E WiFi | External WiFi module
- 042 - MicroPython TechNotes: JDY-32 | Bluetooth Low Energy BLE
- 041 - MicroPython TechNotes: Bluetooth HC-06
- 040 - MicroPython TechNotes: Relay
- 039 - MicroPython TechNotes: Electromagnet
- 038 - MicroPython TechNotes: Buzzer
- 037 - MicroPython TechNotes: Servo Motor
- 036 - MicroPython TechNotes: Stepper Motor
- 035 - MicroPython TechNotes: Dual Motor Driver
- 034 - MicroPython TechNotes: DC Motors | Gear Motor and Fan Motor
- 033 - MicroPython TechNotes: TCS34725 RGB Color Sensor
- 032 - MicroPython TechNotes: BMP280 Sensor
- 031 - MicroPython TechNotes: TOF Distance Sensor
- 030 - MicroPython TechNotes: DS3231 RTC
- 029 - MicroPython TechNotes: HC-SR04 Ultrasonic Sensor
- 028 - MicroPython TechNotes: DHT11 Temperature and Humidity Sensor
- 027 - MicroPython TechNotes: Rotary Encoder
- 026 - MicroPython TechNotes: Light Dependent Resistor (LDR)
- 025 - MicroPython TechNotes: Joystick
- 024 - MicroPython TechNotes: Slider Switch
- 023 - MicroPython TechNotes: Continuous Rotation Potentiometer
- 022 - MicroPython TechNotes: Potentiometer | Reading an Analog Input
- 021 - MicroPython TechNotes: Color Touch Sensor
- 020 - MicroPython TechNotes: Touch Sensor
- 019 - MicroPython TechNotes: Switch Module
- 017 - MicroPython TechNotes: LASER Module
- 016 - MicroPython TechNotes: RGB LED Matrix
- 015 - MicroPython TechNotes: Neopixel 16
- 014 - MicroPython TechNotes: 8x8 Dot Matrix Display (I2C)
- 013 - MicroPython TechNotes: 8x16 Dot Matrix Display (SPI)
- 012 - MicroPython TechNotes: 8x8 Dot Matrix Display (SPI)
- 011 - MicroPython TechNotes: 1.3 OLED Display
- 010 - MicroPython TechNotes: 0.96 OLED Display
- 009 - MicroPython TechNotes: 7 Segment Display
- 008 - MicroPython TechNotes: 16x2 LCD
- 007 - MicroPython TechNotes: RGB LED
- 006 - MicroPython TechNotes: Traffic Light LED Module
- 005 - MicroPython TechNotes: Gorilla Cell LED | MicroPython Hello World
- 004 - MicroPython TechNotes: Gorilla Cell I/O Devices
- 003 - MicroPython TechNotes: Gorillacell ESP32 Shield
- 002 - MicroPython TechNotes: Introduction for Gorillacell ESP32 Dev Kit
- 001 - MicroPython TechNotes: Get Started with MicroPython
- 000 - MicroPython TechNotes: Unboxing Gorillacell ESP32 Development Kit
No comments yet!