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

  1. G – for the ground pin.
  2. V – for the VCC or supply voltage.
  3. S – for the signal pin.

Bill Of Materials

  1. 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.
  2. 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. 3-pin female-female dupont wires which will connects the button modules to the ESP32 shield pin headers.
  4. Button modules which will serve as an input device.

Hardware Instruction

  1. First, attach the ESP32 on top of the ESP32 shield and make sure that both USB ports are on the same side.
  2. 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.
  3. 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.
  4. 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.
  5. 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

  1. Purchase your Gorillacell ESP32 development kit at: gorillacell.kr


Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.