019 – MicroPython TechNotes: Switch Module

In this article, we will learn on how to use switch module with ESP32 using MicroPython programming language.
PINOUT:
- G – for the ground pin.
- V – for the supply voltage.
- S – for the signal pin.
BILL OF MATERIALS:
- ESP32 development board which will serve as the brain for this experiment.
- ESP32 shield from Gorillacell which will extend the ESP32 pins to shield’s pin headers for easy circuit connection.
- 3-pin Female-Female dupont wires to attach the switch module to ESP32 shield.
- Switch module.
HARDWARE INSTRUCTION:
- First, attach the ESP32 on top of ESP32 shield and make sure that both USB port are on the same side.
- Next, attach the dupont wires to switch module according to color coding which is black for the ground, red for the VCC, and yellow for the signal pin.
- Next, attach the other end of the dupont wires to ESP32 shield by matching the colors of the wires to the colors of the pin headers. For this experiment, I choose GPIO 32 as the signal pin.
- Next, power the ESP32 shield with an external power supply with a type-C USB cable.
- Lastly, connect the ESP32 to the computer through a micro USB cable. Our circuit should now be ready.
SOFTWARE INSTRUCTION:
For this, I prepared 2 example source code for you to try. Copy and paste it to Thonny IDE.
Play with it and adapt it according to your needs.
Enjoy.
VIDEO DEMONSTRATION:
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, exploring the basics:
# More details can be found in TechToTinker.blogspot.com
# George Bantique | tech.to.tinker@gmail.com
from machine import Pin
switch = Pin(32, Pin.IN)
# # The following lines of code can be tested in the REPL:
# switch.value() # Reads the current value of the switch.
# # Return value is 1 when switch is set to ON state,
# # the value is 0 when switch is set to OFF state.
How the code works:
from machine import Pin
Loads the Pin class from the machine module in order to access the pins of ESP32.
switch = Pin(32, Pin.IN)
Creates the “switch” object which is connected to GPIO 32 with a pin direction as input.
switch.value()
Reads the value of the pin associated to “switch” object, in this case, its GPIO 32.
2. Example # 2, simple application for the switch module:
# More details can be found in TechToTinker.blogspot.com
# George Bantique | tech.to.tinker@gmail.com
from machine import Pin
from time import sleep_ms
switch = Pin(32, Pin.IN)
led = Pin(2, Pin.OUT)
while True:
if switch.value()==1:
# Switch is set to ON state
# Lets toggle the state of the LED
# to create a blinking LED.
led.value(not led.value())
sleep_ms(300)
else:
# Switch is set to OFF state
# Lets turn OFF the LED.
led.value(0)
How the code works:
from machine import Pin
from time import sleep_ms
Loads the sleep_ms class from the time module. sleep_ms produces a blocking delay with a resolution in milliseconds.
switch = Pin(32, Pin.IN)
led = Pin(2, Pin.OUT)
Creates the “led” object which is connected to GPIO 2 with a pin direction set to output.
while True:
Creates an infinite loop in order to continuously check for the switch.
if switch.value()==1:
This if statement checks if the switch is set to ON by reading the value of the pin associated with the switch.
led.value(not led.value())
This line of code toggles the state of the pin associated with the led. If the state of led is 1, then make it as 0 or if the state is 0, then make it as 1.
sleep_ms(300)
This creates a 300 milliseconds blocking delay.
led.value(0)
Sets the pin associated to led to 0.
REFERENCES AND CREDITS:
Purchase your Gorillacell ESP32 development kit at: